On Mon, May 20, 2019 at 10:35:53AM +0200, Alejandro Sanchez wrote:

> alex@polaris:~/slurm/source$ git add -u
> alex@polaris:~/slurm/source$ git am -i --continue
> Applying: Handle duplicate archive file names.
> error: object 861d3c6f689a3ca5eb5fb5c409d46de0ad5555e1 is a commit, not a tree
> BUG: diff-lib.c:526: run_diff_index must be passed exactly one tree
> Aborted (core dumped)

Hmm. So I think the interesting error is probably that first line: some
code expects to look up a tree but sees a commit, and then as a result
it probably feeds too few items to run_diff_index(), triggering the
assertion failure.

Just grepping around, this looks quite suspicious:

  $ git grep -hW get_oid_tree builtin/am.c
  static void write_index_patch(const struct am_state *state)
  ...
  if (!get_oid_tree("HEAD", &head))
        tree = lookup_tree(the_repository, &head);
  else
        tree = lookup_tree(the_repository,
                           the_repository->hash_algo->empty_tree);

Using get_oid_tree() does not actually return a tree; it just
prioritizes trees when disambiguating names (which is pointless here,
since we're not feeding an ambiguous oid). HEAD will always be a commit,
and then lookup_tree() similarly does not peel that down to an actual
tree. And this whole function is called only in interactive-mode, so
it's possible that it's simply not used much and nobody noticed.

I haven't tried to reproduce yet. Is the repository (and patch) that you
used to demonstrate this publicly available? Or alternatively, is it
possible to show a backtrace from the coredump?

If my blind guess is right, then something like this probably fixes it:

diff --git a/builtin/am.c b/builtin/am.c
index bdd1bbc35d..93305560c1 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -1339,9 +1339,17 @@ static void write_index_patch(const struct am_state 
*state)
        struct rev_info rev_info;
        FILE *fp;
 
-       if (!get_oid_tree("HEAD", &head))
-               tree = lookup_tree(the_repository, &head);
-       else
+       if (!get_oid("HEAD", &head)) {
+               struct object *obj;
+               struct commit *commit;
+
+               obj = parse_object_or_die(&head, NULL);
+               commit = object_as_type(the_repository, obj, OBJ_COMMIT, 0);
+               if (!commit)
+                       die("unable to parse HEAD as a commit");
+
+               tree = get_commit_tree(commit);
+       } else
                tree = lookup_tree(the_repository,
                                   the_repository->hash_algo->empty_tree);
 

-Peff

Reply via email to