From 84095bca8fe74764b23d919dfe36611612f84dfd Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Wed, 4 Dec 2019 18:24:20 +0100 Subject: [PATCH] Make reclone with implicit branch update branch in .gitrepo correctly 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. --- lib/git-subrepo | 7 +++++++ test/reclone.t | 9 ++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/git-subrepo b/lib/git-subrepo index 1ddef7cf..bb42953f 100755 --- a/lib/git-subrepo +++ b/lib/git-subrepo @@ -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 diff --git a/test/reclone.t b/test/reclone.t index 38f59cc2..0643ca16 100644 --- a/test/reclone.t +++ b/test/reclone.t @@ -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" @@ -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/"