Skip to content

Commit b23e0b9

Browse files
committed
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 8af0605 edca415 (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 8af0605 (tags/v1.8.3) edca415 (tags/v1.8.3^0) Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9608a19 commit b23e0b9

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

builtin/name-rev.c

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "tag.h"
55
#include "refs.h"
66
#include "parse-options.h"
7+
#include "sha1-lookup.h"
78

89
#define CUTOFF_DATE_SLOP 86400 /* one day */
910

@@ -113,6 +114,34 @@ struct name_ref_data {
113114
const char *ref_filter;
114115
};
115116

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+
116145
static int name_ref(const char *path, const unsigned char *sha1, int flags, void *cb_data)
117146
{
118147
struct object *o = parse_object(sha1);
@@ -135,6 +164,8 @@ static int name_ref(const char *path, const unsigned char *sha1, int flags, void
135164
}
136165
}
137166

167+
add_to_tip_table(sha1, path, can_abbreviate_output);
168+
138169
while (o && o->type == OBJ_TAG) {
139170
struct tag *t = (struct tag *) o;
140171
if (!t->tagged)
@@ -151,6 +182,32 @@ static int name_ref(const char *path, const unsigned char *sha1, int flags, void
151182
return 0;
152183
}
153184

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+
154211
/* returns a static buffer */
155212
static const char *get_rev_name(const struct object *o)
156213
{
@@ -159,7 +216,7 @@ static const char *get_rev_name(const struct object *o)
159216
struct commit *c;
160217

161218
if (o->type != OBJ_COMMIT)
162-
return NULL;
219+
return get_exact_ref_match(o);
163220
c = (struct commit *) o;
164221
n = c->util;
165222
if (!n)

0 commit comments

Comments
 (0)