Skip to content

Commit da3efdb

Browse files
jaysoffiangitster
authored andcommitted
receive-pack: detect aliased updates which can occur with symrefs
When pushing to a remote repo the sending side filters out aliased updates (e.g., foo:baz bar:baz). However, it is not possible for the sender to know if two refs are aliased on the receiving side via symrefs. Here is one such scenario: $ git init origin $ (cd origin && touch file && git add file && git commit -a -m intial) $ git clone --bare origin origin.git $ rm -rf origin $ git clone origin.git client $ git clone --mirror client backup.git && $ (cd backup.git && git remote set-head origin --auto) $ (cd client && git remote add --mirror backup ../backup.git && echo change1 > file && git commit -a -m change1 && git push origin && git push backup ) The push to backup fails with: Counting objects: 5, done. Writing objects: 100% (3/3), 244 bytes, done. Total 3 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. error: Ref refs/remotes/origin/master is at ef3... but expected 262... remote: error: failed to lock refs/remotes/origin/master To ../backup.git 262cd57..ef307ff master -> master 262cd57..ef307ff origin/HEAD -> origin/HEAD ! [remote rejected] origin/master -> origin/master (failed to lock) error: failed to push some refs to '../backup.git' The reason is that refs/remotes/origin/HEAD is a symref to refs/remotes/origin/master, but it is not possible for the sending side to unambiguously know this. This commit fixes the issue by having receive-pack ignore any update to a symref whose target is being identically updated. If a symref and its target are being updated inconsistently, then the update for both fails with an error message ("refusing inconsistent update...") to help diagnose the situation. Signed-off-by: Jay Soffian <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5e1c71f commit da3efdb

File tree

2 files changed

+113
-4
lines changed

2 files changed

+113
-4
lines changed

builtin-receive-pack.c

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "object.h"
1010
#include "remote.h"
1111
#include "transport.h"
12+
#include "string-list.h"
1213

1314
static const char receive_pack_usage[] = "git receive-pack <git-dir>";
1415

@@ -129,6 +130,7 @@ static void write_head_info(void)
129130
struct command {
130131
struct command *next;
131132
const char *error_string;
133+
unsigned int skip_update;
132134
unsigned char old_sha1[20];
133135
unsigned char new_sha1[20];
134136
char ref_name[FLEX_ARRAY]; /* more */
@@ -486,6 +488,63 @@ static void run_update_post_hook(struct command *commands)
486488
}
487489
}
488490

491+
static void check_aliased_update(struct command *cmd, struct string_list *list)
492+
{
493+
struct string_list_item *item;
494+
struct command *dst_cmd;
495+
unsigned char sha1[20];
496+
char cmd_oldh[41], cmd_newh[41], dst_oldh[41], dst_newh[41];
497+
int flag;
498+
499+
const char *dst_name = resolve_ref(cmd->ref_name, sha1, 0, &flag);
500+
501+
if (!(flag & REF_ISSYMREF))
502+
return;
503+
504+
if ((item = string_list_lookup(dst_name, list)) == NULL)
505+
return;
506+
507+
cmd->skip_update = 1;
508+
509+
dst_cmd = (struct command *) item->util;
510+
511+
if (!hashcmp(cmd->old_sha1, dst_cmd->old_sha1) &&
512+
!hashcmp(cmd->new_sha1, dst_cmd->new_sha1))
513+
return;
514+
515+
dst_cmd->skip_update = 1;
516+
517+
strcpy(cmd_oldh, find_unique_abbrev(cmd->old_sha1, DEFAULT_ABBREV));
518+
strcat(cmd_newh, find_unique_abbrev(cmd->new_sha1, DEFAULT_ABBREV));
519+
strcpy(dst_oldh, find_unique_abbrev(dst_cmd->old_sha1, DEFAULT_ABBREV));
520+
strcat(dst_newh, find_unique_abbrev(dst_cmd->new_sha1, DEFAULT_ABBREV));
521+
rp_error("refusing inconsistent update between symref '%s' (%s..%s) and"
522+
" its target '%s' (%s..%s)",
523+
cmd->ref_name, cmd_oldh, cmd_newh,
524+
dst_cmd->ref_name, dst_oldh, dst_newh);
525+
526+
cmd->error_string = dst_cmd->error_string =
527+
"inconsistent aliased update";
528+
}
529+
530+
static void check_aliased_updates(struct command *commands)
531+
{
532+
struct command *cmd;
533+
struct string_list ref_list = { NULL, 0, 0, 0 };
534+
535+
for (cmd = commands; cmd; cmd = cmd->next) {
536+
struct string_list_item *item =
537+
string_list_append(cmd->ref_name, &ref_list);
538+
item->util = (void *)cmd;
539+
}
540+
sort_string_list(&ref_list);
541+
542+
for (cmd = commands; cmd; cmd = cmd->next)
543+
check_aliased_update(cmd, &ref_list);
544+
545+
string_list_clear(&ref_list, 0);
546+
}
547+
489548
static void execute_commands(struct command *commands, const char *unpacker_error)
490549
{
491550
struct command *cmd;
@@ -503,10 +562,13 @@ static void execute_commands(struct command *commands, const char *unpacker_erro
503562
return;
504563
}
505564

565+
check_aliased_updates(commands);
566+
506567
head_name = resolve_ref("HEAD", sha1, 0, NULL);
507568

508569
for (cmd = commands; cmd; cmd = cmd->next)
509-
cmd->error_string = update(cmd);
570+
if (!cmd->skip_update)
571+
cmd->error_string = update(cmd);
510572
}
511573

