Skip to content

Commit

Permalink
Merge branch 'jk/fetch-reachability-error-fix'
Browse files Browse the repository at this point in the history
Code clean-up and a fix for "git fetch" by an explicit object name
(as opposed to fetching refs by name).

* jk/fetch-reachability-error-fix:
  fetch: do not consider peeled tags as advertised tips
  remote.c: make singular free_ref() public
  fetch: use free_refs()
  pkt-line: prepare buffer before handling ERR packets
  upload-pack: send ERR packet for non-tip objects
  t5530: check protocol response for "not our ref"
  t5516: drop ok=sigpipe from unreachable-want tests
  • Loading branch information
gitster committed Apr 25, 2019
2 parents 449f2db + 34066f0 commit 57a6b93
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 25 deletions.
16 changes: 9 additions & 7 deletions fetch-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -573,9 +573,14 @@ static void filter_refs(struct fetch_pack_args *args,
next = ref->next;

if (starts_with(ref->name, "refs/") &&
check_refname_format(ref->name, 0))
; /* trash */
else {
check_refname_format(ref->name, 0)) {
/*
* trash or a peeled value; do not even add it to
* unmatched list
*/
free_one_ref(ref);
continue;
} else {
while (i < nr_sought) {
int cmp = strcmp(ref->name, sought[i]->name);
if (cmp < 0)
Expand Down Expand Up @@ -630,10 +635,7 @@ static void filter_refs(struct fetch_pack_args *args,
}

oidset_clear(&tip_oids);
for (ref = unmatched; ref; ref = next) {
next = ref->next;
free(ref);
}
free_refs(unmatched);

*refs = newlist;
}
Expand Down
9 changes: 5 additions & 4 deletions pkt-line.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,16 +350,17 @@ enum packet_read_status packet_read_with_status(int fd, char **src_buffer,
return PACKET_READ_EOF;
}

if ((options & PACKET_READ_DIE_ON_ERR_PACKET) &&
starts_with(buffer, "ERR "))
die(_("remote error: %s"), buffer + 4);

if ((options & PACKET_READ_CHOMP_NEWLINE) &&
len && buffer[len-1] == '\n')
len--;

buffer[len] = 0;
packet_trace(buffer, len, 0);

if ((options & PACKET_READ_DIE_ON_ERR_PACKET) &&
starts_with(buffer, "ERR "))
die(_("remote error: %s"), buffer + 4);

*pktlen = len;
return PACKET_READ_NORMAL;
}
Expand Down
6 changes: 3 additions & 3 deletions remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -820,11 +820,11 @@ struct ref *copy_ref_list(const struct ref *ref)
return ret;
}

static void free_ref(struct ref *ref)
void free_one_ref(struct ref *ref)
{
if (!ref)
return;
free_ref(ref->peer_ref);
free_one_ref(ref->peer_ref);
free(ref->remote_status);
free(ref->symref);
free(ref);
Expand All @@ -835,7 +835,7 @@ void free_refs(struct ref *ref)
struct ref *next;
while (ref) {
next = ref->next;
free_ref(ref);
free_one_ref(ref);
ref = next;
}
}
Expand Down
4 changes: 3 additions & 1 deletion remote.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,10 @@ int ref_compare_name(const void *, const void *);
int check_ref_type(const struct ref *ref, int flags);

/*
* Frees the entire list and peers of elements.
* Free a single ref and its peer, or an entire list of refs and their peers,
* respectively.
*/
void free_one_ref(struct ref *ref);
void free_refs(struct ref *ref);

