11 /* bits #0 and #1 in revision.h */
13 #define COUNTED (1u << 2)
14 #define SHOWN (1u << 3)
15 #define TREECHANGE (1u << 4)
16 #define TMP_MARK (1u << 5) /* for isolated cases; clean after use */
18 static const char rev_list_usage[] =
19 "git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
29 " --merge-order [ --show-breaks ]\n"
32 " formatting output:\n"
34 " --objects | --objects-edge\n"
36 " --header | --pretty\n"
37 " --abbrev=nr | --no-abbrev\n"
44 static int unpacked = 0;
45 static int bisect_list = 0;
46 static int verbose_header = 0;
47 static int abbrev = DEFAULT_ABBREV;
48 static int show_parents = 0;
49 static int hdr_termination = 0;
50 static const char *commit_prefix = "";
51 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
52 static int merge_order = 0;
53 static int show_breaks = 0;
54 static int stop_traversal = 0;
55 static int no_merges = 0;
57 static void show_commit(struct commit *commit)
59 commit->object.flags |= SHOWN;
62 if (commit->object.flags & DISCONTINUITY) {
64 } else if (commit->object.flags & BOUNDARY) {
68 printf("%s%s", commit_prefix, sha1_to_hex(commit->object.sha1));
70 struct commit_list *parents = commit->parents;
72 struct object *o = &(parents->item->object);
73 parents = parents->next;
74 if (o->flags & TMP_MARK)
76 printf(" %s", sha1_to_hex(o->sha1));
79 /* TMP_MARK is a general purpose flag that can
80 * be used locally, but the user should clean
81 * things up after it is done with them.
83 for (parents = commit->parents;
85 parents = parents->next)
86 parents->item->object.flags &= ~TMP_MARK;
88 if (commit_format == CMIT_FMT_ONELINE)
94 static char pretty_header[16384];
95 pretty_print_commit(commit_format, commit, ~0, pretty_header, sizeof(pretty_header), abbrev);
96 printf("%s%c", pretty_header, hdr_termination);
101 static int rewrite_one(struct commit **pp)
104 struct commit *p = *pp;
105 if (p->object.flags & (TREECHANGE | UNINTERESTING))
109 *pp = p->parents->item;
113 static void rewrite_parents(struct commit *commit)
115 struct commit_list **pp = &commit->parents;
117 struct commit_list *parent = *pp;
118 if (rewrite_one(&parent->item) < 0) {
126 static int filter_commit(struct commit * commit)
128 if (stop_traversal && (commit->object.flags & BOUNDARY))
130 if (commit->object.flags & (UNINTERESTING|SHOWN))
132 if (revs.min_age != -1 && (commit->date > revs.min_age))
134 if (revs.max_age != -1 && (commit->date < revs.max_age)) {
138 if (no_merges && (commit->parents && commit->parents->next))
140 if (revs.paths && revs.dense) {
141 if (!(commit->object.flags & TREECHANGE))
143 rewrite_parents(commit);
148 static int process_commit(struct commit * commit)
150 int action=filter_commit(commit);
152 if (action == STOP) {
156 if (action == CONTINUE) {
160 if (revs.max_count != -1 && !revs.max_count--)
168 static struct object_list **process_blob(struct blob *blob,
169 struct object_list **p,
170 struct name_path *path,
173 struct object *obj = &blob->object;
175 if (!revs.blob_objects)
177 if (obj->flags & (UNINTERESTING | SEEN))
180 return add_object(obj, p, path, name);
183 static struct object_list **process_tree(struct tree *tree,
184 struct object_list **p,
185 struct name_path *path,
188 struct object *obj = &tree->object;
189 struct tree_entry_list *entry;
192 if (!revs.tree_objects)
194 if (obj->flags & (UNINTERESTING | SEEN))
196 if (parse_tree(tree) < 0)
197 die("bad tree object %s", sha1_to_hex(obj->sha1));
199 p = add_object(obj, p, path, name);
202 me.elem_len = strlen(name);
203 entry = tree->entries;
204 tree->entries = NULL;
206 struct tree_entry_list *next = entry->next;
207 if (entry->directory)
208 p = process_tree(entry->item.tree, p, &me, entry->name);
210 p = process_blob(entry->item.blob, p, &me, entry->name);
217 static void show_commit_list(struct commit_list *list)
219 struct object_list *objects = NULL, **p = &objects, *pending;
221 struct commit *commit = pop_most_recent_commit(&list, SEEN);
223 p = process_tree(commit->tree, p, NULL, "");
224 if (process_commit(commit) == STOP)
227 for (pending = revs.pending_objects; pending; pending = pending->next) {
228 struct object *obj = pending->item;
229 const char *name = pending->name;
230 if (obj->flags & (UNINTERESTING | SEEN))
232 if (obj->type == tag_type) {
234 p = add_object(obj, p, NULL, name);
237 if (obj->type == tree_type) {
238 p = process_tree((struct tree *)obj, p, NULL, name);
241 if (obj->type == blob_type) {
242 p = process_blob((struct blob *)obj, p, NULL, name);
245 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
248 /* An object with name "foo\n0000000..." can be used to
249 * confuse downstream git-pack-objects very badly.
251 const char *ep = strchr(objects->name, '\n');
253 printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
254 (int) (ep - objects->name),
258 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
259 objects = objects->next;
263 static int everybody_uninteresting(struct commit_list *orig)
265 struct commit_list *list = orig;
267 struct commit *commit = list->item;
269 if (commit->object.flags & UNINTERESTING)
277 * This is a truly stupid algorithm, but it's only
278 * used for bisection, and we just don't care enough.
280 * We care just barely enough to avoid recursing for
283 static int count_distance(struct commit_list *entry)
288 struct commit *commit = entry->item;
289 struct commit_list *p;
291 if (commit->object.flags & (UNINTERESTING | COUNTED))
293 if (!revs.paths || (commit->object.flags & TREECHANGE))
295 commit->object.flags |= COUNTED;
301 nr += count_distance(p);
310 static void clear_distance(struct commit_list *list)
313 struct commit *commit = list->item;
314 commit->object.flags &= ~COUNTED;
319 static struct commit_list *find_bisection(struct commit_list *list)
322 struct commit_list *p, *best;
327 if (!revs.paths || (p->item->object.flags & TREECHANGE))
334 for (p = list; p; p = p->next) {
337 if (revs.paths && !(p->item->object.flags & TREECHANGE))
340 distance = count_distance(p);
341 clear_distance(list);
342 if (nr - distance < distance)
343 distance = nr - distance;
344 if (distance > closest) {
354 static void mark_edge_parents_uninteresting(struct commit *commit)
356 struct commit_list *parents;
358 for (parents = commit->parents; parents; parents = parents->next) {
359 struct commit *parent = parents->item;
360 if (!(parent->object.flags & UNINTERESTING))
362 mark_tree_uninteresting(parent->tree);
363 if (revs.edge_hint && !(parent->object.flags & SHOWN)) {
364 parent->object.flags |= SHOWN;
365 printf("-%s\n", sha1_to_hex(parent->object.sha1));
370 static void mark_edges_uninteresting(struct commit_list *list)
372 for ( ; list; list = list->next) {
373 struct commit *commit = list->item;
375 if (commit->object.flags & UNINTERESTING) {
376 mark_tree_uninteresting(commit->tree);
379 mark_edge_parents_uninteresting(commit);
385 #define TREE_DIFFERENT 2
386 static int tree_difference = TREE_SAME;
388 static void file_add_remove(struct diff_options *options,
389 int addremove, unsigned mode,
390 const unsigned char *sha1,
391 const char *base, const char *path)
393 int diff = TREE_DIFFERENT;
396 * Is it an add of a new file? It means that
397 * the old tree didn't have it at all, so we
398 * will turn "TREE_SAME" -> "TREE_NEW", but
399 * leave any "TREE_DIFFERENT" alone (and if
400 * it already was "TREE_NEW", we'll keep it
401 * "TREE_NEW" of course).
403 if (addremove == '+') {
404 diff = tree_difference;
405 if (diff != TREE_SAME)
409 tree_difference = diff;
412 static void file_change(struct diff_options *options,
413 unsigned old_mode, unsigned new_mode,
414 const unsigned char *old_sha1,
415 const unsigned char *new_sha1,
416 const char *base, const char *path)
418 tree_difference = TREE_DIFFERENT;
421 static struct diff_options diff_opt = {
423 .add_remove = file_add_remove,
424 .change = file_change,
427 static int compare_tree(struct tree *t1, struct tree *t2)
432 return TREE_DIFFERENT;
433 tree_difference = TREE_SAME;
434 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "", &diff_opt) < 0)
435 return TREE_DIFFERENT;
436 return tree_difference;
439 static int same_tree_as_empty(struct tree *t1)
443 struct tree_desc empty, real;
448 tree = read_object_with_reference(t1->object.sha1, "tree", &real.size, NULL);
457 retval = diff_tree(&empty, &real, "", &diff_opt);
460 return retval >= 0 && !tree_difference;
463 static void try_to_simplify_commit(struct commit *commit)
465 struct commit_list **pp, *parent;
470 if (!commit->parents) {
471 if (!same_tree_as_empty(commit->tree))
472 commit->object.flags |= TREECHANGE;
476 pp = &commit->parents;
477 while ((parent = *pp) != NULL) {
478 struct commit *p = parent->item;
480 if (p->object.flags & UNINTERESTING) {
486 switch (compare_tree(p->tree, commit->tree)) {
489 commit->parents = parent;
493 if (revs.remove_empty_trees && same_tree_as_empty(p->tree)) {
502 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
504 commit->object.flags |= TREECHANGE;
507 static void add_parents_to_list(struct commit *commit, struct commit_list **list)
509 struct commit_list *parent = commit->parents;
512 * If the commit is uninteresting, don't try to
513 * prune parents - we want the maximal uninteresting
516 * Normally we haven't parsed the parent
517 * yet, so we won't have a parent of a parent
518 * here. However, it may turn out that we've
519 * reached this commit some other way (where it
520 * wasn't uninteresting), in which case we need
521 * to mark its parents recursively too..
523 if (commit->object.flags & UNINTERESTING) {
525 struct commit *p = parent->item;
526 parent = parent->next;
528 p->object.flags |= UNINTERESTING;
530 mark_parents_uninteresting(p);
531 if (p->object.flags & SEEN)
533 p->object.flags |= SEEN;
534 insert_by_date(p, list);
540 * Ok, the commit wasn't uninteresting. Try to
541 * simplify the commit history and find the parent
542 * that has no differences in the path set if one exists.
545 try_to_simplify_commit(commit);
547 parent = commit->parents;
549 struct commit *p = parent->item;
551 parent = parent->next;
554 if (p->object.flags & SEEN)
556 p->object.flags |= SEEN;
557 insert_by_date(p, list);
561 static struct commit_list *limit_list(struct commit_list *list)
563 struct commit_list *newlist = NULL;
564 struct commit_list **p = &newlist;
566 struct commit_list *entry = list;
567 struct commit *commit = list->item;
568 struct object *obj = &commit->object;
573 if (revs.max_age != -1 && (commit->date < revs.max_age))
574 obj->flags |= UNINTERESTING;
575 if (unpacked && has_sha1_pack(obj->sha1))
576 obj->flags |= UNINTERESTING;
577 add_parents_to_list(commit, &list);
578 if (obj->flags & UNINTERESTING) {
579 mark_parents_uninteresting(commit);
580 if (everybody_uninteresting(list))
584 if (revs.min_age != -1 && (commit->date > revs.min_age))
586 p = &commit_list_insert(commit, p)->next;
588 if (revs.tree_objects)
589 mark_edges_uninteresting(newlist);
591 newlist = find_bisection(newlist);
595 int main(int argc, const char **argv)
597 struct commit_list *list;
600 argc = setup_revisions(argc, argv, &revs);
602 for (i = 1 ; i < argc; i++) {
603 const char *arg = argv[i];
605 /* accept -<digit>, like traditilnal "head" */
606 if ((*arg == '-') && isdigit(arg[1])) {
607 revs.max_count = atoi(arg + 1);
610 if (!strcmp(arg, "-n")) {
612 die("-n requires an argument");
613 revs.max_count = atoi(argv[i]);
616 if (!strncmp(arg,"-n",2)) {
617 revs.max_count = atoi(arg + 2);
620 if (!strcmp(arg, "--header")) {
624 if (!strcmp(arg, "--no-abbrev")) {
628 if (!strncmp(arg, "--abbrev=", 9)) {
629 abbrev = strtoul(arg + 9, NULL, 10);
630 if (abbrev && abbrev < MINIMUM_ABBREV)
631 abbrev = MINIMUM_ABBREV;
632 else if (40 < abbrev)
636 if (!strncmp(arg, "--pretty", 8)) {
637 commit_format = get_commit_format(arg+8);
639 hdr_termination = '\n';
640 if (commit_format == CMIT_FMT_ONELINE)
643 commit_prefix = "commit ";
646 if (!strncmp(arg, "--no-merges", 11)) {
650 if (!strcmp(arg, "--parents")) {
654 if (!strcmp(arg, "--bisect")) {
658 if (!strcmp(arg, "--unpacked")) {
663 if (!strcmp(arg, "--merge-order")) {
667 if (!strcmp(arg, "--show-breaks")) {
671 usage(rev_list_usage);
683 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) && !revs.pending_objects))
684 usage(rev_list_usage);
688 diff_tree_setup_paths(revs.paths);
690 if (revs.max_age != -1 || revs.min_age != -1)
693 save_commit_buffer = verbose_header;
694 track_object_refs = 0;
698 if (list && !limited && revs.max_count == 1 &&
699 !revs.tag_objects && !revs.tree_objects && !revs.blob_objects) {
700 show_commit(list->item);
704 list = limit_list(list);
706 sort_in_topological_order(&list, revs.lifo);
707 show_commit_list(list);
710 if (sort_list_in_merge_order(list, &process_commit)) {
711 die("merge order sort failed\n");
714 die("merge order sort unsupported, OpenSSL not linked");