name-rev: allow converting the exact object name at the tip of a ref

"git name-rev" is supposed to convert given object names into strings that name the same objects based on refs, that can be fed to "git rev-parse" to get the same object names back, so the output for the commit object v1.8.3^0 (i.e. the commit tagged as v1.8.3) $ git rev-parse v1.8.3 v1.8.3^0 | git name-rev --stdin 8af06057d0c31a24e8737ae846ac2e116e8bafb9 edca4152560522a431a51fc0a06147fc680b5b18 (tags/v1.8.3^0) has to have "^0" at the end, as "edca41" is a commit, not the tag that references it. But we do not get anything for the tag object (8af0605) itself. This is because the command however did not bother to see if the object is at the tip of some ref, and failed to convert a tag object. Teach it to show this instead: $ git rev-parse v1.8.3 v1.8.3^0 | git name-rev --stdin 8af06057d0c31a24e8737ae846ac2e116e8bafb9 (tags/v1.8.3) edca4152560522a431a51fc0a06147fc680b5b18 (tags/v1.8.3^0) Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jul 7, 2013 at 14:14 UTC b23e0b9353e38149e791bde0feb1220aa1c8bac1
1 file changed +58 -1
builtin/name-rev.c
+58 -1
@@ -4,6 +4,7 @@
4 #include "tag.h"
5 #include "refs.h"
6 #include "parse-options.h"
7 +#include "sha1-lookup.h"
8
9 #define CUTOFF_DATE_SLOP 86400 /* one day */
10
@@ -113,6 +114,34 @@ struct name_ref_data {
114 const char *ref_filter;
115 };
116
117 +static struct tip_table {
118 + struct tip_table_entry {
119 + unsigned char sha1[20];
120 + const char *refname;
121 + } *table;
122 + int nr;
123 + int alloc;
124 + int sorted;
125 +} tip_table;
126 +
127 +static void add_to_tip_table(const unsigned char *sha1, const char *refname,
128 + int shorten_unambiguous)
129 +{
130 + refname = name_ref_abbrev(refname, shorten_unambiguous);
131 +
132 + ALLOC_GROW(tip_table.table, tip_table.nr + 1, tip_table.alloc);
133 + hashcpy(tip_table.table[tip_table.nr].sha1, sha1);
134 + tip_table.table[tip_table.nr].refname = xstrdup(refname);
135 + tip_table.nr++;
136 + tip_table.sorted = 0;
137 +}
138 +
139 +static int tipcmp(const void *a_, const void *b_)
140 +{
141 + const struct tip_table_entry *a = a_, *b = b_;
142 + return hashcmp(a->sha1, b->sha1);
143 +}
144 +
145 static int name_ref(const char *path, const unsigned char *sha1, int flags, void *cb_data)
146 {
147 struct object *o = parse_object(sha1);
@@ -135,6 +164,8 @@ static int name_ref(const char *path, const unsigned char *sha1, int flags, void
164 }
165 }
166
167 + add_to_tip_table(sha1, path, can_abbreviate_output);
168 +
169 while (o && o->type == OBJ_TAG) {
170 struct tag *t = (struct tag *) o;
171 if (!t->tagged)
@@ -151,6 +182,32 @@ static int name_ref(const char *path, const unsigned char *sha1, int flags, void
182 return 0;
183 }
184
185 +static const unsigned char *nth_tip_table_ent(size_t ix, void *table_)
186 +{
187 + struct tip_table_entry *table = table_;
188 + return table[ix].sha1;
189 +}
190 +
191 +static const char *get_exact_ref_match(const struct object *o)
192 +{
193 + int found;
194 +
195 + if (!tip_table.table || !tip_table.nr)
196 + return NULL;
197 +
198 + if (!tip_table.sorted) {
199 + qsort(tip_table.table, tip_table.nr, sizeof(*tip_table.table),
200 + tipcmp);
201 + tip_table.sorted = 1;
202 + }
203 +
204 + found = sha1_pos(o->sha1, tip_table.table, tip_table.nr,
205 + nth_tip_table_ent);
206 + if (0 <= found)
207 + return tip_table.table[found].refname;
208 + return NULL;
209 +}
210 +
211 /* returns a static buffer */
212 static const char *get_rev_name(const struct object *o)
213 {
@@ -159,7 +216,7 @@ static const char *get_rev_name(const struct object *o)
216 struct commit *c;
217
218 if (o->type != OBJ_COMMIT)
162 - return NULL;
219 + return get_exact_ref_match(o);
220 c = (struct commit *) o;
221 n = c->util;
222 if (!n)