Skip to content

Commit e5f522d

Browse files
jlehmanngitster
authored andcommitted
submodule update: Don't fetch when the submodule commit is already present
If the commit to be checked out on "git submodule update" has already been fetched in the submodule there is no need to run "git fetch" again. Since "git fetch" recently learned recursion (and the new on-demand mode to fetch commits recorded in the superproject is enabled by default) this will happen pretty often, thereby making the unconditional fetch during "git submodule update" unnecessary. If the commit is not present in the submodule (e.g. the user disabled the fetch on-demand mode) the fetch will be run as before. Signed-off-by: Jens Lehmann <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c16c3e4 commit e5f522d

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

git-submodule.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,11 @@ cmd_update()
464464

465465
if test -z "$nofetch"
466466
then
467+
# Run fetch only if $sha1 isn't present or it
468+
# is not reachable from a ref.
467469
(clear_local_git_env; cd "$path" &&
468-
git-fetch) ||
470+
((rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
471+
test -z "$rev") || git-fetch)) ||
469472
die "Unable to fetch in submodule path '$path'"
470473
fi
471474

t/t7406-submodule-update.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,26 @@ test_expect_success 'submodule update detaching the HEAD ' '
7474
)
7575
'
7676

77+
apos="'";
78+
test_expect_success 'submodule update does not fetch already present commits' '
79+
(cd submodule &&
80+
echo line3 >> file &&
81+
git add file &&
82+
test_tick &&
83+
git commit -m "upstream line3"
84+
) &&
85+
(cd super/submodule &&
86+
head=$(git rev-parse --verify HEAD) &&
87+
echo "Submodule path ${apos}submodule$apos: checked out $apos$head$apos" > ../../expected &&
88+
git reset --hard HEAD~1
89+
) &&
90+
(cd super &&
91+
git submodule update > ../actual 2> ../actual.err
92+
) &&
93+
test_cmp expected actual &&
94+
! test -s actual.err
95+
'
96+
7797
test_expect_success 'submodule update --rebase staying on master' '
7898
(cd super/submodule &&
7999
git checkout master

0 commit comments

Comments
 (0)