Skip to content

Commit f0e278b

Browse files
committed
check_everything_connected(): refactor to use an iterator
We will be using the same "rev-list --verify-objects" logic to add a sanity check to the receiving end of "git push" in the same way, but the list of commits that are checked come from a structure with a different shape over there. Update the function to take an iterator to make it easier to reuse it in different contexts. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6d4bb38 commit f0e278b

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

builtin/fetch.c

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -346,27 +346,34 @@ static int update_local_ref(struct ref *ref,
346346
}
347347

348348
/*
349-
* The ref_map records the tips of the refs we are fetching. If
349+
* Take callback data, and return next object name in the buffer.
350+
* When called after returning the name for the last object, return -1
351+
* to signal EOF, otherwise return 0.
352+
*/
353+
typedef int (*sha1_iterate_fn)(void *, unsigned char [20]);
354+
355+
/*
356+
* If we feed all the commits we want to verify to this command
350357
*
351358
* $ git rev-list --verify-objects --stdin --not --all
352359
*
353-
* (feeding all the refs in ref_map on its standard input) does not
354-
* error out, that means everything reachable from these updated refs
355-
* locally exists and is connected to some of our existing refs.
360+
* and if it does not error out, that means everything reachable from
361+
* these commits locally exists and is connected to some of our
362+
* existing refs.
356363
*
357364
* Returns 0 if everything is connected, non-zero otherwise.
358365
*/
359-
static int check_everything_connected(struct ref *ref_map, int quiet)
366+
static int check_everything_connected(sha1_iterate_fn fn, int quiet, void *cb_data)
360367
{
361368
struct child_process rev_list;
362369
const char *argv[] = {"rev-list", "--verify-objects",
363370
"--stdin", "--not", "--all", NULL, NULL};
364371
char commit[41];
365-
struct ref *ref;
372+
unsigned char sha1[20];
366373
int err = 0;
367374

368-
if (!ref_map)
369-
return 0;
375+
if (fn(cb_data, sha1))
376+
return err;
370377

371378
if (quiet)
372379
argv[5] = "--quiet";
@@ -383,26 +390,38 @@ static int check_everything_connected(struct ref *ref_map, int quiet)
383390
sigchain_push(SIGPIPE, SIG_IGN);
384391

385392
commit[40] = '\n';
386-
for (ref = ref_map; ref; ref = ref->next) {
387-
memcpy(commit, sha1_to_hex(ref->old_sha1), 40);
393+
do {
394+
memcpy(commit, sha1_to_hex(sha1), 40);
388395
if (write_in_full(rev_list.in, commit, 41) < 0) {
389396
if (errno != EPIPE && errno != EINVAL)
390397
error(_("failed write to rev-list: %s"),
391398
strerror(errno));
392399
err = -1;
393400
break;
394401
}
395-
}
402+
} while (!fn(cb_data, sha1));
403+
396404
if (close(rev_list.in)) {
397405
error(_("failed to close rev-list's stdin: %s"), strerror(errno));
398406
err = -1;
399407
}
400408

401409
sigchain_pop(SIGPIPE);
402-
403410
return finish_command(&rev_list) || err;
404411
}
405412

413+
static int iterate_ref_map(void *cb_data, unsigned char sha1[20])
414+
{
415+
struct ref **rm = cb_data;
416+
struct ref *ref = *rm;
417+
418+
if (!ref)
419+
return -1; /* end of the list */
420+
*rm = ref->next;
421+
hashcpy(sha1, ref->old_sha1);
422+
return 0;
423+
}
424+
406425
static int store_updated_refs(const char *raw_url, const char *remote_name,
407426
struct ref *ref_map)
408427
{
@@ -423,7 +442,8 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
423442
else
424443
url = xstrdup("foreign");
425444

426-
if (check_everything_connected(ref_map, 0))
445+
rm = ref_map;
446+
if (check_everything_connected(iterate_ref_map, 0, &rm))
427447
return error(_("%s did not send all necessary objects\n"), url);
428448

429449
for (rm = ref_map; rm; rm = rm->next) {
@@ -522,6 +542,8 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
522542
*/
523543
static int quickfetch(struct ref *ref_map)
524544
{
545+
struct ref *rm = ref_map;
546+
525547
/*
526548
* If we are deepening a shallow clone we already have these
527549
* objects reachable. Running rev-list here will return with
@@ -531,7 +553,7 @@ static int quickfetch(struct ref *ref_map)
531553
*/
532554
if (depth)
533555
return -1;
534-
return check_everything_connected(ref_map, 1);
556+
return check_everything_connected(iterate_ref_map, 1, &rm);
535557
}
536558

537559
static int fetch_refs(struct transport *transport, struct ref *ref_map)

0 commit comments

Comments
 (0)