512574
static struct command *read_head_info(void)
@@ -541,12 +603,10 @@ static struct command *read_head_info(void)
541603
if (strstr(refname + reflen + 1, "side-band-64k"))
542604
use_sideband = LARGE_PACKET_MAX;
543605
}
544-
cmd = xmalloc(sizeof(struct command) + len - 80);
606+
cmd = xcalloc(1, sizeof(struct command) + len - 80);
545607
hashcpy(cmd->old_sha1, old_sha1);
546608
hashcpy(cmd->new_sha1, new_sha1);
547609
memcpy(cmd->ref_name, line + 82, len - 81);
548-
cmd->error_string = NULL;
549-
cmd->next = NULL;
550610
*p = cmd;
551611
p = &cmd->next;
552612
}

t/t5516-fetch-push.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,4 +660,53 @@ test_expect_success 'push with branches containing #' '
660660
git checkout master
661661
'
662662

663+
test_expect_success 'push into aliased refs (consistent)' '
664+
mk_test heads/master &&
665+
mk_child child1 &&
666+
mk_child child2 &&
667+
(
668+
cd child1 &&
669+
git branch foo &&
670+
git symbolic-ref refs/heads/bar refs/heads/foo
671+
git config receive.denyCurrentBranch false
672+
) &&
673+
(
674+
cd child2 &&
675+
>path2 &&
676+
git add path2 &&
677+
test_tick &&
678+
git commit -a -m child2 &&
679+
git branch foo &&
680+
git branch bar &&
681+
git push ../child1 foo bar
682+
)
683+
'
684+
685+
test_expect_success 'push into aliased refs (inconsistent)' '
686+
mk_test heads/master &&
687+
mk_child child1 &&
688+
mk_child child2 &&
689+
(
690+
cd child1 &&
691+
git branch foo &&
692+
git symbolic-ref refs/heads/bar refs/heads/foo
693+
git config receive.denyCurrentBranch false
694+
) &&
695+
(
696+
cd child2 &&
697+
>path2 &&
698+
git add path2 &&
699+
test_tick &&
700+
git commit -a -m child2 &&
701+
git branch foo &&
702+
>path3 &&
703+
git add path3 &&
704+
test_tick &&
705+
git commit -a -m child2 &&
706+
git branch bar &&
707+
test_must_fail git push ../child1 foo bar 2>stderr &&
708+
grep "refusing inconsistent update" stderr
709+
)
710+
'
711+
663712
test_done

0 commit comments

Comments
 (0)