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 <gitster@pobox.com>
Junio C Hamano committed
Sep 2, 2011 at 16:22 UTC
f0e278b1b7a2c0ecbecf644a18c1383dc426db30
1 file changed
+36
-14
builtin/fetch.c
+36
-14
@@ -346,27 +346,34 @@ static int update_local_ref(struct ref *ref,
346
}
347
348
/*
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
357
*
358
* $ git rev-list --verify-objects --stdin --not --all
359
*
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.
363
*
364
* Returns 0 if everything is connected, non-zero otherwise.
365
*/
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)
367
{
368
struct child_process rev_list;
369
const char *argv[] = {"rev-list", "--verify-objects",
370
"--stdin", "--not", "--all", NULL, NULL};
371
char commit[41];
365
- struct ref *ref;
372
+ unsigned char sha1[20];
373
int err = 0;
374
368
- if (!ref_map)
369
- return 0;
375
+ if (fn(cb_data, sha1))
376
+ return err;
377
378
if (quiet)
379
argv[5] = "--quiet";
@@ -383,8 +390,8 @@ static int check_everything_connected(struct ref *ref_map, int quiet)
390
sigchain_push(SIGPIPE, SIG_IGN);
391
392
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);
395
if (write_in_full(rev_list.in, commit, 41) < 0) {
396
if (errno != EPIPE && errno != EINVAL)
397
error(_("failed write to rev-list: %s"),
@@ -392,17 +399,29 @@ static int check_everything_connected(struct ref *ref_map, int quiet)
399
err = -1;
400
break;
401
}
395
- }
402
+ } while (!fn(cb_data, sha1));
403
+
404
if (close(rev_list.in)) {
405
error(_("failed to close rev-list's stdin: %s"), strerror(errno));
406
err = -1;
407
}
408
409
sigchain_pop(SIGPIPE);
402
-
410
return finish_command(&rev_list) || err;
411
}
412
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
+
425
static int store_updated_refs(const char *raw_url, const char *remote_name,
426
struct ref *ref_map)
427
{
@@ -423,7 +442,8 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
442
else
443
url = xstrdup("foreign");
444
426
- if (check_everything_connected(ref_map, 0))
445
+ rm = ref_map;
446
+ if (check_everything_connected(iterate_ref_map, 0, &rm))
447
return error(_("%s did not send all necessary objects\n"), url);
448
449
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,
542
*/
543
static int quickfetch(struct ref *ref_map)
544
{
545
+ struct ref *rm = ref_map;
546
+
547
/*
548
* If we are deepening a shallow clone we already have these
549
* objects reachable. Running rev-list here will return with
@@ -531,7 +553,7 @@ static int quickfetch(struct ref *ref_map)
553
*/
554
if (depth)
555
return -1;
534
- return check_everything_connected(ref_map, 1);
556
+ return check_everything_connected(iterate_ref_map, 1, &rm);
557
}
558
559
static int fetch_refs(struct transport *transport, struct ref *ref_map)