]>
Commit | Line | Data |
---|---|---|
d6b64ed0 | 1 | #include "builtin.h" |
bd321bcc JS |
2 | #include "cache.h" |
3 | #include "commit.h" | |
4 | #include "tag.h" | |
5 | #include "refs.h" | |
edefb1a2 | 6 | #include "parse-options.h" |
b23e0b93 | 7 | #include "sha1-lookup.h" |
bd321bcc | 8 | |
c075aea5 JH |
9 | #define CUTOFF_DATE_SLOP 86400 /* one day */ |
10 | ||
bd321bcc JS |
11 | typedef struct rev_name { |
12 | const char *tip_name; | |
75504248 | 13 | unsigned long taggerdate; |
bd321bcc | 14 | int generation; |
ac076c29 | 15 | int distance; |
bd321bcc JS |
16 | } rev_name; |
17 | ||
18 | static long cutoff = LONG_MAX; | |
19 | ||
ac076c29 JS |
20 | /* How many generations are maximally preferred over _one_ merge traversal? */ |
21 | #define MERGE_TRAVERSAL_WEIGHT 65535 | |
22 | ||
bd321bcc | 23 | static void name_rev(struct commit *commit, |
75504248 JS |
24 | const char *tip_name, unsigned long taggerdate, |
25 | int generation, int distance, | |
bd321bcc JS |
26 | int deref) |
27 | { | |
d3ff6f55 | 28 | struct rev_name *name = (struct rev_name *)commit->util; |
bd321bcc | 29 | struct commit_list *parents; |
f2e6f1c9 | 30 | int parent_number = 1; |
bd321bcc | 31 | |
0064053b | 32 | parse_commit(commit); |
bd321bcc JS |
33 | |
34 | if (commit->date < cutoff) | |
35 | return; | |
36 | ||
37 | if (deref) { | |
b2724c87 | 38 | tip_name = xstrfmt("%s^0", tip_name); |
bd321bcc JS |
39 | |
40 | if (generation) | |
41 | die("generation: %d, but deref?", generation); | |
42 | } | |
43 | ||
44 | if (name == NULL) { | |
45 | name = xmalloc(sizeof(rev_name)); | |
d3ff6f55 | 46 | commit->util = name; |
bd321bcc | 47 | goto copy_data; |
75504248 JS |
48 | } else if (name->taggerdate > taggerdate || |
49 | (name->taggerdate == taggerdate && | |
50 | name->distance > distance)) { | |
bd321bcc JS |
51 | copy_data: |
52 | name->tip_name = tip_name; | |
75504248 | 53 | name->taggerdate = taggerdate; |
bd321bcc | 54 | name->generation = generation; |
ac076c29 | 55 | name->distance = distance; |
bd321bcc JS |
56 | } else |
57 | return; | |
58 | ||
59 | for (parents = commit->parents; | |
60 | parents; | |
61 | parents = parents->next, parent_number++) { | |
f2e6f1c9 | 62 | if (parent_number > 1) { |
34e02deb | 63 | size_t len; |
75faa45a | 64 | char *new_name; |
bd321bcc | 65 | |
34e02deb | 66 | strip_suffix(tip_name, "^0", &len); |
bd321bcc | 67 | if (generation > 0) |
34e02deb | 68 | new_name = xstrfmt("%.*s~%d^%d", (int)len, tip_name, |
75faa45a | 69 | generation, parent_number); |
bd321bcc | 70 | else |
34e02deb | 71 | new_name = xstrfmt("%.*s^%d", (int)len, tip_name, |
75faa45a | 72 | parent_number); |
bd321bcc | 73 | |
75504248 | 74 | name_rev(parents->item, new_name, taggerdate, 0, |
ac076c29 | 75 | distance + MERGE_TRAVERSAL_WEIGHT, 0); |
bd321bcc | 76 | } else { |
75504248 JS |
77 | name_rev(parents->item, tip_name, taggerdate, |
78 | generation + 1, distance + 1, 0); | |
bd321bcc JS |
79 | } |
80 | } | |
81 | } | |
82 | ||
98c5c4ad NK |
83 | static int subpath_matches(const char *path, const char *filter) |
84 | { | |
85 | const char *subpath = path; | |
86 | ||
87 | while (subpath) { | |
eb07894f | 88 | if (!wildmatch(filter, subpath, 0, NULL)) |
98c5c4ad NK |
89 | return subpath - path; |
90 | subpath = strchr(subpath, '/'); | |
91 | if (subpath) | |
92 | subpath++; | |
93 | } | |
94 | return -1; | |
95 | } | |
96 | ||
9608a190 JH |
97 | static const char *name_ref_abbrev(const char *refname, int shorten_unambiguous) |
98 | { | |
99 | if (shorten_unambiguous) | |
100 | refname = shorten_unambiguous_ref(refname, 0); | |
59556548 | 101 | else if (starts_with(refname, "refs/heads/")) |
9608a190 | 102 | refname = refname + 11; |
59556548 | 103 | else if (starts_with(refname, "refs/")) |
9608a190 JH |
104 | refname = refname + 5; |
105 | return refname; | |
106 | } | |
107 | ||
2afc29aa JS |
108 | struct name_ref_data { |
109 | int tags_only; | |
23615708 | 110 | int name_only; |
2afc29aa JS |
111 | const char *ref_filter; |
112 | }; | |
113 | ||
b23e0b93 JH |
114 | static struct tip_table { |
115 | struct tip_table_entry { | |
116 | unsigned char sha1[20]; | |
117 | const char *refname; | |
118 | } *table; | |
119 | int nr; | |
120 | int alloc; | |
121 | int sorted; | |
122 | } tip_table; | |
123 | ||
124 | static void add_to_tip_table(const unsigned char *sha1, const char *refname, | |
125 | int shorten_unambiguous) | |
126 | { | |
127 | refname = name_ref_abbrev(refname, shorten_unambiguous); | |
128 | ||
129 | ALLOC_GROW(tip_table.table, tip_table.nr + 1, tip_table.alloc); | |
130 | hashcpy(tip_table.table[tip_table.nr].sha1, sha1); | |
131 | tip_table.table[tip_table.nr].refname = xstrdup(refname); | |
132 | tip_table.nr++; | |
133 | tip_table.sorted = 0; | |
134 | } | |
135 | ||
136 | static int tipcmp(const void *a_, const void *b_) | |
137 | { | |
138 | const struct tip_table_entry *a = a_, *b = b_; | |
139 | return hashcmp(a->sha1, b->sha1); | |
140 | } | |
141 | ||
73868486 | 142 | static int name_ref(const char *path, const struct object_id *oid, int flags, void *cb_data) |
bd321bcc | 143 | { |
73868486 | 144 | struct object *o = parse_object(oid->hash); |
2afc29aa | 145 | struct name_ref_data *data = cb_data; |
98c5c4ad | 146 | int can_abbreviate_output = data->tags_only && data->name_only; |
bd321bcc | 147 | int deref = 0; |
75504248 | 148 | unsigned long taggerdate = ULONG_MAX; |
bd321bcc | 149 | |
59556548 | 150 | if (data->tags_only && !starts_with(path, "refs/tags/")) |
2afc29aa JS |
151 | return 0; |
152 | ||
98c5c4ad NK |
153 | if (data->ref_filter) { |
154 | switch (subpath_matches(path, data->ref_filter)) { | |
155 | case -1: /* did not match */ | |
156 | return 0; | |
157 | case 0: /* matched fully */ | |
158 | break; | |
159 | default: /* matched subpath */ | |
160 | can_abbreviate_output = 1; | |
161 | break; | |
162 | } | |
163 | } | |
bd321bcc | 164 | |
73868486 | 165 | add_to_tip_table(oid->hash, path, can_abbreviate_output); |
b23e0b93 | 166 | |
1974632c | 167 | while (o && o->type == OBJ_TAG) { |
bd321bcc JS |
168 | struct tag *t = (struct tag *) o; |
169 | if (!t->tagged) | |
170 | break; /* broken repository */ | |
ed1c9977 | 171 | o = parse_object(t->tagged->oid.hash); |
bd321bcc | 172 | deref = 1; |
75504248 | 173 | taggerdate = t->date; |
bd321bcc | 174 | } |
1974632c | 175 | if (o && o->type == OBJ_COMMIT) { |
bd321bcc | 176 | struct commit *commit = (struct commit *)o; |
bd321bcc | 177 | |
9608a190 | 178 | path = name_ref_abbrev(path, can_abbreviate_output); |
75504248 | 179 | name_rev(commit, xstrdup(path), taggerdate, 0, 0, deref); |
bd321bcc JS |
180 | } |
181 | return 0; | |
182 | } | |
183 | ||
b23e0b93 JH |
184 | static const unsigned char *nth_tip_table_ent(size_t ix, void *table_) |
185 | { | |
186 | struct tip_table_entry *table = table_; | |
187 | return table[ix].sha1; | |
188 | } | |
189 | ||
190 | static const char *get_exact_ref_match(const struct object *o) | |
191 | { | |
192 | int found; | |
193 | ||
194 | if (!tip_table.table || !tip_table.nr) | |
195 | return NULL; | |
196 | ||
197 | if (!tip_table.sorted) { | |
198 | qsort(tip_table.table, tip_table.nr, sizeof(*tip_table.table), | |
199 | tipcmp); | |
200 | tip_table.sorted = 1; | |
201 | } | |
202 | ||
ed1c9977 | 203 | found = sha1_pos(o->oid.hash, tip_table.table, tip_table.nr, |
b23e0b93 JH |
204 | nth_tip_table_ent); |
205 | if (0 <= found) | |
206 | return tip_table.table[found].refname; | |
207 | return NULL; | |
208 | } | |
209 | ||
bd321bcc | 210 | /* returns a static buffer */ |
da2478db | 211 | static const char *get_rev_name(const struct object *o) |
bd321bcc JS |
212 | { |
213 | static char buffer[1024]; | |
d3ff6f55 LT |
214 | struct rev_name *n; |
215 | struct commit *c; | |
216 | ||
1974632c | 217 | if (o->type != OBJ_COMMIT) |
b23e0b93 | 218 | return get_exact_ref_match(o); |
d3ff6f55 LT |
219 | c = (struct commit *) o; |
220 | n = c->util; | |
bd321bcc | 221 | if (!n) |
a2cf9f44 | 222 | return NULL; |
bd321bcc JS |
223 | |
224 | if (!n->generation) | |
225 | return n->tip_name; | |
59d3f541 JS |
226 | else { |
227 | int len = strlen(n->tip_name); | |
228 | if (len > 2 && !strcmp(n->tip_name + len - 2, "^0")) | |
229 | len -= 2; | |
230 | snprintf(buffer, sizeof(buffer), "%.*s~%d", len, n->tip_name, | |
231 | n->generation); | |
232 | ||
233 | return buffer; | |
234 | } | |
bd321bcc | 235 | } |
1f1e895f | 236 | |
da2478db JH |
237 | static void show_name(const struct object *obj, |
238 | const char *caller_name, | |
239 | int always, int allow_undefined, int name_only) | |
240 | { | |
241 | const char *name; | |
f2fd0760 | 242 | const struct object_id *oid = &obj->oid; |
da2478db JH |
243 | |
244 | if (!name_only) | |
f2fd0760 | 245 | printf("%s ", caller_name ? caller_name : oid_to_hex(oid)); |
da2478db JH |
246 | name = get_rev_name(obj); |
247 | if (name) | |
248 | printf("%s\n", name); | |
249 | else if (allow_undefined) | |
250 | printf("undefined\n"); | |
251 | else if (always) | |
f2fd0760 | 252 | printf("%s\n", find_unique_abbrev(oid->hash, DEFAULT_ABBREV)); |
da2478db | 253 | else |
f2fd0760 | 254 | die("cannot describe '%s'", oid_to_hex(oid)); |
da2478db JH |
255 | } |
256 | ||
edefb1a2 | 257 | static char const * const name_rev_usage[] = { |
9c9b4f2f AH |
258 | N_("git name-rev [<options>] <commit>..."), |
259 | N_("git name-rev [<options>] --all"), | |
260 | N_("git name-rev [<options>] --stdin"), | |
edefb1a2 PH |
261 | NULL |
262 | }; | |
263 | ||
e8b55fab JH |
264 | static void name_rev_line(char *p, struct name_ref_data *data) |
265 | { | |
266 | int forty = 0; | |
267 | char *p_start; | |
268 | for (p_start = p; *p; p++) { | |
269 | #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f')) | |
270 | if (!ishex(*p)) | |
271 | forty = 0; | |
272 | else if (++forty == 40 && | |
273 | !ishex(*(p+1))) { | |
274 | unsigned char sha1[40]; | |
275 | const char *name = NULL; | |
276 | char c = *(p+1); | |
7c5b1675 | 277 | int p_len = p - p_start + 1; |
e8b55fab JH |
278 | |
279 | forty = 0; | |
280 | ||
281 | *(p+1) = 0; | |
282 | if (!get_sha1(p - 39, sha1)) { | |
283 | struct object *o = | |
284 | lookup_object(sha1); | |
285 | if (o) | |
286 | name = get_rev_name(o); | |
287 | } | |
288 | *(p+1) = c; | |
289 | ||
290 | if (!name) | |
291 | continue; | |
292 | ||
7c5b1675 RS |
293 | if (data->name_only) |
294 | printf("%.*s%s", p_len - 40, p_start, name); | |
295 | else | |
296 | printf("%.*s (%s)", p_len, p_start, name); | |
e8b55fab JH |
297 | p_start = p + 1; |
298 | } | |
299 | } | |
300 | ||
301 | /* flush */ | |
302 | if (p_start != p) | |
303 | fwrite(p_start, p - p_start, 1, stdout); | |
304 | } | |
305 | ||
d6b64ed0 | 306 | int cmd_name_rev(int argc, const char **argv, const char *prefix) |
bd321bcc | 307 | { |
3cd47459 | 308 | struct object_array revs = OBJECT_ARRAY_INIT; |
adfc1857 | 309 | int all = 0, transform_stdin = 0, allow_undefined = 1, always = 0, peel_tag = 0; |
23615708 | 310 | struct name_ref_data data = { 0, 0, NULL }; |
edefb1a2 | 311 | struct option opts[] = { |
d5d09d47 SB |
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")), | |
422cad0a NTND |
314 | OPT_STRING(0, "refs", &data.ref_filter, N_("pattern"), |
315 | N_("only use refs matching <pattern>")), | |
edefb1a2 | 316 | OPT_GROUP(""), |
d5d09d47 SB |
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, | |
422cad0a | 321 | N_("show abbreviated commit object as fallback")), |
adfc1857 JH |
322 | { |
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, | |
327 | }, | |
edefb1a2 PH |
328 | OPT_END(), |
329 | }; | |
bd321bcc | 330 | |
ef90d6d4 | 331 | git_config(git_default_config, NULL); |
37782920 | 332 | argc = parse_options(argc, argv, prefix, opts, name_rev_usage, 0); |
05efb7b7 | 333 | if (all + transform_stdin + !!argc > 1) { |
edefb1a2 PH |
334 | error("Specify either a list, or --all, not both!"); |
335 | usage_with_options(name_rev_usage, opts); | |
336 | } | |
337 | if (all || transform_stdin) | |
338 | cutoff = 0; | |
bd321bcc | 339 | |
edefb1a2 | 340 | for (; argc; argc--, argv++) { |
bd321bcc | 341 | unsigned char sha1[20]; |
118aa4ac | 342 | struct object *object; |
bd321bcc JS |
343 | struct commit *commit; |
344 | ||
bd321bcc JS |
345 | if (get_sha1(*argv, sha1)) { |
346 | fprintf(stderr, "Could not get sha1 for %s. Skipping.\n", | |
347 | *argv); | |
348 | continue; | |
349 | } | |
350 | ||
118aa4ac JH |
351 | commit = NULL; |
352 | object = parse_object(sha1); | |
353 | if (object) { | |
354 | struct object *peeled = deref_tag(object, *argv, 0); | |
355 | if (peeled && peeled->type == OBJ_COMMIT) | |
356 | commit = (struct commit *)peeled; | |
357 | } | |
358 | ||
359 | if (!object) { | |
360 | fprintf(stderr, "Could not get object for %s. Skipping.\n", | |
bd321bcc JS |
361 | *argv); |
362 | continue; | |
363 | } | |
364 | ||
118aa4ac JH |
365 | if (commit) { |
366 | if (cutoff > commit->date) | |
367 | cutoff = commit->date; | |
368 | } | |
adfc1857 JH |
369 | |
370 | if (peel_tag) { | |
371 | if (!commit) { | |
372 | fprintf(stderr, "Could not get commit for %s. Skipping.\n", | |
373 | *argv); | |
374 | continue; | |
375 | } | |
376 | object = (struct object *)commit; | |
377 | } | |
118aa4ac | 378 | add_object_array(object, *argv, &revs); |
bd321bcc JS |
379 | } |
380 | ||
c075aea5 JH |
381 | if (cutoff) |
382 | cutoff = cutoff - CUTOFF_DATE_SLOP; | |
73868486 | 383 | for_each_ref(name_ref, &data); |
bd321bcc JS |
384 | |
385 | if (transform_stdin) { | |
386 | char buffer[2048]; | |
bd321bcc JS |
387 | |
388 | while (!feof(stdin)) { | |
e8b55fab | 389 | char *p = fgets(buffer, sizeof(buffer), stdin); |
bd321bcc JS |
390 | if (!p) |
391 | break; | |
e8b55fab | 392 | name_rev_line(p, &data); |
bd321bcc JS |
393 | } |
394 | } else if (all) { | |
fc046a75 | 395 | int i, max; |
bd321bcc | 396 | |
fc046a75 | 397 | max = get_max_object_index(); |
a83123de BS |
398 | for (i = 0; i < max; i++) { |
399 | struct object *obj = get_indexed_object(i); | |
e8b14d7e | 400 | if (!obj || obj->type != OBJ_COMMIT) |
a83123de BS |
401 | continue; |
402 | show_name(obj, NULL, | |
da2478db | 403 | always, allow_undefined, data.name_only); |
a83123de | 404 | } |
1f1e895f LT |
405 | } else { |
406 | int i; | |
da2478db JH |
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); | |
1f1e895f | 410 | } |
bd321bcc JS |
411 | |
412 | return 0; | |
413 | } |