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 bisect_list
= 0;
45 static int verbose_header
= 0;
46 static int abbrev
= DEFAULT_ABBREV
;
47 static int show_parents
= 0;
48 static int hdr_termination
= 0;
49 static const char *commit_prefix
= "";
50 static enum cmit_fmt commit_format
= CMIT_FMT_RAW
;
51 static int merge_order
= 0;
52 static int show_breaks
= 0;
53 static int stop_traversal
= 0;
54 static int no_merges
= 0;
56 static void show_commit(struct commit
*commit
)
58 commit
->object
.flags
|= SHOWN
;
61 if (commit
->object
.flags
& DISCONTINUITY
) {
63 } else if (commit
->object
.flags
& BOUNDARY
) {
67 printf("%s%s", commit_prefix
, sha1_to_hex(commit
->object
.sha1
));
69 struct commit_list
*parents
= commit
->parents
;
71 struct object
*o
= &(parents
->item
->object
);
72 parents
= parents
->next
;
73 if (o
->flags
& TMP_MARK
)
75 printf(" %s", sha1_to_hex(o
->sha1
));
78 /* TMP_MARK is a general purpose flag that can
79 * be used locally, but the user should clean
80 * things up after it is done with them.
82 for (parents
= commit
->parents
;
84 parents
= parents
->next
)
85 parents
->item
->object
.flags
&= ~TMP_MARK
;
87 if (commit_format
== CMIT_FMT_ONELINE
)
93 static char pretty_header
[16384];
94 pretty_print_commit(commit_format
, commit
, ~0, pretty_header
, sizeof(pretty_header
), abbrev
);
95 printf("%s%c", pretty_header
, hdr_termination
);
100 static int rewrite_one(struct commit
**pp
)
103 struct commit
*p
= *pp
;
104 if (p
->object
.flags
& (TREECHANGE
| UNINTERESTING
))
108 *pp
= p
->parents
->item
;
112 static void rewrite_parents(struct commit
*commit
)
114 struct commit_list
**pp
= &commit
->parents
;
116 struct commit_list
*parent
= *pp
;
117 if (rewrite_one(&parent
->item
) < 0) {
125 static int filter_commit(struct commit
* commit
)
127 if (stop_traversal
&& (commit
->object
.flags
& BOUNDARY
))
129 if (commit
->object
.flags
& (UNINTERESTING
|SHOWN
))
131 if (revs
.min_age
!= -1 && (commit
->date
> revs
.min_age
))
133 if (revs
.max_age
!= -1 && (commit
->date
< revs
.max_age
)) {
137 if (no_merges
&& (commit
->parents
&& commit
->parents
->next
))
139 if (revs
.paths
&& revs
.dense
) {
140 if (!(commit
->object
.flags
& TREECHANGE
))
142 rewrite_parents(commit
);
147 static int process_commit(struct commit
* commit
)
149 int action
=filter_commit(commit
);
151 if (action
== STOP
) {
155 if (action
== CONTINUE
) {
159 if (revs
.max_count
!= -1 && !revs
.max_count
--)
167 static struct object_list
**process_blob(struct blob
*blob
,
168 struct object_list
**p
,
169 struct name_path
*path
,
172 struct object
*obj
= &blob
->object
;
174 if (!revs
.blob_objects
)
176 if (obj
->flags
& (UNINTERESTING
| SEEN
))
179 return add_object(obj
, p
, path
, name
);
182 static struct object_list
**process_tree(struct tree
*tree
,
183 struct object_list
**p
,
184 struct name_path
*path
,
187 struct object
*obj
= &tree
->object
;
188 struct tree_entry_list
*entry
;
191 if (!revs
.tree_objects
)
193 if (obj
->flags
& (UNINTERESTING
| SEEN
))
195 if (parse_tree(tree
) < 0)
196 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
198 p
= add_object(obj
, p
, path
, name
);
201 me
.elem_len
= strlen(name
);
202 entry
= tree
->entries
;
203 tree
->entries
= NULL
;
205 struct tree_entry_list
*next
= entry
->next
;
206 if (entry
->directory
)
207 p
= process_tree(entry
->item
.tree
, p
, &me
, entry
->name
);
209 p
= process_blob(entry
->item
.blob
, p
, &me
, entry
->name
);
216 static void show_commit_list(struct commit_list
*list
)
218 struct object_list
*objects
= NULL
, **p
= &objects
, *pending
;
220 struct commit
*commit
= pop_most_recent_commit(&list
, SEEN
);
222 p
= process_tree(commit
->tree
, p
, NULL
, "");
223 if (process_commit(commit
) == STOP
)
226 for (pending
= revs
.pending_objects
; pending
; pending
= pending
->next
) {
227 struct object
*obj
= pending
->item
;
228 const char *name
= pending
->name
;
229 if (obj
->flags
& (UNINTERESTING
| SEEN
))
231 if (obj
->type
== tag_type
) {
233 p
= add_object(obj
, p
, NULL
, name
);
236 if (obj
->type
== tree_type
) {
237 p
= process_tree((struct tree
*)obj
, p
, NULL
, name
);
240 if (obj
->type
== blob_type
) {
241 p
= process_blob((struct blob
*)obj
, p
, NULL
, name
);
244 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
247 /* An object with name "foo\n0000000..." can be used to
248 * confuse downstream git-pack-objects very badly.
250 const char *ep
= strchr(objects
->name
, '\n');
252 printf("%s %.*s\n", sha1_to_hex(objects
->item
->sha1
),
253 (int) (ep
- objects
->name
),
257 printf("%s %s\n", sha1_to_hex(objects
->item
->sha1
), objects
->name
);
258 objects
= objects
->next
;
262 static int everybody_uninteresting(struct commit_list
*orig
)
264 struct commit_list
*list
= orig
;
266 struct commit
*commit
= list
->item
;
268 if (commit
->object
.flags
& UNINTERESTING
)
276 * This is a truly stupid algorithm, but it's only
277 * used for bisection, and we just don't care enough.
279 * We care just barely enough to avoid recursing for
282 static int count_distance(struct commit_list
*entry
)
287 struct commit
*commit
= entry
->item
;
288 struct commit_list
*p
;
290 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
292 if (!revs
.paths
|| (commit
->object
.flags
& TREECHANGE
))
294 commit
->object
.flags
|= COUNTED
;
300 nr
+= count_distance(p
);
309 static void clear_distance(struct commit_list
*list
)
312 struct commit
*commit
= list
->item
;
313 commit
->object
.flags
&= ~COUNTED
;
318 static struct commit_list
*find_bisection(struct commit_list
*list
)
321 struct commit_list
*p
, *best
;
326 if (!revs
.paths
|| (p
->item
->object
.flags
& TREECHANGE
))
333 for (p
= list
; p
; p
= p
->next
) {
336 if (revs
.paths
&& !(p
->item
->object
.flags
& TREECHANGE
))
339 distance
= count_distance(p
);
340 clear_distance(list
);
341 if (nr
- distance
< distance
)
342 distance
= nr
- distance
;
343 if (distance
> closest
) {
353 static void mark_edge_parents_uninteresting(struct commit
*commit
)
355 struct commit_list
*parents
;
357 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
358 struct commit
*parent
= parents
->item
;
359 if (!(parent
->object
.flags
& UNINTERESTING
))
361 mark_tree_uninteresting(parent
->tree
);
362 if (revs
.edge_hint
&& !(parent
->object
.flags
& SHOWN
)) {
363 parent
->object
.flags
|= SHOWN
;
364 printf("-%s\n", sha1_to_hex(parent
->object
.sha1
));
369 static void mark_edges_uninteresting(struct commit_list
*list
)
371 for ( ; list
; list
= list
->next
) {
372 struct commit
*commit
= list
->item
;
374 if (commit
->object
.flags
& UNINTERESTING
) {
375 mark_tree_uninteresting(commit
->tree
);
378 mark_edge_parents_uninteresting(commit
);
384 #define TREE_DIFFERENT 2
385 static int tree_difference
= TREE_SAME
;
387 static void file_add_remove(struct diff_options
*options
,
388 int addremove
, unsigned mode
,
389 const unsigned char *sha1
,
390 const char *base
, const char *path
)
392 int diff
= TREE_DIFFERENT
;
395 * Is it an add of a new file? It means that
396 * the old tree didn't have it at all, so we
397 * will turn "TREE_SAME" -> "TREE_NEW", but
398 * leave any "TREE_DIFFERENT" alone (and if
399 * it already was "TREE_NEW", we'll keep it
400 * "TREE_NEW" of course).
402 if (addremove
== '+') {
403 diff
= tree_difference
;
404 if (diff
!= TREE_SAME
)
408 tree_difference
= diff
;
411 static void file_change(struct diff_options
*options
,
412 unsigned old_mode
, unsigned new_mode
,
413 const unsigned char *old_sha1
,
414 const unsigned char *new_sha1
,
415 const char *base
, const char *path
)
417 tree_difference
= TREE_DIFFERENT
;
420 static struct diff_options diff_opt
= {
422 .add_remove
= file_add_remove
,
423 .change
= file_change
,
426 static int compare_tree(struct tree
*t1
, struct tree
*t2
)
431 return TREE_DIFFERENT
;
432 tree_difference
= TREE_SAME
;
433 if (diff_tree_sha1(t1
->object
.sha1
, t2
->object
.sha1
, "", &diff_opt
) < 0)
434 return TREE_DIFFERENT
;
435 return tree_difference
;
438 static int same_tree_as_empty(struct tree
*t1
)
442 struct tree_desc empty
, real
;
447 tree
= read_object_with_reference(t1
->object
.sha1
, "tree", &real
.size
, NULL
);
456 retval
= diff_tree(&empty
, &real
, "", &diff_opt
);
459 return retval
>= 0 && !tree_difference
;
462 static void try_to_simplify_commit(struct commit
*commit
)
464 struct commit_list
**pp
, *parent
;
469 if (!commit
->parents
) {
470 if (!same_tree_as_empty(commit
->tree
))
471 commit
->object
.flags
|= TREECHANGE
;
475 pp
= &commit
->parents
;
476 while ((parent
= *pp
) != NULL
) {
477 struct commit
*p
= parent
->item
;
479 if (p
->object
.flags
& UNINTERESTING
) {
485 switch (compare_tree(p
->tree
, commit
->tree
)) {
488 commit
->parents
= parent
;
492 if (revs
.remove_empty_trees
&& same_tree_as_empty(p
->tree
)) {
501 die("bad tree compare for commit %s", sha1_to_hex(commit
->object
.sha1
));
503 commit
->object
.flags
|= TREECHANGE
;
506 static void add_parents_to_list(struct commit
*commit
, struct commit_list
**list
)
508 struct commit_list
*parent
= commit
->parents
;
511 * If the commit is uninteresting, don't try to
512 * prune parents - we want the maximal uninteresting
515 * Normally we haven't parsed the parent
516 * yet, so we won't have a parent of a parent
517 * here. However, it may turn out that we've
518 * reached this commit some other way (where it
519 * wasn't uninteresting), in which case we need
520 * to mark its parents recursively too..
522 if (commit
->object
.flags
& UNINTERESTING
) {
524 struct commit
*p
= parent
->item
;
525 parent
= parent
->next
;
527 p
->object
.flags
|= UNINTERESTING
;
529 mark_parents_uninteresting(p
);
530 if (p
->object
.flags
& SEEN
)
532 p
->object
.flags
|= SEEN
;
533 insert_by_date(p
, list
);
539 * Ok, the commit wasn't uninteresting. Try to
540 * simplify the commit history and find the parent
541 * that has no differences in the path set if one exists.
544 try_to_simplify_commit(commit
);
546 parent
= commit
->parents
;
548 struct commit
*p
= parent
->item
;
550 parent
= parent
->next
;
553 if (p
->object
.flags
& SEEN
)
555 p
->object
.flags
|= SEEN
;
556 insert_by_date(p
, list
);
560 static struct commit_list
*limit_list(struct commit_list
*list
)
562 struct commit_list
*newlist
= NULL
;
563 struct commit_list
**p
= &newlist
;
565 struct commit_list
*entry
= list
;
566 struct commit
*commit
= list
->item
;
567 struct object
*obj
= &commit
->object
;
572 if (revs
.max_age
!= -1 && (commit
->date
< revs
.max_age
))
573 obj
->flags
|= UNINTERESTING
;
574 if (revs
.unpacked
&& has_sha1_pack(obj
->sha1
))
575 obj
->flags
|= UNINTERESTING
;
576 add_parents_to_list(commit
, &list
);
577 if (obj
->flags
& UNINTERESTING
) {
578 mark_parents_uninteresting(commit
);
579 if (everybody_uninteresting(list
))
583 if (revs
.min_age
!= -1 && (commit
->date
> revs
.min_age
))
585 p
= &commit_list_insert(commit
, p
)->next
;
587 if (revs
.tree_objects
)
588 mark_edges_uninteresting(newlist
);
590 newlist
= find_bisection(newlist
);
594 int main(int argc
, const char **argv
)
596 struct commit_list
*list
;
599 argc
= setup_revisions(argc
, argv
, &revs
);
601 for (i
= 1 ; i
< argc
; i
++) {
602 const char *arg
= argv
[i
];
604 /* accept -<digit>, like traditilnal "head" */
605 if ((*arg
== '-') && isdigit(arg
[1])) {
606 revs
.max_count
= atoi(arg
+ 1);
609 if (!strcmp(arg
, "-n")) {
611 die("-n requires an argument");
612 revs
.max_count
= atoi(argv
[i
]);
615 if (!strncmp(arg
,"-n",2)) {
616 revs
.max_count
= atoi(arg
+ 2);
619 if (!strcmp(arg
, "--header")) {
623 if (!strcmp(arg
, "--no-abbrev")) {
627 if (!strncmp(arg
, "--abbrev=", 9)) {
628 abbrev
= strtoul(arg
+ 9, NULL
, 10);
629 if (abbrev
&& abbrev
< MINIMUM_ABBREV
)
630 abbrev
= MINIMUM_ABBREV
;
631 else if (40 < abbrev
)
635 if (!strncmp(arg
, "--pretty", 8)) {
636 commit_format
= get_commit_format(arg
+8);
638 hdr_termination
= '\n';
639 if (commit_format
== CMIT_FMT_ONELINE
)
642 commit_prefix
= "commit ";
645 if (!strncmp(arg
, "--no-merges", 11)) {
649 if (!strcmp(arg
, "--parents")) {
653 if (!strcmp(arg
, "--bisect")) {
657 if (!strcmp(arg
, "--merge-order")) {
661 if (!strcmp(arg
, "--show-breaks")) {
665 usage(rev_list_usage
);
672 (!(revs
.tag_objects
||revs
.tree_objects
||revs
.blob_objects
) && !revs
.pending_objects
))
673 usage(rev_list_usage
);
676 diff_tree_setup_paths(revs
.paths
);
678 save_commit_buffer
= verbose_header
;
679 track_object_refs
= 0;
683 if (list
&& !revs
.limited
&& revs
.max_count
== 1 &&
684 !revs
.tag_objects
&& !revs
.tree_objects
&& !revs
.blob_objects
) {
685 show_commit(list
->item
);
689 list
= limit_list(list
);
691 sort_in_topological_order(&list
, revs
.lifo
);
692 show_commit_list(list
);
695 if (sort_list_in_merge_order(list
, &process_commit
)) {
696 die("merge order sort failed\n");
699 die("merge order sort unsupported, OpenSSL not linked");