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;
655 revs
->prune_fn
= NULL
;
656 revs
->prune_data
= NULL
;
658 revs
->topo_setter
= topo_sort_default_setter
;
659 revs
->topo_getter
= topo_sort_default_getter
;
661 revs
->commit_format
= CMIT_FMT_DEFAULT
;
663 diff_setup(&revs
->diffopt
);
666 static void add_pending_commit_list(struct rev_info
*revs
,
667 struct commit_list
*commit_list
,
670 while (commit_list
) {
671 struct object
*object
= &commit_list
->item
->object
;
672 object
->flags
|= flags
;
673 add_pending_object(revs
, object
, sha1_to_hex(object
->sha1
));
674 commit_list
= commit_list
->next
;
678 static void prepare_show_merge(struct rev_info
*revs
)
680 struct commit_list
*bases
;
681 struct commit
*head
, *other
;
682 unsigned char sha1
[20];
683 const char **prune
= NULL
;
684 int i
, prune_num
= 1; /* counting terminating NULL */
686 if (get_sha1("HEAD", sha1
) || !(head
= lookup_commit(sha1
)))
687 die("--merge without HEAD?");
688 if (get_sha1("MERGE_HEAD", sha1
) || !(other
= lookup_commit(sha1
)))
689 die("--merge without MERGE_HEAD?");
690 add_pending_object(revs
, &head
->object
, "HEAD");
691 add_pending_object(revs
, &other
->object
, "MERGE_HEAD");
692 bases
= get_merge_bases(head
, other
, 1);
694 struct commit
*it
= bases
->item
;
695 struct commit_list
*n
= bases
->next
;
698 it
->object
.flags
|= UNINTERESTING
;
699 add_pending_object(revs
, &it
->object
, "(merge-base)");
704 for (i
= 0; i
< active_nr
; i
++) {
705 struct cache_entry
*ce
= active_cache
[i
];
708 if (ce_path_match(ce
, revs
->prune_data
)) {
710 prune
= xrealloc(prune
, sizeof(*prune
) * prune_num
);
711 prune
[prune_num
-2] = ce
->name
;
712 prune
[prune_num
-1] = NULL
;
714 while ((i
+1 < active_nr
) &&
715 ce_same_name(ce
, active_cache
[i
+1]))
718 revs
->prune_data
= prune
;
721 int handle_revision_arg(const char *arg
, struct rev_info
*revs
,
723 int cant_be_filename
)
726 struct object
*object
;
727 unsigned char sha1
[20];
730 dotdot
= strstr(arg
, "..");
732 unsigned char from_sha1
[20];
733 const char *next
= dotdot
+ 2;
734 const char *this = arg
;
735 int symmetric
= *next
== '.';
736 unsigned int flags_exclude
= flags
^ UNINTERESTING
;
745 if (!get_sha1(this, from_sha1
) &&
746 !get_sha1(next
, sha1
)) {
747 struct commit
*a
, *b
;
748 struct commit_list
*exclude
;
750 a
= lookup_commit_reference(from_sha1
);
751 b
= lookup_commit_reference(sha1
);
754 "Invalid symmetric difference expression %s...%s" :
755 "Invalid revision range %s..%s",
759 if (!cant_be_filename
) {
761 verify_non_filename(revs
->prefix
, arg
);
765 exclude
= get_merge_bases(a
, b
, 1);
766 add_pending_commit_list(revs
, exclude
,
768 free_commit_list(exclude
);
769 a
->object
.flags
|= flags
| SYMMETRIC_LEFT
;
771 a
->object
.flags
|= flags_exclude
;
772 b
->object
.flags
|= flags
;
773 add_pending_object(revs
, &a
->object
, this);
774 add_pending_object(revs
, &b
->object
, next
);
779 dotdot
= strstr(arg
, "^@");
780 if (dotdot
&& !dotdot
[2]) {
782 if (add_parents_only(revs
, arg
, flags
))
786 dotdot
= strstr(arg
, "^!");
787 if (dotdot
&& !dotdot
[2]) {
789 if (!add_parents_only(revs
, arg
, flags
^ UNINTERESTING
))
795 local_flags
= UNINTERESTING
;
798 if (get_sha1(arg
, sha1
))
800 if (!cant_be_filename
)
801 verify_non_filename(revs
->prefix
, arg
);
802 object
= get_reference(revs
, arg
, sha1
, flags
^ local_flags
);
803 add_pending_object(revs
, object
, arg
);
807 static void add_grep(struct rev_info
*revs
, const char *ptn
, enum grep_pat_token what
)
809 if (!revs
->grep_filter
) {
810 struct grep_opt
*opt
= xcalloc(1, sizeof(*opt
));
811 opt
->status_only
= 1;
812 opt
->pattern_tail
= &(opt
->pattern_list
);
813 opt
->regflags
= REG_NEWLINE
;
814 revs
->grep_filter
= opt
;
816 append_grep_pattern(revs
->grep_filter
, ptn
,
817 "command line", 0, what
);
820 static void add_header_grep(struct rev_info
*revs
, const char *field
, const char *pattern
)
826 fldlen
= strlen(field
);
827 patlen
= strlen(pattern
);
828 pat
= xmalloc(patlen
+ fldlen
+ 10);
830 if (*pattern
== '^') {
834 sprintf(pat
, "^%s %s%s", field
, prefix
, pattern
);
835 add_grep(revs
, pat
, GREP_PATTERN_HEAD
);
838 static void add_message_grep(struct rev_info
*revs
, const char *pattern
)
840 add_grep(revs
, pattern
, GREP_PATTERN_BODY
);
843 static void add_ignore_packed(struct rev_info
*revs
, const char *name
)
845 int num
= ++revs
->num_ignore_packed
;
847 revs
->ignore_packed
= xrealloc(revs
->ignore_packed
,
848 sizeof(const char **) * (num
+ 1));
849 revs
->ignore_packed
[num
-1] = name
;
850 revs
->ignore_packed
[num
] = NULL
;
854 * Parse revision information, filling in the "rev_info" structure,
855 * and removing the used arguments from the argument list.
857 * Returns the number of arguments left that weren't recognized
858 * (which are also moved to the head of the argument list)
860 int setup_revisions(int argc
, const char **argv
, struct rev_info
*revs
, const char *def
)
862 int i
, flags
, seen_dashdash
, show_merge
;
863 const char **unrecognized
= argv
+ 1;
867 /* First, search for "--" */
869 for (i
= 1; i
< argc
; i
++) {
870 const char *arg
= argv
[i
];
871 if (strcmp(arg
, "--"))
875 revs
->prune_data
= get_pathspec(revs
->prefix
, argv
+ i
+ 1);
880 flags
= show_merge
= 0;
881 for (i
= 1; i
< argc
; i
++) {
882 const char *arg
= argv
[i
];
885 if (!prefixcmp(arg
, "--max-count=")) {
886 revs
->max_count
= atoi(arg
+ 12);
889 if (!prefixcmp(arg
, "--skip=")) {
890 revs
->skip_count
= atoi(arg
+ 7);
893 /* accept -<digit>, like traditional "head" */
894 if ((*arg
== '-') && isdigit(arg
[1])) {
895 revs
->max_count
= atoi(arg
+ 1);
898 if (!strcmp(arg
, "-n")) {
900 die("-n requires an argument");
901 revs
->max_count
= atoi(argv
[++i
]);
904 if (!prefixcmp(arg
, "-n")) {
905 revs
->max_count
= atoi(arg
+ 2);
908 if (!prefixcmp(arg
, "--max-age=")) {
909 revs
->max_age
= atoi(arg
+ 10);
912 if (!prefixcmp(arg
, "--since=")) {
913 revs
->max_age
= approxidate(arg
+ 8);
916 if (!prefixcmp(arg
, "--after=")) {
917 revs
->max_age
= approxidate(arg
+ 8);
920 if (!prefixcmp(arg
, "--min-age=")) {
921 revs
->min_age
= atoi(arg
+ 10);
924 if (!prefixcmp(arg
, "--before=")) {
925 revs
->min_age
= approxidate(arg
+ 9);
928 if (!prefixcmp(arg
, "--until=")) {
929 revs
->min_age
= approxidate(arg
+ 8);
932 if (!strcmp(arg
, "--all")) {
933 handle_all(revs
, flags
);
936 if (!strcmp(arg
, "--first-parent")) {
937 revs
->first_parent_only
= 1;
940 if (!strcmp(arg
, "--reflog")) {
941 handle_reflog(revs
, flags
);
944 if (!strcmp(arg
, "-g") ||
945 !strcmp(arg
, "--walk-reflogs")) {
946 init_reflog_walk(&revs
->reflog_info
);
949 if (!strcmp(arg
, "--not")) {
950 flags
^= UNINTERESTING
;
953 if (!strcmp(arg
, "--default")) {
955 die("bad --default argument");
959 if (!strcmp(arg
, "--merge")) {
963 if (!strcmp(arg
, "--topo-order")) {
964 revs
->topo_order
= 1;
967 if (!strcmp(arg
, "--date-order")) {
969 revs
->topo_order
= 1;
972 if (!strcmp(arg
, "--parents")) {
976 if (!strcmp(arg
, "--dense")) {
980 if (!strcmp(arg
, "--sparse")) {
984 if (!strcmp(arg
, "--remove-empty")) {
985 revs
->remove_empty_trees
= 1;
988 if (!strcmp(arg
, "--no-merges")) {
992 if (!strcmp(arg
, "--boundary")) {
996 if (!strcmp(arg
, "--left-right")) {
997 revs
->left_right
= 1;
1000 if (!strcmp(arg
, "--cherry-pick")) {
1001 revs
->cherry_pick
= 1;
1004 if (!strcmp(arg
, "--objects")) {
1005 revs
->tag_objects
= 1;
1006 revs
->tree_objects
= 1;
1007 revs
->blob_objects
= 1;
1010 if (!strcmp(arg
, "--objects-edge")) {
1011 revs
->tag_objects
= 1;
1012 revs
->tree_objects
= 1;
1013 revs
->blob_objects
= 1;
1014 revs
->edge_hint
= 1;
1017 if (!strcmp(arg
, "--unpacked")) {
1019 free(revs
->ignore_packed
);
1020 revs
->ignore_packed
= NULL
;
1021 revs
->num_ignore_packed
= 0;
1024 if (!prefixcmp(arg
, "--unpacked=")) {
1026 add_ignore_packed(revs
, arg
+11);
1029 if (!strcmp(arg
, "-r")) {
1031 revs
->diffopt
.recursive
= 1;
1034 if (!strcmp(arg
, "-t")) {
1036 revs
->diffopt
.recursive
= 1;
1037 revs
->diffopt
.tree_in_recursive
= 1;
1040 if (!strcmp(arg
, "-m")) {
1041 revs
->ignore_merges
= 0;
1044 if (!strcmp(arg
, "-c")) {
1046 revs
->dense_combined_merges
= 0;
1047 revs
->combine_merges
= 1;
1050 if (!strcmp(arg
, "--cc")) {
1052 revs
->dense_combined_merges
= 1;
1053 revs
->combine_merges
= 1;
1056 if (!strcmp(arg
, "-v")) {
1057 revs
->verbose_header
= 1;
1060 if (!prefixcmp(arg
, "--pretty")) {
1061 revs
->verbose_header
= 1;
1062 revs
->commit_format
= get_commit_format(arg
+8);
1065 if (!strcmp(arg
, "--root")) {
1066 revs
->show_root_diff
= 1;
1069 if (!strcmp(arg
, "--no-commit-id")) {
1070 revs
->no_commit_id
= 1;
1073 if (!strcmp(arg
, "--always")) {
1074 revs
->always_show_header
= 1;
1077 if (!strcmp(arg
, "--no-abbrev")) {
1081 if (!strcmp(arg
, "--abbrev")) {
1082 revs
->abbrev
= DEFAULT_ABBREV
;
1085 if (!prefixcmp(arg
, "--abbrev=")) {
1086 revs
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
1087 if (revs
->abbrev
< MINIMUM_ABBREV
)
1088 revs
->abbrev
= MINIMUM_ABBREV
;
1089 else if (revs
->abbrev
> 40)
1093 if (!strcmp(arg
, "--abbrev-commit")) {
1094 revs
->abbrev_commit
= 1;
1097 if (!strcmp(arg
, "--full-diff")) {
1099 revs
->full_diff
= 1;
1102 if (!strcmp(arg
, "--full-history")) {
1103 revs
->simplify_history
= 0;
1106 if (!strcmp(arg
, "--relative-date")) {
1107 revs
->relative_date
= 1;
1112 * Grepping the commit log
1114 if (!prefixcmp(arg
, "--author=")) {
1115 add_header_grep(revs
, "author", arg
+9);
1118 if (!prefixcmp(arg
, "--committer=")) {
1119 add_header_grep(revs
, "committer", arg
+12);
1122 if (!prefixcmp(arg
, "--grep=")) {
1123 add_message_grep(revs
, arg
+7);
1126 if (!strcmp(arg
, "--all-match")) {
1130 if (!prefixcmp(arg
, "--encoding=")) {
1132 if (strcmp(arg
, "none"))
1133 git_log_output_encoding
= xstrdup(arg
);
1135 git_log_output_encoding
= "";
1138 if (!strcmp(arg
, "--reverse")) {
1143 opts
= diff_opt_parse(&revs
->diffopt
, argv
+i
, argc
-i
);
1149 *unrecognized
++ = arg
;
1154 if (handle_revision_arg(arg
, revs
, flags
, seen_dashdash
)) {
1156 if (seen_dashdash
|| *arg
== '^')
1157 die("bad revision '%s'", arg
);
1159 /* If we didn't have a "--":
1160 * (1) all filenames must exist;
1161 * (2) all rev-args must not be interpretable
1162 * as a valid filename.
1163 * but the latter we have checked in the main loop.
1165 for (j
= i
; j
< argc
; j
++)
1166 verify_filename(revs
->prefix
, argv
[j
]);
1168 revs
->prune_data
= get_pathspec(revs
->prefix
,
1175 prepare_show_merge(revs
);
1176 if (def
&& !revs
->pending
.nr
) {
1177 unsigned char sha1
[20];
1178 struct object
*object
;
1179 if (get_sha1(def
, sha1
))
1180 die("bad default revision '%s'", def
);
1181 object
= get_reference(revs
, def
, sha1
, 0);
1182 add_pending_object(revs
, object
, def
);
1185 if (revs
->topo_order
)
1188 if (revs
->prune_data
) {
1189 diff_tree_setup_paths(revs
->prune_data
, &revs
->pruning
);
1190 revs
->prune_fn
= try_to_simplify_commit
;
1191 if (!revs
->full_diff
)
1192 diff_tree_setup_paths(revs
->prune_data
, &revs
->diffopt
);
1194 if (revs
->combine_merges
) {
1195 revs
->ignore_merges
= 0;
1196 if (revs
->dense_combined_merges
&& !revs
->diffopt
.output_format
)
1197 revs
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
1199 revs
->diffopt
.abbrev
= revs
->abbrev
;
1200 if (diff_setup_done(&revs
->diffopt
) < 0)
1201 die("diff_setup_done failed");
1203 if (revs
->grep_filter
) {
1204 revs
->grep_filter
->all_match
= all_match
;
1205 compile_grep_patterns(revs
->grep_filter
);
1211 void prepare_revision_walk(struct rev_info
*revs
)
1213 int nr
= revs
->pending
.nr
;
1214 struct object_array_entry
*e
, *list
;
1216 e
= list
= revs
->pending
.objects
;
1217 revs
->pending
.nr
= 0;
1218 revs
->pending
.alloc
= 0;
1219 revs
->pending
.objects
= NULL
;
1221 struct commit
*commit
= handle_commit(revs
, e
->item
, e
->name
);
1223 if (!(commit
->object
.flags
& SEEN
)) {
1224 commit
->object
.flags
|= SEEN
;
1225 insert_by_date(commit
, &revs
->commits
);
1236 if (revs
->topo_order
)
1237 sort_in_topological_order_fn(&revs
->commits
, revs
->lifo
,
1242 static int rewrite_one(struct rev_info
*revs
, struct commit
**pp
)
1245 struct commit
*p
= *pp
;
1247 add_parents_to_list(revs
, p
, &revs
->commits
);
1248 if (p
->parents
&& p
->parents
->next
)
1250 if (p
->object
.flags
& (TREECHANGE
| UNINTERESTING
))
1254 *pp
= p
->parents
->item
;
1258 static void rewrite_parents(struct rev_info
*revs
, struct commit
*commit
)
1260 struct commit_list
**pp
= &commit
->parents
;
1262 struct commit_list
*parent
= *pp
;
1263 if (rewrite_one(revs
, &parent
->item
) < 0) {
1271 static int commit_match(struct commit
*commit
, struct rev_info
*opt
)
1273 if (!opt
->grep_filter
)
1275 return grep_buffer(opt
->grep_filter
,
1276 NULL
, /* we say nothing, not even filename */
1277 commit
->buffer
, strlen(commit
->buffer
));
1280 static struct commit
*get_revision_1(struct rev_info
*revs
)
1286 struct commit_list
*entry
= revs
->commits
;
1287 struct commit
*commit
= entry
->item
;
1289 revs
->commits
= entry
->next
;
1292 if (revs
->reflog_info
)
1293 fake_reflog_parent(revs
->reflog_info
, commit
);
1296 * If we haven't done the list limiting, we need to look at
1297 * the parents here. We also need to do the date-based limiting
1298 * that we'd otherwise have done in limit_list().
1300 if (!revs
->limited
) {
1301 if (revs
->max_age
!= -1 &&
1302 (commit
->date
< revs
->max_age
))
1304 add_parents_to_list(revs
, commit
, &revs
->commits
);
1306 if (commit
->object
.flags
& SHOWN
)
1309 if (revs
->unpacked
&& has_sha1_pack(commit
->object
.sha1
,
1310 revs
->ignore_packed
))
1313 if (commit
->object
.flags
& UNINTERESTING
)
1315 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
1317 if (revs
->no_merges
&&
1318 commit
->parents
&& commit
->parents
->next
)
1320 if (!commit_match(commit
, revs
))
1322 if (revs
->prune_fn
&& revs
->dense
) {
1323 /* Commit without changes? */
1324 if (!(commit
->object
.flags
& TREECHANGE
)) {
1325 /* drop merges unless we want parenthood */
1328 /* non-merge - always ignore it */
1329 if (!commit
->parents
|| !commit
->parents
->next
)
1333 rewrite_parents(revs
, commit
);
1336 } while (revs
->commits
);
1340 static void gc_boundary(struct object_array
*array
)
1342 unsigned nr
= array
->nr
;
1343 unsigned alloc
= array
->alloc
;
1344 struct object_array_entry
*objects
= array
->objects
;
1348 for (i
= j
= 0; i
< nr
; i
++) {
1349 if (objects
[i
].item
->flags
& SHOWN
)
1352 objects
[j
] = objects
[i
];
1355 for (i
= j
; i
< nr
; i
++)
1356 objects
[i
].item
= NULL
;
1361 struct commit
*get_revision(struct rev_info
*revs
)
1363 struct commit
*c
= NULL
;
1364 struct commit_list
*l
;
1366 if (revs
->boundary
== 2) {
1368 struct object_array
*array
= &revs
->boundary_commits
;
1369 struct object_array_entry
*objects
= array
->objects
;
1370 for (i
= 0; i
< array
->nr
; i
++) {
1371 c
= (struct commit
*)(objects
[i
].item
);
1374 if (!(c
->object
.flags
& CHILD_SHOWN
))
1376 if (!(c
->object
.flags
& SHOWN
))
1382 c
->object
.flags
|= SHOWN
| BOUNDARY
;
1386 if (revs
->reverse
) {
1389 if (0 <= revs
->max_count
) {
1390 limit
= revs
->max_count
;
1391 if (0 < revs
->skip_count
)
1392 limit
+= revs
->skip_count
;
1395 while ((c
= get_revision_1(revs
))) {
1396 commit_list_insert(c
, &l
);
1397 if ((0 < limit
) && !--limit
)
1402 revs
->max_count
= -1;
1407 * Now pick up what they want to give us
1409 c
= get_revision_1(revs
);
1411 while (0 < revs
->skip_count
) {
1413 c
= get_revision_1(revs
);
1420 * Check the max_count.
1422 switch (revs
->max_count
) {
1433 c
->object
.flags
|= SHOWN
;
1435 if (!revs
->boundary
) {
1441 * get_revision_1() runs out the commits, and
1442 * we are done computing the boundaries.
1443 * switch to boundary commits output mode.
1446 return get_revision(revs
);
1450 * boundary commits are the commits that are parents of the
1451 * ones we got from get_revision_1() but they themselves are
1452 * not returned from get_revision_1(). Before returning
1453 * 'c', we need to mark its parents that they could be boundaries.
1456 for (l
= c
->parents
; l
; l
= l
->next
) {
1458 p
= &(l
->item
->object
);
1459 if (p
->flags
& (CHILD_SHOWN
| SHOWN
))
1461 p
->flags
|= CHILD_SHOWN
;
1462 gc_boundary(&revs
->boundary_commits
);
1463 add_object_array(p
, NULL
, &revs
->boundary_commits
);