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 struct object_list
*pending_objects
= NULL
;
219 static void show_commit_list(struct commit_list
*list
)
221 struct object_list
*objects
= NULL
, **p
= &objects
, *pending
;
223 struct commit
*commit
= pop_most_recent_commit(&list
, SEEN
);
225 p
= process_tree(commit
->tree
, p
, NULL
, "");
226 if (process_commit(commit
) == STOP
)
229 for (pending
= pending_objects
; pending
; pending
= pending
->next
) {
230 struct object
*obj
= pending
->item
;
231 const char *name
= pending
->name
;
232 if (obj
->flags
& (UNINTERESTING
| SEEN
))
234 if (obj
->type
== tag_type
) {
236 p
= add_object(obj
, p
, NULL
, name
);
239 if (obj
->type
== tree_type
) {
240 p
= process_tree((struct tree
*)obj
, p
, NULL
, name
);
243 if (obj
->type
== blob_type
) {
244 p
= process_blob((struct blob
*)obj
, p
, NULL
, name
);
247 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
250 /* An object with name "foo\n0000000..." can be used to
251 * confuse downstream git-pack-objects very badly.
253 const char *ep
= strchr(objects
->name
, '\n');
255 printf("%s %.*s\n", sha1_to_hex(objects
->item
->sha1
),
256 (int) (ep
- objects
->name
),
260 printf("%s %s\n", sha1_to_hex(objects
->item
->sha1
), objects
->name
);
261 objects
= objects
->next
;
265 static int everybody_uninteresting(struct commit_list
*orig
)
267 struct commit_list
*list
= orig
;
269 struct commit
*commit
= list
->item
;
271 if (commit
->object
.flags
& UNINTERESTING
)
279 * This is a truly stupid algorithm, but it's only
280 * used for bisection, and we just don't care enough.
282 * We care just barely enough to avoid recursing for
285 static int count_distance(struct commit_list
*entry
)
290 struct commit
*commit
= entry
->item
;
291 struct commit_list
*p
;
293 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
295 if (!revs
.paths
|| (commit
->object
.flags
& TREECHANGE
))
297 commit
->object
.flags
|= COUNTED
;
303 nr
+= count_distance(p
);
312 static void clear_distance(struct commit_list
*list
)
315 struct commit
*commit
= list
->item
;
316 commit
->object
.flags
&= ~COUNTED
;
321 static struct commit_list
*find_bisection(struct commit_list
*list
)
324 struct commit_list
*p
, *best
;
329 if (!revs
.paths
|| (p
->item
->object
.flags
& TREECHANGE
))
336 for (p
= list
; p
; p
= p
->next
) {
339 if (revs
.paths
&& !(p
->item
->object
.flags
& TREECHANGE
))
342 distance
= count_distance(p
);
343 clear_distance(list
);
344 if (nr
- distance
< distance
)
345 distance
= nr
- distance
;
346 if (distance
> closest
) {
356 static void mark_edge_parents_uninteresting(struct commit
*commit
)
358 struct commit_list
*parents
;
360 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
361 struct commit
*parent
= parents
->item
;
362 if (!(parent
->object
.flags
& UNINTERESTING
))
364 mark_tree_uninteresting(parent
->tree
);
365 if (revs
.edge_hint
&& !(parent
->object
.flags
& SHOWN
)) {
366 parent
->object
.flags
|= SHOWN
;
367 printf("-%s\n", sha1_to_hex(parent
->object
.sha1
));
372 static void mark_edges_uninteresting(struct commit_list
*list
)
374 for ( ; list
; list
= list
->next
) {
375 struct commit
*commit
= list
->item
;
377 if (commit
->object
.flags
& UNINTERESTING
) {
378 mark_tree_uninteresting(commit
->tree
);
381 mark_edge_parents_uninteresting(commit
);
387 #define TREE_DIFFERENT 2
388 static int tree_difference
= TREE_SAME
;
390 static void file_add_remove(struct diff_options
*options
,
391 int addremove
, unsigned mode
,
392 const unsigned char *sha1
,
393 const char *base
, const char *path
)
395 int diff
= TREE_DIFFERENT
;
398 * Is it an add of a new file? It means that
399 * the old tree didn't have it at all, so we
400 * will turn "TREE_SAME" -> "TREE_NEW", but
401 * leave any "TREE_DIFFERENT" alone (and if
402 * it already was "TREE_NEW", we'll keep it
403 * "TREE_NEW" of course).
405 if (addremove
== '+') {
406 diff
= tree_difference
;
407 if (diff
!= TREE_SAME
)
411 tree_difference
= diff
;
414 static void file_change(struct diff_options
*options
,
415 unsigned old_mode
, unsigned new_mode
,
416 const unsigned char *old_sha1
,
417 const unsigned char *new_sha1
,
418 const char *base
, const char *path
)
420 tree_difference
= TREE_DIFFERENT
;
423 static struct diff_options diff_opt
= {
425 .add_remove
= file_add_remove
,
426 .change
= file_change
,
429 static int compare_tree(struct tree
*t1
, struct tree
*t2
)
434 return TREE_DIFFERENT
;
435 tree_difference
= TREE_SAME
;
436 if (diff_tree_sha1(t1
->object
.sha1
, t2
->object
.sha1
, "", &diff_opt
) < 0)
437 return TREE_DIFFERENT
;
438 return tree_difference
;
441 static int same_tree_as_empty(struct tree
*t1
)
445 struct tree_desc empty
, real
;
450 tree
= read_object_with_reference(t1
->object
.sha1
, "tree", &real
.size
, NULL
);
459 retval
= diff_tree(&empty
, &real
, "", &diff_opt
);
462 return retval
>= 0 && !tree_difference
;
465 static void try_to_simplify_commit(struct commit
*commit
)
467 struct commit_list
**pp
, *parent
;
472 if (!commit
->parents
) {
473 if (!same_tree_as_empty(commit
->tree
))
474 commit
->object
.flags
|= TREECHANGE
;
478 pp
= &commit
->parents
;
479 while ((parent
= *pp
) != NULL
) {
480 struct commit
*p
= parent
->item
;
482 if (p
->object
.flags
& UNINTERESTING
) {
488 switch (compare_tree(p
->tree
, commit
->tree
)) {
491 commit
->parents
= parent
;
495 if (revs
.remove_empty_trees
&& same_tree_as_empty(p
->tree
)) {
504 die("bad tree compare for commit %s", sha1_to_hex(commit
->object
.sha1
));
506 commit
->object
.flags
|= TREECHANGE
;
509 static void add_parents_to_list(struct commit
*commit
, struct commit_list
**list
)
511 struct commit_list
*parent
= commit
->parents
;
514 * If the commit is uninteresting, don't try to
515 * prune parents - we want the maximal uninteresting
518 * Normally we haven't parsed the parent
519 * yet, so we won't have a parent of a parent
520 * here. However, it may turn out that we've
521 * reached this commit some other way (where it
522 * wasn't uninteresting), in which case we need
523 * to mark its parents recursively too..
525 if (commit
->object
.flags
& UNINTERESTING
) {
527 struct commit
*p
= parent
->item
;
528 parent
= parent
->next
;
530 p
->object
.flags
|= UNINTERESTING
;
532 mark_parents_uninteresting(p
);
533 if (p
->object
.flags
& SEEN
)
535 p
->object
.flags
|= SEEN
;
536 insert_by_date(p
, list
);
542 * Ok, the commit wasn't uninteresting. Try to
543 * simplify the commit history and find the parent
544 * that has no differences in the path set if one exists.
547 try_to_simplify_commit(commit
);
549 parent
= commit
->parents
;
551 struct commit
*p
= parent
->item
;
553 parent
= parent
->next
;
556 if (p
->object
.flags
& SEEN
)
558 p
->object
.flags
|= SEEN
;
559 insert_by_date(p
, list
);
563 static struct commit_list
*limit_list(struct commit_list
*list
)
565 struct commit_list
*newlist
= NULL
;
566 struct commit_list
**p
= &newlist
;
568 struct commit_list
*entry
= list
;
569 struct commit
*commit
= list
->item
;
570 struct object
*obj
= &commit
->object
;
575 if (revs
.max_age
!= -1 && (commit
->date
< revs
.max_age
))
576 obj
->flags
|= UNINTERESTING
;
577 if (unpacked
&& has_sha1_pack(obj
->sha1
))
578 obj
->flags
|= UNINTERESTING
;
579 add_parents_to_list(commit
, &list
);
580 if (obj
->flags
& UNINTERESTING
) {
581 mark_parents_uninteresting(commit
);
582 if (everybody_uninteresting(list
))
586 if (revs
.min_age
!= -1 && (commit
->date
> revs
.min_age
))
588 p
= &commit_list_insert(commit
, p
)->next
;
590 if (revs
.tree_objects
)
591 mark_edges_uninteresting(newlist
);
593 newlist
= find_bisection(newlist
);
597 int main(int argc
, const char **argv
)
599 struct commit_list
*list
;
602 argc
= setup_revisions(argc
, argv
, &revs
);
604 for (i
= 1 ; i
< argc
; i
++) {
605 const char *arg
= argv
[i
];
607 /* accept -<digit>, like traditilnal "head" */
608 if ((*arg
== '-') && isdigit(arg
[1])) {
609 revs
.max_count
= atoi(arg
+ 1);
612 if (!strcmp(arg
, "-n")) {
614 die("-n requires an argument");
615 revs
.max_count
= atoi(argv
[i
]);
618 if (!strncmp(arg
,"-n",2)) {
619 revs
.max_count
= atoi(arg
+ 2);
622 if (!strcmp(arg
, "--header")) {
626 if (!strcmp(arg
, "--no-abbrev")) {
630 if (!strncmp(arg
, "--abbrev=", 9)) {
631 abbrev
= strtoul(arg
+ 9, NULL
, 10);
632 if (abbrev
&& abbrev
< MINIMUM_ABBREV
)
633 abbrev
= MINIMUM_ABBREV
;
634 else if (40 < abbrev
)
638 if (!strncmp(arg
, "--pretty", 8)) {
639 commit_format
= get_commit_format(arg
+8);
641 hdr_termination
= '\n';
642 if (commit_format
== CMIT_FMT_ONELINE
)
645 commit_prefix
= "commit ";
648 if (!strncmp(arg
, "--no-merges", 11)) {
652 if (!strcmp(arg
, "--parents")) {
656 if (!strcmp(arg
, "--bisect")) {
660 if (!strcmp(arg
, "--unpacked")) {
665 if (!strcmp(arg
, "--merge-order")) {
669 if (!strcmp(arg
, "--show-breaks")) {
673 usage(rev_list_usage
);
678 if (list
&& list
->next
)
685 (!(revs
.tag_objects
||revs
.tree_objects
||revs
.blob_objects
) && !revs
.pending_objects
))
686 usage(rev_list_usage
);
690 diff_tree_setup_paths(revs
.paths
);
692 if (revs
.max_age
|| revs
.min_age
)
695 save_commit_buffer
= verbose_header
;
696 track_object_refs
= 0;
700 if (list
&& !limited
&& revs
.max_count
== 1 &&
701 !revs
.tag_objects
&& !revs
.tree_objects
&& !revs
.blob_objects
) {
702 show_commit(list
->item
);
706 list
= limit_list(list
);
708 sort_in_topological_order(&list
, revs
.lifo
);
709 show_commit_list(list
);
712 if (sort_list_in_merge_order(list
, &process_commit
)) {
713 die("merge order sort failed\n");
716 die("merge order sort unsupported, OpenSSL not linked");