10 #include "reflog-walk.h"
11 #include "patch-ids.h"
13 static char *path_name(struct name_path
*path
, const char *name
)
17 int nlen
= strlen(name
);
20 for (p
= path
; p
; p
= p
->up
) {
22 len
+= p
->elem_len
+ 1;
25 m
= n
+ len
- (nlen
+ 1);
27 for (p
= path
; p
; p
= p
->up
) {
30 memcpy(m
, p
->elem
, p
->elem_len
);
37 void add_object(struct object
*obj
,
38 struct object_array
*p
,
39 struct name_path
*path
,
42 add_object_array(obj
, path_name(path
, name
), p
);
45 static void mark_blob_uninteresting(struct blob
*blob
)
47 if (blob
->object
.flags
& UNINTERESTING
)
49 blob
->object
.flags
|= UNINTERESTING
;
52 void mark_tree_uninteresting(struct tree
*tree
)
54 struct tree_desc desc
;
55 struct name_entry entry
;
56 struct object
*obj
= &tree
->object
;
58 if (obj
->flags
& UNINTERESTING
)
60 obj
->flags
|= UNINTERESTING
;
61 if (!has_sha1_file(obj
->sha1
))
63 if (parse_tree(tree
) < 0)
64 die("bad tree %s", sha1_to_hex(obj
->sha1
));
66 init_tree_desc(&desc
, tree
->buffer
, 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
)
217 * The goal is to get REV_TREE_NEW as the result only if the
218 * diff consists of all '+' (and no other changes), and
219 * REV_TREE_DIFFERENT otherwise (of course if the trees are
220 * the same we want REV_TREE_SAME). That means that once we
221 * get to REV_TREE_DIFFERENT, we do not have to look any further.
223 static int tree_difference
= REV_TREE_SAME
;
225 static void file_add_remove(struct diff_options
*options
,
226 int addremove
, unsigned mode
,
227 const unsigned char *sha1
,
228 const char *base
, const char *path
)
230 int diff
= REV_TREE_DIFFERENT
;
233 * Is it an add of a new file? It means that the old tree
234 * didn't have it at all, so we will turn "REV_TREE_SAME" ->
235 * "REV_TREE_NEW", but leave any "REV_TREE_DIFFERENT" alone
236 * (and if it already was "REV_TREE_NEW", we'll keep it
237 * "REV_TREE_NEW" of course).
239 if (addremove
== '+') {
240 diff
= tree_difference
;
241 if (diff
!= REV_TREE_SAME
)
245 tree_difference
= diff
;
246 if (tree_difference
== REV_TREE_DIFFERENT
)
247 options
->has_changes
= 1;
250 static void file_change(struct diff_options
*options
,
251 unsigned old_mode
, unsigned new_mode
,
252 const unsigned char *old_sha1
,
253 const unsigned char *new_sha1
,
254 const char *base
, const char *path
)
256 tree_difference
= REV_TREE_DIFFERENT
;
257 options
->has_changes
= 1;
260 int rev_compare_tree(struct rev_info
*revs
, struct tree
*t1
, struct tree
*t2
)
265 return REV_TREE_DIFFERENT
;
266 tree_difference
= REV_TREE_SAME
;
267 revs
->pruning
.has_changes
= 0;
268 if (diff_tree_sha1(t1
->object
.sha1
, t2
->object
.sha1
, "",
270 return REV_TREE_DIFFERENT
;
271 return tree_difference
;
274 int rev_same_tree_as_empty(struct rev_info
*revs
, struct tree
*t1
)
279 struct tree_desc empty
, real
;
284 tree
= read_object_with_reference(t1
->object
.sha1
, tree_type
, &size
, NULL
);
287 init_tree_desc(&real
, tree
, size
);
288 init_tree_desc(&empty
, "", 0);
290 tree_difference
= REV_TREE_SAME
;
291 revs
->pruning
.has_changes
= 0;
292 retval
= diff_tree(&empty
, &real
, "", &revs
->pruning
);
295 return retval
>= 0 && (tree_difference
== REV_TREE_SAME
);
298 static void try_to_simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
300 struct commit_list
**pp
, *parent
;
301 int tree_changed
= 0, tree_same
= 0;
306 if (!commit
->parents
) {
307 if (!rev_same_tree_as_empty(revs
, commit
->tree
))
308 commit
->object
.flags
|= TREECHANGE
;
312 pp
= &commit
->parents
;
313 while ((parent
= *pp
) != NULL
) {
314 struct commit
*p
= parent
->item
;
317 switch (rev_compare_tree(revs
, p
->tree
, commit
->tree
)) {
320 if (!revs
->simplify_history
|| (p
->object
.flags
& UNINTERESTING
)) {
321 /* Even if a merge with an uninteresting
322 * side branch brought the entire change
323 * we are interested in, we do not want
324 * to lose the other branches of this
325 * merge, so we just keep going.
331 commit
->parents
= parent
;
335 if (revs
->remove_empty_trees
&&
336 rev_same_tree_as_empty(revs
, p
->tree
)) {
337 /* We are adding all the specified
338 * paths from this parent, so the
339 * history beyond this parent is not
340 * interesting. Remove its parents
341 * (they are grandparents for us).
342 * IOW, we pretend this parent is a
349 case REV_TREE_DIFFERENT
:
354 die("bad tree compare for commit %s", sha1_to_hex(commit
->object
.sha1
));
356 if (tree_changed
&& !tree_same
)
357 commit
->object
.flags
|= TREECHANGE
;
360 static void add_parents_to_list(struct rev_info
*revs
, struct commit
*commit
, struct commit_list
**list
)
362 struct commit_list
*parent
= commit
->parents
;
366 if (commit
->object
.flags
& ADDED
)
368 commit
->object
.flags
|= ADDED
;
371 * If the commit is uninteresting, don't try to
372 * prune parents - we want the maximal uninteresting
375 * Normally we haven't parsed the parent
376 * yet, so we won't have a parent of a parent
377 * here. However, it may turn out that we've
378 * reached this commit some other way (where it
379 * wasn't uninteresting), in which case we need
380 * to mark its parents recursively too..
382 if (commit
->object
.flags
& UNINTERESTING
) {
384 struct commit
*p
= parent
->item
;
385 parent
= parent
->next
;
387 p
->object
.flags
|= UNINTERESTING
;
389 mark_parents_uninteresting(p
);
390 if (p
->object
.flags
& SEEN
)
392 p
->object
.flags
|= SEEN
;
393 insert_by_date(p
, list
);
399 * Ok, the commit wasn't uninteresting. Try to
400 * simplify the commit history and find the parent
401 * that has no differences in the path set if one exists.
404 revs
->prune_fn(revs
, commit
);
409 left_flag
= (commit
->object
.flags
& SYMMETRIC_LEFT
);
411 rest
= !revs
->first_parent_only
;
412 for (parent
= commit
->parents
, add
= 1; parent
; add
= rest
) {
413 struct commit
*p
= parent
->item
;
415 parent
= parent
->next
;
417 p
->object
.flags
|= left_flag
;
418 if (p
->object
.flags
& SEEN
)
420 p
->object
.flags
|= SEEN
;
422 insert_by_date(p
, list
);
426 static void cherry_pick_list(struct commit_list
*list
)
428 struct commit_list
*p
;
429 int left_count
= 0, right_count
= 0;
431 struct patch_ids ids
;
433 /* First count the commits on the left and on the right */
434 for (p
= list
; p
; p
= p
->next
) {
435 struct commit
*commit
= p
->item
;
436 unsigned flags
= commit
->object
.flags
;
437 if (flags
& BOUNDARY
)
439 else if (flags
& SYMMETRIC_LEFT
)
445 left_first
= left_count
< right_count
;
446 init_patch_ids(&ids
);
448 /* Compute patch-ids for one side */
449 for (p
= list
; p
; p
= p
->next
) {
450 struct commit
*commit
= p
->item
;
451 unsigned flags
= commit
->object
.flags
;
453 if (flags
& BOUNDARY
)
456 * If we have fewer left, left_first is set and we omit
457 * commits on the right branch in this loop. If we have
458 * fewer right, we skip the left ones.
460 if (left_first
!= !!(flags
& SYMMETRIC_LEFT
))
462 commit
->util
= add_commit_patch_id(commit
, &ids
);
465 /* Check the other side */
466 for (p
= list
; p
; p
= p
->next
) {
467 struct commit
*commit
= p
->item
;
469 unsigned flags
= commit
->object
.flags
;
471 if (flags
& BOUNDARY
)
474 * If we have fewer left, left_first is set and we omit
475 * commits on the left branch in this loop.
477 if (left_first
== !!(flags
& SYMMETRIC_LEFT
))
481 * Have we seen the same patch id?
483 id
= has_commit_patch_id(commit
, &ids
);
487 commit
->object
.flags
|= SHOWN
;
490 /* Now check the original side for seen ones */
491 for (p
= list
; p
; p
= p
->next
) {
492 struct commit
*commit
= p
->item
;
493 struct patch_id
*ent
;
499 commit
->object
.flags
|= SHOWN
;
503 free_patch_ids(&ids
);
506 static void limit_list(struct rev_info
*revs
)
508 struct commit_list
*list
= revs
->commits
;
509 struct commit_list
*newlist
= NULL
;
510 struct commit_list
**p
= &newlist
;
513 struct commit_list
*entry
= list
;
514 struct commit
*commit
= list
->item
;
515 struct object
*obj
= &commit
->object
;
520 if (revs
->max_age
!= -1 && (commit
->date
< revs
->max_age
))
521 obj
->flags
|= UNINTERESTING
;
522 add_parents_to_list(revs
, commit
, &list
);
523 if (obj
->flags
& UNINTERESTING
) {
524 mark_parents_uninteresting(commit
);
525 if (everybody_uninteresting(list
))
529 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
531 p
= &commit_list_insert(commit
, p
)->next
;
533 if (revs
->cherry_pick
)
534 cherry_pick_list(newlist
);
536 revs
->commits
= newlist
;
541 int warned_bad_reflog
;
542 struct rev_info
*all_revs
;
543 const char *name_for_errormsg
;
546 static int handle_one_ref(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
548 struct all_refs_cb
*cb
= cb_data
;
549 struct object
*object
= get_reference(cb
->all_revs
, path
, sha1
,
551 add_pending_object(cb
->all_revs
, object
, path
);
555 static void handle_all(struct rev_info
*revs
, unsigned flags
)
557 struct all_refs_cb cb
;
559 cb
.all_flags
= flags
;
560 for_each_ref(handle_one_ref
, &cb
);
563 static void handle_one_reflog_commit(unsigned char *sha1
, void *cb_data
)
565 struct all_refs_cb
*cb
= cb_data
;
566 if (!is_null_sha1(sha1
)) {
567 struct object
*o
= parse_object(sha1
);
569 o
->flags
|= cb
->all_flags
;
570 add_pending_object(cb
->all_revs
, o
, "");
572 else if (!cb
->warned_bad_reflog
) {
573 warning("reflog of '%s' references pruned commits",
574 cb
->name_for_errormsg
);
575 cb
->warned_bad_reflog
= 1;
580 static int handle_one_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
581 const char *email
, unsigned long timestamp
, int tz
,
582 const char *message
, void *cb_data
)
584 handle_one_reflog_commit(osha1
, cb_data
);
585 handle_one_reflog_commit(nsha1
, cb_data
);
589 static int handle_one_reflog(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
591 struct all_refs_cb
*cb
= cb_data
;
592 cb
->warned_bad_reflog
= 0;
593 cb
->name_for_errormsg
= path
;
594 for_each_reflog_ent(path
, handle_one_reflog_ent
, cb_data
);
598 static void handle_reflog(struct rev_info
*revs
, unsigned flags
)
600 struct all_refs_cb cb
;
602 cb
.all_flags
= flags
;
603 for_each_reflog(handle_one_reflog
, &cb
);
606 static int add_parents_only(struct rev_info
*revs
, const char *arg
, int flags
)
608 unsigned char sha1
[20];
610 struct commit
*commit
;
611 struct commit_list
*parents
;
614 flags
^= UNINTERESTING
;
617 if (get_sha1(arg
, sha1
))
620 it
= get_reference(revs
, arg
, sha1
, 0);
621 if (it
->type
!= OBJ_TAG
)
623 hashcpy(sha1
, ((struct tag
*)it
)->tagged
->sha1
);
625 if (it
->type
!= OBJ_COMMIT
)
627 commit
= (struct commit
*)it
;
628 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
629 it
= &parents
->item
->object
;
631 add_pending_object(revs
, it
, arg
);
636 void init_revisions(struct rev_info
*revs
, const char *prefix
)
638 memset(revs
, 0, sizeof(*revs
));
640 revs
->abbrev
= DEFAULT_ABBREV
;
641 revs
->ignore_merges
= 1;
642 revs
->simplify_history
= 1;
643 revs
->pruning
.recursive
= 1;
644 revs
->pruning
.quiet
= 1;
645 revs
->pruning
.add_remove
= file_add_remove
;
646 revs
->pruning
.change
= file_change
;
649 revs
->prefix
= prefix
;
652 revs
->skip_count
= -1;
653 revs
->max_count
= -1;
654 revs
->subject_prefix
= "PATCH";
656 revs
->prune_fn
= NULL
;
657 revs
->prune_data
= NULL
;
659 revs
->topo_setter
= topo_sort_default_setter
;
660 revs
->topo_getter
= topo_sort_default_getter
;
662 revs
->commit_format
= CMIT_FMT_DEFAULT
;
664 diff_setup(&revs
->diffopt
);
667 static void add_pending_commit_list(struct rev_info
*revs
,
668 struct commit_list
*commit_list
,
671 while (commit_list
) {
672 struct object
*object
= &commit_list
->item
->object
;
673 object
->flags
|= flags
;
674 add_pending_object(revs
, object
, sha1_to_hex(object
->sha1
));
675 commit_list
= commit_list
->next
;
679 static void prepare_show_merge(struct rev_info
*revs
)
681 struct commit_list
*bases
;
682 struct commit
*head
, *other
;
683 unsigned char sha1
[20];
684 const char **prune
= NULL
;
685 int i
, prune_num
= 1; /* counting terminating NULL */
687 if (get_sha1("HEAD", sha1
) || !(head
= lookup_commit(sha1
)))
688 die("--merge without HEAD?");
689 if (get_sha1("MERGE_HEAD", sha1
) || !(other
= lookup_commit(sha1
)))
690 die("--merge without MERGE_HEAD?");
691 add_pending_object(revs
, &head
->object
, "HEAD");
692 add_pending_object(revs
, &other
->object
, "MERGE_HEAD");
693 bases
= get_merge_bases(head
, other
, 1);
695 struct commit
*it
= bases
->item
;
696 struct commit_list
*n
= bases
->next
;
699 it
->object
.flags
|= UNINTERESTING
;
700 add_pending_object(revs
, &it
->object
, "(merge-base)");
705 for (i
= 0; i
< active_nr
; i
++) {
706 struct cache_entry
*ce
= active_cache
[i
];
709 if (ce_path_match(ce
, revs
->prune_data
)) {
711 prune
= xrealloc(prune
, sizeof(*prune
) * prune_num
);
712 prune
[prune_num
-2] = ce
->name
;
713 prune
[prune_num
-1] = NULL
;
715 while ((i
+1 < active_nr
) &&
716 ce_same_name(ce
, active_cache
[i
+1]))
719 revs
->prune_data
= prune
;
722 int handle_revision_arg(const char *arg
, struct rev_info
*revs
,
724 int cant_be_filename
)
727 struct object
*object
;
728 unsigned char sha1
[20];
731 dotdot
= strstr(arg
, "..");
733 unsigned char from_sha1
[20];
734 const char *next
= dotdot
+ 2;
735 const char *this = arg
;
736 int symmetric
= *next
== '.';
737 unsigned int flags_exclude
= flags
^ UNINTERESTING
;
746 if (!get_sha1(this, from_sha1
) &&
747 !get_sha1(next
, sha1
)) {
748 struct commit
*a
, *b
;
749 struct commit_list
*exclude
;
751 a
= lookup_commit_reference(from_sha1
);
752 b
= lookup_commit_reference(sha1
);
755 "Invalid symmetric difference expression %s...%s" :
756 "Invalid revision range %s..%s",
760 if (!cant_be_filename
) {
762 verify_non_filename(revs
->prefix
, arg
);
766 exclude
= get_merge_bases(a
, b
, 1);
767 add_pending_commit_list(revs
, exclude
,
769 free_commit_list(exclude
);
770 a
->object
.flags
|= flags
| SYMMETRIC_LEFT
;
772 a
->object
.flags
|= flags_exclude
;
773 b
->object
.flags
|= flags
;
774 add_pending_object(revs
, &a
->object
, this);
775 add_pending_object(revs
, &b
->object
, next
);
780 dotdot
= strstr(arg
, "^@");
781 if (dotdot
&& !dotdot
[2]) {
783 if (add_parents_only(revs
, arg
, flags
))
787 dotdot
= strstr(arg
, "^!");
788 if (dotdot
&& !dotdot
[2]) {
790 if (!add_parents_only(revs
, arg
, flags
^ UNINTERESTING
))
796 local_flags
= UNINTERESTING
;
799 if (get_sha1(arg
, sha1
))
801 if (!cant_be_filename
)
802 verify_non_filename(revs
->prefix
, arg
);
803 object
= get_reference(revs
, arg
, sha1
, flags
^ local_flags
);
804 add_pending_object(revs
, object
, arg
);
808 static void add_grep(struct rev_info
*revs
, const char *ptn
, enum grep_pat_token what
)
810 if (!revs
->grep_filter
) {
811 struct grep_opt
*opt
= xcalloc(1, sizeof(*opt
));
812 opt
->status_only
= 1;
813 opt
->pattern_tail
= &(opt
->pattern_list
);
814 opt
->regflags
= REG_NEWLINE
;
815 revs
->grep_filter
= opt
;
817 append_grep_pattern(revs
->grep_filter
, ptn
,
818 "command line", 0, what
);
821 static void add_header_grep(struct rev_info
*revs
, const char *field
, const char *pattern
)
827 fldlen
= strlen(field
);
828 patlen
= strlen(pattern
);
829 pat
= xmalloc(patlen
+ fldlen
+ 10);
831 if (*pattern
== '^') {
835 sprintf(pat
, "^%s %s%s", field
, prefix
, pattern
);
836 add_grep(revs
, pat
, GREP_PATTERN_HEAD
);
839 static void add_message_grep(struct rev_info
*revs
, const char *pattern
)
841 add_grep(revs
, pattern
, GREP_PATTERN_BODY
);
844 static void add_ignore_packed(struct rev_info
*revs
, const char *name
)
846 int num
= ++revs
->num_ignore_packed
;
848 revs
->ignore_packed
= xrealloc(revs
->ignore_packed
,
849 sizeof(const char **) * (num
+ 1));
850 revs
->ignore_packed
[num
-1] = name
;
851 revs
->ignore_packed
[num
] = NULL
;
855 * Parse revision information, filling in the "rev_info" structure,
856 * and removing the used arguments from the argument list.
858 * Returns the number of arguments left that weren't recognized
859 * (which are also moved to the head of the argument list)
861 int setup_revisions(int argc
, const char **argv
, struct rev_info
*revs
, const char *def
)
863 int i
, flags
, seen_dashdash
, show_merge
;
864 const char **unrecognized
= argv
+ 1;
868 /* First, search for "--" */
870 for (i
= 1; i
< argc
; i
++) {
871 const char *arg
= argv
[i
];
872 if (strcmp(arg
, "--"))
876 revs
->prune_data
= get_pathspec(revs
->prefix
, argv
+ i
+ 1);
881 flags
= show_merge
= 0;
882 for (i
= 1; i
< argc
; i
++) {
883 const char *arg
= argv
[i
];
886 if (!prefixcmp(arg
, "--max-count=")) {
887 revs
->max_count
= atoi(arg
+ 12);
890 if (!prefixcmp(arg
, "--skip=")) {
891 revs
->skip_count
= atoi(arg
+ 7);
894 /* accept -<digit>, like traditional "head" */
895 if ((*arg
== '-') && isdigit(arg
[1])) {
896 revs
->max_count
= atoi(arg
+ 1);
899 if (!strcmp(arg
, "-n")) {
901 die("-n requires an argument");
902 revs
->max_count
= atoi(argv
[++i
]);
905 if (!prefixcmp(arg
, "-n")) {
906 revs
->max_count
= atoi(arg
+ 2);
909 if (!prefixcmp(arg
, "--max-age=")) {
910 revs
->max_age
= atoi(arg
+ 10);
913 if (!prefixcmp(arg
, "--since=")) {
914 revs
->max_age
= approxidate(arg
+ 8);
917 if (!prefixcmp(arg
, "--after=")) {
918 revs
->max_age
= approxidate(arg
+ 8);
921 if (!prefixcmp(arg
, "--min-age=")) {
922 revs
->min_age
= atoi(arg
+ 10);
925 if (!prefixcmp(arg
, "--before=")) {
926 revs
->min_age
= approxidate(arg
+ 9);
929 if (!prefixcmp(arg
, "--until=")) {
930 revs
->min_age
= approxidate(arg
+ 8);
933 if (!strcmp(arg
, "--all")) {
934 handle_all(revs
, flags
);
937 if (!strcmp(arg
, "--first-parent")) {
938 revs
->first_parent_only
= 1;
941 if (!strcmp(arg
, "--reflog")) {
942 handle_reflog(revs
, flags
);
945 if (!strcmp(arg
, "-g") ||
946 !strcmp(arg
, "--walk-reflogs")) {
947 init_reflog_walk(&revs
->reflog_info
);
950 if (!strcmp(arg
, "--not")) {
951 flags
^= UNINTERESTING
;
954 if (!strcmp(arg
, "--default")) {
956 die("bad --default argument");
960 if (!strcmp(arg
, "--merge")) {
964 if (!strcmp(arg
, "--topo-order")) {
965 revs
->topo_order
= 1;
968 if (!strcmp(arg
, "--date-order")) {
970 revs
->topo_order
= 1;
973 if (!strcmp(arg
, "--parents")) {
977 if (!strcmp(arg
, "--dense")) {
981 if (!strcmp(arg
, "--sparse")) {
985 if (!strcmp(arg
, "--remove-empty")) {
986 revs
->remove_empty_trees
= 1;
989 if (!strcmp(arg
, "--no-merges")) {
993 if (!strcmp(arg
, "--boundary")) {
997 if (!strcmp(arg
, "--left-right")) {
998 revs
->left_right
= 1;
1001 if (!strcmp(arg
, "--cherry-pick")) {
1002 revs
->cherry_pick
= 1;
1005 if (!strcmp(arg
, "--objects")) {
1006 revs
->tag_objects
= 1;
1007 revs
->tree_objects
= 1;
1008 revs
->blob_objects
= 1;
1011 if (!strcmp(arg
, "--objects-edge")) {
1012 revs
->tag_objects
= 1;
1013 revs
->tree_objects
= 1;
1014 revs
->blob_objects
= 1;
1015 revs
->edge_hint
= 1;
1018 if (!strcmp(arg
, "--unpacked")) {
1020 free(revs
->ignore_packed
);
1021 revs
->ignore_packed
= NULL
;
1022 revs
->num_ignore_packed
= 0;
1025 if (!prefixcmp(arg
, "--unpacked=")) {
1027 add_ignore_packed(revs
, arg
+11);
1030 if (!strcmp(arg
, "-r")) {
1032 revs
->diffopt
.recursive
= 1;
1035 if (!strcmp(arg
, "-t")) {
1037 revs
->diffopt
.recursive
= 1;
1038 revs
->diffopt
.tree_in_recursive
= 1;
1041 if (!strcmp(arg
, "-m")) {
1042 revs
->ignore_merges
= 0;
1045 if (!strcmp(arg
, "-c")) {
1047 revs
->dense_combined_merges
= 0;
1048 revs
->combine_merges
= 1;
1051 if (!strcmp(arg
, "--cc")) {
1053 revs
->dense_combined_merges
= 1;
1054 revs
->combine_merges
= 1;
1057 if (!strcmp(arg
, "-v")) {
1058 revs
->verbose_header
= 1;
1061 if (!prefixcmp(arg
, "--pretty")) {
1062 revs
->verbose_header
= 1;
1063 revs
->commit_format
= get_commit_format(arg
+8);
1066 if (!strcmp(arg
, "--root")) {
1067 revs
->show_root_diff
= 1;
1070 if (!strcmp(arg
, "--no-commit-id")) {
1071 revs
->no_commit_id
= 1;
1074 if (!strcmp(arg
, "--always")) {
1075 revs
->always_show_header
= 1;
1078 if (!strcmp(arg
, "--no-abbrev")) {
1082 if (!strcmp(arg
, "--abbrev")) {
1083 revs
->abbrev
= DEFAULT_ABBREV
;
1086 if (!prefixcmp(arg
, "--abbrev=")) {
1087 revs
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
1088 if (revs
->abbrev
< MINIMUM_ABBREV
)
1089 revs
->abbrev
= MINIMUM_ABBREV
;
1090 else if (revs
->abbrev
> 40)
1094 if (!strcmp(arg
, "--abbrev-commit")) {
1095 revs
->abbrev_commit
= 1;
1098 if (!strcmp(arg
, "--full-diff")) {
1100 revs
->full_diff
= 1;
1103 if (!strcmp(arg
, "--full-history")) {
1104 revs
->simplify_history
= 0;
1107 if (!strcmp(arg
, "--relative-date")) {
1108 revs
->relative_date
= 1;
1113 * Grepping the commit log
1115 if (!prefixcmp(arg
, "--author=")) {
1116 add_header_grep(revs
, "author", arg
+9);
1119 if (!prefixcmp(arg
, "--committer=")) {
1120 add_header_grep(revs
, "committer", arg
+12);
1123 if (!prefixcmp(arg
, "--grep=")) {
1124 add_message_grep(revs
, arg
+7);
1127 if (!strcmp(arg
, "--all-match")) {
1131 if (!prefixcmp(arg
, "--encoding=")) {
1133 if (strcmp(arg
, "none"))
1134 git_log_output_encoding
= xstrdup(arg
);
1136 git_log_output_encoding
= "";
1139 if (!strcmp(arg
, "--reverse")) {
1144 opts
= diff_opt_parse(&revs
->diffopt
, argv
+i
, argc
-i
);
1150 *unrecognized
++ = arg
;
1155 if (handle_revision_arg(arg
, revs
, flags
, seen_dashdash
)) {
1157 if (seen_dashdash
|| *arg
== '^')
1158 die("bad revision '%s'", arg
);
1160 /* If we didn't have a "--":
1161 * (1) all filenames must exist;
1162 * (2) all rev-args must not be interpretable
1163 * as a valid filename.
1164 * but the latter we have checked in the main loop.
1166 for (j
= i
; j
< argc
; j
++)
1167 verify_filename(revs
->prefix
, argv
[j
]);
1169 revs
->prune_data
= get_pathspec(revs
->prefix
,
1176 prepare_show_merge(revs
);
1177 if (def
&& !revs
->pending
.nr
) {
1178 unsigned char sha1
[20];
1179 struct object
*object
;
1180 if (get_sha1(def
, sha1
))
1181 die("bad default revision '%s'", def
);
1182 object
= get_reference(revs
, def
, sha1
, 0);
1183 add_pending_object(revs
, object
, def
);
1186 if (revs
->topo_order
)
1189 if (revs
->prune_data
) {
1190 diff_tree_setup_paths(revs
->prune_data
, &revs
->pruning
);
1191 revs
->prune_fn
= try_to_simplify_commit
;
1192 if (!revs
->full_diff
)
1193 diff_tree_setup_paths(revs
->prune_data
, &revs
->diffopt
);
1195 if (revs
->combine_merges
) {
1196 revs
->ignore_merges
= 0;
1197 if (revs
->dense_combined_merges
&& !revs
->diffopt
.output_format
)
1198 revs
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
1200 revs
->diffopt
.abbrev
= revs
->abbrev
;
1201 if (diff_setup_done(&revs
->diffopt
) < 0)
1202 die("diff_setup_done failed");
1204 if (revs
->grep_filter
) {
1205 revs
->grep_filter
->all_match
= all_match
;
1206 compile_grep_patterns(revs
->grep_filter
);
1212 void prepare_revision_walk(struct rev_info
*revs
)
1214 int nr
= revs
->pending
.nr
;
1215 struct object_array_entry
*e
, *list
;
1217 e
= list
= revs
->pending
.objects
;
1218 revs
->pending
.nr
= 0;
1219 revs
->pending
.alloc
= 0;
1220 revs
->pending
.objects
= NULL
;
1222 struct commit
*commit
= handle_commit(revs
, e
->item
, e
->name
);
1224 if (!(commit
->object
.flags
& SEEN
)) {
1225 commit
->object
.flags
|= SEEN
;
1226 insert_by_date(commit
, &revs
->commits
);
1237 if (revs
->topo_order
)
1238 sort_in_topological_order_fn(&revs
->commits
, revs
->lifo
,
1243 static int rewrite_one(struct rev_info
*revs
, struct commit
**pp
)
1246 struct commit
*p
= *pp
;
1248 add_parents_to_list(revs
, p
, &revs
->commits
);
1249 if (p
->parents
&& p
->parents
->next
)
1251 if (p
->object
.flags
& (TREECHANGE
| UNINTERESTING
))
1255 *pp
= p
->parents
->item
;
1259 static void rewrite_parents(struct rev_info
*revs
, struct commit
*commit
)
1261 struct commit_list
**pp
= &commit
->parents
;
1263 struct commit_list
*parent
= *pp
;
1264 if (rewrite_one(revs
, &parent
->item
) < 0) {
1272 static int commit_match(struct commit
*commit
, struct rev_info
*opt
)
1274 if (!opt
->grep_filter
)
1276 return grep_buffer(opt
->grep_filter
,
1277 NULL
, /* we say nothing, not even filename */
1278 commit
->buffer
, strlen(commit
->buffer
));
1281 static struct commit
*get_revision_1(struct rev_info
*revs
)
1287 struct commit_list
*entry
= revs
->commits
;
1288 struct commit
*commit
= entry
->item
;
1290 revs
->commits
= entry
->next
;
1293 if (revs
->reflog_info
)
1294 fake_reflog_parent(revs
->reflog_info
, commit
);
1297 * If we haven't done the list limiting, we need to look at
1298 * the parents here. We also need to do the date-based limiting
1299 * that we'd otherwise have done in limit_list().
1301 if (!revs
->limited
) {
1302 if (revs
->max_age
!= -1 &&
1303 (commit
->date
< revs
->max_age
))
1305 add_parents_to_list(revs
, commit
, &revs
->commits
);
1307 if (commit
->object
.flags
& SHOWN
)
1310 if (revs
->unpacked
&& has_sha1_pack(commit
->object
.sha1
,
1311 revs
->ignore_packed
))
1314 if (commit
->object
.flags
& UNINTERESTING
)
1316 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
1318 if (revs
->no_merges
&&
1319 commit
->parents
&& commit
->parents
->next
)
1321 if (!commit_match(commit
, revs
))
1323 if (revs
->prune_fn
&& revs
->dense
) {
1324 /* Commit without changes? */
1325 if (!(commit
->object
.flags
& TREECHANGE
)) {
1326 /* drop merges unless we want parenthood */
1329 /* non-merge - always ignore it */
1330 if (!commit
->parents
|| !commit
->parents
->next
)
1334 rewrite_parents(revs
, commit
);
1337 } while (revs
->commits
);
1341 static void gc_boundary(struct object_array
*array
)
1343 unsigned nr
= array
->nr
;
1344 unsigned alloc
= array
->alloc
;
1345 struct object_array_entry
*objects
= array
->objects
;
1349 for (i
= j
= 0; i
< nr
; i
++) {
1350 if (objects
[i
].item
->flags
& SHOWN
)
1353 objects
[j
] = objects
[i
];
1356 for (i
= j
; i
< nr
; i
++)
1357 objects
[i
].item
= NULL
;
1362 struct commit
*get_revision(struct rev_info
*revs
)
1364 struct commit
*c
= NULL
;
1365 struct commit_list
*l
;
1367 if (revs
->boundary
== 2) {
1369 struct object_array
*array
= &revs
->boundary_commits
;
1370 struct object_array_entry
*objects
= array
->objects
;
1371 for (i
= 0; i
< array
->nr
; i
++) {
1372 c
= (struct commit
*)(objects
[i
].item
);
1375 if (!(c
->object
.flags
& CHILD_SHOWN
))
1377 if (!(c
->object
.flags
& SHOWN
))
1383 c
->object
.flags
|= SHOWN
| BOUNDARY
;
1387 if (revs
->reverse
) {
1390 if (0 <= revs
->max_count
) {
1391 limit
= revs
->max_count
;
1392 if (0 < revs
->skip_count
)
1393 limit
+= revs
->skip_count
;
1396 while ((c
= get_revision_1(revs
))) {
1397 commit_list_insert(c
, &l
);
1398 if ((0 < limit
) && !--limit
)
1403 revs
->max_count
= -1;
1408 * Now pick up what they want to give us
1410 c
= get_revision_1(revs
);
1412 while (0 < revs
->skip_count
) {
1414 c
= get_revision_1(revs
);
1421 * Check the max_count.
1423 switch (revs
->max_count
) {
1434 c
->object
.flags
|= SHOWN
;
1436 if (!revs
->boundary
) {
1442 * get_revision_1() runs out the commits, and
1443 * we are done computing the boundaries.
1444 * switch to boundary commits output mode.
1447 return get_revision(revs
);
1451 * boundary commits are the commits that are parents of the
1452 * ones we got from get_revision_1() but they themselves are
1453 * not returned from get_revision_1(). Before returning
1454 * 'c', we need to mark its parents that they could be boundaries.
1457 for (l
= c
->parents
; l
; l
= l
->next
) {
1459 p
= &(l
->item
->object
);
1460 if (p
->flags
& (CHILD_SHOWN
| SHOWN
))
1462 p
->flags
|= CHILD_SHOWN
;
1463 gc_boundary(&revs
->boundary_commits
);
1464 add_object_array(p
, NULL
, &revs
->boundary_commits
);