From baa43b5d71bda93b59473dac3b77c629a451c1fd Mon Sep 17 00:00:00 2001 From: Hartmut Schirmer <61006726+Kinokin@users.noreply.github.com> Date: Mon, 25 Jul 2022 09:29:48 +0200 Subject: [PATCH 1/2] Resolve relative submodule url Change-Id: I6de67e5961cd1ff8a8d72e741d23c7c78bb8c7b3 --- git-partial-submodule.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/git-partial-submodule.py b/git-partial-submodule.py index dab0abc..81df007 100644 --- a/git-partial-submodule.py +++ b/git-partial-submodule.py @@ -117,8 +117,9 @@ def ReadGitmodules(worktreeRoot): # Locate directories worktreeRoot = os.path.abspath(ReadGitOutput('rev-parse', '--show-toplevel').strip()) repoRoot = os.path.abspath(ReadGitOutput('rev-parse', '--git-dir').strip()) +repoUrl = ReadGitOutput('config', '--get', 'remote.origin.url').strip() if args.verbose: - print("worktree root: %s\nrepo root: %s" % (worktreeRoot, repoRoot)) + print("worktree root: %s\nrepo root: %s\nrepo url: %s" % (worktreeRoot, repoRoot, repoUrl)) # Process commands @@ -227,13 +228,27 @@ def ReadGitmodules(worktreeRoot): os.makedirs(os.path.dirname(submoduleRepoRoot), exist_ok=True) os.makedirs(submoduleWorktreeRoot, exist_ok=True) # Should have been created by 'git submodule init', but just make sure + # Resolve relative submodule url + join = False + subUrl = submodule['url'] + baseUrl = repoUrl + while subUrl.startswith('./') or subUrl.startswith('../'): + join = True + if subUrl.startswith('./'): + subUrl = subUrl[2:] + else: + subUrl = subUrl[3:] + baseUrl = os.path.split(baseUrl)[0] + if join: + subUrl = baseUrl+'/'+subUrl + # Perform the partial clone!!! Git('clone', '--filter=blob:none', '--no-checkout', '--separate-git-dir', submoduleRepoRoot, *(['--branch', branchName] if (branchName := submodule.get('branch')) else []), - submodule['url'], + subUrl, submoduleWorktreeRoot) # Apply sparse-checkout patterns in the submodule worktree From 33149a205d87c87190b2a63398ad6449c3a28221 Mon Sep 17 00:00:00 2001 From: Hartmut Schirmer <61006726+Kinokin@users.noreply.github.com> Date: Mon, 25 Jul 2022 09:29:48 +0200 Subject: [PATCH 2/2] Resolve special branch '.' Change-Id: Ie3422c05529497bfa3cf1d99ec33227f418c8996 --- git-partial-submodule.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/git-partial-submodule.py b/git-partial-submodule.py index 81df007..8a41b55 100644 --- a/git-partial-submodule.py +++ b/git-partial-submodule.py @@ -111,6 +111,11 @@ def ReadGitmodules(worktreeRoot): gitmodules.byPath[contents['path']] = contents return gitmodules +def ResolveBranch(branchName): + if branchName == '.': + return repoBranch + return branchName + # Version 2.27.0 is needed for --filter and --sparse options on git clone CheckGitVersion((2, 27, 0)) @@ -118,8 +123,9 @@ def ReadGitmodules(worktreeRoot): worktreeRoot = os.path.abspath(ReadGitOutput('rev-parse', '--show-toplevel').strip()) repoRoot = os.path.abspath(ReadGitOutput('rev-parse', '--git-dir').strip()) repoUrl = ReadGitOutput('config', '--get', 'remote.origin.url').strip() +repoBranch = ReadGitOutput('rev-parse', '--abbrev-ref', 'HEAD').strip() if args.verbose: - print("worktree root: %s\nrepo root: %s\nrepo url: %s" % (worktreeRoot, repoRoot, repoUrl)) + print("worktree root: %s\nrepo root: %s\nrepo url: %s\nrepo branch: %s" % (worktreeRoot, repoRoot, repoUrl, repoBranch)) # Process commands @@ -247,7 +253,7 @@ def ReadGitmodules(worktreeRoot): '--filter=blob:none', '--no-checkout', '--separate-git-dir', submoduleRepoRoot, - *(['--branch', branchName] if (branchName := submodule.get('branch')) else []), + *(['--branch', ResolveBranch(branchName)] if (branchName := submodule.get('branch')) else []), subUrl, submoduleWorktreeRoot)