10 #include "reflog-walk.h"
12 static char *path_name(struct name_path
*path
, const char *name
)
16 int nlen
= strlen(name
);
19 for (p
= path
; p
; p
= p
->up
) {
21 len
+= p
->elem_len
+ 1;
24 m
= n
+ len
- (nlen
+ 1);
26 for (p
= path
; p
; p
= p
->up
) {
29 memcpy(m
, p
->elem
, p
->elem_len
);
36 void add_object(struct object
*obj
,
37 struct object_array
*p
,
38 struct name_path
*path
,
41 add_object_array(obj
, path_name(path
, name
), p
);
44 static void mark_blob_uninteresting(struct blob
*blob
)
46 if (blob
->object
.flags
& UNINTERESTING
)
48 blob
->object
.flags
|= UNINTERESTING
;
51 void mark_tree_uninteresting(struct tree
*tree
)
53 struct tree_desc desc
;
54 struct name_entry entry
;
55 struct object
*obj
= &tree
->object
;
57 if (obj
->flags
& UNINTERESTING
)
59 obj
->flags
|= UNINTERESTING
;
60 if (!has_sha1_file(obj
->sha1
))
62 if (parse_tree(tree
) < 0)
63 die("bad tree %s", sha1_to_hex(obj
->sha1
));
65 desc
.buf
= tree
->buffer
;
66 desc
.size
= tree
->size
;
67 while (tree_entry(&desc
, &entry
)) {
68 if (S_ISDIR(entry
.mode
))
69 mark_tree_uninteresting(lookup_tree(entry
.sha1
));
71 mark_blob_uninteresting(lookup_blob(entry
.sha1
));
75 * We don't care about the tree any more
76 * after it has been marked uninteresting.
82 void mark_parents_uninteresting(struct commit
*commit
)
84 struct commit_list
*parents
= commit
->parents
;
87 struct commit
*commit
= parents
->item
;
88 if (!(commit
->object
.flags
& UNINTERESTING
)) {
89 commit
->object
.flags
|= UNINTERESTING
;
92 * Normally we haven't parsed the parent
93 * yet, so we won't have a parent of a parent
94 * here. However, it may turn out that we've
95 * reached this commit some other way (where it
96 * wasn't uninteresting), in which case we need
97 * to mark its parents recursively too..
100 mark_parents_uninteresting(commit
);
104 * A missing commit is ok iff its parent is marked
107 * We just mark such a thing parsed, so that when
108 * it is popped next time around, we won't be trying
109 * to parse it and get an error.
111 if (!has_sha1_file(commit
->object
.sha1
))
112 commit
->object
.parsed
= 1;
113 parents
= parents
->next
;
117 void add_pending_object(struct rev_info
*revs
, struct object
*obj
, const char *name
)
119 if (revs
->no_walk
&& (obj
->flags
& UNINTERESTING
))
120 die("object ranges do not make sense when not walking revisions");
121 add_object_array(obj
, name
, &revs
->pending
);
122 if (revs
->reflog_info
&& obj
->type
== OBJ_COMMIT
)
123 add_reflog_for_walk(revs
->reflog_info
,
124 (struct commit
*)obj
, name
);
127 static struct object
*get_reference(struct rev_info
*revs
, const char *name
, const unsigned char *sha1
, unsigned int flags
)
129 struct object
*object
;
131 object
= parse_object(sha1
);
133 die("bad object %s", name
);
134 object
->flags
|= flags
;
138 static struct commit
*handle_commit(struct rev_info
*revs
, struct object
*object
, const char *name
)
140 unsigned long flags
= object
->flags
;
143 * Tag object? Look what it points to..
145 while (object
->type
== OBJ_TAG
) {
146 struct tag
*tag
= (struct tag
*) object
;
147 if (revs
->tag_objects
&& !(flags
& UNINTERESTING
))
148 add_pending_object(revs
, object
, tag
->tag
);
149 object
= parse_object(tag
->tagged
->sha1
);
151 die("bad object %s", sha1_to_hex(tag
->tagged
->sha1
));
155 * Commit object? Just return it, we'll do all the complex
158 if (object
->type
== OBJ_COMMIT
) {
159 struct commit
*commit
= (struct commit
*)object
;
160 if (parse_commit(commit
) < 0)
161 die("unable to parse commit %s", name
);
162 if (flags
& UNINTERESTING
) {
163 commit
->object
.flags
|= UNINTERESTING
;
164 mark_parents_uninteresting(commit
);
171 * Tree object? Either mark it uniniteresting, or add it
172 * to the list of objects to look at later..
174 if (object
->type
== OBJ_TREE
) {
175 struct tree
*tree
= (struct tree
*)object
;
176 if (!revs
->tree_objects
)
178 if (flags
& UNINTERESTING
) {
179 mark_tree_uninteresting(tree
);
182 add_pending_object(revs
, object
, "");
187 * Blob object? You know the drill by now..
189 if (object
->type
== OBJ_BLOB
) {
190 struct blob
*blob
= (struct blob
*)object
;
191 if (!revs
->blob_objects
)
193 if (flags
& UNINTERESTING
) {
194 mark_blob_uninteresting(blob
);
197 add_pending_object(revs
, object
, "");
200 die("%s is unknown object", name
);
203 static int everybody_uninteresting(struct commit_list
*orig
)
205 struct commit_list
*list
= orig
;
207 struct commit
*commit
= list
->item
;
209 if (commit
->object
.flags
& UNINTERESTING
)
216 static int tree_difference
= REV_TREE_SAME
;
218 static void file_add_remove(struct diff_options
*options
,
219 int addremove
, unsigned mode
,
220 const unsigned char *sha1
,
221 const char *base
, const char *path
)
223 int diff
= REV_TREE_DIFFERENT
;
226 * Is it an add of a new file? It means that the old tree
227 * didn't have it at all, so we will turn "REV_TREE_SAME" ->
228 * "REV_TREE_NEW", but leave any "REV_TREE_DIFFERENT" alone
229 * (and if it already was "REV_TREE_NEW", we'll keep it
230 * "REV_TREE_NEW" of course).
232 if (addremove
== '+') {
233 diff
= tree_difference
;
234 if (diff
!= REV_TREE_SAME
)
238 tree_difference
= diff
;
241 static void file_change(struct diff_options
*options
,
242 unsigned old_mode
, unsigned new_mode
,
243 const unsigned char *old_sha1
,
244 const unsigned char *new_sha1
,
245 const char *base
, const char *path
)
247 tree_difference
= REV_TREE_DIFFERENT
;
250 int rev_compare_tree(struct rev_info
*revs
, struct tree
*t1
, struct tree
*t2
)
255 return REV_TREE_DIFFERENT
;
256 tree_difference
= REV_TREE_SAME
;
257 if (diff_tree_sha1(t1
->object
.sha1
, t2
->object
.sha1
, "",
259 return REV_TREE_DIFFERENT
;
260 return tree_difference
;
263 int rev_same_tree_as_empty(struct rev_info
*revs
, struct tree
*t1
)
267 struct tree_desc empty
, real
;
272 tree
= read_object_with_reference(t1
->object
.sha1
, tree_type
, &real
.size
, NULL
);
281 retval
= diff_tree(&empty
, &real
, "", &revs
->pruning
);
284 return retval
>= 0 && !tree_difference
;
287 static void try_to_simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
289 struct commit_list
**pp
, *parent
;
290 int tree_changed
= 0, tree_same
= 0;
295 if (!commit
->parents
) {
296 if (!rev_same_tree_as_empty(revs
, commit
->tree
))
297 commit
->object
.flags
|= TREECHANGE
;
301 pp
= &commit
->parents
;
302 while ((parent
= *pp
) != NULL
) {
303 struct commit
*p
= parent
->item
;
306 switch (rev_compare_tree(revs
, p
->tree
, commit
->tree
)) {
309 if (!revs
->simplify_history
|| (p
->object
.flags
& UNINTERESTING
)) {
310 /* Even if a merge with an uninteresting
311 * side branch brought the entire change
312 * we are interested in, we do not want
313 * to lose the other branches of this
314 * merge, so we just keep going.
320 commit
->parents
= parent
;
324 if (revs
->remove_empty_trees
&&
325 rev_same_tree_as_empty(revs
, p
->tree
)) {
326 /* We are adding all the specified
327 * paths from this parent, so the
328 * history beyond this parent is not
329 * interesting. Remove its parents
330 * (they are grandparents for us).
331 * IOW, we pretend this parent is a
338 case REV_TREE_DIFFERENT
:
343 die("bad tree compare for commit %s", sha1_to_hex(commit
->object
.sha1
));
345 if (tree_changed
&& !tree_same
)
346 commit
->object
.flags
|= TREECHANGE
;
349 static void add_parents_to_list(struct rev_info
*revs
, struct commit
*commit
, struct commit_list
**list
)
351 struct commit_list
*parent
= commit
->parents
;
354 if (commit
->object
.flags
& ADDED
)
356 commit
->object
.flags
|= ADDED
;
359 * If the commit is uninteresting, don't try to
360 * prune parents - we want the maximal uninteresting
363 * Normally we haven't parsed the parent
364 * yet, so we won't have a parent of a parent
365 * here. However, it may turn out that we've
366 * reached this commit some other way (where it
367 * wasn't uninteresting), in which case we need
368 * to mark its parents recursively too..
370 if (commit
->object
.flags
& UNINTERESTING
) {
372 struct commit
*p
= parent
->item
;
373 parent
= parent
->next
;
375 p
->object
.flags
|= UNINTERESTING
;
377 mark_parents_uninteresting(p
);
378 if (p
->object
.flags
& SEEN
)
380 p
->object
.flags
|= SEEN
;
381 insert_by_date(p
, list
);
387 * Ok, the commit wasn't uninteresting. Try to
388 * simplify the commit history and find the parent
389 * that has no differences in the path set if one exists.
392 revs
->prune_fn(revs
, commit
);
397 left_flag
= (commit
->object
.flags
& SYMMETRIC_LEFT
);
398 parent
= commit
->parents
;
400 struct commit
*p
= parent
->item
;
402 parent
= parent
->next
;
405 p
->object
.flags
|= left_flag
;
406 if (p
->object
.flags
& SEEN
)
408 p
->object
.flags
|= SEEN
;
409 insert_by_date(p
, list
);
413 static void limit_list(struct rev_info
*revs
)
415 struct commit_list
*list
= revs
->commits
;
416 struct commit_list
*newlist
= NULL
;
417 struct commit_list
**p
= &newlist
;
420 struct commit_list
*entry
= list
;
421 struct commit
*commit
= list
->item
;
422 struct object
*obj
= &commit
->object
;
427 if (revs
->max_age
!= -1 && (commit
->date
< revs
->max_age
))
428 obj
->flags
|= UNINTERESTING
;
429 add_parents_to_list(revs
, commit
, &list
);
430 if (obj
->flags
& UNINTERESTING
) {
431 mark_parents_uninteresting(commit
);
432 if (everybody_uninteresting(list
))
436 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
438 p
= &commit_list_insert(commit
, p
)->next
;
440 if (revs
->boundary
) {
441 /* mark the ones that are on the result list first */
442 for (list
= newlist
; list
; list
= list
->next
) {
443 struct commit
*commit
= list
->item
;
444 commit
->object
.flags
|= TMP_MARK
;
446 for (list
= newlist
; list
; list
= list
->next
) {
447 struct commit
*commit
= list
->item
;
448 struct object
*obj
= &commit
->object
;
449 struct commit_list
*parent
;
450 if (obj
->flags
& UNINTERESTING
)
452 for (parent
= commit
->parents
;
454 parent
= parent
->next
) {
455 struct commit
*pcommit
= parent
->item
;
456 if (!(pcommit
->object
.flags
& UNINTERESTING
))
458 pcommit
->object
.flags
|= BOUNDARY
;
459 if (pcommit
->object
.flags
& TMP_MARK
)
461 pcommit
->object
.flags
|= TMP_MARK
;
462 p
= &commit_list_insert(pcommit
, p
)->next
;
465 for (list
= newlist
; list
; list
= list
->next
) {
466 struct commit
*commit
= list
->item
;
467 commit
->object
.flags
&= ~TMP_MARK
;
470 revs
->commits
= newlist
;
475 int warned_bad_reflog
;
476 struct rev_info
*all_revs
;
477 const char *name_for_errormsg
;
480 static int handle_one_ref(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
482 struct all_refs_cb
*cb
= cb_data
;
483 struct object
*object
= get_reference(cb
->all_revs
, path
, sha1
,
485 add_pending_object(cb
->all_revs
, object
, path
);
489 static void handle_all(struct rev_info
*revs
, unsigned flags
)
491 struct all_refs_cb cb
;
493 cb
.all_flags
= flags
;
494 for_each_ref(handle_one_ref
, &cb
);
497 static void handle_one_reflog_commit(unsigned char *sha1
, void *cb_data
)
499 struct all_refs_cb
*cb
= cb_data
;
500 if (!is_null_sha1(sha1
)) {
501 struct object
*o
= parse_object(sha1
);
503 o
->flags
|= cb
->all_flags
;
504 add_pending_object(cb
->all_revs
, o
, "");
506 else if (!cb
->warned_bad_reflog
) {
507 warn("reflog of '%s' references pruned commits",
508 cb
->name_for_errormsg
);
509 cb
->warned_bad_reflog
= 1;
514 static int handle_one_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
515 const char *email
, unsigned long timestamp
, int tz
,
516 const char *message
, void *cb_data
)
518 handle_one_reflog_commit(osha1
, cb_data
);
519 handle_one_reflog_commit(nsha1
, cb_data
);
523 static int handle_one_reflog(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
525 struct all_refs_cb
*cb
= cb_data
;
526 cb
->warned_bad_reflog
= 0;
527 cb
->name_for_errormsg
= path
;
528 for_each_reflog_ent(path
, handle_one_reflog_ent
, cb_data
);
532 static void handle_reflog(struct rev_info
*revs
, unsigned flags
)
534 struct all_refs_cb cb
;
536 cb
.all_flags
= flags
;
537 for_each_reflog(handle_one_reflog
, &cb
);
540 static int add_parents_only(struct rev_info
*revs
, const char *arg
, int flags
)
542 unsigned char sha1
[20];
544 struct commit
*commit
;
545 struct commit_list
*parents
;
548 flags
^= UNINTERESTING
;
551 if (get_sha1(arg
, sha1
))
554 it
= get_reference(revs
, arg
, sha1
, 0);
555 if (it
->type
!= OBJ_TAG
)
557 hashcpy(sha1
, ((struct tag
*)it
)->tagged
->sha1
);
559 if (it
->type
!= OBJ_COMMIT
)
561 commit
= (struct commit
*)it
;
562 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
563 it
= &parents
->item
->object
;
565 add_pending_object(revs
, it
, arg
);
570 void init_revisions(struct rev_info
*revs
, const char *prefix
)
572 memset(revs
, 0, sizeof(*revs
));
574 revs
->abbrev
= DEFAULT_ABBREV
;
575 revs
->ignore_merges
= 1;
576 revs
->simplify_history
= 1;
577 revs
->pruning
.recursive
= 1;
578 revs
->pruning
.add_remove
= file_add_remove
;
579 revs
->pruning
.change
= file_change
;
582 revs
->prefix
= prefix
;
585 revs
->skip_count
= -1;
586 revs
->max_count
= -1;
588 revs
->prune_fn
= NULL
;
589 revs
->prune_data
= NULL
;
591 revs
->topo_setter
= topo_sort_default_setter
;
592 revs
->topo_getter
= topo_sort_default_getter
;
594 revs
->commit_format
= CMIT_FMT_DEFAULT
;
596 diff_setup(&revs
->diffopt
);
599 static void add_pending_commit_list(struct rev_info
*revs
,
600 struct commit_list
*commit_list
,
603 while (commit_list
) {
604 struct object
*object
= &commit_list
->item
->object
;
605 object
->flags
|= flags
;
606 add_pending_object(revs
, object
, sha1_to_hex(object
->sha1
));
607 commit_list
= commit_list
->next
;
611 static void prepare_show_merge(struct rev_info
*revs
)
613 struct commit_list
*bases
;
614 struct commit
*head
, *other
;
615 unsigned char sha1
[20];
616 const char **prune
= NULL
;
617 int i
, prune_num
= 1; /* counting terminating NULL */
619 if (get_sha1("HEAD", sha1
) || !(head
= lookup_commit(sha1
)))
620 die("--merge without HEAD?");
621 if (get_sha1("MERGE_HEAD", sha1
) || !(other
= lookup_commit(sha1
)))
622 die("--merge without MERGE_HEAD?");
623 add_pending_object(revs
, &head
->object
, "HEAD");
624 add_pending_object(revs
, &other
->object
, "MERGE_HEAD");
625 bases
= get_merge_bases(head
, other
, 1);
627 struct commit
*it
= bases
->item
;
628 struct commit_list
*n
= bases
->next
;
631 it
->object
.flags
|= UNINTERESTING
;
632 add_pending_object(revs
, &it
->object
, "(merge-base)");
637 for (i
= 0; i
< active_nr
; i
++) {
638 struct cache_entry
*ce
= active_cache
[i
];
641 if (ce_path_match(ce
, revs
->prune_data
)) {
643 prune
= xrealloc(prune
, sizeof(*prune
) * prune_num
);
644 prune
[prune_num
-2] = ce
->name
;
645 prune
[prune_num
-1] = NULL
;
647 while ((i
+1 < active_nr
) &&
648 ce_same_name(ce
, active_cache
[i
+1]))
651 revs
->prune_data
= prune
;
654 int handle_revision_arg(const char *arg
, struct rev_info
*revs
,
656 int cant_be_filename
)
659 struct object
*object
;
660 unsigned char sha1
[20];
663 dotdot
= strstr(arg
, "..");
665 unsigned char from_sha1
[20];
666 const char *next
= dotdot
+ 2;
667 const char *this = arg
;
668 int symmetric
= *next
== '.';
669 unsigned int flags_exclude
= flags
^ UNINTERESTING
;
678 if (!get_sha1(this, from_sha1
) &&
679 !get_sha1(next
, sha1
)) {
680 struct commit
*a
, *b
;
681 struct commit_list
*exclude
;
683 a
= lookup_commit_reference(from_sha1
);
684 b
= lookup_commit_reference(sha1
);
687 "Invalid symmetric difference expression %s...%s" :
688 "Invalid revision range %s..%s",
692 if (!cant_be_filename
) {
694 verify_non_filename(revs
->prefix
, arg
);
698 exclude
= get_merge_bases(a
, b
, 1);
699 add_pending_commit_list(revs
, exclude
,
701 free_commit_list(exclude
);
702 a
->object
.flags
|= flags
| SYMMETRIC_LEFT
;
704 a
->object
.flags
|= flags_exclude
;
705 b
->object
.flags
|= flags
;
706 add_pending_object(revs
, &a
->object
, this);
707 add_pending_object(revs
, &b
->object
, next
);
712 dotdot
= strstr(arg
, "^@");
713 if (dotdot
&& !dotdot
[2]) {
715 if (add_parents_only(revs
, arg
, flags
))
719 dotdot
= strstr(arg
, "^!");
720 if (dotdot
&& !dotdot
[2]) {
722 if (!add_parents_only(revs
, arg
, flags
^ UNINTERESTING
))
728 local_flags
= UNINTERESTING
;
731 if (get_sha1(arg
, sha1
))
733 if (!cant_be_filename
)
734 verify_non_filename(revs
->prefix
, arg
);
735 object
= get_reference(revs
, arg
, sha1
, flags
^ local_flags
);
736 add_pending_object(revs
, object
, arg
);
740 static void add_grep(struct rev_info
*revs
, const char *ptn
, enum grep_pat_token what
)
742 if (!revs
->grep_filter
) {
743 struct grep_opt
*opt
= xcalloc(1, sizeof(*opt
));
744 opt
->status_only
= 1;
745 opt
->pattern_tail
= &(opt
->pattern_list
);
746 opt
->regflags
= REG_NEWLINE
;
747 revs
->grep_filter
= opt
;
749 append_grep_pattern(revs
->grep_filter
, ptn
,
750 "command line", 0, what
);
753 static void add_header_grep(struct rev_info
*revs
, const char *field
, const char *pattern
)
759 fldlen
= strlen(field
);
760 patlen
= strlen(pattern
);
761 pat
= xmalloc(patlen
+ fldlen
+ 10);
763 if (*pattern
== '^') {
767 sprintf(pat
, "^%s %s%s", field
, prefix
, pattern
);
768 add_grep(revs
, pat
, GREP_PATTERN_HEAD
);
771 static void add_message_grep(struct rev_info
*revs
, const char *pattern
)
773 add_grep(revs
, pattern
, GREP_PATTERN_BODY
);
776 static void add_ignore_packed(struct rev_info
*revs
, const char *name
)
778 int num
= ++revs
->num_ignore_packed
;
780 revs
->ignore_packed
= xrealloc(revs
->ignore_packed
,
781 sizeof(const char **) * (num
+ 1));
782 revs
->ignore_packed
[num
-1] = name
;
783 revs
->ignore_packed
[num
] = NULL
;
787 * Parse revision information, filling in the "rev_info" structure,
788 * and removing the used arguments from the argument list.
790 * Returns the number of arguments left that weren't recognized
791 * (which are also moved to the head of the argument list)
793 int setup_revisions(int argc
, const char **argv
, struct rev_info
*revs
, const char *def
)
795 int i
, flags
, seen_dashdash
, show_merge
;
796 const char **unrecognized
= argv
+ 1;
800 /* First, search for "--" */
802 for (i
= 1; i
< argc
; i
++) {
803 const char *arg
= argv
[i
];
804 if (strcmp(arg
, "--"))
808 revs
->prune_data
= get_pathspec(revs
->prefix
, argv
+ i
+ 1);
813 flags
= show_merge
= 0;
814 for (i
= 1; i
< argc
; i
++) {
815 const char *arg
= argv
[i
];
818 if (!prefixcmp(arg
, "--max-count=")) {
819 revs
->max_count
= atoi(arg
+ 12);
822 if (!prefixcmp(arg
, "--skip=")) {
823 revs
->skip_count
= atoi(arg
+ 7);
826 /* accept -<digit>, like traditional "head" */
827 if ((*arg
== '-') && isdigit(arg
[1])) {
828 revs
->max_count
= atoi(arg
+ 1);
831 if (!strcmp(arg
, "-n")) {
833 die("-n requires an argument");
834 revs
->max_count
= atoi(argv
[++i
]);
837 if (!prefixcmp(arg
, "-n")) {
838 revs
->max_count
= atoi(arg
+ 2);
841 if (!prefixcmp(arg
, "--max-age=")) {
842 revs
->max_age
= atoi(arg
+ 10);
845 if (!prefixcmp(arg
, "--since=")) {
846 revs
->max_age
= approxidate(arg
+ 8);
849 if (!prefixcmp(arg
, "--after=")) {
850 revs
->max_age
= approxidate(arg
+ 8);
853 if (!prefixcmp(arg
, "--min-age=")) {
854 revs
->min_age
= atoi(arg
+ 10);
857 if (!prefixcmp(arg
, "--before=")) {
858 revs
->min_age
= approxidate(arg
+ 9);
861 if (!prefixcmp(arg
, "--until=")) {
862 revs
->min_age
= approxidate(arg
+ 8);
865 if (!strcmp(arg
, "--all")) {
866 handle_all(revs
, flags
);
869 if (!strcmp(arg
, "--reflog")) {
870 handle_reflog(revs
, flags
);
873 if (!strcmp(arg
, "-g") ||
874 !strcmp(arg
, "--walk-reflogs")) {
875 init_reflog_walk(&revs
->reflog_info
);
878 if (!strcmp(arg
, "--not")) {
879 flags
^= UNINTERESTING
;
882 if (!strcmp(arg
, "--default")) {
884 die("bad --default argument");
888 if (!strcmp(arg
, "--merge")) {
892 if (!strcmp(arg
, "--topo-order")) {
893 revs
->topo_order
= 1;
896 if (!strcmp(arg
, "--date-order")) {
898 revs
->topo_order
= 1;
901 if (!strcmp(arg
, "--parents")) {
905 if (!strcmp(arg
, "--dense")) {
909 if (!strcmp(arg
, "--sparse")) {
913 if (!strcmp(arg
, "--remove-empty")) {
914 revs
->remove_empty_trees
= 1;
917 if (!strcmp(arg
, "--no-merges")) {
921 if (!strcmp(arg
, "--boundary")) {
925 if (!strcmp(arg
, "--left-right")) {
926 revs
->left_right
= 1;
929 if (!strcmp(arg
, "--objects")) {
930 revs
->tag_objects
= 1;
931 revs
->tree_objects
= 1;
932 revs
->blob_objects
= 1;
935 if (!strcmp(arg
, "--objects-edge")) {
936 revs
->tag_objects
= 1;
937 revs
->tree_objects
= 1;
938 revs
->blob_objects
= 1;
942 if (!strcmp(arg
, "--unpacked")) {
944 free(revs
->ignore_packed
);
945 revs
->ignore_packed
= NULL
;
946 revs
->num_ignore_packed
= 0;
949 if (!prefixcmp(arg
, "--unpacked=")) {
951 add_ignore_packed(revs
, arg
+11);
954 if (!strcmp(arg
, "-r")) {
956 revs
->diffopt
.recursive
= 1;
959 if (!strcmp(arg
, "-t")) {
961 revs
->diffopt
.recursive
= 1;
962 revs
->diffopt
.tree_in_recursive
= 1;
965 if (!strcmp(arg
, "-m")) {
966 revs
->ignore_merges
= 0;
969 if (!strcmp(arg
, "-c")) {
971 revs
->dense_combined_merges
= 0;
972 revs
->combine_merges
= 1;
975 if (!strcmp(arg
, "--cc")) {
977 revs
->dense_combined_merges
= 1;
978 revs
->combine_merges
= 1;
981 if (!strcmp(arg
, "-v")) {
982 revs
->verbose_header
= 1;
985 if (!prefixcmp(arg
, "--pretty")) {
986 revs
->verbose_header
= 1;
987 revs
->commit_format
= get_commit_format(arg
+8);
990 if (!strcmp(arg
, "--root")) {
991 revs
->show_root_diff
= 1;
994 if (!strcmp(arg
, "--no-commit-id")) {
995 revs
->no_commit_id
= 1;
998 if (!strcmp(arg
, "--always")) {
999 revs
->always_show_header
= 1;
1002 if (!strcmp(arg
, "--no-abbrev")) {
1006 if (!strcmp(arg
, "--abbrev")) {
1007 revs
->abbrev
= DEFAULT_ABBREV
;
1010 if (!prefixcmp(arg
, "--abbrev=")) {
1011 revs
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
1012 if (revs
->abbrev
< MINIMUM_ABBREV
)
1013 revs
->abbrev
= MINIMUM_ABBREV
;
1014 else if (revs
->abbrev
> 40)
1018 if (!strcmp(arg
, "--abbrev-commit")) {
1019 revs
->abbrev_commit
= 1;
1022 if (!strcmp(arg
, "--full-diff")) {
1024 revs
->full_diff
= 1;
1027 if (!strcmp(arg
, "--full-history")) {
1028 revs
->simplify_history
= 0;
1031 if (!strcmp(arg
, "--relative-date")) {
1032 revs
->relative_date
= 1;
1037 * Grepping the commit log
1039 if (!prefixcmp(arg
, "--author=")) {
1040 add_header_grep(revs
, "author", arg
+9);
1043 if (!prefixcmp(arg
, "--committer=")) {
1044 add_header_grep(revs
, "committer", arg
+12);
1047 if (!prefixcmp(arg
, "--grep=")) {
1048 add_message_grep(revs
, arg
+7);
1051 if (!strcmp(arg
, "--all-match")) {
1055 if (!prefixcmp(arg
, "--encoding=")) {
1057 if (strcmp(arg
, "none"))
1058 git_log_output_encoding
= strdup(arg
);
1060 git_log_output_encoding
= "";
1063 if (!strcmp(arg
, "--reverse")) {
1068 opts
= diff_opt_parse(&revs
->diffopt
, argv
+i
, argc
-i
);
1074 *unrecognized
++ = arg
;
1079 if (handle_revision_arg(arg
, revs
, flags
, seen_dashdash
)) {
1081 if (seen_dashdash
|| *arg
== '^')
1082 die("bad revision '%s'", arg
);
1084 /* If we didn't have a "--":
1085 * (1) all filenames must exist;
1086 * (2) all rev-args must not be interpretable
1087 * as a valid filename.
1088 * but the latter we have checked in the main loop.
1090 for (j
= i
; j
< argc
; j
++)
1091 verify_filename(revs
->prefix
, argv
[j
]);
1093 revs
->prune_data
= get_pathspec(revs
->prefix
,
1100 prepare_show_merge(revs
);
1101 if (def
&& !revs
->pending
.nr
) {
1102 unsigned char sha1
[20];
1103 struct object
*object
;
1104 if (get_sha1(def
, sha1
))
1105 die("bad default revision '%s'", def
);
1106 object
= get_reference(revs
, def
, sha1
, 0);
1107 add_pending_object(revs
, object
, def
);
1110 if (revs
->topo_order
)
1113 if (revs
->prune_data
) {
1114 diff_tree_setup_paths(revs
->prune_data
, &revs
->pruning
);
1115 revs
->prune_fn
= try_to_simplify_commit
;
1116 if (!revs
->full_diff
)
1117 diff_tree_setup_paths(revs
->prune_data
, &revs
->diffopt
);
1119 if (revs
->combine_merges
) {
1120 revs
->ignore_merges
= 0;
1121 if (revs
->dense_combined_merges
&& !revs
->diffopt
.output_format
)
1122 revs
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
1124 revs
->diffopt
.abbrev
= revs
->abbrev
;
1125 if (diff_setup_done(&revs
->diffopt
) < 0)
1126 die("diff_setup_done failed");
1128 if (revs
->grep_filter
) {
1129 revs
->grep_filter
->all_match
= all_match
;
1130 compile_grep_patterns(revs
->grep_filter
);
1136 void prepare_revision_walk(struct rev_info
*revs
)
1138 int nr
= revs
->pending
.nr
;
1139 struct object_array_entry
*e
, *list
;
1141 e
= list
= revs
->pending
.objects
;
1142 revs
->pending
.nr
= 0;
1143 revs
->pending
.alloc
= 0;
1144 revs
->pending
.objects
= NULL
;
1146 struct commit
*commit
= handle_commit(revs
, e
->item
, e
->name
);
1148 if (!(commit
->object
.flags
& SEEN
)) {
1149 commit
->object
.flags
|= SEEN
;
1150 insert_by_date(commit
, &revs
->commits
);
1161 if (revs
->topo_order
)
1162 sort_in_topological_order_fn(&revs
->commits
, revs
->lifo
,
1167 static int rewrite_one(struct rev_info
*revs
, struct commit
**pp
)
1170 struct commit
*p
= *pp
;
1172 add_parents_to_list(revs
, p
, &revs
->commits
);
1173 if (p
->parents
&& p
->parents
->next
)
1175 if (p
->object
.flags
& (TREECHANGE
| UNINTERESTING
))
1179 *pp
= p
->parents
->item
;
1183 static void rewrite_parents(struct rev_info
*revs
, struct commit
*commit
)
1185 struct commit_list
**pp
= &commit
->parents
;
1187 struct commit_list
*parent
= *pp
;
1188 if (rewrite_one(revs
, &parent
->item
) < 0) {
1196 static void mark_boundary_to_show(struct commit
*commit
)
1198 struct commit_list
*p
= commit
->parents
;
1202 if (commit
->object
.flags
& BOUNDARY
)
1203 commit
->object
.flags
|= BOUNDARY_SHOW
;
1207 static int commit_match(struct commit
*commit
, struct rev_info
*opt
)
1209 if (!opt
->grep_filter
)
1211 return grep_buffer(opt
->grep_filter
,
1212 NULL
, /* we say nothing, not even filename */
1213 commit
->buffer
, strlen(commit
->buffer
));
1216 static struct commit
*get_revision_1(struct rev_info
*revs
)
1222 struct commit_list
*entry
= revs
->commits
;
1223 struct commit
*commit
= entry
->item
;
1225 revs
->commits
= entry
->next
;
1228 if (revs
->reflog_info
)
1229 fake_reflog_parent(revs
->reflog_info
, commit
);
1232 * If we haven't done the list limiting, we need to look at
1233 * the parents here. We also need to do the date-based limiting
1234 * that we'd otherwise have done in limit_list().
1236 if (!revs
->limited
) {
1237 if (revs
->max_age
!= -1 &&
1238 (commit
->date
< revs
->max_age
)) {
1240 commit
->object
.flags
|=
1241 BOUNDARY_SHOW
| BOUNDARY
;
1245 add_parents_to_list(revs
, commit
,
1248 if (commit
->object
.flags
& SHOWN
)
1251 if (revs
->unpacked
&& has_sha1_pack(commit
->object
.sha1
,
1252 revs
->ignore_packed
))
1255 /* We want to show boundary commits only when their
1256 * children are shown. When path-limiter is in effect,
1257 * rewrite_parents() drops some commits from getting shown,
1258 * and there is no point showing boundary parents that
1259 * are not shown. After rewrite_parents() rewrites the
1260 * parents of a commit that is shown, we mark the boundary
1261 * parents with BOUNDARY_SHOW.
1263 if (commit
->object
.flags
& BOUNDARY_SHOW
) {
1264 commit
->object
.flags
|= SHOWN
;
1267 if (commit
->object
.flags
& UNINTERESTING
)
1269 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
1271 if (revs
->no_merges
&&
1272 commit
->parents
&& commit
->parents
->next
)
1274 if (!commit_match(commit
, revs
))
1276 if (revs
->prune_fn
&& revs
->dense
) {
1277 /* Commit without changes? */
1278 if (!(commit
->object
.flags
& TREECHANGE
)) {
1279 /* drop merges unless we want parenthood */
1282 /* non-merge - always ignore it */
1283 if (!commit
->parents
|| !commit
->parents
->next
)
1287 rewrite_parents(revs
, commit
);
1290 mark_boundary_to_show(commit
);
1291 commit
->object
.flags
|= SHOWN
;
1293 } while (revs
->commits
);
1297 struct commit
*get_revision(struct rev_info
*revs
)
1299 struct commit
*c
= NULL
;
1301 if (revs
->reverse
) {
1302 struct commit_list
*list
;
1305 * rev_info.reverse is used to note the fact that we
1306 * want to output the list of revisions in reverse
1307 * order. To accomplish this goal, reverse can have
1311 * 1 reverse the list
1312 * 2 internal use: we have already obtained and
1313 * reversed the list, now we only need to yield
1317 if (revs
->reverse
== 1) {
1320 while ((c
= get_revision(revs
)))
1321 commit_list_insert(c
, &list
);
1322 revs
->commits
= list
;
1328 c
= revs
->commits
->item
;
1329 list
= revs
->commits
->next
;
1330 free(revs
->commits
);
1331 revs
->commits
= list
;
1335 if (0 < revs
->skip_count
) {
1336 while ((c
= get_revision_1(revs
)) != NULL
) {
1337 if (revs
->skip_count
-- <= 0)
1342 /* Check the max_count ... */
1343 switch (revs
->max_count
) {
1347 if (revs
->boundary
) {
1348 struct commit_list
*list
= revs
->commits
;
1350 list
->item
->object
.flags
|=
1351 BOUNDARY_SHOW
| BOUNDARY
;
1354 /* all remaining commits are boundary commits */
1355 revs
->max_count
= -1;
1364 return get_revision_1(revs
);