Skip to content

Commit f269048

Browse files
peffgitster
authored andcommitted
fetch: opportunistically update tracking refs
When we run a regular "git fetch" without arguments, we update the tracking refs according to the configured refspec. However, when we run "git fetch origin master" (or "git pull origin master"), we do not look at the configured refspecs at all, and just update FETCH_HEAD. We miss an opportunity to update "refs/remotes/origin/master" (or whatever the user has configured). Some users find this confusing, because they would want to do further comparisons against the old state of the remote master, like: $ git pull origin master $ git log HEAD...origin/master In the currnet code, they are comparing against whatever commit happened to be in origin/master from the last time they did a complete "git fetch". This patch will update a ref from the RHS of a configured refspec whenever we happen to be fetching its LHS. That makes the case above work. The downside is that any users who really care about whether and when their tracking branches are updated may be surprised. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 900f281 commit f269048

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

Documentation/pull-fetch-param.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,4 @@ ifdef::git-pull[]
7575
* A parameter <ref> without a colon merges <ref> into the current
7676
branch,
7777
endif::git-pull[]
78-
while not storing the branch anywhere locally.
78+
and updates the remote-tracking branches (if any).

builtin/fetch.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ static struct ref *get_ref_map(struct transport *transport,
160160
const struct ref *remote_refs = transport_get_remote_refs(transport);
161161

162162
if (ref_count || tags == TAGS_SET) {
163+
struct ref **old_tail;
164+
163165
for (i = 0; i < ref_count; i++) {
164166
get_fetch_map(remote_refs, &refs[i], &tail, 0);
165167
if (refs[i].dst && refs[i].dst[0])
@@ -170,6 +172,20 @@ static struct ref *get_ref_map(struct transport *transport,
170172
rm->fetch_head_status = FETCH_HEAD_MERGE;
171173
if (tags == TAGS_SET)
172174
get_fetch_map(remote_refs, tag_refspec, &tail, 0);
175+
176+
/*
177+
* For any refs that we happen to be fetching via command-line
178+
* arguments, take the opportunity to update their configured
179+
* counterparts. However, we do not want to mention these
180+
* entries in FETCH_HEAD at all, as they would simply be
181+
* duplicates of existing entries.
182+
*/
183+
old_tail = tail;
184+
for (i = 0; i < transport->remote->fetch_refspec_nr; i++)
185+
get_fetch_map(ref_map, &transport->remote->fetch[i],
186+
&tail, 0);
187+
for (rm = *old_tail; rm; rm = rm->next)
188+
rm->fetch_head_status = FETCH_HEAD_IGNORE;
173189
} else {
174190
/* Use the defaults */
175191
struct remote *remote = transport->remote;

t/t5510-fetch.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ test_expect_success 'mark initial state of origin/master' '
377377
)
378378
'
379379

380-
test_expect_success 'explicit fetch should not update tracking' '
380+
test_expect_success 'explicit fetch should update tracking' '
381381
382382
cd "$D" &&
383383
git branch -f side &&
@@ -387,12 +387,12 @@ test_expect_success 'explicit fetch should not update tracking' '
387387
o=$(git rev-parse --verify refs/remotes/origin/master) &&
388388
git fetch origin master &&
389389
n=$(git rev-parse --verify refs/remotes/origin/master) &&
390-
test "$o" = "$n" &&
390+
test "$o" != "$n" &&
391391
test_must_fail git rev-parse --verify refs/remotes/origin/side
392392
)
393393
'
394394

395-
test_expect_success 'explicit pull should not update tracking' '
395+
test_expect_success 'explicit pull should update tracking' '
396396
397397
cd "$D" &&
398398
git branch -f side &&
@@ -402,7 +402,7 @@ test_expect_success 'explicit pull should not update tracking' '
402402
o=$(git rev-parse --verify refs/remotes/origin/master) &&
403403
git pull origin master &&
404404
n=$(git rev-parse --verify refs/remotes/origin/master) &&
405-
test "$o" = "$n" &&
405+
test "$o" != "$n" &&
406406
test_must_fail git rev-parse --verify refs/remotes/origin/side
407407
)
408408
'

0 commit comments

Comments
 (0)