6 #include "parse-options.h"
7 #include "sha1-lookup.h"
9 #define CUTOFF_DATE_SLOP 86400 /* one day */
11 typedef struct rev_name
{
13 unsigned long taggerdate
;
19 static long cutoff
= LONG_MAX
;
21 /* How many generations are maximally preferred over _one_ merge traversal? */
22 #define MERGE_TRAVERSAL_WEIGHT 65535
24 static int is_better_name(struct rev_name
*name
,
26 unsigned long taggerdate
,
32 * When comparing names based on tags, prefer names
33 * based on the older tag, even if it is farther away.
35 if (from_tag
&& name
->from_tag
)
36 return (name
->taggerdate
> taggerdate
||
37 (name
->taggerdate
== taggerdate
&&
38 name
->distance
> distance
));
41 * We know that at least one of them is a non-tag at this point.
42 * favor a tag over a non-tag.
44 if (name
->from_tag
!= from_tag
)
48 * We are now looking at two non-tags. Tiebreak to favor
51 if (name
->distance
!= distance
)
52 return name
->distance
> distance
;
54 /* ... or tiebreak to favor older date */
55 if (name
->taggerdate
!= taggerdate
)
56 return name
->taggerdate
> taggerdate
;
58 /* keep the current one if we cannot decide */
62 static void name_rev(struct commit
*commit
,
63 const char *tip_name
, unsigned long taggerdate
,
64 int generation
, int distance
, int from_tag
,
67 struct rev_name
*name
= (struct rev_name
*)commit
->util
;
68 struct commit_list
*parents
;
69 int parent_number
= 1;
74 if (commit
->date
< cutoff
)
78 tip_name
= to_free
= xstrfmt("%s^0", tip_name
);
81 die("generation: %d, but deref?", generation
);
85 name
= xmalloc(sizeof(rev_name
));
88 } else if (is_better_name(name
, tip_name
, taggerdate
,
89 generation
, distance
, from_tag
)) {
91 name
->tip_name
= tip_name
;
92 name
->taggerdate
= taggerdate
;
93 name
->generation
= generation
;
94 name
->distance
= distance
;
95 name
->from_tag
= from_tag
;
101 for (parents
= commit
->parents
;
103 parents
= parents
->next
, parent_number
++) {
104 if (parent_number
> 1) {
108 strip_suffix(tip_name
, "^0", &len
);
110 new_name
= xstrfmt("%.*s~%d^%d", (int)len
, tip_name
,
111 generation
, parent_number
);
113 new_name
= xstrfmt("%.*s^%d", (int)len
, tip_name
,
116 name_rev(parents
->item
, new_name
, taggerdate
, 0,
117 distance
+ MERGE_TRAVERSAL_WEIGHT
,
120 name_rev(parents
->item
, tip_name
, taggerdate
,
121 generation
+ 1, distance
+ 1,
127 static int subpath_matches(const char *path
, const char *filter
)
129 const char *subpath
= path
;
132 if (!wildmatch(filter
, subpath
, 0, NULL
))
133 return subpath
- path
;
134 subpath
= strchr(subpath
, '/');
141 static const char *name_ref_abbrev(const char *refname
, int shorten_unambiguous
)
143 if (shorten_unambiguous
)
144 refname
= shorten_unambiguous_ref(refname
, 0);
145 else if (starts_with(refname
, "refs/heads/"))
146 refname
= refname
+ 11;
147 else if (starts_with(refname
, "refs/"))
148 refname
= refname
+ 5;
152 struct name_ref_data
{
155 struct string_list ref_filters
;
156 struct string_list exclude_filters
;
159 static struct tip_table
{
160 struct tip_table_entry
{
161 unsigned char sha1
[20];
169 static void add_to_tip_table(const unsigned char *sha1
, const char *refname
,
170 int shorten_unambiguous
)
172 refname
= name_ref_abbrev(refname
, shorten_unambiguous
);
174 ALLOC_GROW(tip_table
.table
, tip_table
.nr
+ 1, tip_table
.alloc
);
175 hashcpy(tip_table
.table
[tip_table
.nr
].sha1
, sha1
);
176 tip_table
.table
[tip_table
.nr
].refname
= xstrdup(refname
);
178 tip_table
.sorted
= 0;
181 static int tipcmp(const void *a_
, const void *b_
)
183 const struct tip_table_entry
*a
= a_
, *b
= b_
;
184 return hashcmp(a
->sha1
, b
->sha1
);
187 static int name_ref(const char *path
, const struct object_id
*oid
, int flags
, void *cb_data
)
189 struct object
*o
= parse_object(oid
->hash
);
190 struct name_ref_data
*data
= cb_data
;
191 int can_abbreviate_output
= data
->tags_only
&& data
->name_only
;
193 unsigned long taggerdate
= ULONG_MAX
;
195 if (data
->tags_only
&& !starts_with(path
, "refs/tags/"))
198 if (data
->exclude_filters
.nr
) {
199 struct string_list_item
*item
;
201 for_each_string_list_item(item
, &data
->exclude_filters
) {
202 if (subpath_matches(path
, item
->string
) >= 0)
207 if (data
->ref_filters
.nr
) {
208 struct string_list_item
*item
;
211 /* See if any of the patterns match. */
212 for_each_string_list_item(item
, &data
->ref_filters
) {
214 * Check all patterns even after finding a match, so
215 * that we can see if a match with a subpath exists.
216 * When a user asked for 'refs/tags/v*' and 'v1.*',
217 * both of which match, the user is showing her
218 * willingness to accept a shortened output by having
219 * the 'v1.*' in the acceptable refnames, so we
220 * shouldn't stop when seeing 'refs/tags/v1.4' matches
221 * 'refs/tags/v*'. We should show it as 'v1.4'.
223 switch (subpath_matches(path
, item
->string
)) {
224 case -1: /* did not match */
226 case 0: /* matched fully */
229 default: /* matched subpath */
231 can_abbreviate_output
= 1;
236 /* If none of the patterns matched, stop now */
241 add_to_tip_table(oid
->hash
, path
, can_abbreviate_output
);
243 while (o
&& o
->type
== OBJ_TAG
) {
244 struct tag
*t
= (struct tag
*) o
;
246 break; /* broken repository */
247 o
= parse_object(t
->tagged
->oid
.hash
);
249 taggerdate
= t
->date
;
251 if (o
&& o
->type
== OBJ_COMMIT
) {
252 struct commit
*commit
= (struct commit
*)o
;
253 int from_tag
= starts_with(path
, "refs/tags/");
255 if (taggerdate
== ULONG_MAX
)
256 taggerdate
= ((struct commit
*)o
)->date
;
257 path
= name_ref_abbrev(path
, can_abbreviate_output
);
258 name_rev(commit
, xstrdup(path
), taggerdate
, 0, 0,
264 static const unsigned char *nth_tip_table_ent(size_t ix
, void *table_
)
266 struct tip_table_entry
*table
= table_
;
267 return table
[ix
].sha1
;
270 static const char *get_exact_ref_match(const struct object
*o
)
274 if (!tip_table
.table
|| !tip_table
.nr
)
277 if (!tip_table
.sorted
) {
278 QSORT(tip_table
.table
, tip_table
.nr
, tipcmp
);
279 tip_table
.sorted
= 1;
282 found
= sha1_pos(o
->oid
.hash
, tip_table
.table
, tip_table
.nr
,
285 return tip_table
.table
[found
].refname
;
289 /* may return a constant string or use "buf" as scratch space */
290 static const char *get_rev_name(const struct object
*o
, struct strbuf
*buf
)
295 if (o
->type
!= OBJ_COMMIT
)
296 return get_exact_ref_match(o
);
297 c
= (struct commit
*) o
;
305 int len
= strlen(n
->tip_name
);
306 if (len
> 2 && !strcmp(n
->tip_name
+ len
- 2, "^0"))
309 strbuf_addf(buf
, "%.*s~%d", len
, n
->tip_name
, n
->generation
);
314 static void show_name(const struct object
*obj
,
315 const char *caller_name
,
316 int always
, int allow_undefined
, int name_only
)
319 const struct object_id
*oid
= &obj
->oid
;
320 struct strbuf buf
= STRBUF_INIT
;
323 printf("%s ", caller_name
? caller_name
: oid_to_hex(oid
));
324 name
= get_rev_name(obj
, &buf
);
326 printf("%s\n", name
);
327 else if (allow_undefined
)
328 printf("undefined\n");
330 printf("%s\n", find_unique_abbrev(oid
->hash
, DEFAULT_ABBREV
));
332 die("cannot describe '%s'", oid_to_hex(oid
));
333 strbuf_release(&buf
);
336 static char const * const name_rev_usage
[] = {
337 N_("git name-rev [<options>] <commit>..."),
338 N_("git name-rev [<options>] --all"),
339 N_("git name-rev [<options>] --stdin"),
343 static void name_rev_line(char *p
, struct name_ref_data
*data
)
345 struct strbuf buf
= STRBUF_INIT
;
348 for (p_start
= p
; *p
; p
++) {
349 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
352 else if (++forty
== 40 &&
354 unsigned char sha1
[40];
355 const char *name
= NULL
;
357 int p_len
= p
- p_start
+ 1;
362 if (!get_sha1(p
- 39, sha1
)) {
366 name
= get_rev_name(o
, &buf
);
374 printf("%.*s%s", p_len
- 40, p_start
, name
);
376 printf("%.*s (%s)", p_len
, p_start
, name
);
383 fwrite(p_start
, p
- p_start
, 1, stdout
);
385 strbuf_release(&buf
);
388 int cmd_name_rev(int argc
, const char **argv
, const char *prefix
)
390 struct object_array revs
= OBJECT_ARRAY_INIT
;
391 int all
= 0, transform_stdin
= 0, allow_undefined
= 1, always
= 0, peel_tag
= 0;
392 struct name_ref_data data
= { 0, 0, STRING_LIST_INIT_NODUP
, STRING_LIST_INIT_NODUP
};
393 struct option opts
[] = {
394 OPT_BOOL(0, "name-only", &data
.name_only
, N_("print only names (no SHA-1)")),
395 OPT_BOOL(0, "tags", &data
.tags_only
, N_("only use tags to name the commits")),
396 OPT_STRING_LIST(0, "refs", &data
.ref_filters
, N_("pattern"),
397 N_("only use refs matching <pattern>")),
398 OPT_STRING_LIST(0, "exclude", &data
.exclude_filters
, N_("pattern"),
399 N_("ignore refs matching <pattern>")),
401 OPT_BOOL(0, "all", &all
, N_("list all commits reachable from all refs")),
402 OPT_BOOL(0, "stdin", &transform_stdin
, N_("read from stdin")),
403 OPT_BOOL(0, "undefined", &allow_undefined
, N_("allow to print `undefined` names (default)")),
404 OPT_BOOL(0, "always", &always
,
405 N_("show abbreviated commit object as fallback")),
407 /* A Hidden OPT_BOOL */
408 OPTION_SET_INT
, 0, "peel-tag", &peel_tag
, NULL
,
409 N_("dereference tags in the input (internal use)"),
410 PARSE_OPT_NOARG
| PARSE_OPT_HIDDEN
, NULL
, 1,
415 git_config(git_default_config
, NULL
);
416 argc
= parse_options(argc
, argv
, prefix
, opts
, name_rev_usage
, 0);
417 if (all
+ transform_stdin
+ !!argc
> 1) {
418 error("Specify either a list, or --all, not both!");
419 usage_with_options(name_rev_usage
, opts
);
421 if (all
|| transform_stdin
)
424 for (; argc
; argc
--, argv
++) {
425 unsigned char sha1
[20];
426 struct object
*object
;
427 struct commit
*commit
;
429 if (get_sha1(*argv
, sha1
)) {
430 fprintf(stderr
, "Could not get sha1 for %s. Skipping.\n",
436 object
= parse_object(sha1
);
438 struct object
*peeled
= deref_tag(object
, *argv
, 0);
439 if (peeled
&& peeled
->type
== OBJ_COMMIT
)
440 commit
= (struct commit
*)peeled
;
444 fprintf(stderr
, "Could not get object for %s. Skipping.\n",
450 if (cutoff
> commit
->date
)
451 cutoff
= commit
->date
;
456 fprintf(stderr
, "Could not get commit for %s. Skipping.\n",
460 object
= (struct object
*)commit
;
462 add_object_array(object
, *argv
, &revs
);
466 cutoff
= cutoff
- CUTOFF_DATE_SLOP
;
467 for_each_ref(name_ref
, &data
);
469 if (transform_stdin
) {
472 while (!feof(stdin
)) {
473 char *p
= fgets(buffer
, sizeof(buffer
), stdin
);
476 name_rev_line(p
, &data
);
481 max
= get_max_object_index();
482 for (i
= 0; i
< max
; i
++) {
483 struct object
*obj
= get_indexed_object(i
);
484 if (!obj
|| obj
->type
!= OBJ_COMMIT
)
487 always
, allow_undefined
, data
.name_only
);
491 for (i
= 0; i
< revs
.nr
; i
++)
492 show_name(revs
.objects
[i
].item
, revs
.objects
[i
].name
,
493 always
, allow_undefined
, data
.name_only
);