struct oid_array;
Expand Down
20 changes: 16 additions & 4 deletions t/t5516-fetch-push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1241,18 +1241,19 @@ do
cd shallow &&
# Some protocol versions (e.g. 2) support fetching
# unadvertised objects, so restrict this test to v0.
test_must_fail ok=sigpipe env GIT_TEST_PROTOCOL_VERSION= \
test_must_fail env GIT_TEST_PROTOCOL_VERSION= \
git fetch ../testrepo/.git $SHA1_3 &&
test_must_fail ok=sigpipe env GIT_TEST_PROTOCOL_VERSION= \
test_must_fail env GIT_TEST_PROTOCOL_VERSION= \
git fetch ../testrepo/.git $SHA1_1 &&
git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
git fetch ../testrepo/.git $SHA1_1 &&
git cat-file commit $SHA1_1 &&
test_must_fail git cat-file commit $SHA1_2 &&
git fetch ../testrepo/.git $SHA1_2 &&
git cat-file commit $SHA1_2 &&
test_must_fail ok=sigpipe env GIT_TEST_PROTOCOL_VERSION= \
git fetch ../testrepo/.git $SHA1_3
test_must_fail env GIT_TEST_PROTOCOL_VERSION= \
git fetch ../testrepo/.git $SHA1_3 2>err &&
test_i18ngrep "remote error:.*not our ref.*$SHA1_3\$" err
)
'
done
Expand Down Expand Up @@ -1284,6 +1285,17 @@ test_expect_success 'fetch follows tags by default' '
test_cmp expect actual
'

test_expect_success 'peeled advertisements are not considered ref tips' '
mk_empty testrepo &&
git -C testrepo commit --allow-empty -m one &&
git -C testrepo commit --allow-empty -m two &&
git -C testrepo tag -m foo mytag HEAD^ &&
oid=$(git -C testrepo rev-parse mytag^{commit}) &&
test_must_fail env GIT_TEST_PROTOCOL_VERSION= \
git fetch testrepo $oid 2>err &&
test_i18ngrep "Server does not allow request for unadvertised object" err
'

test_expect_success 'pushing a specific ref applies remote.$name.push as refmap' '
mk_test testrepo heads/master &&
rm -fr src dst &&
Expand Down
18 changes: 15 additions & 3 deletions t/t5530-upload-pack-error.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,25 @@ test_expect_success 'upload-pack fails due to error in rev-list' '
grep "bad tree object" output.err
'

test_expect_success 'upload-pack error message when bad ref requested' '
test_expect_success 'upload-pack fails due to bad want (no object)' '
printf "0045want %s multi_ack_detailed\n00000009done\n0000" \
"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef" >input &&
test_must_fail git upload-pack . <input >output 2>output.err &&
grep -q "not our ref" output.err &&
! grep -q multi_ack_detailed output.err
grep "not our ref" output.err &&
grep "ERR" output &&
! grep multi_ack_detailed output.err
'

test_expect_success 'upload-pack fails due to bad want (not tip)' '
oid=$(echo an object we have | git hash-object -w --stdin) &&
printf "0045want %s multi_ack_detailed\n00000009done\n0000" \
"$oid" >input &&
test_must_fail git upload-pack . <input >output 2>output.err &&
grep "not our ref" output.err &&
grep "ERR" output &&
! grep multi_ack_detailed output.err
'

test_expect_success 'upload-pack fails due to error in pack-objects enumeration' '
Expand Down
11 changes: 8 additions & 3 deletions upload-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,8 @@ static int has_unreachable(struct object_array *src)
return 1;
}

static void check_non_tip(struct object_array *want_obj)
static void check_non_tip(struct object_array *want_obj,
struct packet_writer *writer)
{
int i;

Expand All @@ -611,9 +612,13 @@ static void check_non_tip(struct object_array *want_obj)
/* Pick one of them (we know there at least is one) */
for (i = 0; i < want_obj->nr; i++) {
struct object *o = want_obj->objects[i].item;
if (!is_our_ref(o))
if (!is_our_ref(o)) {
packet_writer_error(writer,
"upload-pack: not our ref %s",
oid_to_hex(&o->oid));
die("git upload-pack: not our ref %s",
oid_to_hex(&o->oid));
}
}
}

Expand Down Expand Up @@ -936,7 +941,7 @@ static void receive_needs(struct packet_reader *reader, struct object_array *wan
* by another process that handled the initial request.
*/
if (has_non_tip)
check_non_tip(want_obj);
check_non_tip(want_obj, &writer);

if (!use_sideband && daemon_mode)
no_progress = 1;
Expand Down

0 comments on commit 57a6b93

Please sign in to comment.