3 #include "repository.h"
8 #include "parse-options.h"
9 #include "sha1-lookup.h"
10 #include "commit-slab.h"
13 * One day. See the 'name a rev shortly after epoch' test in t6120 when
16 #define CUTOFF_DATE_SLOP 86400
18 typedef struct rev_name
{
20 timestamp_t taggerdate
;
26 define_commit_slab(commit_rev_name
, struct rev_name
*);
28 static timestamp_t cutoff
= TIME_MAX
;
29 static struct commit_rev_name rev_names
;
31 /* How many generations are maximally preferred over _one_ merge traversal? */
32 #define MERGE_TRAVERSAL_WEIGHT 65535
34 static struct rev_name
*get_commit_rev_name(struct commit
*commit
)
36 struct rev_name
**slot
= commit_rev_name_peek(&rev_names
, commit
);
38 return slot
? *slot
: NULL
;
41 static void set_commit_rev_name(struct commit
*commit
, struct rev_name
*name
)
43 *commit_rev_name_at(&rev_names
, commit
) = name
;
46 static int is_better_name(struct rev_name
*name
,
47 timestamp_t taggerdate
,
52 * When comparing names based on tags, prefer names
53 * based on the older tag, even if it is farther away.
55 if (from_tag
&& name
->from_tag
)
56 return (name
->taggerdate
> taggerdate
||
57 (name
->taggerdate
== taggerdate
&&
58 name
->distance
> distance
));
61 * We know that at least one of them is a non-tag at this point.
62 * favor a tag over a non-tag.
64 if (name
->from_tag
!= from_tag
)
68 * We are now looking at two non-tags. Tiebreak to favor
71 if (name
->distance
!= distance
)
72 return name
->distance
> distance
;
74 /* ... or tiebreak to favor older date */
75 if (name
->taggerdate
!= taggerdate
)
76 return name
->taggerdate
> taggerdate
;
78 /* keep the current one if we cannot decide */
82 static void name_rev(struct commit
*commit
,
83 const char *tip_name
, timestamp_t taggerdate
,
84 int generation
, int distance
, int from_tag
,
87 struct rev_name
*name
= get_commit_rev_name(commit
);
88 struct commit_list
*parents
;
89 int parent_number
= 1;
94 if (commit
->date
< cutoff
)
98 tip_name
= to_free
= xstrfmt("%s^0", tip_name
);
101 die("generation: %d, but deref?", generation
);
105 name
= xmalloc(sizeof(rev_name
));
106 set_commit_rev_name(commit
, name
);
108 } else if (is_better_name(name
, taggerdate
, distance
, from_tag
)) {
110 name
->tip_name
= tip_name
;
111 name
->taggerdate
= taggerdate
;
112 name
->generation
= generation
;
113 name
->distance
= distance
;
114 name
->from_tag
= from_tag
;
120 for (parents
= commit
->parents
;
122 parents
= parents
->next
, parent_number
++) {
123 if (parent_number
> 1) {
127 strip_suffix(tip_name
, "^0", &len
);
129 new_name
= xstrfmt("%.*s~%d^%d", (int)len
, tip_name
,
130 generation
, parent_number
);
132 new_name
= xstrfmt("%.*s^%d", (int)len
, tip_name
,
135 name_rev(parents
->item
, new_name
, taggerdate
, 0,
136 distance
+ MERGE_TRAVERSAL_WEIGHT
,
139 name_rev(parents
->item
, tip_name
, taggerdate
,
140 generation
+ 1, distance
+ 1,
146 static int subpath_matches(const char *path
, const char *filter
)
148 const char *subpath
= path
;
151 if (!wildmatch(filter
, subpath
, 0))
152 return subpath
- path
;
153 subpath
= strchr(subpath
, '/');
160 static const char *name_ref_abbrev(const char *refname
, int shorten_unambiguous
)
162 if (shorten_unambiguous
)
163 refname
= shorten_unambiguous_ref(refname
, 0);
164 else if (starts_with(refname
, "refs/heads/"))
165 refname
= refname
+ 11;
166 else if (starts_with(refname
, "refs/"))
167 refname
= refname
+ 5;
171 struct name_ref_data
{
174 struct string_list ref_filters
;
175 struct string_list exclude_filters
;
178 static struct tip_table
{
179 struct tip_table_entry
{
180 struct object_id oid
;
188 static void add_to_tip_table(const struct object_id
*oid
, const char *refname
,
189 int shorten_unambiguous
)
191 refname
= name_ref_abbrev(refname
, shorten_unambiguous
);
193 ALLOC_GROW(tip_table
.table
, tip_table
.nr
+ 1, tip_table
.alloc
);
194 oidcpy(&tip_table
.table
[tip_table
.nr
].oid
, oid
);
195 tip_table
.table
[tip_table
.nr
].refname
= xstrdup(refname
);
197 tip_table
.sorted
= 0;
200 static int tipcmp(const void *a_
, const void *b_
)
202 const struct tip_table_entry
*a
= a_
, *b
= b_
;
203 return oidcmp(&a
->oid
, &b
->oid
);
206 static int name_ref(const char *path
, const struct object_id
*oid
, int flags
, void *cb_data
)
208 struct object
*o
= parse_object(the_repository
, oid
);
209 struct name_ref_data
*data
= cb_data
;
210 int can_abbreviate_output
= data
->tags_only
&& data
->name_only
;
212 timestamp_t taggerdate
= TIME_MAX
;
214 if (data
->tags_only
&& !starts_with(path
, "refs/tags/"))
217 if (data
->exclude_filters
.nr
) {
218 struct string_list_item
*item
;
220 for_each_string_list_item(item
, &data
->exclude_filters
) {
221 if (subpath_matches(path
, item
->string
) >= 0)
226 if (data
->ref_filters
.nr
) {
227 struct string_list_item
*item
;
230 /* See if any of the patterns match. */
231 for_each_string_list_item(item
, &data
->ref_filters
) {
233 * Check all patterns even after finding a match, so
234 * that we can see if a match with a subpath exists.
235 * When a user asked for 'refs/tags/v*' and 'v1.*',
236 * both of which match, the user is showing her
237 * willingness to accept a shortened output by having
238 * the 'v1.*' in the acceptable refnames, so we
239 * shouldn't stop when seeing 'refs/tags/v1.4' matches
240 * 'refs/tags/v*'. We should show it as 'v1.4'.
242 switch (subpath_matches(path
, item
->string
)) {
243 case -1: /* did not match */
245 case 0: /* matched fully */
248 default: /* matched subpath */
250 can_abbreviate_output
= 1;
255 /* If none of the patterns matched, stop now */
260 add_to_tip_table(oid
, path
, can_abbreviate_output
);
262 while (o
&& o
->type
== OBJ_TAG
) {
263 struct tag
*t
= (struct tag
*) o
;
265 break; /* broken repository */
266 o
= parse_object(the_repository
, &t
->tagged
->oid
);
268 taggerdate
= t
->date
;
270 if (o
&& o
->type
== OBJ_COMMIT
) {
271 struct commit
*commit
= (struct commit
*)o
;
272 int from_tag
= starts_with(path
, "refs/tags/");
274 if (taggerdate
== TIME_MAX
)
275 taggerdate
= ((struct commit
*)o
)->date
;
276 path
= name_ref_abbrev(path
, can_abbreviate_output
);
277 name_rev(commit
, xstrdup(path
), taggerdate
, 0, 0,
283 static const unsigned char *nth_tip_table_ent(size_t ix
, void *table_
)
285 struct tip_table_entry
*table
= table_
;
286 return table
[ix
].oid
.hash
;
289 static const char *get_exact_ref_match(const struct object
*o
)
293 if (!tip_table
.table
|| !tip_table
.nr
)
296 if (!tip_table
.sorted
) {
297 QSORT(tip_table
.table
, tip_table
.nr
, tipcmp
);
298 tip_table
.sorted
= 1;
301 found
= sha1_pos(o
->oid
.hash
, tip_table
.table
, tip_table
.nr
,
304 return tip_table
.table
[found
].refname
;
308 /* may return a constant string or use "buf" as scratch space */
309 static const char *get_rev_name(const struct object
*o
, struct strbuf
*buf
)
314 if (o
->type
!= OBJ_COMMIT
)
315 return get_exact_ref_match(o
);
316 c
= (struct commit
*) o
;
317 n
= get_commit_rev_name(c
);
325 strbuf_addstr(buf
, n
->tip_name
);
326 strbuf_strip_suffix(buf
, "^0");
327 strbuf_addf(buf
, "~%d", n
->generation
);
332 static void show_name(const struct object
*obj
,
333 const char *caller_name
,
334 int always
, int allow_undefined
, int name_only
)
337 const struct object_id
*oid
= &obj
->oid
;
338 struct strbuf buf
= STRBUF_INIT
;
341 printf("%s ", caller_name
? caller_name
: oid_to_hex(oid
));
342 name
= get_rev_name(obj
, &buf
);
344 printf("%s\n", name
);
345 else if (allow_undefined
)
346 printf("undefined\n");
348 printf("%s\n", find_unique_abbrev(oid
, DEFAULT_ABBREV
));
350 die("cannot describe '%s'", oid_to_hex(oid
));
351 strbuf_release(&buf
);
354 static char const * const name_rev_usage
[] = {
355 N_("git name-rev [<options>] <commit>..."),
356 N_("git name-rev [<options>] --all"),
357 N_("git name-rev [<options>] --stdin"),
361 static void name_rev_line(char *p
, struct name_ref_data
*data
)
363 struct strbuf buf
= STRBUF_INIT
;
366 const unsigned hexsz
= the_hash_algo
->hexsz
;
368 for (p_start
= p
; *p
; p
++) {
369 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
372 else if (++counter
== hexsz
&&
374 struct object_id oid
;
375 const char *name
= NULL
;
377 int p_len
= p
- p_start
+ 1;
382 if (!get_oid(p
- (hexsz
- 1), &oid
)) {
384 lookup_object(the_repository
, &oid
);
386 name
= get_rev_name(o
, &buf
);
394 printf("%.*s%s", p_len
- hexsz
, p_start
, name
);
396 printf("%.*s (%s)", p_len
, p_start
, name
);
403 fwrite(p_start
, p
- p_start
, 1, stdout
);
405 strbuf_release(&buf
);
408 int cmd_name_rev(int argc
, const char **argv
, const char *prefix
)
410 struct object_array revs
= OBJECT_ARRAY_INIT
;
411 int all
= 0, transform_stdin
= 0, allow_undefined
= 1, always
= 0, peel_tag
= 0;
412 struct name_ref_data data
= { 0, 0, STRING_LIST_INIT_NODUP
, STRING_LIST_INIT_NODUP
};
413 struct option opts
[] = {
414 OPT_BOOL(0, "name-only", &data
.name_only
, N_("print only names (no SHA-1)")),
415 OPT_BOOL(0, "tags", &data
.tags_only
, N_("only use tags to name the commits")),
416 OPT_STRING_LIST(0, "refs", &data
.ref_filters
, N_("pattern"),
417 N_("only use refs matching <pattern>")),
418 OPT_STRING_LIST(0, "exclude", &data
.exclude_filters
, N_("pattern"),
419 N_("ignore refs matching <pattern>")),
421 OPT_BOOL(0, "all", &all
, N_("list all commits reachable from all refs")),
422 OPT_BOOL(0, "stdin", &transform_stdin
, N_("read from stdin")),
423 OPT_BOOL(0, "undefined", &allow_undefined
, N_("allow to print `undefined` names (default)")),
424 OPT_BOOL(0, "always", &always
,
425 N_("show abbreviated commit object as fallback")),
427 /* A Hidden OPT_BOOL */
428 OPTION_SET_INT
, 0, "peel-tag", &peel_tag
, NULL
,
429 N_("dereference tags in the input (internal use)"),
430 PARSE_OPT_NOARG
| PARSE_OPT_HIDDEN
, NULL
, 1,
435 init_commit_rev_name(&rev_names
);
436 git_config(git_default_config
, NULL
);
437 argc
= parse_options(argc
, argv
, prefix
, opts
, name_rev_usage
, 0);
438 if (all
+ transform_stdin
+ !!argc
> 1) {
439 error("Specify either a list, or --all, not both!");
440 usage_with_options(name_rev_usage
, opts
);
442 if (all
|| transform_stdin
)
445 for (; argc
; argc
--, argv
++) {
446 struct object_id oid
;
447 struct object
*object
;
448 struct commit
*commit
;
450 if (get_oid(*argv
, &oid
)) {
451 fprintf(stderr
, "Could not get sha1 for %s. Skipping.\n",
457 object
= parse_object(the_repository
, &oid
);
459 struct object
*peeled
= deref_tag(the_repository
,
461 if (peeled
&& peeled
->type
== OBJ_COMMIT
)
462 commit
= (struct commit
*)peeled
;
466 fprintf(stderr
, "Could not get object for %s. Skipping.\n",
472 if (cutoff
> commit
->date
)
473 cutoff
= commit
->date
;
478 fprintf(stderr
, "Could not get commit for %s. Skipping.\n",
482 object
= (struct object
*)commit
;
484 add_object_array(object
, *argv
, &revs
);
488 /* check for undeflow */
489 if (cutoff
> TIME_MIN
+ CUTOFF_DATE_SLOP
)
490 cutoff
= cutoff
- CUTOFF_DATE_SLOP
;
494 for_each_ref(name_ref
, &data
);
496 if (transform_stdin
) {
499 while (!feof(stdin
)) {
500 char *p
= fgets(buffer
, sizeof(buffer
), stdin
);
503 name_rev_line(p
, &data
);
508 max
= get_max_object_index();
509 for (i
= 0; i
< max
; i
++) {
510 struct object
*obj
= get_indexed_object(i
);
511 if (!obj
|| obj
->type
!= OBJ_COMMIT
)
514 always
, allow_undefined
, data
.name_only
);
518 for (i
= 0; i
< revs
.nr
; i
++)
519 show_name(revs
.objects
[i
].item
, revs
.objects
[i
].name
,
520 always
, allow_undefined
, data
.name_only
);