6 #include "parse-options.h"
7 #include "sha1-lookup.h"
9 #define CUTOFF_DATE_SLOP 86400 /* one day */
11 typedef struct rev_name
{
17 static long cutoff
= LONG_MAX
;
19 /* How many generations are maximally preferred over _one_ merge traversal? */
20 #define MERGE_TRAVERSAL_WEIGHT 65535
22 static void name_rev(struct commit
*commit
,
23 const char *tip_name
, int generation
, int distance
,
26 struct rev_name
*name
= (struct rev_name
*)commit
->util
;
27 struct commit_list
*parents
;
28 int parent_number
= 1;
32 if (commit
->date
< cutoff
)
36 char *new_name
= xmalloc(strlen(tip_name
)+3);
37 strcpy(new_name
, tip_name
);
38 strcat(new_name
, "^0");
42 die("generation: %d, but deref?", generation
);
46 name
= xmalloc(sizeof(rev_name
));
49 } else if (name
->distance
> distance
) {
51 name
->tip_name
= tip_name
;
52 name
->generation
= generation
;
53 name
->distance
= distance
;
57 for (parents
= commit
->parents
;
59 parents
= parents
->next
, parent_number
++) {
60 if (parent_number
> 1) {
61 int len
= strlen(tip_name
);
62 char *new_name
= xmalloc(len
+
63 1 + decimal_length(generation
) + /* ~<n> */
67 if (len
> 2 && !strcmp(tip_name
+ len
- 2, "^0"))
70 sprintf(new_name
, "%.*s~%d^%d", len
, tip_name
,
71 generation
, parent_number
);
73 sprintf(new_name
, "%.*s^%d", len
, tip_name
,
76 name_rev(parents
->item
, new_name
, 0,
77 distance
+ MERGE_TRAVERSAL_WEIGHT
, 0);
79 name_rev(parents
->item
, tip_name
, generation
+ 1,
85 static int subpath_matches(const char *path
, const char *filter
)
87 const char *subpath
= path
;
90 if (!wildmatch(filter
, subpath
, 0, NULL
))
91 return subpath
- path
;
92 subpath
= strchr(subpath
, '/');
99 static const char *name_ref_abbrev(const char *refname
, int shorten_unambiguous
)
101 if (shorten_unambiguous
)
102 refname
= shorten_unambiguous_ref(refname
, 0);
103 else if (starts_with(refname
, "refs/heads/"))
104 refname
= refname
+ 11;
105 else if (starts_with(refname
, "refs/"))
106 refname
= refname
+ 5;
110 struct name_ref_data
{
113 const char *ref_filter
;
116 static struct tip_table
{
117 struct tip_table_entry
{
118 unsigned char sha1
[20];
126 static void add_to_tip_table(const unsigned char *sha1
, const char *refname
,
127 int shorten_unambiguous
)
129 refname
= name_ref_abbrev(refname
, shorten_unambiguous
);
131 ALLOC_GROW(tip_table
.table
, tip_table
.nr
+ 1, tip_table
.alloc
);
132 hashcpy(tip_table
.table
[tip_table
.nr
].sha1
, sha1
);
133 tip_table
.table
[tip_table
.nr
].refname
= xstrdup(refname
);
135 tip_table
.sorted
= 0;
138 static int tipcmp(const void *a_
, const void *b_
)
140 const struct tip_table_entry
*a
= a_
, *b
= b_
;
141 return hashcmp(a
->sha1
, b
->sha1
);
144 static int name_ref(const char *path
, const unsigned char *sha1
, int flags
, void *cb_data
)
146 struct object
*o
= parse_object(sha1
);
147 struct name_ref_data
*data
= cb_data
;
148 int can_abbreviate_output
= data
->tags_only
&& data
->name_only
;
151 if (data
->tags_only
&& !starts_with(path
, "refs/tags/"))
154 if (data
->ref_filter
) {
155 switch (subpath_matches(path
, data
->ref_filter
)) {
156 case -1: /* did not match */
158 case 0: /* matched fully */
160 default: /* matched subpath */
161 can_abbreviate_output
= 1;
166 add_to_tip_table(sha1
, path
, can_abbreviate_output
);
168 while (o
&& o
->type
== OBJ_TAG
) {
169 struct tag
*t
= (struct tag
*) o
;
171 break; /* broken repository */
172 o
= parse_object(t
->tagged
->sha1
);
175 if (o
&& o
->type
== OBJ_COMMIT
) {
176 struct commit
*commit
= (struct commit
*)o
;
178 path
= name_ref_abbrev(path
, can_abbreviate_output
);
179 name_rev(commit
, xstrdup(path
), 0, 0, deref
);
184 static const unsigned char *nth_tip_table_ent(size_t ix
, void *table_
)
186 struct tip_table_entry
*table
= table_
;
187 return table
[ix
].sha1
;
190 static const char *get_exact_ref_match(const struct object
*o
)
194 if (!tip_table
.table
|| !tip_table
.nr
)
197 if (!tip_table
.sorted
) {
198 qsort(tip_table
.table
, tip_table
.nr
, sizeof(*tip_table
.table
),
200 tip_table
.sorted
= 1;
203 found
= sha1_pos(o
->sha1
, tip_table
.table
, tip_table
.nr
,
206 return tip_table
.table
[found
].refname
;
210 /* returns a static buffer */
211 static const char *get_rev_name(const struct object
*o
)
213 static char buffer
[1024];
217 if (o
->type
!= OBJ_COMMIT
)
218 return get_exact_ref_match(o
);
219 c
= (struct commit
*) o
;
227 int len
= strlen(n
->tip_name
);
228 if (len
> 2 && !strcmp(n
->tip_name
+ len
- 2, "^0"))
230 snprintf(buffer
, sizeof(buffer
), "%.*s~%d", len
, n
->tip_name
,
237 static void show_name(const struct object
*obj
,
238 const char *caller_name
,
239 int always
, int allow_undefined
, int name_only
)
242 const unsigned char *sha1
= obj
->sha1
;
245 printf("%s ", caller_name
? caller_name
: sha1_to_hex(sha1
));
246 name
= get_rev_name(obj
);
248 printf("%s\n", name
);
249 else if (allow_undefined
)
250 printf("undefined\n");
252 printf("%s\n", find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
254 die("cannot describe '%s'", sha1_to_hex(sha1
));
257 static char const * const name_rev_usage
[] = {
258 N_("git name-rev [options] <commit>..."),
259 N_("git name-rev [options] --all"),
260 N_("git name-rev [options] --stdin"),
264 static void name_rev_line(char *p
, struct name_ref_data
*data
)
268 for (p_start
= p
; *p
; p
++) {
269 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
272 else if (++forty
== 40 &&
274 unsigned char sha1
[40];
275 const char *name
= NULL
;
277 int p_len
= p
- p_start
+ 1;
282 if (!get_sha1(p
- 39, sha1
)) {
286 name
= get_rev_name(o
);
294 printf("%.*s%s", p_len
- 40, p_start
, name
);
296 printf("%.*s (%s)", p_len
, p_start
, name
);
303 fwrite(p_start
, p
- p_start
, 1, stdout
);
306 int cmd_name_rev(int argc
, const char **argv
, const char *prefix
)
308 struct object_array revs
= OBJECT_ARRAY_INIT
;
309 int all
= 0, transform_stdin
= 0, allow_undefined
= 1, always
= 0, peel_tag
= 0;
310 struct name_ref_data data
= { 0, 0, NULL
};
311 struct option opts
[] = {
312 OPT_BOOL(0, "name-only", &data
.name_only
, N_("print only names (no SHA-1)")),
313 OPT_BOOL(0, "tags", &data
.tags_only
, N_("only use tags to name the commits")),
314 OPT_STRING(0, "refs", &data
.ref_filter
, N_("pattern"),
315 N_("only use refs matching <pattern>")),
317 OPT_BOOL(0, "all", &all
, N_("list all commits reachable from all refs")),
318 OPT_BOOL(0, "stdin", &transform_stdin
, N_("read from stdin")),
319 OPT_BOOL(0, "undefined", &allow_undefined
, N_("allow to print `undefined` names (default)")),
320 OPT_BOOL(0, "always", &always
,
321 N_("show abbreviated commit object as fallback")),
323 /* A Hidden OPT_BOOL */
324 OPTION_SET_INT
, 0, "peel-tag", &peel_tag
, NULL
,
325 N_("dereference tags in the input (internal use)"),
326 PARSE_OPT_NOARG
| PARSE_OPT_HIDDEN
, NULL
, 1,
331 git_config(git_default_config
, NULL
);
332 argc
= parse_options(argc
, argv
, prefix
, opts
, name_rev_usage
, 0);
333 if (all
+ transform_stdin
+ !!argc
> 1) {
334 error("Specify either a list, or --all, not both!");
335 usage_with_options(name_rev_usage
, opts
);
337 if (all
|| transform_stdin
)
340 for (; argc
; argc
--, argv
++) {
341 unsigned char sha1
[20];
342 struct object
*object
;
343 struct commit
*commit
;
345 if (get_sha1(*argv
, sha1
)) {
346 fprintf(stderr
, "Could not get sha1 for %s. Skipping.\n",
352 object
= parse_object(sha1
);
354 struct object
*peeled
= deref_tag(object
, *argv
, 0);
355 if (peeled
&& peeled
->type
== OBJ_COMMIT
)
356 commit
= (struct commit
*)peeled
;
360 fprintf(stderr
, "Could not get object for %s. Skipping.\n",
366 if (cutoff
> commit
->date
)
367 cutoff
= commit
->date
;
372 fprintf(stderr
, "Could not get commit for %s. Skipping.\n",
376 object
= (struct object
*)commit
;
378 add_object_array(object
, *argv
, &revs
);
382 cutoff
= cutoff
- CUTOFF_DATE_SLOP
;
383 for_each_ref(name_ref
, &data
);
385 if (transform_stdin
) {
388 while (!feof(stdin
)) {
389 char *p
= fgets(buffer
, sizeof(buffer
), stdin
);
392 name_rev_line(p
, &data
);
397 max
= get_max_object_index();
398 for (i
= 0; i
< max
; i
++) {
399 struct object
*obj
= get_indexed_object(i
);
400 if (!obj
|| obj
->type
!= OBJ_COMMIT
)
403 always
, allow_undefined
, data
.name_only
);
407 for (i
= 0; i
< revs
.nr
; i
++)
408 show_name(revs
.objects
[i
].item
, revs
.objects
[i
].name
,
409 always
, allow_undefined
, data
.name_only
);