]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tools/fetch-distro: support the case where the sources are in a subdirectory
authorAntonio Alvarez Feijoo <antonio.feijoo@suse.com>
Wed, 15 Jan 2025 11:01:22 +0000 (12:01 +0100)
committerLuca Boccassi <luca.boccassi@gmail.com>
Wed, 15 Jan 2025 15:33:00 +0000 (15:33 +0000)
If the GIT_SUBDIR environment variable is set, do not checkout the full sources
of the git repository, but perform a sparse checkout of the directory containing
the package. In this case, check only the commit history in this subdirectory.

tools/fetch-distro.py

index 1da1b8486e23700b4268bf750b85d40d94cbe2c6..1d9f10005ea29c784cfe38e9531678900686358f 100755 (executable)
@@ -60,35 +60,57 @@ def checkout_distro(args, distro: str, config: dict):
 
     url = config['Environment']['GIT_URL']
     branch = config['Environment']['GIT_BRANCH']
+    subdir = config['Environment'].get('GIT_SUBDIR')
+
+    # Do not checkout the full sources if the package is in a subdirectory,
+    # a sparse checkout will be done after
+    sparse = ['--no-checkout', '--filter=blob:none'] if subdir is not None else []
 
     # Only debian uses source-git for now…
-    reference = [f'--reference-if-able=.'] if distro == 'debian' else []
+    reference = ['--reference-if-able=.'] if distro == 'debian' else []
 
     cmd = [
         'git', 'clone', url,
         f'--branch={branch}',
+        *sparse,
         dest.as_posix(),
         *reference,
     ]
     print(f"+ {shlex.join(cmd)}")
     subprocess.check_call(cmd)
 
+    # Sparse checkout if the package is in a subdirectory
+    if subdir is not None:
+        cmd = ['git', '-C', f'pkg/{distro}', 'sparse-checkout', 'set',
+               '--no-cone', f'{subdir}']
+        print(f"+ {shlex.join(cmd)}")
+        subprocess.check_call(cmd)
+
+        cmd = ['git', '-C', f'pkg/{distro}', 'checkout', 'HEAD']
+        print(f"+ {shlex.join(cmd)}")
+        subprocess.check_call(cmd)
+
     args.fetch = False  # no need to fetch if we just cloned
 
 def update_distro(args, distro: str, config: dict):
     branch = config['Environment']['GIT_BRANCH']
+    subdir = config['Environment'].get('GIT_SUBDIR')
     old_commit = config['Environment']['GIT_COMMIT']
 
     cmd = ['git', '-C', f'pkg/{distro}', 'switch', branch]
     print(f"+ {shlex.join(cmd)}")
     subprocess.check_call(cmd)
 
-    cmd = ['git', '-C', f'pkg/{distro}', 'fetch', 'origin', '-v',
-           f'{branch}:remotes/origin/{branch}']
-    print(f"+ {shlex.join(cmd)}")
-    subprocess.check_call(cmd)
+    if args.fetch:
+        cmd = ['git', '-C', f'pkg/{distro}', 'fetch', 'origin', '-v',
+               f'{branch}:remotes/origin/{branch}']
+        print(f"+ {shlex.join(cmd)}")
+        subprocess.check_call(cmd)
 
-    cmd = ['git', '-C', f'pkg/{distro}', 'rev-parse', f'refs/remotes/origin/{branch}']
+    cmd = ['git', '-C', f'pkg/{distro}', 'log', '-n1', '--format=%H',
+           f'refs/remotes/origin/{branch}']
+    if subdir is not None:
+        cmd += [f'{subdir}']
     print(f"+ {shlex.join(cmd)}")
     new_commit = subprocess.check_output(cmd, text=True).strip()
 
@@ -99,6 +121,8 @@ def update_distro(args, distro: str, config: dict):
     cmd = ['git', '-C', f'pkg/{distro}', 'log', '--graph', '--first-parent',
            '--pretty=oneline', '--no-decorate', '--abbrev-commit', '--abbrev=10',
            f'{old_commit}..{new_commit}']
+    if subdir is not None:
+        cmd += [f'{subdir}']
     print(f"+ {shlex.join(cmd)}")
     changes = subprocess.check_output(cmd, text=True).strip()