]>
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" |
bd321bcc | 7 | |
c075aea5 JH |
8 | #define CUTOFF_DATE_SLOP 86400 /* one day */ |
9 | ||
bd321bcc JS |
10 | typedef struct rev_name { |
11 | const char *tip_name; | |
bd321bcc | 12 | int generation; |
ac076c29 | 13 | int distance; |
bd321bcc JS |
14 | } rev_name; |
15 | ||
16 | static long cutoff = LONG_MAX; | |
17 | ||
ac076c29 JS |
18 | /* How many generations are maximally preferred over _one_ merge traversal? */ |
19 | #define MERGE_TRAVERSAL_WEIGHT 65535 | |
20 | ||
bd321bcc | 21 | static void name_rev(struct commit *commit, |
ac076c29 | 22 | const char *tip_name, int generation, int distance, |
bd321bcc JS |
23 | int deref) |
24 | { | |
d3ff6f55 | 25 | struct rev_name *name = (struct rev_name *)commit->util; |
bd321bcc | 26 | struct commit_list *parents; |
f2e6f1c9 | 27 | int parent_number = 1; |
bd321bcc JS |
28 | |
29 | if (!commit->object.parsed) | |
30 | parse_commit(commit); | |
31 | ||
32 | if (commit->date < cutoff) | |
33 | return; | |
34 | ||
35 | if (deref) { | |
36 | char *new_name = xmalloc(strlen(tip_name)+3); | |
37 | strcpy(new_name, tip_name); | |
38 | strcat(new_name, "^0"); | |
39 | tip_name = new_name; | |
40 | ||
41 | if (generation) | |
42 | die("generation: %d, but deref?", generation); | |
43 | } | |
44 | ||
45 | if (name == NULL) { | |
46 | name = xmalloc(sizeof(rev_name)); | |
d3ff6f55 | 47 | commit->util = name; |
bd321bcc | 48 | goto copy_data; |
ac076c29 | 49 | } else if (name->distance > distance) { |
bd321bcc JS |
50 | copy_data: |
51 | name->tip_name = tip_name; | |
bd321bcc | 52 | name->generation = generation; |
ac076c29 | 53 | name->distance = distance; |
bd321bcc JS |
54 | } else |
55 | return; | |
56 | ||
57 | for (parents = commit->parents; | |
58 | parents; | |
59 | parents = parents->next, parent_number++) { | |
f2e6f1c9 | 60 | if (parent_number > 1) { |
59d3f541 | 61 | int len = strlen(tip_name); |
cf606e3d AW |
62 | char *new_name = xmalloc(len + |
63 | 1 + decimal_length(generation) + /* ~<n> */ | |
64 | 1 + 2 + /* ^NN */ | |
65 | 1); | |
bd321bcc | 66 | |
59d3f541 JS |
67 | if (len > 2 && !strcmp(tip_name + len - 2, "^0")) |
68 | len -= 2; | |
bd321bcc | 69 | if (generation > 0) |
59d3f541 | 70 | sprintf(new_name, "%.*s~%d^%d", len, tip_name, |
bd321bcc JS |
71 | generation, parent_number); |
72 | else | |
59d3f541 JS |
73 | sprintf(new_name, "%.*s^%d", len, tip_name, |
74 | parent_number); | |
bd321bcc | 75 | |
ac076c29 JS |
76 | name_rev(parents->item, new_name, 0, |
77 | distance + MERGE_TRAVERSAL_WEIGHT, 0); | |
bd321bcc | 78 | } else { |
ac076c29 JS |
79 | name_rev(parents->item, tip_name, generation + 1, |
80 | distance + 1, 0); | |
bd321bcc JS |
81 | } |
82 | } | |
83 | } | |
84 | ||
98c5c4ad NK |
85 | static int subpath_matches(const char *path, const char *filter) |
86 | { | |
87 | const char *subpath = path; | |
88 | ||
89 | while (subpath) { | |
90 | if (!fnmatch(filter, subpath, 0)) | |
91 | return subpath - path; | |
92 | subpath = strchr(subpath, '/'); | |
93 | if (subpath) | |
94 | subpath++; | |
95 | } | |
96 | return -1; | |
97 | } | |
98 | ||
9608a190 JH |
99 | static const char *name_ref_abbrev(const char *refname, int shorten_unambiguous) |
100 | { | |
101 | if (shorten_unambiguous) | |
102 | refname = shorten_unambiguous_ref(refname, 0); | |
103 | else if (!prefixcmp(refname, "refs/heads/")) | |
104 | refname = refname + 11; | |
105 | else if (!prefixcmp(refname, "refs/")) | |
106 | refname = refname + 5; | |
107 | return refname; | |
108 | } | |
109 | ||
2afc29aa JS |
110 | struct name_ref_data { |
111 | int tags_only; | |
23615708 | 112 | int name_only; |
2afc29aa JS |
113 | const char *ref_filter; |
114 | }; | |
115 | ||
8da19775 | 116 | static int name_ref(const char *path, const unsigned char *sha1, int flags, void *cb_data) |
bd321bcc JS |
117 | { |
118 | struct object *o = parse_object(sha1); | |
2afc29aa | 119 | struct name_ref_data *data = cb_data; |
98c5c4ad | 120 | int can_abbreviate_output = data->tags_only && data->name_only; |
bd321bcc JS |
121 | int deref = 0; |
122 | ||
cc44c765 | 123 | if (data->tags_only && prefixcmp(path, "refs/tags/")) |
2afc29aa JS |
124 | return 0; |
125 | ||
98c5c4ad NK |
126 | if (data->ref_filter) { |
127 | switch (subpath_matches(path, data->ref_filter)) { | |
128 | case -1: /* did not match */ | |
129 | return 0; | |
130 | case 0: /* matched fully */ | |
131 | break; | |
132 | default: /* matched subpath */ | |
133 | can_abbreviate_output = 1; | |
134 | break; | |
135 | } | |
136 | } | |
bd321bcc | 137 | |
1974632c | 138 | while (o && o->type == OBJ_TAG) { |
bd321bcc JS |
139 | struct tag *t = (struct tag *) o; |
140 | if (!t->tagged) | |
141 | break; /* broken repository */ | |
142 | o = parse_object(t->tagged->sha1); | |
143 | deref = 1; | |
144 | } | |
1974632c | 145 | if (o && o->type == OBJ_COMMIT) { |
bd321bcc | 146 | struct commit *commit = (struct commit *)o; |
bd321bcc | 147 | |
9608a190 | 148 | path = name_ref_abbrev(path, can_abbreviate_output); |
9befac47 | 149 | name_rev(commit, xstrdup(path), 0, 0, deref); |
bd321bcc JS |
150 | } |
151 | return 0; | |
152 | } | |
153 | ||
154 | /* returns a static buffer */ | |
da2478db | 155 | static const char *get_rev_name(const struct object *o) |
bd321bcc JS |
156 | { |
157 | static char buffer[1024]; | |
d3ff6f55 LT |
158 | struct rev_name *n; |
159 | struct commit *c; | |
160 | ||
1974632c | 161 | if (o->type != OBJ_COMMIT) |
a2cf9f44 | 162 | return NULL; |
d3ff6f55 LT |
163 | c = (struct commit *) o; |
164 | n = c->util; | |
bd321bcc | 165 | if (!n) |
a2cf9f44 | 166 | return NULL; |
bd321bcc JS |
167 | |
168 | if (!n->generation) | |
169 | return n->tip_name; | |
59d3f541 JS |
170 | else { |
171 | int len = strlen(n->tip_name); | |
172 | if (len > 2 && !strcmp(n->tip_name + len - 2, "^0")) | |
173 | len -= 2; | |
174 | snprintf(buffer, sizeof(buffer), "%.*s~%d", len, n->tip_name, | |
175 | n->generation); | |
176 | ||
177 | return buffer; | |
178 | } | |
bd321bcc | 179 | } |
1f1e895f | 180 | |
da2478db JH |
181 | static void show_name(const struct object *obj, |
182 | const char *caller_name, | |
183 | int always, int allow_undefined, int name_only) | |
184 | { | |
185 | const char *name; | |
186 | const unsigned char *sha1 = obj->sha1; | |
187 | ||
188 | if (!name_only) | |
189 | printf("%s ", caller_name ? caller_name : sha1_to_hex(sha1)); | |
190 | name = get_rev_name(obj); | |
191 | if (name) | |
192 | printf("%s\n", name); | |
193 | else if (allow_undefined) | |
194 | printf("undefined\n"); | |
195 | else if (always) | |
196 | printf("%s\n", find_unique_abbrev(sha1, DEFAULT_ABBREV)); | |
197 | else | |
198 | die("cannot describe '%s'", sha1_to_hex(sha1)); | |
199 | } | |
200 | ||
edefb1a2 | 201 | static char const * const name_rev_usage[] = { |
422cad0a NTND |
202 | N_("git name-rev [options] <commit>..."), |
203 | N_("git name-rev [options] --all"), | |
204 | N_("git name-rev [options] --stdin"), | |
edefb1a2 PH |
205 | NULL |
206 | }; | |
207 | ||
e8b55fab JH |
208 | static void name_rev_line(char *p, struct name_ref_data *data) |
209 | { | |
210 | int forty = 0; | |
211 | char *p_start; | |
212 | for (p_start = p; *p; p++) { | |
213 | #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f')) | |
214 | if (!ishex(*p)) | |
215 | forty = 0; | |
216 | else if (++forty == 40 && | |
217 | !ishex(*(p+1))) { | |
218 | unsigned char sha1[40]; | |
219 | const char *name = NULL; | |
220 | char c = *(p+1); | |
7c5b1675 | 221 | int p_len = p - p_start + 1; |
e8b55fab JH |
222 | |
223 | forty = 0; | |
224 | ||
225 | *(p+1) = 0; | |
226 | if (!get_sha1(p - 39, sha1)) { | |
227 | struct object *o = | |
228 | lookup_object(sha1); | |
229 | if (o) | |
230 | name = get_rev_name(o); | |
231 | } | |
232 | *(p+1) = c; | |
233 | ||
234 | if (!name) | |
235 | continue; | |
236 | ||
7c5b1675 RS |
237 | if (data->name_only) |
238 | printf("%.*s%s", p_len - 40, p_start, name); | |
239 | else | |
240 | printf("%.*s (%s)", p_len, p_start, name); | |
e8b55fab JH |
241 | p_start = p + 1; |
242 | } | |
243 | } | |
244 | ||
245 | /* flush */ | |
246 | if (p_start != p) | |
247 | fwrite(p_start, p - p_start, 1, stdout); | |
248 | } | |
249 | ||
d6b64ed0 | 250 | int cmd_name_rev(int argc, const char **argv, const char *prefix) |
bd321bcc | 251 | { |
3cd47459 | 252 | struct object_array revs = OBJECT_ARRAY_INIT; |
da2478db | 253 | int all = 0, transform_stdin = 0, allow_undefined = 1, always = 0; |
23615708 | 254 | struct name_ref_data data = { 0, 0, NULL }; |
edefb1a2 | 255 | struct option opts[] = { |
422cad0a NTND |
256 | OPT_BOOLEAN(0, "name-only", &data.name_only, N_("print only names (no SHA-1)")), |
257 | OPT_BOOLEAN(0, "tags", &data.tags_only, N_("only use tags to name the commits")), | |
258 | OPT_STRING(0, "refs", &data.ref_filter, N_("pattern"), | |
259 | N_("only use refs matching <pattern>")), | |
edefb1a2 | 260 | OPT_GROUP(""), |
422cad0a NTND |
261 | OPT_BOOLEAN(0, "all", &all, N_("list all commits reachable from all refs")), |
262 | OPT_BOOLEAN(0, "stdin", &transform_stdin, N_("read from stdin")), | |
263 | OPT_BOOLEAN(0, "undefined", &allow_undefined, N_("allow to print `undefined` names")), | |
da2478db | 264 | OPT_BOOLEAN(0, "always", &always, |
422cad0a | 265 | N_("show abbreviated commit object as fallback")), |
edefb1a2 PH |
266 | OPT_END(), |
267 | }; | |
bd321bcc | 268 | |
ef90d6d4 | 269 | git_config(git_default_config, NULL); |
37782920 | 270 | argc = parse_options(argc, argv, prefix, opts, name_rev_usage, 0); |
edefb1a2 PH |
271 | if (!!all + !!transform_stdin + !!argc > 1) { |
272 | error("Specify either a list, or --all, not both!"); | |
273 | usage_with_options(name_rev_usage, opts); | |
274 | } | |
275 | if (all || transform_stdin) | |
276 | cutoff = 0; | |
bd321bcc | 277 | |
edefb1a2 | 278 | for (; argc; argc--, argv++) { |
bd321bcc JS |
279 | unsigned char sha1[20]; |
280 | struct object *o; | |
281 | struct commit *commit; | |
282 | ||
bd321bcc JS |
283 | if (get_sha1(*argv, sha1)) { |
284 | fprintf(stderr, "Could not get sha1 for %s. Skipping.\n", | |
285 | *argv); | |
286 | continue; | |
287 | } | |
288 | ||
9534f40b | 289 | o = deref_tag(parse_object(sha1), *argv, 0); |
1974632c | 290 | if (!o || o->type != OBJ_COMMIT) { |
bd321bcc JS |
291 | fprintf(stderr, "Could not get commit for %s. Skipping.\n", |
292 | *argv); | |
293 | continue; | |
294 | } | |
295 | ||
296 | commit = (struct commit *)o; | |
bd321bcc JS |
297 | if (cutoff > commit->date) |
298 | cutoff = commit->date; | |
1f1e895f | 299 | add_object_array((struct object *)commit, *argv, &revs); |
bd321bcc JS |
300 | } |
301 | ||
c075aea5 JH |
302 | if (cutoff) |
303 | cutoff = cutoff - CUTOFF_DATE_SLOP; | |
2afc29aa | 304 | for_each_ref(name_ref, &data); |
bd321bcc JS |
305 | |
306 | if (transform_stdin) { | |
307 | char buffer[2048]; | |
bd321bcc JS |
308 | |
309 | while (!feof(stdin)) { | |
e8b55fab | 310 | char *p = fgets(buffer, sizeof(buffer), stdin); |
bd321bcc JS |
311 | if (!p) |
312 | break; | |
e8b55fab | 313 | name_rev_line(p, &data); |
bd321bcc JS |
314 | } |
315 | } else if (all) { | |
fc046a75 | 316 | int i, max; |
bd321bcc | 317 | |
fc046a75 | 318 | max = get_max_object_index(); |
a83123de BS |
319 | for (i = 0; i < max; i++) { |
320 | struct object *obj = get_indexed_object(i); | |
e8b14d7e | 321 | if (!obj || obj->type != OBJ_COMMIT) |
a83123de BS |
322 | continue; |
323 | show_name(obj, NULL, | |
da2478db | 324 | always, allow_undefined, data.name_only); |
a83123de | 325 | } |
1f1e895f LT |
326 | } else { |
327 | int i; | |
da2478db JH |
328 | for (i = 0; i < revs.nr; i++) |
329 | show_name(revs.objects[i].item, revs.objects[i].name, | |
330 | always, allow_undefined, data.name_only); | |
1f1e895f | 331 | } |
bd321bcc JS |
332 | |
333 | return 0; | |
334 | } |