Skip to content

Commit 16ed2f4

Browse files
Clemens Buchachergitster
authored andcommitted
push: fix local refs update if already up-to-date
git push normally updates local refs only after a successful push. If the remote already has the updates -- pushed indirectly through another repository, for example -- we forget to update local tracking refs. Signed-off-by: Clemens Buchacher <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5bdd8d4 commit 16ed2f4

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

builtin-send-pack.c

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ static void update_tracking_ref(struct remote *remote, struct ref *ref)
222222
{
223223
struct refspec rs;
224224

225-
if (ref->status != REF_STATUS_OK)
225+
if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
226226
return;
227227

228228
rs.src = ref->name;
@@ -424,24 +424,19 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest
424424
*/
425425
new_refs = 0;
426426
for (ref = remote_refs; ref; ref = ref->next) {
427-
const unsigned char *new_sha1;
428-
429-
if (!ref->peer_ref) {
430-
if (!args.send_mirror)
431-
continue;
432-
new_sha1 = null_sha1;
433-
}
434-
else
435-
new_sha1 = ref->peer_ref->new_sha1;
436427

428+
if (ref->peer_ref)
429+
hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
430+
else if (!args.send_mirror)
431+
continue;
437432

438-
ref->deletion = is_null_sha1(new_sha1);
433+
ref->deletion = is_null_sha1(ref->new_sha1);
439434
if (ref->deletion && !allow_deleting_refs) {
440435
ref->status = REF_STATUS_REJECT_NODELETE;
441436
continue;
442437
}
443438
if (!ref->deletion &&
444-
!hashcmp(ref->old_sha1, new_sha1)) {
439+
!hashcmp(ref->old_sha1, ref->new_sha1)) {
445440
ref->status = REF_STATUS_UPTODATE;
446441
continue;
447442
}
@@ -469,14 +464,13 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest
469464
!ref->deletion &&
470465
!is_null_sha1(ref->old_sha1) &&
471466
(!has_sha1_file(ref->old_sha1)
472-
|| !ref_newer(new_sha1, ref->old_sha1));
467+
|| !ref_newer(ref->new_sha1, ref->old_sha1));
473468

474469
if (ref->nonfastforward && !ref->force && !args.force_update) {
475470
ref->status = REF_STATUS_REJECT_NONFASTFORWARD;
476471
continue;
477472
}
478473

479-
hashcpy(ref->new_sha1, new_sha1);
480474
if (!ref->deletion)
481475
new_refs++;
482476

t/t5516-fetch-push.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,37 @@ test_expect_success 'push updates local refs' '
437437
438438
'
439439

440+
test_expect_success 'push updates up-to-date local refs' '
441+
442+
rm -rf parent child &&
443+
mkdir parent &&
444+
(cd parent && git init &&
445+
echo one >foo && git add foo && git commit -m one) &&
446+
git clone parent child1 &&
447+
git clone parent child2 &&
448+
(cd child1 &&
449+
echo two >foo && git commit -a -m two &&
450+
git push) &&
451+
(cd child2 &&
452+
git pull ../child1 master &&
453+
git push &&
454+
test $(git rev-parse master) = $(git rev-parse remotes/origin/master))
455+
456+
'
457+
458+
test_expect_success 'push preserves up-to-date packed refs' '
459+
460+
rm -rf parent child &&
461+
mkdir parent &&
462+
(cd parent && git init &&
463+
echo one >foo && git add foo && git commit -m one) &&
464+
git clone parent child &&
465+
(cd child &&
466+
git push &&
467+
! test -f .git/refs/remotes/origin/master)
468+
469+
'
470+
440471
test_expect_success 'push does not update local refs on failure' '
441472
442473
rm -rf parent child &&

0 commit comments

Comments
 (0)