]> git.ipfire.org Git - thirdparty/git.git/commitdiff
check_everything_connected(): refactor to use an iterator
authorJunio C Hamano <gitster@pobox.com>
Fri, 2 Sep 2011 23:22:47 +0000 (16:22 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 9 Sep 2011 22:07:56 +0000 (15:07 -0700)
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>
builtin/fetch.c

index fc254b6b961913ad84879e2a4c8887f351ee9b33..0ef912eac0f76b4195d9ec7a3d9021b8ec020fa1 100644 (file)
@@ -346,27 +346,34 @@ static int update_local_ref(struct ref *ref,
 }
 
 /*
- * The ref_map records the tips of the refs we are fetching. If
+ * Take callback data, and return next object name in the buffer.
+ * When called after returning the name for the last object, return -1
+ * to signal EOF, otherwise return 0.
+ */
+typedef int (*sha1_iterate_fn)(void *, unsigned char [20]);
+
+/*
+ * If we feed all the commits we want to verify to this command
  *
  *  $ git rev-list --verify-objects --stdin --not --all
  *
- * (feeding all the refs in ref_map on its standard input) does not
- * error out, that means everything reachable from these updated refs
- * locally exists and is connected to some of our existing refs.
+ * and if it does not error out, that means everything reachable from
+ * these commits locally exists and is connected to some of our
+ * existing refs.
  *
  * Returns 0 if everything is connected, non-zero otherwise.
  */
-static int check_everything_connected(struct ref *ref_map, int quiet)
+static int check_everything_connected(sha1_iterate_fn fn, int quiet, void *cb_data)
 {
        struct child_process rev_list;
        const char *argv[] = {"rev-list", "--verify-objects",
                              "--stdin", "--not", "--all", NULL, NULL};
        char commit[41];
-       struct ref *ref;
+       unsigned char sha1[20];
        int err = 0;
 
-       if (!ref_map)
-               return 0;
+       if (fn(cb_data, sha1))
+               return err;
 
        if (quiet)
                argv[5] = "--quiet";
@@ -383,8 +390,8 @@ static int check_everything_connected(struct ref *ref_map, int quiet)
        sigchain_push(SIGPIPE, SIG_IGN);
 
        commit[40] = '\n';
-       for (ref = ref_map; ref; ref = ref->next) {
-               memcpy(commit, sha1_to_hex(ref->old_sha1), 40);
+       do {
+               memcpy(commit, sha1_to_hex(sha1), 40);
                if (write_in_full(rev_list.in, commit, 41) < 0) {
                        if (errno != EPIPE && errno != EINVAL)
                                error(_("failed write to rev-list: %s"),
@@ -392,17 +399,29 @@ static int check_everything_connected(struct ref *ref_map, int quiet)
                        err = -1;
                        break;
                }
-       }
+       } while (!fn(cb_data, sha1));
+
        if (close(rev_list.in)) {
                error(_("failed to close rev-list's stdin: %s"), strerror(errno));
                err = -1;
        }
 
        sigchain_pop(SIGPIPE);
-
        return finish_command(&rev_list) || err;
 }
 
+static int iterate_ref_map(void *cb_data, unsigned char sha1[20])
+{
+       struct ref **rm = cb_data;
+       struct ref *ref = *rm;
+
+       if (!ref)
+               return -1; /* end of the list */
+       *rm = ref->next;
+       hashcpy(sha1, ref->old_sha1);
+       return 0;
+}
+
 static int store_updated_refs(const char *raw_url, const char *remote_name,
                struct ref *ref_map)
 {
@@ -423,7 +442,8 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
        else
                url = xstrdup("foreign");
 
-       if (check_everything_connected(ref_map, 0))
+       rm = ref_map;
+       if (check_everything_connected(iterate_ref_map, 0, &rm))
                return error(_("%s did not send all necessary objects\n"), url);
 
        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,
  */
 static int quickfetch(struct ref *ref_map)
 {
+       struct ref *rm = ref_map;
+
        /*
         * If we are deepening a shallow clone we already have these
         * objects reachable.  Running rev-list here will return with
@@ -531,7 +553,7 @@ static int quickfetch(struct ref *ref_map)
         */
        if (depth)
                return -1;
-       return check_everything_connected(ref_map, 1);
+       return check_everything_connected(iterate_ref_map, 1, &rm);
 }
 
 static int fetch_refs(struct transport *transport, struct ref *ref_map)