]>
Commit | Line | Data |
---|---|---|
64745109 | 1 | #include "cache.h" |
e091eb93 | 2 | #include "refs.h" |
36f8d174 | 3 | #include "tag.h" |
64745109 | 4 | #include "commit.h" |
9de48752 LT |
5 | #include "tree.h" |
6 | #include "blob.h" | |
1b0c7174 | 7 | #include "tree-walk.h" |
ae563542 LT |
8 | #include "revision.h" |
9 | ||
384e99a4 | 10 | /* bits #0-5 in revision.h */ |
64745109 | 11 | |
384e99a4 | 12 | #define COUNTED (1u<<6) |
8906300f | 13 | |
a6f68d47 | 14 | static const char rev_list_usage[] = |
69e0c256 JH |
15 | "git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n" |
16 | " limiting output:\n" | |
17 | " --max-count=nr\n" | |
18 | " --max-age=epoch\n" | |
19 | " --min-age=epoch\n" | |
20 | " --sparse\n" | |
21 | " --no-merges\n" | |
93b74bca | 22 | " --remove-empty\n" |
69e0c256 JH |
23 | " --all\n" |
24 | " ordering output:\n" | |
69e0c256 | 25 | " --topo-order\n" |
4c8725f1 | 26 | " --date-order\n" |
69e0c256 JH |
27 | " formatting output:\n" |
28 | " --parents\n" | |
c6496575 | 29 | " --objects | --objects-edge\n" |
69e0c256 JH |
30 | " --unpacked\n" |
31 | " --header | --pretty\n" | |
9da5c2f0 | 32 | " --abbrev=nr | --no-abbrev\n" |
69e0c256 JH |
33 | " special purpose:\n" |
34 | " --bisect" | |
35 | ; | |
a6f68d47 | 36 | |
ae563542 LT |
37 | struct rev_info revs; |
38 | ||
8b3a1e05 | 39 | static int bisect_list = 0; |
81f2bb1f | 40 | static int verbose_header = 0; |
9da5c2f0 | 41 | static int abbrev = DEFAULT_ABBREV; |
81f2bb1f | 42 | static int show_parents = 0; |
dc68c4ff | 43 | static int show_timestamp = 0; |
81f2bb1f | 44 | static int hdr_termination = 0; |
d998a089 | 45 | static const char *commit_prefix = ""; |
000182ea | 46 | static enum cmit_fmt commit_format = CMIT_FMT_RAW; |
e646de0d | 47 | |
81f2bb1f LT |
48 | static void show_commit(struct commit *commit) |
49 | { | |
dc68c4ff JH |
50 | if (show_timestamp) |
51 | printf("%lu ", commit->date); | |
52 | if (commit_prefix[0]) | |
53 | fputs(commit_prefix, stdout); | |
384e99a4 JH |
54 | if (commit->object.flags & BOUNDARY) |
55 | putchar('-'); | |
dc68c4ff | 56 | fputs(sha1_to_hex(commit->object.sha1), stdout); |
81f2bb1f LT |
57 | if (show_parents) { |
58 | struct commit_list *parents = commit->parents; | |
59 | while (parents) { | |
88494423 | 60 | struct object *o = &(parents->item->object); |
81f2bb1f | 61 | parents = parents->next; |
88494423 JH |
62 | if (o->flags & TMP_MARK) |
63 | continue; | |
64 | printf(" %s", sha1_to_hex(o->sha1)); | |
65 | o->flags |= TMP_MARK; | |
81f2bb1f | 66 | } |
88494423 JH |
67 | /* TMP_MARK is a general purpose flag that can |
68 | * be used locally, but the user should clean | |
69 | * things up after it is done with them. | |
70 | */ | |
71 | for (parents = commit->parents; | |
72 | parents; | |
73 | parents = parents->next) | |
74 | parents->item->object.flags &= ~TMP_MARK; | |
81f2bb1f | 75 | } |
d87449c5 JH |
76 | if (commit_format == CMIT_FMT_ONELINE) |
77 | putchar(' '); | |
78 | else | |
79 | putchar('\n'); | |
80 | ||
81f2bb1f | 81 | if (verbose_header) { |
000182ea | 82 | static char pretty_header[16384]; |
9da5c2f0 | 83 | pretty_print_commit(commit_format, commit, ~0, pretty_header, sizeof(pretty_header), abbrev); |
000182ea | 84 | printf("%s%c", pretty_header, hdr_termination); |
7620d39f LT |
85 | } |
86 | fflush(stdout); | |
a3437b8c JS |
87 | } |
88 | ||
e646de0d JH |
89 | static struct object_list **process_blob(struct blob *blob, |
90 | struct object_list **p, | |
91 | struct name_path *path, | |
92 | const char *name) | |
9de48752 LT |
93 | { |
94 | struct object *obj = &blob->object; | |
95 | ||
ae563542 | 96 | if (!revs.blob_objects) |
9de48752 LT |
97 | return p; |
98 | if (obj->flags & (UNINTERESTING | SEEN)) | |
99 | return p; | |
100 | obj->flags |= SEEN; | |
e646de0d | 101 | return add_object(obj, p, path, name); |
9de48752 LT |
102 | } |
103 | ||
e646de0d JH |
104 | static struct object_list **process_tree(struct tree *tree, |
105 | struct object_list **p, | |
106 | struct name_path *path, | |
107 | const char *name) | |
9de48752 LT |
108 | { |
109 | struct object *obj = &tree->object; | |
110 | struct tree_entry_list *entry; | |
e646de0d | 111 | struct name_path me; |
9de48752 | 112 | |
ae563542 | 113 | if (!revs.tree_objects) |
9de48752 LT |
114 | return p; |
115 | if (obj->flags & (UNINTERESTING | SEEN)) | |
116 | return p; | |
117 | if (parse_tree(tree) < 0) | |
118 | die("bad tree object %s", sha1_to_hex(obj->sha1)); | |
119 | obj->flags |= SEEN; | |
e646de0d JH |
120 | p = add_object(obj, p, path, name); |
121 | me.up = path; | |
122 | me.elem = name; | |
123 | me.elem_len = strlen(name); | |
b0d8923e LT |
124 | entry = tree->entries; |
125 | tree->entries = NULL; | |
126 | while (entry) { | |
127 | struct tree_entry_list *next = entry->next; | |
9de48752 | 128 | if (entry->directory) |
e646de0d | 129 | p = process_tree(entry->item.tree, p, &me, entry->name); |
9de48752 | 130 | else |
e646de0d | 131 | p = process_blob(entry->item.blob, p, &me, entry->name); |
b0d8923e LT |
132 | free(entry); |
133 | entry = next; | |
9de48752 LT |
134 | } |
135 | return p; | |
136 | } | |
137 | ||
a4a88b2b | 138 | static void show_commit_list(struct rev_info *revs) |
81f2bb1f | 139 | { |
a4a88b2b | 140 | struct commit *commit; |
36f8d174 | 141 | struct object_list *objects = NULL, **p = &objects, *pending; |
81f2bb1f | 142 | |
a4a88b2b | 143 | while ((commit = get_revision(revs)) != NULL) { |
e646de0d | 144 | p = process_tree(commit->tree, p, NULL, ""); |
765ac8ec | 145 | show_commit(commit); |
81f2bb1f | 146 | } |
a4a88b2b | 147 | for (pending = revs->pending_objects; pending; pending = pending->next) { |
36f8d174 LT |
148 | struct object *obj = pending->item; |
149 | const char *name = pending->name; | |
150 | if (obj->flags & (UNINTERESTING | SEEN)) | |
151 | continue; | |
152 | if (obj->type == tag_type) { | |
153 | obj->flags |= SEEN; | |
e646de0d | 154 | p = add_object(obj, p, NULL, name); |
36f8d174 LT |
155 | continue; |
156 | } | |
157 | if (obj->type == tree_type) { | |
e646de0d | 158 | p = process_tree((struct tree *)obj, p, NULL, name); |
36f8d174 LT |
159 | continue; |
160 | } | |
161 | if (obj->type == blob_type) { | |
e646de0d | 162 | p = process_blob((struct blob *)obj, p, NULL, name); |
36f8d174 LT |
163 | continue; |
164 | } | |
165 | die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name); | |
166 | } | |
9de48752 | 167 | while (objects) { |
50319850 JH |
168 | /* An object with name "foo\n0000000..." can be used to |
169 | * confuse downstream git-pack-objects very badly. | |
c807f771 JH |
170 | */ |
171 | const char *ep = strchr(objects->name, '\n'); | |
172 | if (ep) { | |
173 | printf("%s %.*s\n", sha1_to_hex(objects->item->sha1), | |
174 | (int) (ep - objects->name), | |
175 | objects->name); | |
176 | } | |
177 | else | |
178 | printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name); | |
9de48752 LT |
179 | objects = objects->next; |
180 | } | |
181 | } | |
182 | ||
8b3a1e05 LT |
183 | /* |
184 | * This is a truly stupid algorithm, but it's only | |
185 | * used for bisection, and we just don't care enough. | |
186 | * | |
187 | * We care just barely enough to avoid recursing for | |
188 | * non-merge entries. | |
189 | */ | |
190 | static int count_distance(struct commit_list *entry) | |
191 | { | |
192 | int nr = 0; | |
193 | ||
194 | while (entry) { | |
195 | struct commit *commit = entry->item; | |
196 | struct commit_list *p; | |
197 | ||
198 | if (commit->object.flags & (UNINTERESTING | COUNTED)) | |
199 | break; | |
8efdc326 | 200 | if (!revs.prune_fn || (commit->object.flags & TREECHANGE)) |
b3cfd939 | 201 | nr++; |
8b3a1e05 LT |
202 | commit->object.flags |= COUNTED; |
203 | p = commit->parents; | |
204 | entry = p; | |
205 | if (p) { | |
206 | p = p->next; | |
207 | while (p) { | |
208 | nr += count_distance(p); | |
209 | p = p->next; | |
210 | } | |
211 | } | |
212 | } | |
b3cfd939 | 213 | |
8b3a1e05 LT |
214 | return nr; |
215 | } | |
216 | ||
3d958064 | 217 | static void clear_distance(struct commit_list *list) |
8b3a1e05 LT |
218 | { |
219 | while (list) { | |
220 | struct commit *commit = list->item; | |
221 | commit->object.flags &= ~COUNTED; | |
222 | list = list->next; | |
223 | } | |
224 | } | |
225 | ||
226 | static struct commit_list *find_bisection(struct commit_list *list) | |
227 | { | |
228 | int nr, closest; | |
229 | struct commit_list *p, *best; | |
230 | ||
231 | nr = 0; | |
232 | p = list; | |
233 | while (p) { | |
8efdc326 | 234 | if (!revs.prune_fn || (p->item->object.flags & TREECHANGE)) |
b3cfd939 | 235 | nr++; |
8b3a1e05 LT |
236 | p = p->next; |
237 | } | |
238 | closest = 0; | |
239 | best = list; | |
240 | ||
b3cfd939 LT |
241 | for (p = list; p; p = p->next) { |
242 | int distance; | |
243 | ||
8efdc326 | 244 | if (revs.prune_fn && !(p->item->object.flags & TREECHANGE)) |
b3cfd939 LT |
245 | continue; |
246 | ||
247 | distance = count_distance(p); | |
8b3a1e05 LT |
248 | clear_distance(list); |
249 | if (nr - distance < distance) | |
250 | distance = nr - distance; | |
251 | if (distance > closest) { | |
252 | best = p; | |
253 | closest = distance; | |
254 | } | |
8b3a1e05 LT |
255 | } |
256 | if (best) | |
257 | best->next = NULL; | |
258 | return best; | |
259 | } | |
260 | ||
c6496575 JH |
261 | static void mark_edge_parents_uninteresting(struct commit *commit) |
262 | { | |
263 | struct commit_list *parents; | |
264 | ||
265 | for (parents = commit->parents; parents; parents = parents->next) { | |
266 | struct commit *parent = parents->item; | |
267 | if (!(parent->object.flags & UNINTERESTING)) | |
268 | continue; | |
269 | mark_tree_uninteresting(parent->tree); | |
ae563542 | 270 | if (revs.edge_hint && !(parent->object.flags & SHOWN)) { |
eb38cc68 | 271 | parent->object.flags |= SHOWN; |
c6496575 | 272 | printf("-%s\n", sha1_to_hex(parent->object.sha1)); |
eb38cc68 | 273 | } |
c6496575 JH |
274 | } |
275 | } | |
276 | ||
5bdbaaa4 LT |
277 | static void mark_edges_uninteresting(struct commit_list *list) |
278 | { | |
279 | for ( ; list; list = list->next) { | |
c6496575 | 280 | struct commit *commit = list->item; |
5bdbaaa4 | 281 | |
c6496575 JH |
282 | if (commit->object.flags & UNINTERESTING) { |
283 | mark_tree_uninteresting(commit->tree); | |
284 | continue; | |
5bdbaaa4 | 285 | } |
c6496575 | 286 | mark_edge_parents_uninteresting(commit); |
5bdbaaa4 LT |
287 | } |
288 | } | |
289 | ||
cf484544 | 290 | int main(int argc, const char **argv) |
64745109 | 291 | { |
ae563542 | 292 | struct commit_list *list; |
d9a83684 | 293 | int i; |
64745109 | 294 | |
a4a88b2b | 295 | argc = setup_revisions(argc, argv, &revs, NULL); |
ae563542 | 296 | |
fcfda02b | 297 | for (i = 1 ; i < argc; i++) { |
cf484544 | 298 | const char *arg = argv[i]; |
fcfda02b | 299 | |
8233340c EW |
300 | /* accept -<digit>, like traditilnal "head" */ |
301 | if ((*arg == '-') && isdigit(arg[1])) { | |
ae563542 | 302 | revs.max_count = atoi(arg + 1); |
8233340c EW |
303 | continue; |
304 | } | |
3af06987 EW |
305 | if (!strcmp(arg, "-n")) { |
306 | if (++i >= argc) | |
307 | die("-n requires an argument"); | |
ae563542 | 308 | revs.max_count = atoi(argv[i]); |
3af06987 EW |
309 | continue; |
310 | } | |
311 | if (!strncmp(arg,"-n",2)) { | |
ae563542 | 312 | revs.max_count = atoi(arg + 2); |
a6f68d47 | 313 | continue; |
fcfda02b | 314 | } |
a6f68d47 LT |
315 | if (!strcmp(arg, "--header")) { |
316 | verbose_header = 1; | |
317 | continue; | |
318 | } | |
9da5c2f0 JH |
319 | if (!strcmp(arg, "--no-abbrev")) { |
320 | abbrev = 0; | |
321 | continue; | |
322 | } | |
323 | if (!strncmp(arg, "--abbrev=", 9)) { | |
324 | abbrev = strtoul(arg + 9, NULL, 10); | |
325 | if (abbrev && abbrev < MINIMUM_ABBREV) | |
326 | abbrev = MINIMUM_ABBREV; | |
327 | else if (40 < abbrev) | |
328 | abbrev = 40; | |
329 | continue; | |
330 | } | |
000182ea LT |
331 | if (!strncmp(arg, "--pretty", 8)) { |
332 | commit_format = get_commit_format(arg+8); | |
9d97aa64 | 333 | verbose_header = 1; |
9d97aa64 | 334 | hdr_termination = '\n'; |
d87449c5 | 335 | if (commit_format == CMIT_FMT_ONELINE) |
d998a089 | 336 | commit_prefix = ""; |
d87449c5 | 337 | else |
d998a089 | 338 | commit_prefix = "commit "; |
9d97aa64 LT |
339 | continue; |
340 | } | |
97658004 LT |
341 | if (!strcmp(arg, "--parents")) { |
342 | show_parents = 1; | |
343 | continue; | |
344 | } | |
dc68c4ff JH |
345 | if (!strcmp(arg, "--timestamp")) { |
346 | show_timestamp = 1; | |
347 | continue; | |
348 | } | |
8b3a1e05 LT |
349 | if (!strcmp(arg, "--bisect")) { |
350 | bisect_list = 1; | |
351 | continue; | |
352 | } | |
ae563542 | 353 | usage(rev_list_usage); |
a6f68d47 | 354 | |
fcfda02b KS |
355 | } |
356 | ||
ae563542 | 357 | list = revs.commits; |
ae563542 | 358 | |
ef1cc2cc | 359 | if (!list && |
ae563542 | 360 | (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) && !revs.pending_objects)) |
7b34c2fa LT |
361 | usage(rev_list_usage); |
362 | ||
9181ca2c JH |
363 | save_commit_buffer = verbose_header; |
364 | track_object_refs = 0; | |
365 | ||
a4a88b2b LT |
366 | prepare_revision_walk(&revs); |
367 | if (revs.tree_objects) | |
368 | mark_edges_uninteresting(revs.commits); | |
369 | ||
370 | if (bisect_list) | |
371 | revs.commits = find_bisection(revs.commits); | |
7b34c2fa | 372 | |
765ac8ec | 373 | show_commit_list(&revs); |
8906300f | 374 | |
64745109 LT |
375 | return 0; |
376 | } |