Merge branch 'ab/get-short-oid'
authorJunio C Hamano <gitster@pobox.com>
Wed, 30 May 2018 05:04:11 +0000 (14:04 +0900)
committerJunio C Hamano <gitster@pobox.com>
Wed, 30 May 2018 05:04:11 +0000 (14:04 +0900)
When a short hexadecimal string is used to name an object but there
are multiple objects that share the string as the prefix of their
names, the code lists these ambiguous candidates in a help message.
These object names are now sorted according to their types for
easier eyeballing.

* ab/get-short-oid:
get_short_oid: sort ambiguous objects by type, then SHA-1
sha1-name.c: move around the collect_ambiguous() function
git-p4: change "commitish" typo to "committish"
sha1-array.h: align function arguments
sha1-name.c: remove stray newline

1  2 
sha1-name.c
diff --cc sha1-name.c
index cc0028ed19d13da0162c42c44bb2dc58c675e30e,46d8b1afa65f174132dffeea1c24abae37f580df..60d9ef3c7e7108c859647656972c171cce4e7d7f
@@@ -373,6 -372,40 +372,40 @@@ static int show_ambiguous_object(const 
        return 0;
  }
  
 -      int a_type = oid_object_info(a, NULL);
 -      int b_type = oid_object_info(b, NULL);
+ static int collect_ambiguous(const struct object_id *oid, void *data)
+ {
+       oid_array_append(data, oid);
+       return 0;
+ }
+ static int sort_ambiguous(const void *a, const void *b)
+ {
++      int a_type = oid_object_info(the_repository, a, NULL);
++      int b_type = oid_object_info(the_repository, b, NULL);
+       int a_type_sort;
+       int b_type_sort;
+       /*
+        * Sorts by hash within the same object type, just as
+        * oid_array_for_each_unique() would do.
+        */
+       if (a_type == b_type)
+               return oidcmp(a, b);
+       /*
+        * Between object types show tags, then commits, and finally
+        * trees and blobs.
+        *
+        * The object_type enum is commit, tree, blob, tag, but we
+        * want tag, commit, tree blob. Cleverly (perhaps too
+        * cleverly) do that with modulus, since the enum assigns 1 to
+        * commit, so tag becomes 0.
+        */
+       a_type_sort = a_type % 4;
+       b_type_sort = b_type % 4;
+       return a_type_sort > b_type_sort ? 1 : -1;
+ }
  static int get_short_oid(const char *name, int len, struct object_id *oid,
                          unsigned flags)
  {