]>
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" | |
a3437b8c | 7 | #include "epoch.h" |
cf484544 | 8 | #include "diff.h" |
64745109 | 9 | |
8906300f LT |
10 | #define SEEN (1u << 0) |
11 | #define INTERESTING (1u << 1) | |
8b3a1e05 | 12 | #define COUNTED (1u << 2) |
bce62866 | 13 | #define SHOWN (1u << 3) |
1b9e059d | 14 | #define TREECHANGE (1u << 4) |
88494423 | 15 | #define TMP_MARK (1u << 5) /* for isolated cases; clean after use */ |
8906300f | 16 | |
a6f68d47 | 17 | static const char rev_list_usage[] = |
69e0c256 JH |
18 | "git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n" |
19 | " limiting output:\n" | |
20 | " --max-count=nr\n" | |
21 | " --max-age=epoch\n" | |
22 | " --min-age=epoch\n" | |
23 | " --sparse\n" | |
24 | " --no-merges\n" | |
93b74bca | 25 | " --remove-empty\n" |
69e0c256 JH |
26 | " --all\n" |
27 | " ordering output:\n" | |
28 | " --merge-order [ --show-breaks ]\n" | |
29 | " --topo-order\n" | |
4c8725f1 | 30 | " --date-order\n" |
69e0c256 JH |
31 | " formatting output:\n" |
32 | " --parents\n" | |
c6496575 | 33 | " --objects | --objects-edge\n" |
69e0c256 JH |
34 | " --unpacked\n" |
35 | " --header | --pretty\n" | |
9da5c2f0 | 36 | " --abbrev=nr | --no-abbrev\n" |
69e0c256 JH |
37 | " special purpose:\n" |
38 | " --bisect" | |
39 | ; | |
a6f68d47 | 40 | |
7b34c2fa | 41 | static int dense = 1; |
12d2a187 | 42 | static int unpacked = 0; |
8b3a1e05 | 43 | static int bisect_list = 0; |
3c90f03d | 44 | static int tag_objects = 0; |
9de48752 LT |
45 | static int tree_objects = 0; |
46 | static int blob_objects = 0; | |
c6496575 | 47 | static int edge_hint = 0; |
81f2bb1f | 48 | static int verbose_header = 0; |
9da5c2f0 | 49 | static int abbrev = DEFAULT_ABBREV; |
81f2bb1f | 50 | static int show_parents = 0; |
81f2bb1f | 51 | static int hdr_termination = 0; |
d998a089 | 52 | static const char *commit_prefix = ""; |
81f2bb1f LT |
53 | static unsigned long max_age = -1; |
54 | static unsigned long min_age = -1; | |
55 | static int max_count = -1; | |
000182ea | 56 | static enum cmit_fmt commit_format = CMIT_FMT_RAW; |
a3437b8c JS |
57 | static int merge_order = 0; |
58 | static int show_breaks = 0; | |
5e749e25 | 59 | static int stop_traversal = 0; |
d2d02a49 | 60 | static int topo_order = 0; |
4c8725f1 | 61 | static int lifo = 1; |
76cd8eb6 | 62 | static int no_merges = 0; |
cf484544 | 63 | static const char **paths = NULL; |
461cf59f | 64 | static int remove_empty_trees = 0; |
81f2bb1f | 65 | |
e646de0d JH |
66 | struct name_path { |
67 | struct name_path *up; | |
68 | int elem_len; | |
69 | const char *elem; | |
70 | }; | |
71 | ||
72 | static char *path_name(struct name_path *path, const char *name) | |
73 | { | |
74 | struct name_path *p; | |
75 | char *n, *m; | |
76 | int nlen = strlen(name); | |
77 | int len = nlen + 1; | |
78 | ||
79 | for (p = path; p; p = p->up) { | |
80 | if (p->elem_len) | |
81 | len += p->elem_len + 1; | |
82 | } | |
83 | n = xmalloc(len); | |
84 | m = n + len - (nlen + 1); | |
85 | strcpy(m, name); | |
86 | for (p = path; p; p = p->up) { | |
87 | if (p->elem_len) { | |
88 | m -= p->elem_len + 1; | |
89 | memcpy(m, p->elem, p->elem_len); | |
90 | m[p->elem_len] = '/'; | |
91 | } | |
92 | } | |
93 | return n; | |
94 | } | |
95 | ||
81f2bb1f LT |
96 | static void show_commit(struct commit *commit) |
97 | { | |
51b1e171 | 98 | commit->object.flags |= SHOWN; |
a3437b8c | 99 | if (show_breaks) { |
d998a089 | 100 | commit_prefix = "| "; |
a3437b8c | 101 | if (commit->object.flags & DISCONTINUITY) { |
d998a089 | 102 | commit_prefix = "^ "; |
a3437b8c | 103 | } else if (commit->object.flags & BOUNDARY) { |
d998a089 | 104 | commit_prefix = "= "; |
a3437b8c JS |
105 | } |
106 | } | |
d998a089 | 107 | printf("%s%s", commit_prefix, sha1_to_hex(commit->object.sha1)); |
81f2bb1f LT |
108 | if (show_parents) { |
109 | struct commit_list *parents = commit->parents; | |
110 | while (parents) { | |
88494423 | 111 | struct object *o = &(parents->item->object); |
81f2bb1f | 112 | parents = parents->next; |
88494423 JH |
113 | if (o->flags & TMP_MARK) |
114 | continue; | |
115 | printf(" %s", sha1_to_hex(o->sha1)); | |
116 | o->flags |= TMP_MARK; | |
81f2bb1f | 117 | } |
88494423 JH |
118 | /* TMP_MARK is a general purpose flag that can |
119 | * be used locally, but the user should clean | |
120 | * things up after it is done with them. | |
121 | */ | |
122 | for (parents = commit->parents; | |
123 | parents; | |
124 | parents = parents->next) | |
125 | parents->item->object.flags &= ~TMP_MARK; | |
81f2bb1f | 126 | } |
d87449c5 JH |
127 | if (commit_format == CMIT_FMT_ONELINE) |
128 | putchar(' '); | |
129 | else | |
130 | putchar('\n'); | |
131 | ||
81f2bb1f | 132 | if (verbose_header) { |
000182ea | 133 | static char pretty_header[16384]; |
9da5c2f0 | 134 | pretty_print_commit(commit_format, commit, ~0, pretty_header, sizeof(pretty_header), abbrev); |
000182ea | 135 | printf("%s%c", pretty_header, hdr_termination); |
7620d39f LT |
136 | } |
137 | fflush(stdout); | |
a3437b8c JS |
138 | } |
139 | ||
129adf4d | 140 | static int rewrite_one(struct commit **pp) |
1b9e059d LT |
141 | { |
142 | for (;;) { | |
143 | struct commit *p = *pp; | |
144 | if (p->object.flags & (TREECHANGE | UNINTERESTING)) | |
129adf4d LT |
145 | return 0; |
146 | if (!p->parents) | |
147 | return -1; | |
1b9e059d LT |
148 | *pp = p->parents->item; |
149 | } | |
150 | } | |
151 | ||
152 | static void rewrite_parents(struct commit *commit) | |
153 | { | |
129adf4d LT |
154 | struct commit_list **pp = &commit->parents; |
155 | while (*pp) { | |
156 | struct commit_list *parent = *pp; | |
157 | if (rewrite_one(&parent->item) < 0) { | |
158 | *pp = parent->next; | |
159 | continue; | |
160 | } | |
161 | pp = &parent->next; | |
1b9e059d LT |
162 | } |
163 | } | |
164 | ||
a3437b8c JS |
165 | static int filter_commit(struct commit * commit) |
166 | { | |
d2775a81 | 167 | if (stop_traversal && (commit->object.flags & BOUNDARY)) |
5e749e25 | 168 | return STOP; |
51b1e171 | 169 | if (commit->object.flags & (UNINTERESTING|SHOWN)) |
a3437b8c JS |
170 | return CONTINUE; |
171 | if (min_age != -1 && (commit->date > min_age)) | |
172 | return CONTINUE; | |
5e749e25 | 173 | if (max_age != -1 && (commit->date < max_age)) { |
d2775a81 | 174 | stop_traversal=1; |
27cfe2e2 | 175 | return CONTINUE; |
5e749e25 | 176 | } |
76cd8eb6 JS |
177 | if (no_merges && (commit->parents && commit->parents->next)) |
178 | return CONTINUE; | |
1b9e059d LT |
179 | if (paths && dense) { |
180 | if (!(commit->object.flags & TREECHANGE)) | |
181 | return CONTINUE; | |
182 | rewrite_parents(commit); | |
183 | } | |
a3437b8c JS |
184 | return DO; |
185 | } | |
186 | ||
187 | static int process_commit(struct commit * commit) | |
188 | { | |
189 | int action=filter_commit(commit); | |
190 | ||
191 | if (action == STOP) { | |
192 | return STOP; | |
193 | } | |
194 | ||
195 | if (action == CONTINUE) { | |
196 | return CONTINUE; | |
81f2bb1f | 197 | } |
a3437b8c | 198 | |
07f92477 LT |
199 | if (max_count != -1 && !max_count--) |
200 | return STOP; | |
201 | ||
a3437b8c JS |
202 | show_commit(commit); |
203 | ||
204 | return CONTINUE; | |
81f2bb1f LT |
205 | } |
206 | ||
e646de0d JH |
207 | static struct object_list **add_object(struct object *obj, |
208 | struct object_list **p, | |
209 | struct name_path *path, | |
210 | const char *name) | |
9de48752 LT |
211 | { |
212 | struct object_list *entry = xmalloc(sizeof(*entry)); | |
213 | entry->item = obj; | |
36f8d174 | 214 | entry->next = *p; |
e646de0d | 215 | entry->name = path_name(path, name); |
9de48752 LT |
216 | *p = entry; |
217 | return &entry->next; | |
218 | } | |
219 | ||
e646de0d JH |
220 | static struct object_list **process_blob(struct blob *blob, |
221 | struct object_list **p, | |
222 | struct name_path *path, | |
223 | const char *name) | |
9de48752 LT |
224 | { |
225 | struct object *obj = &blob->object; | |
226 | ||
227 | if (!blob_objects) | |
228 | return p; | |
229 | if (obj->flags & (UNINTERESTING | SEEN)) | |
230 | return p; | |
231 | obj->flags |= SEEN; | |
e646de0d | 232 | return add_object(obj, p, path, name); |
9de48752 LT |
233 | } |
234 | ||
e646de0d JH |
235 | static struct object_list **process_tree(struct tree *tree, |
236 | struct object_list **p, | |
237 | struct name_path *path, | |
238 | const char *name) | |
9de48752 LT |
239 | { |
240 | struct object *obj = &tree->object; | |
241 | struct tree_entry_list *entry; | |
e646de0d | 242 | struct name_path me; |
9de48752 LT |
243 | |
244 | if (!tree_objects) | |
245 | return p; | |
246 | if (obj->flags & (UNINTERESTING | SEEN)) | |
247 | return p; | |
248 | if (parse_tree(tree) < 0) | |
249 | die("bad tree object %s", sha1_to_hex(obj->sha1)); | |
250 | obj->flags |= SEEN; | |
e646de0d JH |
251 | p = add_object(obj, p, path, name); |
252 | me.up = path; | |
253 | me.elem = name; | |
254 | me.elem_len = strlen(name); | |
b0d8923e LT |
255 | entry = tree->entries; |
256 | tree->entries = NULL; | |
257 | while (entry) { | |
258 | struct tree_entry_list *next = entry->next; | |
9de48752 | 259 | if (entry->directory) |
e646de0d | 260 | p = process_tree(entry->item.tree, p, &me, entry->name); |
9de48752 | 261 | else |
e646de0d | 262 | p = process_blob(entry->item.blob, p, &me, entry->name); |
b0d8923e LT |
263 | free(entry); |
264 | entry = next; | |
9de48752 LT |
265 | } |
266 | return p; | |
267 | } | |
268 | ||
36f8d174 LT |
269 | static struct object_list *pending_objects = NULL; |
270 | ||
81f2bb1f LT |
271 | static void show_commit_list(struct commit_list *list) |
272 | { | |
36f8d174 | 273 | struct object_list *objects = NULL, **p = &objects, *pending; |
81f2bb1f LT |
274 | while (list) { |
275 | struct commit *commit = pop_most_recent_commit(&list, SEEN); | |
276 | ||
e646de0d | 277 | p = process_tree(commit->tree, p, NULL, ""); |
a3437b8c | 278 | if (process_commit(commit) == STOP) |
81f2bb1f | 279 | break; |
81f2bb1f | 280 | } |
36f8d174 LT |
281 | for (pending = pending_objects; pending; pending = pending->next) { |
282 | struct object *obj = pending->item; | |
283 | const char *name = pending->name; | |
284 | if (obj->flags & (UNINTERESTING | SEEN)) | |
285 | continue; | |
286 | if (obj->type == tag_type) { | |
287 | obj->flags |= SEEN; | |
e646de0d | 288 | p = add_object(obj, p, NULL, name); |
36f8d174 LT |
289 | continue; |
290 | } | |
291 | if (obj->type == tree_type) { | |
e646de0d | 292 | p = process_tree((struct tree *)obj, p, NULL, name); |
36f8d174 LT |
293 | continue; |
294 | } | |
295 | if (obj->type == blob_type) { | |
e646de0d | 296 | p = process_blob((struct blob *)obj, p, NULL, name); |
36f8d174 LT |
297 | continue; |
298 | } | |
299 | die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name); | |
300 | } | |
9de48752 | 301 | while (objects) { |
50319850 JH |
302 | /* An object with name "foo\n0000000..." can be used to |
303 | * confuse downstream git-pack-objects very badly. | |
c807f771 JH |
304 | */ |
305 | const char *ep = strchr(objects->name, '\n'); | |
306 | if (ep) { | |
307 | printf("%s %.*s\n", sha1_to_hex(objects->item->sha1), | |
308 | (int) (ep - objects->name), | |
309 | objects->name); | |
310 | } | |
311 | else | |
312 | printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name); | |
9de48752 LT |
313 | objects = objects->next; |
314 | } | |
315 | } | |
316 | ||
317 | static void mark_blob_uninteresting(struct blob *blob) | |
318 | { | |
319 | if (!blob_objects) | |
320 | return; | |
321 | if (blob->object.flags & UNINTERESTING) | |
322 | return; | |
323 | blob->object.flags |= UNINTERESTING; | |
324 | } | |
325 | ||
326 | static void mark_tree_uninteresting(struct tree *tree) | |
327 | { | |
328 | struct object *obj = &tree->object; | |
329 | struct tree_entry_list *entry; | |
330 | ||
331 | if (!tree_objects) | |
332 | return; | |
333 | if (obj->flags & UNINTERESTING) | |
334 | return; | |
335 | obj->flags |= UNINTERESTING; | |
454fbbcd LT |
336 | if (!has_sha1_file(obj->sha1)) |
337 | return; | |
9de48752 LT |
338 | if (parse_tree(tree) < 0) |
339 | die("bad tree %s", sha1_to_hex(obj->sha1)); | |
340 | entry = tree->entries; | |
b0d8923e | 341 | tree->entries = NULL; |
9de48752 | 342 | while (entry) { |
b0d8923e | 343 | struct tree_entry_list *next = entry->next; |
9de48752 LT |
344 | if (entry->directory) |
345 | mark_tree_uninteresting(entry->item.tree); | |
346 | else | |
347 | mark_blob_uninteresting(entry->item.blob); | |
b0d8923e LT |
348 | free(entry); |
349 | entry = next; | |
9de48752 | 350 | } |
81f2bb1f LT |
351 | } |
352 | ||
8906300f LT |
353 | static void mark_parents_uninteresting(struct commit *commit) |
354 | { | |
355 | struct commit_list *parents = commit->parents; | |
356 | ||
357 | while (parents) { | |
358 | struct commit *commit = parents->item; | |
359 | commit->object.flags |= UNINTERESTING; | |
454fbbcd | 360 | |
6c3b84c8 LT |
361 | /* |
362 | * Normally we haven't parsed the parent | |
363 | * yet, so we won't have a parent of a parent | |
364 | * here. However, it may turn out that we've | |
365 | * reached this commit some other way (where it | |
366 | * wasn't uninteresting), in which case we need | |
367 | * to mark its parents recursively too.. | |
368 | */ | |
369 | if (commit->parents) | |
370 | mark_parents_uninteresting(commit); | |
371 | ||
454fbbcd LT |
372 | /* |
373 | * A missing commit is ok iff its parent is marked | |
374 | * uninteresting. | |
375 | * | |
376 | * We just mark such a thing parsed, so that when | |
377 | * it is popped next time around, we won't be trying | |
378 | * to parse it and get an error. | |
379 | */ | |
380 | if (!has_sha1_file(commit->object.sha1)) | |
381 | commit->object.parsed = 1; | |
8906300f LT |
382 | parents = parents->next; |
383 | } | |
384 | } | |
385 | ||
4311d328 | 386 | static int everybody_uninteresting(struct commit_list *orig) |
8906300f | 387 | { |
4311d328 | 388 | struct commit_list *list = orig; |
8906300f LT |
389 | while (list) { |
390 | struct commit *commit = list->item; | |
391 | list = list->next; | |
392 | if (commit->object.flags & UNINTERESTING) | |
393 | continue; | |
394 | return 0; | |
395 | } | |
396 | return 1; | |
397 | } | |
398 | ||
8b3a1e05 LT |
399 | /* |
400 | * This is a truly stupid algorithm, but it's only | |
401 | * used for bisection, and we just don't care enough. | |
402 | * | |
403 | * We care just barely enough to avoid recursing for | |
404 | * non-merge entries. | |
405 | */ | |
406 | static int count_distance(struct commit_list *entry) | |
407 | { | |
408 | int nr = 0; | |
409 | ||
410 | while (entry) { | |
411 | struct commit *commit = entry->item; | |
412 | struct commit_list *p; | |
413 | ||
414 | if (commit->object.flags & (UNINTERESTING | COUNTED)) | |
415 | break; | |
b3cfd939 LT |
416 | if (!paths || (commit->object.flags & TREECHANGE)) |
417 | nr++; | |
8b3a1e05 LT |
418 | commit->object.flags |= COUNTED; |
419 | p = commit->parents; | |
420 | entry = p; | |
421 | if (p) { | |
422 | p = p->next; | |
423 | while (p) { | |
424 | nr += count_distance(p); | |
425 | p = p->next; | |
426 | } | |
427 | } | |
428 | } | |
b3cfd939 | 429 | |
8b3a1e05 LT |
430 | return nr; |
431 | } | |
432 | ||
3d958064 | 433 | static void clear_distance(struct commit_list *list) |
8b3a1e05 LT |
434 | { |
435 | while (list) { | |
436 | struct commit *commit = list->item; | |
437 | commit->object.flags &= ~COUNTED; | |
438 | list = list->next; | |
439 | } | |
440 | } | |
441 | ||
442 | static struct commit_list *find_bisection(struct commit_list *list) | |
443 | { | |
444 | int nr, closest; | |
445 | struct commit_list *p, *best; | |
446 | ||
447 | nr = 0; | |
448 | p = list; | |
449 | while (p) { | |
b3cfd939 LT |
450 | if (!paths || (p->item->object.flags & TREECHANGE)) |
451 | nr++; | |
8b3a1e05 LT |
452 | p = p->next; |
453 | } | |
454 | closest = 0; | |
455 | best = list; | |
456 | ||
b3cfd939 LT |
457 | for (p = list; p; p = p->next) { |
458 | int distance; | |
459 | ||
460 | if (paths && !(p->item->object.flags & TREECHANGE)) | |
461 | continue; | |
462 | ||
463 | distance = count_distance(p); | |
8b3a1e05 LT |
464 | clear_distance(list); |
465 | if (nr - distance < distance) | |
466 | distance = nr - distance; | |
467 | if (distance > closest) { | |
468 | best = p; | |
469 | closest = distance; | |
470 | } | |
8b3a1e05 LT |
471 | } |
472 | if (best) | |
473 | best->next = NULL; | |
474 | return best; | |
475 | } | |
476 | ||
c6496575 JH |
477 | static void mark_edge_parents_uninteresting(struct commit *commit) |
478 | { | |
479 | struct commit_list *parents; | |
480 | ||
481 | for (parents = commit->parents; parents; parents = parents->next) { | |
482 | struct commit *parent = parents->item; | |
483 | if (!(parent->object.flags & UNINTERESTING)) | |
484 | continue; | |
485 | mark_tree_uninteresting(parent->tree); | |
eb38cc68 JH |
486 | if (edge_hint && !(parent->object.flags & SHOWN)) { |
487 | parent->object.flags |= SHOWN; | |
c6496575 | 488 | printf("-%s\n", sha1_to_hex(parent->object.sha1)); |
eb38cc68 | 489 | } |
c6496575 JH |
490 | } |
491 | } | |
492 | ||
5bdbaaa4 LT |
493 | static void mark_edges_uninteresting(struct commit_list *list) |
494 | { | |
495 | for ( ; list; list = list->next) { | |
c6496575 | 496 | struct commit *commit = list->item; |
5bdbaaa4 | 497 | |
c6496575 JH |
498 | if (commit->object.flags & UNINTERESTING) { |
499 | mark_tree_uninteresting(commit->tree); | |
500 | continue; | |
5bdbaaa4 | 501 | } |
c6496575 | 502 | mark_edge_parents_uninteresting(commit); |
5bdbaaa4 LT |
503 | } |
504 | } | |
505 | ||
461cf59f LT |
506 | #define TREE_SAME 0 |
507 | #define TREE_NEW 1 | |
508 | #define TREE_DIFFERENT 2 | |
509 | static int tree_difference = TREE_SAME; | |
cf484544 LT |
510 | |
511 | static void file_add_remove(struct diff_options *options, | |
512 | int addremove, unsigned mode, | |
513 | const unsigned char *sha1, | |
514 | const char *base, const char *path) | |
515 | { | |
461cf59f LT |
516 | int diff = TREE_DIFFERENT; |
517 | ||
518 | /* | |
519 | * Is it an add of a new file? It means that | |
520 | * the old tree didn't have it at all, so we | |
521 | * will turn "TREE_SAME" -> "TREE_NEW", but | |
522 | * leave any "TREE_DIFFERENT" alone (and if | |
523 | * it already was "TREE_NEW", we'll keep it | |
524 | * "TREE_NEW" of course). | |
525 | */ | |
526 | if (addremove == '+') { | |
527 | diff = tree_difference; | |
528 | if (diff != TREE_SAME) | |
529 | return; | |
530 | diff = TREE_NEW; | |
531 | } | |
532 | tree_difference = diff; | |
cf484544 LT |
533 | } |
534 | ||
535 | static void file_change(struct diff_options *options, | |
536 | unsigned old_mode, unsigned new_mode, | |
537 | const unsigned char *old_sha1, | |
538 | const unsigned char *new_sha1, | |
539 | const char *base, const char *path) | |
540 | { | |
461cf59f | 541 | tree_difference = TREE_DIFFERENT; |
cf484544 LT |
542 | } |
543 | ||
544 | static struct diff_options diff_opt = { | |
545 | .recursive = 1, | |
546 | .add_remove = file_add_remove, | |
547 | .change = file_change, | |
548 | }; | |
549 | ||
461cf59f | 550 | static int compare_tree(struct tree *t1, struct tree *t2) |
1b9e059d | 551 | { |
461cf59f LT |
552 | if (!t1) |
553 | return TREE_NEW; | |
554 | if (!t2) | |
555 | return TREE_DIFFERENT; | |
556 | tree_difference = TREE_SAME; | |
1b9e059d | 557 | if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "", &diff_opt) < 0) |
461cf59f LT |
558 | return TREE_DIFFERENT; |
559 | return tree_difference; | |
1b9e059d LT |
560 | } |
561 | ||
129adf4d LT |
562 | static int same_tree_as_empty(struct tree *t1) |
563 | { | |
564 | int retval; | |
565 | void *tree; | |
566 | struct tree_desc empty, real; | |
567 | ||
568 | if (!t1) | |
569 | return 0; | |
570 | ||
571 | tree = read_object_with_reference(t1->object.sha1, "tree", &real.size, NULL); | |
572 | if (!tree) | |
573 | return 0; | |
574 | real.buf = tree; | |
575 | ||
576 | empty.buf = ""; | |
577 | empty.size = 0; | |
578 | ||
461cf59f | 579 | tree_difference = 0; |
129adf4d LT |
580 | retval = diff_tree(&empty, &real, "", &diff_opt); |
581 | free(tree); | |
582 | ||
461cf59f | 583 | return retval >= 0 && !tree_difference; |
129adf4d LT |
584 | } |
585 | ||
461cf59f | 586 | static void try_to_simplify_commit(struct commit *commit) |
cf484544 | 587 | { |
461cf59f LT |
588 | struct commit_list **pp, *parent; |
589 | ||
cf484544 | 590 | if (!commit->tree) |
461cf59f | 591 | return; |
cf484544 | 592 | |
461cf59f LT |
593 | if (!commit->parents) { |
594 | if (!same_tree_as_empty(commit->tree)) | |
595 | commit->object.flags |= TREECHANGE; | |
596 | return; | |
597 | } | |
598 | ||
599 | pp = &commit->parents; | |
600 | while ((parent = *pp) != NULL) { | |
cf484544 | 601 | struct commit *p = parent->item; |
461cf59f LT |
602 | |
603 | if (p->object.flags & UNINTERESTING) { | |
604 | pp = &parent->next; | |
605 | continue; | |
606 | } | |
607 | ||
cf484544 | 608 | parse_commit(p); |
461cf59f LT |
609 | switch (compare_tree(p->tree, commit->tree)) { |
610 | case TREE_SAME: | |
611 | parent->next = NULL; | |
612 | commit->parents = parent; | |
613 | return; | |
614 | ||
615 | case TREE_NEW: | |
616 | if (remove_empty_trees && same_tree_as_empty(p->tree)) { | |
617 | *pp = parent->next; | |
618 | continue; | |
619 | } | |
620 | /* fallthrough */ | |
621 | case TREE_DIFFERENT: | |
622 | pp = &parent->next; | |
cf484544 | 623 | continue; |
461cf59f LT |
624 | } |
625 | die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1)); | |
cf484544 | 626 | } |
461cf59f | 627 | commit->object.flags |= TREECHANGE; |
cf484544 LT |
628 | } |
629 | ||
630 | static void add_parents_to_list(struct commit *commit, struct commit_list **list) | |
631 | { | |
632 | struct commit_list *parent = commit->parents; | |
633 | ||
634 | /* | |
635 | * If the commit is uninteresting, don't try to | |
636 | * prune parents - we want the maximal uninteresting | |
637 | * set. | |
638 | * | |
639 | * Normally we haven't parsed the parent | |
640 | * yet, so we won't have a parent of a parent | |
641 | * here. However, it may turn out that we've | |
642 | * reached this commit some other way (where it | |
643 | * wasn't uninteresting), in which case we need | |
644 | * to mark its parents recursively too.. | |
645 | */ | |
646 | if (commit->object.flags & UNINTERESTING) { | |
647 | while (parent) { | |
648 | struct commit *p = parent->item; | |
649 | parent = parent->next; | |
650 | parse_commit(p); | |
651 | p->object.flags |= UNINTERESTING; | |
652 | if (p->parents) | |
653 | mark_parents_uninteresting(p); | |
654 | if (p->object.flags & SEEN) | |
655 | continue; | |
656 | p->object.flags |= SEEN; | |
657 | insert_by_date(p, list); | |
658 | } | |
659 | return; | |
660 | } | |
661 | ||
662 | /* | |
461cf59f LT |
663 | * Ok, the commit wasn't uninteresting. Try to |
664 | * simplify the commit history and find the parent | |
665 | * that has no differences in the path set if one exists. | |
cf484544 | 666 | */ |
461cf59f LT |
667 | if (paths) |
668 | try_to_simplify_commit(commit); | |
cf484544 | 669 | |
461cf59f | 670 | parent = commit->parents; |
cf484544 LT |
671 | while (parent) { |
672 | struct commit *p = parent->item; | |
673 | ||
674 | parent = parent->next; | |
675 | ||
676 | parse_commit(p); | |
677 | if (p->object.flags & SEEN) | |
678 | continue; | |
679 | p->object.flags |= SEEN; | |
680 | insert_by_date(p, list); | |
681 | } | |
682 | } | |
683 | ||
6da4016a | 684 | static struct commit_list *limit_list(struct commit_list *list) |
3b42a63c LT |
685 | { |
686 | struct commit_list *newlist = NULL; | |
687 | struct commit_list **p = &newlist; | |
36f8d174 | 688 | while (list) { |
cf484544 LT |
689 | struct commit_list *entry = list; |
690 | struct commit *commit = list->item; | |
3b42a63c LT |
691 | struct object *obj = &commit->object; |
692 | ||
cf484544 LT |
693 | list = list->next; |
694 | free(entry); | |
695 | ||
27cfe2e2 LT |
696 | if (max_age != -1 && (commit->date < max_age)) |
697 | obj->flags |= UNINTERESTING; | |
12d2a187 LT |
698 | if (unpacked && has_sha1_pack(obj->sha1)) |
699 | obj->flags |= UNINTERESTING; | |
cf484544 | 700 | add_parents_to_list(commit, &list); |
337cb3fb | 701 | if (obj->flags & UNINTERESTING) { |
3b42a63c LT |
702 | mark_parents_uninteresting(commit); |
703 | if (everybody_uninteresting(list)) | |
704 | break; | |
705 | continue; | |
706 | } | |
27cfe2e2 LT |
707 | if (min_age != -1 && (commit->date > min_age)) |
708 | continue; | |
3b42a63c | 709 | p = &commit_list_insert(commit, p)->next; |
36f8d174 | 710 | } |
5bdbaaa4 LT |
711 | if (tree_objects) |
712 | mark_edges_uninteresting(newlist); | |
8b3a1e05 LT |
713 | if (bisect_list) |
714 | newlist = find_bisection(newlist); | |
3b42a63c LT |
715 | return newlist; |
716 | } | |
717 | ||
36f8d174 LT |
718 | static void add_pending_object(struct object *obj, const char *name) |
719 | { | |
e646de0d | 720 | add_object(obj, &pending_objects, NULL, name); |
36f8d174 LT |
721 | } |
722 | ||
19a7e715 | 723 | static struct commit *get_commit_reference(const char *name, const unsigned char *sha1, unsigned int flags) |
3c90f03d | 724 | { |
36f8d174 | 725 | struct object *object; |
3c90f03d | 726 | |
36f8d174 LT |
727 | object = parse_object(sha1); |
728 | if (!object) | |
729 | die("bad object %s", name); | |
730 | ||
731 | /* | |
732 | * Tag object? Look what it points to.. | |
733 | */ | |
013aab82 | 734 | while (object->type == tag_type) { |
36f8d174 LT |
735 | struct tag *tag = (struct tag *) object; |
736 | object->flags |= flags; | |
737 | if (tag_objects && !(object->flags & UNINTERESTING)) | |
738 | add_pending_object(object, tag->tag); | |
013aab82 | 739 | object = parse_object(tag->tagged->sha1); |
7f1335c7 SV |
740 | if (!object) |
741 | die("bad object %s", sha1_to_hex(tag->tagged->sha1)); | |
36f8d174 LT |
742 | } |
743 | ||
744 | /* | |
745 | * Commit object? Just return it, we'll do all the complex | |
746 | * reachability crud. | |
747 | */ | |
748 | if (object->type == commit_type) { | |
749 | struct commit *commit = (struct commit *)object; | |
750 | object->flags |= flags; | |
751 | if (parse_commit(commit) < 0) | |
752 | die("unable to parse commit %s", name); | |
454fbbcd LT |
753 | if (flags & UNINTERESTING) |
754 | mark_parents_uninteresting(commit); | |
36f8d174 LT |
755 | return commit; |
756 | } | |
757 | ||
758 | /* | |
759 | * Tree object? Either mark it uniniteresting, or add it | |
760 | * to the list of objects to look at later.. | |
761 | */ | |
762 | if (object->type == tree_type) { | |
763 | struct tree *tree = (struct tree *)object; | |
764 | if (!tree_objects) | |
960bba0d | 765 | return NULL; |
36f8d174 LT |
766 | if (flags & UNINTERESTING) { |
767 | mark_tree_uninteresting(tree); | |
768 | return NULL; | |
769 | } | |
770 | add_pending_object(object, ""); | |
771 | return NULL; | |
772 | } | |
773 | ||
774 | /* | |
775 | * Blob object? You know the drill by now.. | |
776 | */ | |
777 | if (object->type == blob_type) { | |
778 | struct blob *blob = (struct blob *)object; | |
779 | if (!blob_objects) | |
960bba0d | 780 | return NULL; |
36f8d174 LT |
781 | if (flags & UNINTERESTING) { |
782 | mark_blob_uninteresting(blob); | |
783 | return NULL; | |
784 | } | |
785 | add_pending_object(object, ""); | |
786 | return NULL; | |
787 | } | |
788 | die("%s is unknown object", name); | |
3c90f03d LT |
789 | } |
790 | ||
1215879c JH |
791 | static void handle_one_commit(struct commit *com, struct commit_list **lst) |
792 | { | |
793 | if (!com || com->object.flags & SEEN) | |
794 | return; | |
795 | com->object.flags |= SEEN; | |
796 | commit_list_insert(com, lst); | |
797 | } | |
798 | ||
e091eb93 JH |
799 | /* for_each_ref() callback does not allow user data -- Yuck. */ |
800 | static struct commit_list **global_lst; | |
801 | ||
802 | static int include_one_commit(const char *path, const unsigned char *sha1) | |
803 | { | |
19a7e715 | 804 | struct commit *com = get_commit_reference(path, sha1, 0); |
e091eb93 JH |
805 | handle_one_commit(com, global_lst); |
806 | return 0; | |
807 | } | |
808 | ||
809 | static void handle_all(struct commit_list **lst) | |
810 | { | |
811 | global_lst = lst; | |
812 | for_each_ref(include_one_commit); | |
813 | global_lst = NULL; | |
814 | } | |
1215879c | 815 | |
cf484544 | 816 | int main(int argc, const char **argv) |
64745109 | 817 | { |
cf484544 | 818 | const char *prefix = setup_git_directory(); |
64745109 | 819 | struct commit_list *list = NULL; |
337cb3fb | 820 | int i, limited = 0; |
64745109 | 821 | |
fcfda02b | 822 | for (i = 1 ; i < argc; i++) { |
337cb3fb | 823 | int flags; |
cf484544 | 824 | const char *arg = argv[i]; |
1215879c | 825 | char *dotdot; |
337cb3fb | 826 | struct commit *commit; |
19a7e715 | 827 | unsigned char sha1[20]; |
fcfda02b | 828 | |
8233340c EW |
829 | /* accept -<digit>, like traditilnal "head" */ |
830 | if ((*arg == '-') && isdigit(arg[1])) { | |
831 | max_count = atoi(arg + 1); | |
832 | continue; | |
833 | } | |
3af06987 EW |
834 | if (!strcmp(arg, "-n")) { |
835 | if (++i >= argc) | |
836 | die("-n requires an argument"); | |
837 | max_count = atoi(argv[i]); | |
838 | continue; | |
839 | } | |
840 | if (!strncmp(arg,"-n",2)) { | |
841 | max_count = atoi(arg + 2); | |
842 | continue; | |
843 | } | |
fcfda02b KS |
844 | if (!strncmp(arg, "--max-count=", 12)) { |
845 | max_count = atoi(arg + 12); | |
a6f68d47 LT |
846 | continue; |
847 | } | |
848 | if (!strncmp(arg, "--max-age=", 10)) { | |
fcfda02b | 849 | max_age = atoi(arg + 10); |
27cfe2e2 | 850 | limited = 1; |
a6f68d47 LT |
851 | continue; |
852 | } | |
853 | if (!strncmp(arg, "--min-age=", 10)) { | |
fcfda02b | 854 | min_age = atoi(arg + 10); |
27cfe2e2 | 855 | limited = 1; |
a6f68d47 | 856 | continue; |
fcfda02b | 857 | } |
a6f68d47 LT |
858 | if (!strcmp(arg, "--header")) { |
859 | verbose_header = 1; | |
860 | continue; | |
861 | } | |
9da5c2f0 JH |
862 | if (!strcmp(arg, "--no-abbrev")) { |
863 | abbrev = 0; | |
864 | continue; | |
865 | } | |
866 | if (!strncmp(arg, "--abbrev=", 9)) { | |
867 | abbrev = strtoul(arg + 9, NULL, 10); | |
868 | if (abbrev && abbrev < MINIMUM_ABBREV) | |
869 | abbrev = MINIMUM_ABBREV; | |
870 | else if (40 < abbrev) | |
871 | abbrev = 40; | |
872 | continue; | |
873 | } | |
000182ea LT |
874 | if (!strncmp(arg, "--pretty", 8)) { |
875 | commit_format = get_commit_format(arg+8); | |
9d97aa64 | 876 | verbose_header = 1; |
9d97aa64 | 877 | hdr_termination = '\n'; |
d87449c5 | 878 | if (commit_format == CMIT_FMT_ONELINE) |
d998a089 | 879 | commit_prefix = ""; |
d87449c5 | 880 | else |
d998a089 | 881 | commit_prefix = "commit "; |
9d97aa64 LT |
882 | continue; |
883 | } | |
76cd8eb6 JS |
884 | if (!strncmp(arg, "--no-merges", 11)) { |
885 | no_merges = 1; | |
886 | continue; | |
887 | } | |
97658004 LT |
888 | if (!strcmp(arg, "--parents")) { |
889 | show_parents = 1; | |
890 | continue; | |
891 | } | |
8b3a1e05 LT |
892 | if (!strcmp(arg, "--bisect")) { |
893 | bisect_list = 1; | |
894 | continue; | |
895 | } | |
e091eb93 JH |
896 | if (!strcmp(arg, "--all")) { |
897 | handle_all(&list); | |
898 | continue; | |
899 | } | |
9de48752 | 900 | if (!strcmp(arg, "--objects")) { |
3c90f03d | 901 | tag_objects = 1; |
9de48752 LT |
902 | tree_objects = 1; |
903 | blob_objects = 1; | |
904 | continue; | |
905 | } | |
c6496575 JH |
906 | if (!strcmp(arg, "--objects-edge")) { |
907 | tag_objects = 1; | |
908 | tree_objects = 1; | |
909 | blob_objects = 1; | |
910 | edge_hint = 1; | |
911 | continue; | |
912 | } | |
12d2a187 LT |
913 | if (!strcmp(arg, "--unpacked")) { |
914 | unpacked = 1; | |
915 | limited = 1; | |
916 | continue; | |
917 | } | |
12ba7eaf | 918 | if (!strcmp(arg, "--merge-order")) { |
a3437b8c JS |
919 | merge_order = 1; |
920 | continue; | |
921 | } | |
12ba7eaf | 922 | if (!strcmp(arg, "--show-breaks")) { |
a3437b8c JS |
923 | show_breaks = 1; |
924 | continue; | |
925 | } | |
d2d02a49 LT |
926 | if (!strcmp(arg, "--topo-order")) { |
927 | topo_order = 1; | |
4c8725f1 JH |
928 | lifo = 1; |
929 | limited = 1; | |
930 | continue; | |
931 | } | |
932 | if (!strcmp(arg, "--date-order")) { | |
933 | topo_order = 1; | |
934 | lifo = 0; | |
e6c3505b | 935 | limited = 1; |
d2d02a49 LT |
936 | continue; |
937 | } | |
1b9e059d LT |
938 | if (!strcmp(arg, "--dense")) { |
939 | dense = 1; | |
940 | continue; | |
941 | } | |
7b34c2fa LT |
942 | if (!strcmp(arg, "--sparse")) { |
943 | dense = 0; | |
944 | continue; | |
945 | } | |
461cf59f LT |
946 | if (!strcmp(arg, "--remove-empty")) { |
947 | remove_empty_trees = 1; | |
948 | continue; | |
949 | } | |
cf484544 | 950 | if (!strcmp(arg, "--")) { |
7b34c2fa | 951 | i++; |
cf484544 LT |
952 | break; |
953 | } | |
a6f68d47 | 954 | |
1215879c JH |
955 | if (show_breaks && !merge_order) |
956 | usage(rev_list_usage); | |
957 | ||
337cb3fb | 958 | flags = 0; |
1215879c JH |
959 | dotdot = strstr(arg, ".."); |
960 | if (dotdot) { | |
19a7e715 | 961 | unsigned char from_sha1[20]; |
1215879c | 962 | char *next = dotdot + 2; |
1215879c | 963 | *dotdot = 0; |
2a7055ae LT |
964 | if (!*next) |
965 | next = "HEAD"; | |
19a7e715 LT |
966 | if (!get_sha1(arg, from_sha1) && !get_sha1(next, sha1)) { |
967 | struct commit *exclude; | |
968 | struct commit *include; | |
969 | ||
970 | exclude = get_commit_reference(arg, from_sha1, UNINTERESTING); | |
971 | include = get_commit_reference(next, sha1, 0); | |
972 | if (!exclude || !include) | |
973 | die("Invalid revision range %s..%s", arg, next); | |
1215879c JH |
974 | limited = 1; |
975 | handle_one_commit(exclude, &list); | |
976 | handle_one_commit(include, &list); | |
977 | continue; | |
978 | } | |
2a7055ae | 979 | *dotdot = '.'; |
1215879c | 980 | } |
337cb3fb LT |
981 | if (*arg == '^') { |
982 | flags = UNINTERESTING; | |
983 | arg++; | |
984 | limited = 1; | |
985 | } | |
d8f6b342 LT |
986 | if (get_sha1(arg, sha1) < 0) { |
987 | struct stat st; | |
988 | if (lstat(arg, &st) < 0) | |
989 | die("'%s': %s", arg, strerror(errno)); | |
7b34c2fa | 990 | break; |
d8f6b342 | 991 | } |
19a7e715 | 992 | commit = get_commit_reference(arg, sha1, flags); |
1215879c | 993 | handle_one_commit(commit, &list); |
fcfda02b KS |
994 | } |
995 | ||
ef1cc2cc JH |
996 | if (!list && |
997 | (!(tag_objects||tree_objects||blob_objects) && !pending_objects)) | |
7b34c2fa LT |
998 | usage(rev_list_usage); |
999 | ||
1000 | paths = get_pathspec(prefix, argv + i); | |
1001 | if (paths) { | |
1002 | limited = 1; | |
1003 | diff_tree_setup_paths(paths); | |
1004 | } | |
1005 | ||
60ab26de | 1006 | save_commit_buffer = verbose_header; |
8805ccac | 1007 | track_object_refs = 0; |
60ab26de | 1008 | |
a3437b8c | 1009 | if (!merge_order) { |
a7336ae5 | 1010 | sort_by_date(&list); |
fe5f51ce LT |
1011 | if (list && !limited && max_count == 1 && |
1012 | !tag_objects && !tree_objects && !blob_objects) { | |
1013 | show_commit(list->item); | |
1014 | return 0; | |
1015 | } | |
17ebe977 | 1016 | if (limited) |
a3437b8c | 1017 | list = limit_list(list); |
d2d02a49 | 1018 | if (topo_order) |
4c8725f1 | 1019 | sort_in_topological_order(&list, lifo); |
a3437b8c | 1020 | show_commit_list(list); |
a3437b8c | 1021 | } else { |
dd53c7ab | 1022 | #ifndef NO_OPENSSL |
a3437b8c | 1023 | if (sort_list_in_merge_order(list, &process_commit)) { |
dd53c7ab | 1024 | die("merge order sort failed\n"); |
a3437b8c | 1025 | } |
dd53c7ab PB |
1026 | #else |
1027 | die("merge order sort unsupported, OpenSSL not linked"); | |
1028 | #endif | |
a3437b8c | 1029 | } |
8906300f | 1030 | |
64745109 LT |
1031 | return 0; |
1032 | } |