check_everything_connected(): libify

Extract the helper function and the type definition of the iterator function it uses out of builtin/fetch.c into a separate source and a header file. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Sep 2, 2011 at 16:33 UTC f96400cb467af82e21acb3034dd214f4c8be11d0
4 files changed +85 -65
Makefile
+2
@@ -511,6 +511,7 @@ LIB_H += compat/win32/pthread.h
511 LIB_H += compat/win32/syslog.h
512 LIB_H += compat/win32/sys/poll.h
513 LIB_H += compat/win32/dirent.h
514 +LIB_H += connected.h
515 LIB_H += csum-file.h
516 LIB_H += decorate.h
517 LIB_H += delta.h
@@ -587,6 +588,7 @@ LIB_OBJS += combine-diff.o
588 LIB_OBJS += commit.o
589 LIB_OBJS += config.o
590 LIB_OBJS += connect.o
591 +LIB_OBJS += connected.o
592 LIB_OBJS += convert.o
593 LIB_OBJS += copy.o
594 LIB_OBJS += csum-file.o
builtin/fetch.c
+1 -65
@@ -13,6 +13,7 @@
13 #include "sigchain.h"
14 #include "transport.h"
15 #include "submodule.h"
16 +#include "connected.h"
17
18 static const char * const builtin_fetch_usage[] = {
19 "git fetch [<options>] [<repository> [<refspec>...]]",
@@ -345,71 +346,6 @@ static int update_local_ref(struct ref *ref,
346 }
347 }
348
348 -/*
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 - *
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 - */
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];
372 - unsigned char sha1[20];
373 - int err = 0;
374 -
375 - if (fn(cb_data, sha1))
376 - return err;
377 -
378 - if (quiet)
379 - argv[5] = "--quiet";
380 -
381 - memset(&rev_list, 0, sizeof(rev_list));
382 - rev_list.argv = argv;
383 - rev_list.git_cmd = 1;
384 - rev_list.in = -1;
385 - rev_list.no_stdout = 1;
386 - rev_list.no_stderr = quiet;
387 - if (start_command(&rev_list))
388 - return error(_("Could not run 'git rev-list'"));
389 -
390 - sigchain_push(SIGPIPE, SIG_IGN);
391 -
392 - commit[40] = '\n';
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"),
398 - strerror(errno));
399 - err = -1;
400 - break;
401 - }
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);
410 - return finish_command(&rev_list) || err;
411 -}
412 -
349 static int iterate_ref_map(void *cb_data, unsigned char sha1[20])
350 {
351 struct ref **rm = cb_data;
connected.c new
+62
@@ -0,0 +1,62 @@
1 +#include "cache.h"
2 +#include "run-command.h"
3 +#include "sigchain.h"
4 +#include "connected.h"
5 +
6 +/*
7 + * If we feed all the commits we want to verify to this command
8 + *
9 + * $ git rev-list --verify-objects --stdin --not --all
10 + *
11 + * and if it does not error out, that means everything reachable from
12 + * these commits locally exists and is connected to some of our
13 + * existing refs.
14 + *
15 + * Returns 0 if everything is connected, non-zero otherwise.
16 + */
17 +int check_everything_connected(sha1_iterate_fn fn, int quiet, void *cb_data)
18 +{
19 + struct child_process rev_list;
20 + const char *argv[] = {"rev-list", "--verify-objects",
21 + "--stdin", "--not", "--all", NULL, NULL};
22 + char commit[41];
23 + unsigned char sha1[20];
24 + int err = 0;
25 +
26 + if (fn(cb_data, sha1))
27 + return err;
28 +
29 + if (quiet)
30 + argv[5] = "--quiet";
31 +
32 + memset(&rev_list, 0, sizeof(rev_list));
33 + rev_list.argv = argv;
34 + rev_list.git_cmd = 1;
35 + rev_list.in = -1;
36 + rev_list.no_stdout = 1;
37 + rev_list.no_stderr = quiet;
38 + if (start_command(&rev_list))
39 + return error(_("Could not run 'git rev-list'"));
40 +
41 + sigchain_push(SIGPIPE, SIG_IGN);
42 +
43 + commit[40] = '\n';
44 + do {
45 + memcpy(commit, sha1_to_hex(sha1), 40);
46 + if (write_in_full(rev_list.in, commit, 41) < 0) {
47 + if (errno != EPIPE && errno != EINVAL)
48 + error(_("failed write to rev-list: %s"),
49 + strerror(errno));
50 + err = -1;
51 + break;
52 + }
53 + } while (!fn(cb_data, sha1));
54 +
55 + if (close(rev_list.in)) {
56 + error(_("failed to close rev-list's stdin: %s"), strerror(errno));
57 + err = -1;
58 + }
59 +
60 + sigchain_pop(SIGPIPE);
61 + return finish_command(&rev_list) || err;
62 +}
connected.h new
+20
@@ -0,0 +1,20 @@
1 +#ifndef CONNECTED_H
2 +#define CONNECTED_H
3 +
4 +/*
5 + * Take callback data, and return next object name in the buffer.
6 + * When called after returning the name for the last object, return -1
7 + * to signal EOF, otherwise return 0.
8 + */
9 +typedef int (*sha1_iterate_fn)(void *, unsigned char [20]);
10 +
11 +/*
12 + * Make sure that our object store has all the commits necessary to
13 + * connect the ancestry chain to some of our existing refs, and all
14 + * the trees and blobs that these commits use.
15 + *
16 + * Return 0 if Ok, non zero otherwise (i.e. some missing objects)
17 + */
18 +extern int check_everything_connected(sha1_iterate_fn, int quiet, void *cb_data);
19 +
20 +#endif /* CONNECTED_H */