Skip to content

Commit

Permalink
fetch: opportunistically update tracking refs
Browse files Browse the repository at this point in the history
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 <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Jeff King authored and Junio C Hamano committed May 12, 2013
1 parent 900f281 commit f269048
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Documentation/pull-fetch-param.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ ifdef::git-pull[]
* A parameter <ref> without a colon merges <ref> into the current
branch,
endif::git-pull[]
while not storing the branch anywhere locally.
and updates the remote-tracking branches (if any).
16 changes: 16 additions & 0 deletions builtin/fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ static struct ref *get_ref_map(struct transport *transport,
const struct ref *remote_refs = transport_get_remote_refs(transport);

if (ref_count || tags == TAGS_SET) {
struct ref **old_tail;

for (i = 0; i < ref_count; i++) {
get_fetch_map(remote_refs, &refs[i], &tail, 0);
if (refs[i].dst && refs[i].dst[0])
Expand All @@ -170,6 +172,20 @@ static struct ref *get_ref_map(struct transport *transport,
rm->fetch_head_status = FETCH_HEAD_MERGE;
if (tags == TAGS_SET)
get_fetch_map(remote_refs, tag_refspec, &tail, 0);

/*
* For any refs that we happen to be fetching via command-line
* arguments, take the opportunity to update their configured
* counterparts. However, we do not want to mention these
* entries in FETCH_HEAD at all, as they would simply be
* duplicates of existing entries.
*/
old_tail = tail;
for (i = 0; i < transport->remote->fetch_refspec_nr; i++)
get_fetch_map(ref_map, &transport->remote->fetch[i],
&tail, 0);
for (rm = *old_tail; rm; rm = rm->next)
rm->fetch_head_status = FETCH_HEAD_IGNORE;
} else {
/* Use the defaults */
struct remote *remote = transport->remote;
Expand Down
8 changes: 4 additions & 4 deletions t/t5510-fetch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ test_expect_success 'mark initial state of origin/master' '
)
'

test_expect_success 'explicit fetch should not update tracking' '
test_expect_success 'explicit fetch should update tracking' '
cd "$D" &&
git branch -f side &&
Expand All @@ -387,12 +387,12 @@ test_expect_success 'explicit fetch should not update tracking' '
o=$(git rev-parse --verify refs/remotes/origin/master) &&
git fetch origin master &&
n=$(git rev-parse --verify refs/remotes/origin/master) &&
test "$o" = "$n" &&
test "$o" != "$n" &&
test_must_fail git rev-parse --verify refs/remotes/origin/side
)
'

test_expect_success 'explicit pull should not update tracking' '
test_expect_success 'explicit pull should update tracking' '
cd "$D" &&
git branch -f side &&
Expand All @@ -402,7 +402,7 @@ test_expect_success 'explicit pull should not update tracking' '
o=$(git rev-parse --verify refs/remotes/origin/master) &&
git pull origin master &&
n=$(git rev-parse --verify refs/remotes/origin/master) &&
test "$o" = "$n" &&
test "$o" != "$n" &&
test_must_fail git rev-parse --verify refs/remotes/origin/side
)
'
Expand Down

0 comments on commit f269048

Please sign in to comment.