Skip to contentSkip to conversation

reproducible: fix git SOURCE_DATE_EPOCH randomness

PATCH Randolph Sapp 1 participant 1 message Feb 13 1 message not in the archive

lore
Share & tools
lore
#1 lore raw
In reply to a message not in the archive
Quoted 53 lines · Randolph Sapp via lists.openembedded.org

From: Randolph Sapp <[email protected]>

Anything that defines multiple git sources should have the largest value taken when calculating the SOURCE_DATE_EPOCH for a package.

The previous iteration actually introduced some degree of randomness, as it would stop on the first git repository reported by os.walk, which does not assure any specific ordering by default.

---

To address issue reported here:
https://lists.openembedded.org/g/openembedded-core/message/231076

 meta/lib/oe/reproducible.py | 64 ++++++++++++++++---------------------
 1 file changed, 28 insertions(+), 36 deletions(-)
meta/lib/oe/reproducible.py +2 -17
index 0270024a83..06ceda8d7f 100644
@@ -3,6 +3,7 @@
 #
 # SPDX-License-Identifier: GPL-2.0-only
 #
+import glob
 import os
 import subprocess
 import bb
@@ -74,52 +75,43 @@ def get_source_date_epoch_from_known_files(d, sourcedir):
         bb.debug(1, "SOURCE_DATE_EPOCH taken from: %s" % newest_file)
     return source_date_epoch
 
-def find_git_folder(d, sourcedir):
-    # First guess: UNPACKDIR/BB_GIT_DEFAULT_DESTSUFFIX
-    # This is the default git fetcher unpack path
+def find_git_folders(d, sourcedir):
     unpackdir = d.getVar('UNPACKDIR')
-    default_destsuffix = d.getVar('BB_GIT_DEFAULT_DESTSUFFIX')
-    gitpath = os.path.join(unpackdir, default_destsuffix, ".git")
-    if os.path.isdir(gitpath):
-        return gitpath
-
-    # Second guess: ${S}
-    gitpath = os.path.join(sourcedir, ".git")
-    if os.path.isdir(gitpath):
-        return gitpath
-
-    # Perhaps there was a subpath or destsuffix specified.
-    # Go looking in the UNPACKDIR
-    for root, dirs, files in os.walk(unpackdir, topdown=True):
-        if '.git' in dirs:

meta/lib/oe/reproducible.py def get_source_date_epoch_from_known_files(d, sourcedir): · line 85

Quoted 10 lines
meta/lib/oe/reproducible.py +5 -4
-            return os.path.join(root, ".git")
+    git_folders = []
 
-    for root, dirs, files in os.walk(sourcedir, topdown=True):
-        if '.git' in dirs:
-            return os.path.join(root, ".git")
+    for mainpath in (sourcedir, unpackdir):
+        gitpath_glob = os.path.join(mainpath, "**/.git/")
+        for gitpath in glob.glob(gitpath_glob, recursive=True):
+            git_folders.append(gitpath)

I honestly don't know if recursively searching the deploy directory is any worse than instantiating a new fetcher and walking the SRC_URI and destsuffix values directly. They both feel a little heavy-handed.

Quoted 48 lines
meta/lib/oe/reproducible.py +21 -16
-    bb.warn("Failed to find a git repository in UNPACKDIR: %s" % unpackdir)
-    return None
+    if not git_folders:
+        bb.warn("Failed to find any git repository in UNPACKDIR or S")
+
+    return git_folders
 
 def get_source_date_epoch_from_git(d, sourcedir):
     if not "git://" in d.getVar('SRC_URI') and not "gitsm://" in d.getVar('SRC_URI'):
         return None
 
-    gitpath = find_git_folder(d, sourcedir)
-    if not gitpath:
-        return None
+    # Get an epoch from all valid git repositoies
+    sources_dates = []
+    for gitpath in find_git_folders(d, sourcedir):
+        # Check that the repository has a valid HEAD; it may not if subdir is used
+        # in SRC_URI
+        p = subprocess.run(['git', '--git-dir', gitpath, 'rev-parse', 'HEAD'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+        if p.returncode != 0:
+            bb.debug(1, "%s does not have a valid HEAD: %s" % (gitpath, p.stdout.decode('utf-8')))
+            continue
 
-    # Check that the repository has a valid HEAD; it may not if subdir is used
-    # in SRC_URI
-    p = subprocess.run(['git', '--git-dir', gitpath, 'rev-parse', 'HEAD'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
-    if p.returncode != 0:
-        bb.debug(1, "%s does not have a valid HEAD: %s" % (gitpath, p.stdout.decode('utf-8')))
-        return None
+        bb.debug(1, "git repository: %s" % gitpath)
+        p = subprocess.run(['git', '-c', 'log.showSignature=false', '--git-dir', gitpath, 'log', '-1', '--pretty=%ct'],
+                           check=True, stdout=subprocess.PIPE)
+        sources_dates.append(int(p.stdout.decode('utf-8')))
+
+    if sources_dates:
+        return sorted(sources_dates, reverse=True)[0]
 
-    bb.debug(1, "git repository: %s" % gitpath)
-    p = subprocess.run(['git', '-c', 'log.showSignature=false', '--git-dir', gitpath, 'log', '-1', '--pretty=%ct'],
-                       check=True, stdout=subprocess.PIPE)
-    return int(p.stdout.decode('utf-8'))
+    return None
 
 def get_source_date_epoch_from_youngest_file(d, sourcedir):
     if sourcedir == d.getVar('UNPACKDIR'):
--

2.52.0