Skip to content

Commit

Permalink
Make reclone with implicit branch update branch in .gitrepo correctly
Browse files Browse the repository at this point in the history
If the subrepo is tracking a branch and you reclone using

    git subrepo clone --force $URL

without specifying -b/--branch then the branch setting in .gitrepo is
not updated.
  • Loading branch information
jrosdahl authored and admorgan committed Jul 9, 2024
1 parent c9ec2a6 commit 84095bc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/git-subrepo
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,13 @@ subrepo:clone() {
fi
o "Remove the old subdir."
RUN git rm -r -- "$subdir"

if [[ -z $override_branch ]]; then
o "Determine the upstream head branch."
get-upstream-head-branch
subrepo_branch="$output"
override_branch="$output" # Force update of the branch in .gitrepo
fi
else
assert-subdir-empty
if [[ -z $subrepo_branch ]]; then
Expand Down
9 changes: 8 additions & 1 deletion test/reclone.t
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ clone-foo-and-bar
test-exists \
"$OWNER/foo/bar/bard/"

# Test that reclone is not done if not needed.
export XYZ=1
is "$(
cd "$OWNER/foo"
Expand All @@ -24,19 +25,25 @@ is "$(
"Subrepo 'bar' is up to date." \
"No reclone if same commit"

# Test that reclone of a different ref works.
(
cd "$OWNER/foo"
git subrepo --quiet clone --force "$UPSTREAM/bar" --branch=refs/tags/A
)

is "$(git -C "$OWNER"/foo subrepo config bar branch)" \
"Subrepo 'bar' option 'branch' has value 'refs/tags/A'."
test-exists \
"!$OWNER/foo/bar/bard/"

# Test that reclone back to (implicit) master works.
(
cd "$OWNER/foo"
git subrepo --quiet clone -f "$UPSTREAM/bar" --branch=master
git subrepo --quiet clone -f "$UPSTREAM/bar"
)

is "$(git -C "$OWNER"/foo subrepo config bar branch)" \
"Subrepo 'bar' option 'branch' has value 'master'."
test-exists \
"$OWNER/foo/bar/bard/"

Expand Down

0 comments on commit 84095bc

Please sign in to comment.