Merge branch 'sg/commit-graph-validate'
authorJunio C Hamano <gitster@pobox.com>
Thu, 22 Aug 2019 19:34:11 +0000 (12:34 -0700)
committerJunio C Hamano <gitster@pobox.com>
Thu, 22 Aug 2019 19:34:11 +0000 (12:34 -0700)
The code to write commit-graph over given commit object names has
been made a bit more robust.

* sg/commit-graph-validate:
commit-graph: error out on invalid commit oids in 'write --stdin-commits'
commit-graph: turn a group of write-related macro flags into an enum
t5318-commit-graph: use 'test_expect_code'

1  2 
commit-graph.c
diff --combined commit-graph.c
index fe954ab5f845e65bc5cad987308aeb6598f2fd24,821900675b27f20f7a1480328f46c53fed97957c..f2888c203b677a8a0091f7f63129efe282899172
@@@ -434,7 -434,6 +434,7 @@@ static struct commit_graph *load_commit
  
        free(oids);
        fclose(fp);
 +      strbuf_release(&line);
  
        return graph_chain;
  }
@@@ -783,7 -782,8 +783,8 @@@ struct write_commit_graph_context 
  
        unsigned append:1,
                 report_progress:1,
-                split:1;
+                split:1,
+                check_oids:1;
  
        const struct split_commit_graph_opts *split_opts;
  };
@@@ -1134,7 -1134,8 +1135,8 @@@ static int add_ref_to_list(const char *
        return 0;
  }
  
- int write_commit_graph_reachable(const char *obj_dir, unsigned int flags,
+ int write_commit_graph_reachable(const char *obj_dir,
+                                enum commit_graph_write_flags flags,
                                 const struct split_commit_graph_opts *split_opts)
  {
        struct string_list list = STRING_LIST_INIT_DUP;
@@@ -1187,14 -1188,14 +1189,14 @@@ static int fill_oids_from_packs(struct 
        }
  
        stop_progress(&ctx->progress);
 -      strbuf_reset(&progress_title);
 +      strbuf_release(&progress_title);
        strbuf_release(&packname);
  
        return 0;
  }
  
- static void fill_oids_from_commit_hex(struct write_commit_graph_context *ctx,
-                                     struct string_list *commit_hex)
+ static int fill_oids_from_commit_hex(struct write_commit_graph_context *ctx,
+                                    struct string_list *commit_hex)
  {
        uint32_t i;
        struct strbuf progress_title = STRBUF_INIT;
                struct commit *result;
  
                display_progress(ctx->progress, i + 1);
-               if (commit_hex->items[i].string &&
-                   parse_oid_hex(commit_hex->items[i].string, &oid, &end))
-                       continue;
-               result = lookup_commit_reference_gently(ctx->r, &oid, 1);
-               if (result) {
+               if (!parse_oid_hex(commit_hex->items[i].string, &oid, &end) &&
+                   (result = lookup_commit_reference_gently(ctx->r, &oid, 1))) {
                        ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
                        oidcpy(&ctx->oids.list[ctx->oids.nr], &(result->object.oid));
                        ctx->oids.nr++;
+               } else if (ctx->check_oids) {
+                       error(_("invalid commit object id: %s"),
+                           commit_hex->items[i].string);
+                       return -1;
                }
        }
        stop_progress(&ctx->progress);
        strbuf_release(&progress_title);
+       return 0;
  }
  
  static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
@@@ -1637,7 -1639,7 +1640,7 @@@ static void sort_and_scan_merged_commit
                                num_parents++;
  
                        if (num_parents > 2)
 -                              ctx->num_extra_edges += num_parents - 2;
 +                              ctx->num_extra_edges += num_parents - 1;
                }
        }
  
@@@ -1714,8 -1716,10 +1717,8 @@@ static void expire_commit_graphs(struc
        strbuf_addstr(&path, "/info/commit-graphs");
        dir = opendir(path.buf);
  
 -      if (!dir) {
 -              strbuf_release(&path);
 -              return;
 -      }
 +      if (!dir)
 +              goto out;
  
        strbuf_addch(&path, '/');
        dirnamelen = path.len;
                if (!found)
                        unlink(path.buf);
        }
 +
 +out:
 +      strbuf_release(&path);
  }
  
  int write_commit_graph(const char *obj_dir,
                       struct string_list *pack_indexes,
                       struct string_list *commit_hex,
-                      unsigned int flags,
+                      enum commit_graph_write_flags flags,
                       const struct split_commit_graph_opts *split_opts)
  {
        struct write_commit_graph_context *ctx;
        if (len && ctx->obj_dir[len - 1] == '/')
                ctx->obj_dir[len - 1] = 0;
  
-       ctx->append = flags & COMMIT_GRAPH_APPEND ? 1 : 0;
-       ctx->report_progress = flags & COMMIT_GRAPH_PROGRESS ? 1 : 0;
-       ctx->split = flags & COMMIT_GRAPH_SPLIT ? 1 : 0;
+       ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
+       ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
+       ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
+       ctx->check_oids = flags & COMMIT_GRAPH_WRITE_CHECK_OIDS ? 1 : 0;
        ctx->split_opts = split_opts;
  
        if (ctx->split) {
                        goto cleanup;
        }
  
-       if (commit_hex)
-               fill_oids_from_commit_hex(ctx, commit_hex);
+       if (commit_hex) {
+               if ((res = fill_oids_from_commit_hex(ctx, commit_hex)))
+                       goto cleanup;
+       }
  
        if (!pack_indexes && !commit_hex)
                fill_oids_from_all_packs(ctx);