10 #include "reflog-walk.h"
11 #include "patch-ids.h"
13 volatile show_early_output_fn_t show_early_output
;
15 static char *path_name(struct name_path
*path
, const char *name
)
19 int nlen
= strlen(name
);
22 for (p
= path
; p
; p
= p
->up
) {
24 len
+= p
->elem_len
+ 1;
27 m
= n
+ len
- (nlen
+ 1);
29 for (p
= path
; p
; p
= p
->up
) {
32 memcpy(m
, p
->elem
, p
->elem_len
);
39 void add_object(struct object
*obj
,
40 struct object_array
*p
,
41 struct name_path
*path
,
44 add_object_array(obj
, path_name(path
, name
), p
);
47 static void mark_blob_uninteresting(struct blob
*blob
)
49 if (blob
->object
.flags
& UNINTERESTING
)
51 blob
->object
.flags
|= UNINTERESTING
;
54 void mark_tree_uninteresting(struct tree
*tree
)
56 struct tree_desc desc
;
57 struct name_entry entry
;
58 struct object
*obj
= &tree
->object
;
60 if (obj
->flags
& UNINTERESTING
)
62 obj
->flags
|= UNINTERESTING
;
63 if (!has_sha1_file(obj
->sha1
))
65 if (parse_tree(tree
) < 0)
66 die("bad tree %s", sha1_to_hex(obj
->sha1
));
68 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
69 while (tree_entry(&desc
, &entry
)) {
70 if (S_ISDIR(entry
.mode
))
71 mark_tree_uninteresting(lookup_tree(entry
.sha1
));
73 mark_blob_uninteresting(lookup_blob(entry
.sha1
));
77 * We don't care about the tree any more
78 * after it has been marked uninteresting.
84 void mark_parents_uninteresting(struct commit
*commit
)
86 struct commit_list
*parents
= commit
->parents
;
89 struct commit
*commit
= parents
->item
;
90 if (!(commit
->object
.flags
& UNINTERESTING
)) {
91 commit
->object
.flags
|= UNINTERESTING
;
94 * Normally we haven't parsed the parent
95 * yet, so we won't have a parent of a parent
96 * here. However, it may turn out that we've
97 * reached this commit some other way (where it
98 * wasn't uninteresting), in which case we need
99 * to mark its parents recursively too..
102 mark_parents_uninteresting(commit
);
106 * A missing commit is ok iff its parent is marked
109 * We just mark such a thing parsed, so that when
110 * it is popped next time around, we won't be trying
111 * to parse it and get an error.
113 if (!has_sha1_file(commit
->object
.sha1
))
114 commit
->object
.parsed
= 1;
115 parents
= parents
->next
;
119 static void add_pending_object_with_mode(struct rev_info
*revs
, struct object
*obj
, const char *name
, unsigned mode
)
121 if (revs
->no_walk
&& (obj
->flags
& UNINTERESTING
))
122 die("object ranges do not make sense when not walking revisions");
123 if (revs
->reflog_info
&& obj
->type
== OBJ_COMMIT
&&
124 add_reflog_for_walk(revs
->reflog_info
,
125 (struct commit
*)obj
, name
))
127 add_object_array_with_mode(obj
, name
, &revs
->pending
, mode
);
130 void add_pending_object(struct rev_info
*revs
, struct object
*obj
, const char *name
)
132 add_pending_object_with_mode(revs
, obj
, name
, S_IFINVALID
);
135 static struct object
*get_reference(struct rev_info
*revs
, const char *name
, const unsigned char *sha1
, unsigned int flags
)
137 struct object
*object
;
139 object
= parse_object(sha1
);
141 die("bad object %s", name
);
142 object
->flags
|= flags
;
146 static struct commit
*handle_commit(struct rev_info
*revs
, struct object
*object
, const char *name
)
148 unsigned long flags
= object
->flags
;
151 * Tag object? Look what it points to..
153 while (object
->type
== OBJ_TAG
) {
154 struct tag
*tag
= (struct tag
*) object
;
155 if (revs
->tag_objects
&& !(flags
& UNINTERESTING
))
156 add_pending_object(revs
, object
, tag
->tag
);
157 object
= parse_object(tag
->tagged
->sha1
);
159 die("bad object %s", sha1_to_hex(tag
->tagged
->sha1
));
163 * Commit object? Just return it, we'll do all the complex
166 if (object
->type
== OBJ_COMMIT
) {
167 struct commit
*commit
= (struct commit
*)object
;
168 if (parse_commit(commit
) < 0)
169 die("unable to parse commit %s", name
);
170 if (flags
& UNINTERESTING
) {
171 commit
->object
.flags
|= UNINTERESTING
;
172 mark_parents_uninteresting(commit
);
179 * Tree object? Either mark it uniniteresting, or add it
180 * to the list of objects to look at later..
182 if (object
->type
== OBJ_TREE
) {
183 struct tree
*tree
= (struct tree
*)object
;
184 if (!revs
->tree_objects
)
186 if (flags
& UNINTERESTING
) {
187 mark_tree_uninteresting(tree
);
190 add_pending_object(revs
, object
, "");
195 * Blob object? You know the drill by now..
197 if (object
->type
== OBJ_BLOB
) {
198 struct blob
*blob
= (struct blob
*)object
;
199 if (!revs
->blob_objects
)
201 if (flags
& UNINTERESTING
) {
202 mark_blob_uninteresting(blob
);
205 add_pending_object(revs
, object
, "");
208 die("%s is unknown object", name
);
211 static int everybody_uninteresting(struct commit_list
*orig
)
213 struct commit_list
*list
= orig
;
215 struct commit
*commit
= list
->item
;
217 if (commit
->object
.flags
& UNINTERESTING
)
225 * The goal is to get REV_TREE_NEW as the result only if the
226 * diff consists of all '+' (and no other changes), and
227 * REV_TREE_DIFFERENT otherwise (of course if the trees are
228 * the same we want REV_TREE_SAME). That means that once we
229 * get to REV_TREE_DIFFERENT, we do not have to look any further.
231 static int tree_difference
= REV_TREE_SAME
;
233 static void file_add_remove(struct diff_options
*options
,
234 int addremove
, unsigned mode
,
235 const unsigned char *sha1
,
236 const char *base
, const char *path
)
238 int diff
= REV_TREE_DIFFERENT
;
241 * Is it an add of a new file? It means that the old tree
242 * didn't have it at all, so we will turn "REV_TREE_SAME" ->
243 * "REV_TREE_NEW", but leave any "REV_TREE_DIFFERENT" alone
244 * (and if it already was "REV_TREE_NEW", we'll keep it
245 * "REV_TREE_NEW" of course).
247 if (addremove
== '+') {
248 diff
= tree_difference
;
249 if (diff
!= REV_TREE_SAME
)
253 tree_difference
= diff
;
254 if (tree_difference
== REV_TREE_DIFFERENT
)
255 options
->has_changes
= 1;
258 static void file_change(struct diff_options
*options
,
259 unsigned old_mode
, unsigned new_mode
,
260 const unsigned char *old_sha1
,
261 const unsigned char *new_sha1
,
262 const char *base
, const char *path
)
264 tree_difference
= REV_TREE_DIFFERENT
;
265 options
->has_changes
= 1;
268 static int rev_compare_tree(struct rev_info
*revs
, struct tree
*t1
, struct tree
*t2
)
273 return REV_TREE_DIFFERENT
;
274 tree_difference
= REV_TREE_SAME
;
275 revs
->pruning
.has_changes
= 0;
276 if (diff_tree_sha1(t1
->object
.sha1
, t2
->object
.sha1
, "",
278 return REV_TREE_DIFFERENT
;
279 return tree_difference
;
282 static int rev_same_tree_as_empty(struct rev_info
*revs
, struct tree
*t1
)
287 struct tree_desc empty
, real
;
292 tree
= read_object_with_reference(t1
->object
.sha1
, tree_type
, &size
, NULL
);
295 init_tree_desc(&real
, tree
, size
);
296 init_tree_desc(&empty
, "", 0);
298 tree_difference
= REV_TREE_SAME
;
299 revs
->pruning
.has_changes
= 0;
300 retval
= diff_tree(&empty
, &real
, "", &revs
->pruning
);
303 return retval
>= 0 && (tree_difference
== REV_TREE_SAME
);
306 static void try_to_simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
308 struct commit_list
**pp
, *parent
;
309 int tree_changed
= 0, tree_same
= 0;
314 if (!commit
->parents
) {
315 if (!rev_same_tree_as_empty(revs
, commit
->tree
))
316 commit
->object
.flags
|= TREECHANGE
;
320 pp
= &commit
->parents
;
321 while ((parent
= *pp
) != NULL
) {
322 struct commit
*p
= parent
->item
;
324 if (parse_commit(p
) < 0)
325 die("cannot simplify commit %s (because of %s)",
326 sha1_to_hex(commit
->object
.sha1
),
327 sha1_to_hex(p
->object
.sha1
));
328 switch (rev_compare_tree(revs
, p
->tree
, commit
->tree
)) {
331 if (!revs
->simplify_history
|| (p
->object
.flags
& UNINTERESTING
)) {
332 /* Even if a merge with an uninteresting
333 * side branch brought the entire change
334 * we are interested in, we do not want
335 * to lose the other branches of this
336 * merge, so we just keep going.
342 commit
->parents
= parent
;
346 if (revs
->remove_empty_trees
&&
347 rev_same_tree_as_empty(revs
, p
->tree
)) {
348 /* We are adding all the specified
349 * paths from this parent, so the
350 * history beyond this parent is not
351 * interesting. Remove its parents
352 * (they are grandparents for us).
353 * IOW, we pretend this parent is a
356 if (parse_commit(p
) < 0)
357 die("cannot simplify commit %s (invalid %s)",
358 sha1_to_hex(commit
->object
.sha1
),
359 sha1_to_hex(p
->object
.sha1
));
363 case REV_TREE_DIFFERENT
:
368 die("bad tree compare for commit %s", sha1_to_hex(commit
->object
.sha1
));
370 if (tree_changed
&& !tree_same
)
371 commit
->object
.flags
|= TREECHANGE
;
374 static int add_parents_to_list(struct rev_info
*revs
, struct commit
*commit
, struct commit_list
**list
)
376 struct commit_list
*parent
= commit
->parents
;
380 if (commit
->object
.flags
& ADDED
)
382 commit
->object
.flags
|= ADDED
;
385 * If the commit is uninteresting, don't try to
386 * prune parents - we want the maximal uninteresting
389 * Normally we haven't parsed the parent
390 * yet, so we won't have a parent of a parent
391 * here. However, it may turn out that we've
392 * reached this commit some other way (where it
393 * wasn't uninteresting), in which case we need
394 * to mark its parents recursively too..
396 if (commit
->object
.flags
& UNINTERESTING
) {
398 struct commit
*p
= parent
->item
;
399 parent
= parent
->next
;
400 if (parse_commit(p
) < 0)
402 p
->object
.flags
|= UNINTERESTING
;
404 mark_parents_uninteresting(p
);
405 if (p
->object
.flags
& SEEN
)
407 p
->object
.flags
|= SEEN
;
408 insert_by_date(p
, list
);
414 * Ok, the commit wasn't uninteresting. Try to
415 * simplify the commit history and find the parent
416 * that has no differences in the path set if one exists.
419 revs
->prune_fn(revs
, commit
);
424 left_flag
= (commit
->object
.flags
& SYMMETRIC_LEFT
);
426 rest
= !revs
->first_parent_only
;
427 for (parent
= commit
->parents
, add
= 1; parent
; add
= rest
) {
428 struct commit
*p
= parent
->item
;
430 parent
= parent
->next
;
431 if (parse_commit(p
) < 0)
433 p
->object
.flags
|= left_flag
;
434 if (p
->object
.flags
& SEEN
)
436 p
->object
.flags
|= SEEN
;
438 insert_by_date(p
, list
);
443 static void cherry_pick_list(struct commit_list
*list
, struct rev_info
*revs
)
445 struct commit_list
*p
;
446 int left_count
= 0, right_count
= 0;
448 struct patch_ids ids
;
450 /* First count the commits on the left and on the right */
451 for (p
= list
; p
; p
= p
->next
) {
452 struct commit
*commit
= p
->item
;
453 unsigned flags
= commit
->object
.flags
;
454 if (flags
& BOUNDARY
)
456 else if (flags
& SYMMETRIC_LEFT
)
462 left_first
= left_count
< right_count
;
463 init_patch_ids(&ids
);
464 if (revs
->diffopt
.nr_paths
) {
465 ids
.diffopts
.nr_paths
= revs
->diffopt
.nr_paths
;
466 ids
.diffopts
.paths
= revs
->diffopt
.paths
;
467 ids
.diffopts
.pathlens
= revs
->diffopt
.pathlens
;
470 /* Compute patch-ids for one side */
471 for (p
= list
; p
; p
= p
->next
) {
472 struct commit
*commit
= p
->item
;
473 unsigned flags
= commit
->object
.flags
;
475 if (flags
& BOUNDARY
)
478 * If we have fewer left, left_first is set and we omit
479 * commits on the right branch in this loop. If we have
480 * fewer right, we skip the left ones.
482 if (left_first
!= !!(flags
& SYMMETRIC_LEFT
))
484 commit
->util
= add_commit_patch_id(commit
, &ids
);
487 /* Check the other side */
488 for (p
= list
; p
; p
= p
->next
) {
489 struct commit
*commit
= p
->item
;
491 unsigned flags
= commit
->object
.flags
;
493 if (flags
& BOUNDARY
)
496 * If we have fewer left, left_first is set and we omit
497 * commits on the left branch in this loop.
499 if (left_first
== !!(flags
& SYMMETRIC_LEFT
))
503 * Have we seen the same patch id?
505 id
= has_commit_patch_id(commit
, &ids
);
509 commit
->object
.flags
|= SHOWN
;
512 /* Now check the original side for seen ones */
513 for (p
= list
; p
; p
= p
->next
) {
514 struct commit
*commit
= p
->item
;
515 struct patch_id
*ent
;
521 commit
->object
.flags
|= SHOWN
;
525 free_patch_ids(&ids
);
528 static int limit_list(struct rev_info
*revs
)
530 struct commit_list
*list
= revs
->commits
;
531 struct commit_list
*newlist
= NULL
;
532 struct commit_list
**p
= &newlist
;
535 struct commit_list
*entry
= list
;
536 struct commit
*commit
= list
->item
;
537 struct object
*obj
= &commit
->object
;
538 show_early_output_fn_t show
;
543 if (revs
->max_age
!= -1 && (commit
->date
< revs
->max_age
))
544 obj
->flags
|= UNINTERESTING
;
545 if (add_parents_to_list(revs
, commit
, &list
) < 0)
547 if (obj
->flags
& UNINTERESTING
) {
548 mark_parents_uninteresting(commit
);
549 if (everybody_uninteresting(list
))
553 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
555 p
= &commit_list_insert(commit
, p
)->next
;
557 show
= show_early_output
;
562 show_early_output
= NULL
;
564 if (revs
->cherry_pick
)
565 cherry_pick_list(newlist
, revs
);
567 revs
->commits
= newlist
;
573 int warned_bad_reflog
;
574 struct rev_info
*all_revs
;
575 const char *name_for_errormsg
;
578 static int handle_one_ref(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
580 struct all_refs_cb
*cb
= cb_data
;
581 struct object
*object
= get_reference(cb
->all_revs
, path
, sha1
,
583 add_pending_object(cb
->all_revs
, object
, path
);
587 static void handle_all(struct rev_info
*revs
, unsigned flags
)
589 struct all_refs_cb cb
;
591 cb
.all_flags
= flags
;
592 for_each_ref(handle_one_ref
, &cb
);
595 static void handle_one_reflog_commit(unsigned char *sha1
, void *cb_data
)
597 struct all_refs_cb
*cb
= cb_data
;
598 if (!is_null_sha1(sha1
)) {
599 struct object
*o
= parse_object(sha1
);
601 o
->flags
|= cb
->all_flags
;
602 add_pending_object(cb
->all_revs
, o
, "");
604 else if (!cb
->warned_bad_reflog
) {
605 warning("reflog of '%s' references pruned commits",
606 cb
->name_for_errormsg
);
607 cb
->warned_bad_reflog
= 1;
612 static int handle_one_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
613 const char *email
, unsigned long timestamp
, int tz
,
614 const char *message
, void *cb_data
)
616 handle_one_reflog_commit(osha1
, cb_data
);
617 handle_one_reflog_commit(nsha1
, cb_data
);
621 static int handle_one_reflog(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
623 struct all_refs_cb
*cb
= cb_data
;
624 cb
->warned_bad_reflog
= 0;
625 cb
->name_for_errormsg
= path
;
626 for_each_reflog_ent(path
, handle_one_reflog_ent
, cb_data
);
630 static void handle_reflog(struct rev_info
*revs
, unsigned flags
)
632 struct all_refs_cb cb
;
634 cb
.all_flags
= flags
;
635 for_each_reflog(handle_one_reflog
, &cb
);
638 static int add_parents_only(struct rev_info
*revs
, const char *arg
, int flags
)
640 unsigned char sha1
[20];
642 struct commit
*commit
;
643 struct commit_list
*parents
;
646 flags
^= UNINTERESTING
;
649 if (get_sha1(arg
, sha1
))
652 it
= get_reference(revs
, arg
, sha1
, 0);
653 if (it
->type
!= OBJ_TAG
)
655 hashcpy(sha1
, ((struct tag
*)it
)->tagged
->sha1
);
657 if (it
->type
!= OBJ_COMMIT
)
659 commit
= (struct commit
*)it
;
660 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
661 it
= &parents
->item
->object
;
663 add_pending_object(revs
, it
, arg
);
668 void init_revisions(struct rev_info
*revs
, const char *prefix
)
670 memset(revs
, 0, sizeof(*revs
));
672 revs
->abbrev
= DEFAULT_ABBREV
;
673 revs
->ignore_merges
= 1;
674 revs
->simplify_history
= 1;
675 revs
->pruning
.recursive
= 1;
676 revs
->pruning
.quiet
= 1;
677 revs
->pruning
.add_remove
= file_add_remove
;
678 revs
->pruning
.change
= file_change
;
681 revs
->prefix
= prefix
;
684 revs
->skip_count
= -1;
685 revs
->max_count
= -1;
687 revs
->prune_fn
= NULL
;
688 revs
->prune_data
= NULL
;
690 revs
->commit_format
= CMIT_FMT_DEFAULT
;
692 diff_setup(&revs
->diffopt
);
695 static void add_pending_commit_list(struct rev_info
*revs
,
696 struct commit_list
*commit_list
,
699 while (commit_list
) {
700 struct object
*object
= &commit_list
->item
->object
;
701 object
->flags
|= flags
;
702 add_pending_object(revs
, object
, sha1_to_hex(object
->sha1
));
703 commit_list
= commit_list
->next
;
707 static void prepare_show_merge(struct rev_info
*revs
)
709 struct commit_list
*bases
;
710 struct commit
*head
, *other
;
711 unsigned char sha1
[20];
712 const char **prune
= NULL
;
713 int i
, prune_num
= 1; /* counting terminating NULL */
715 if (get_sha1("HEAD", sha1
) || !(head
= lookup_commit(sha1
)))
716 die("--merge without HEAD?");
717 if (get_sha1("MERGE_HEAD", sha1
) || !(other
= lookup_commit(sha1
)))
718 die("--merge without MERGE_HEAD?");
719 add_pending_object(revs
, &head
->object
, "HEAD");
720 add_pending_object(revs
, &other
->object
, "MERGE_HEAD");
721 bases
= get_merge_bases(head
, other
, 1);
723 struct commit
*it
= bases
->item
;
724 struct commit_list
*n
= bases
->next
;
727 it
->object
.flags
|= UNINTERESTING
;
728 add_pending_object(revs
, &it
->object
, "(merge-base)");
733 for (i
= 0; i
< active_nr
; i
++) {
734 struct cache_entry
*ce
= active_cache
[i
];
737 if (ce_path_match(ce
, revs
->prune_data
)) {
739 prune
= xrealloc(prune
, sizeof(*prune
) * prune_num
);
740 prune
[prune_num
-2] = ce
->name
;
741 prune
[prune_num
-1] = NULL
;
743 while ((i
+1 < active_nr
) &&
744 ce_same_name(ce
, active_cache
[i
+1]))
747 revs
->prune_data
= prune
;
750 int handle_revision_arg(const char *arg
, struct rev_info
*revs
,
752 int cant_be_filename
)
756 struct object
*object
;
757 unsigned char sha1
[20];
760 dotdot
= strstr(arg
, "..");
762 unsigned char from_sha1
[20];
763 const char *next
= dotdot
+ 2;
764 const char *this = arg
;
765 int symmetric
= *next
== '.';
766 unsigned int flags_exclude
= flags
^ UNINTERESTING
;
775 if (!get_sha1(this, from_sha1
) &&
776 !get_sha1(next
, sha1
)) {
777 struct commit
*a
, *b
;
778 struct commit_list
*exclude
;
780 a
= lookup_commit_reference(from_sha1
);
781 b
= lookup_commit_reference(sha1
);
784 "Invalid symmetric difference expression %s...%s" :
785 "Invalid revision range %s..%s",
789 if (!cant_be_filename
) {
791 verify_non_filename(revs
->prefix
, arg
);
795 exclude
= get_merge_bases(a
, b
, 1);
796 add_pending_commit_list(revs
, exclude
,
798 free_commit_list(exclude
);
799 a
->object
.flags
|= flags
| SYMMETRIC_LEFT
;
801 a
->object
.flags
|= flags_exclude
;
802 b
->object
.flags
|= flags
;
803 add_pending_object(revs
, &a
->object
, this);
804 add_pending_object(revs
, &b
->object
, next
);
809 dotdot
= strstr(arg
, "^@");
810 if (dotdot
&& !dotdot
[2]) {
812 if (add_parents_only(revs
, arg
, flags
))
816 dotdot
= strstr(arg
, "^!");
817 if (dotdot
&& !dotdot
[2]) {
819 if (!add_parents_only(revs
, arg
, flags
^ UNINTERESTING
))
825 local_flags
= UNINTERESTING
;
828 if (get_sha1_with_mode(arg
, sha1
, &mode
))
830 if (!cant_be_filename
)
831 verify_non_filename(revs
->prefix
, arg
);
832 object
= get_reference(revs
, arg
, sha1
, flags
^ local_flags
);
833 add_pending_object_with_mode(revs
, object
, arg
, mode
);
837 static void add_grep(struct rev_info
*revs
, const char *ptn
, enum grep_pat_token what
)
839 if (!revs
->grep_filter
) {
840 struct grep_opt
*opt
= xcalloc(1, sizeof(*opt
));
841 opt
->status_only
= 1;
842 opt
->pattern_tail
= &(opt
->pattern_list
);
843 opt
->regflags
= REG_NEWLINE
;
844 revs
->grep_filter
= opt
;
846 append_grep_pattern(revs
->grep_filter
, ptn
,
847 "command line", 0, what
);
850 static void add_header_grep(struct rev_info
*revs
, const char *field
, const char *pattern
)
856 fldlen
= strlen(field
);
857 patlen
= strlen(pattern
);
858 pat
= xmalloc(patlen
+ fldlen
+ 10);
860 if (*pattern
== '^') {
864 sprintf(pat
, "^%s %s%s", field
, prefix
, pattern
);
865 add_grep(revs
, pat
, GREP_PATTERN_HEAD
);
868 static void add_message_grep(struct rev_info
*revs
, const char *pattern
)
870 add_grep(revs
, pattern
, GREP_PATTERN_BODY
);
873 static void add_ignore_packed(struct rev_info
*revs
, const char *name
)
875 int num
= ++revs
->num_ignore_packed
;
877 revs
->ignore_packed
= xrealloc(revs
->ignore_packed
,
878 sizeof(const char **) * (num
+ 1));
879 revs
->ignore_packed
[num
-1] = name
;
880 revs
->ignore_packed
[num
] = NULL
;
884 * Parse revision information, filling in the "rev_info" structure,
885 * and removing the used arguments from the argument list.
887 * Returns the number of arguments left that weren't recognized
888 * (which are also moved to the head of the argument list)
890 int setup_revisions(int argc
, const char **argv
, struct rev_info
*revs
, const char *def
)
892 int i
, flags
, seen_dashdash
, show_merge
;
893 const char **unrecognized
= argv
+ 1;
898 /* First, search for "--" */
900 for (i
= 1; i
< argc
; i
++) {
901 const char *arg
= argv
[i
];
902 if (strcmp(arg
, "--"))
907 revs
->prune_data
= get_pathspec(revs
->prefix
, argv
+ i
+ 1);
912 flags
= show_merge
= 0;
913 for (i
= 1; i
< argc
; i
++) {
914 const char *arg
= argv
[i
];
917 if (!prefixcmp(arg
, "--max-count=")) {
918 revs
->max_count
= atoi(arg
+ 12);
921 if (!prefixcmp(arg
, "--skip=")) {
922 revs
->skip_count
= atoi(arg
+ 7);
925 /* accept -<digit>, like traditional "head" */
926 if ((*arg
== '-') && isdigit(arg
[1])) {
927 revs
->max_count
= atoi(arg
+ 1);
930 if (!strcmp(arg
, "-n")) {
932 die("-n requires an argument");
933 revs
->max_count
= atoi(argv
[++i
]);
936 if (!prefixcmp(arg
, "-n")) {
937 revs
->max_count
= atoi(arg
+ 2);
940 if (!prefixcmp(arg
, "--max-age=")) {
941 revs
->max_age
= atoi(arg
+ 10);
944 if (!prefixcmp(arg
, "--since=")) {
945 revs
->max_age
= approxidate(arg
+ 8);
948 if (!prefixcmp(arg
, "--after=")) {
949 revs
->max_age
= approxidate(arg
+ 8);
952 if (!prefixcmp(arg
, "--min-age=")) {
953 revs
->min_age
= atoi(arg
+ 10);
956 if (!prefixcmp(arg
, "--before=")) {
957 revs
->min_age
= approxidate(arg
+ 9);
960 if (!prefixcmp(arg
, "--until=")) {
961 revs
->min_age
= approxidate(arg
+ 8);
964 if (!strcmp(arg
, "--all")) {
965 handle_all(revs
, flags
);
968 if (!strcmp(arg
, "--first-parent")) {
969 revs
->first_parent_only
= 1;
972 if (!strcmp(arg
, "--reflog")) {
973 handle_reflog(revs
, flags
);
976 if (!strcmp(arg
, "-g") ||
977 !strcmp(arg
, "--walk-reflogs")) {
978 init_reflog_walk(&revs
->reflog_info
);
981 if (!strcmp(arg
, "--not")) {
982 flags
^= UNINTERESTING
;
985 if (!strcmp(arg
, "--default")) {
987 die("bad --default argument");
991 if (!strcmp(arg
, "--merge")) {
995 if (!strcmp(arg
, "--topo-order")) {
996 revs
->topo_order
= 1;
999 if (!strcmp(arg
, "--date-order")) {
1001 revs
->topo_order
= 1;
1004 if (!prefixcmp(arg
, "--early-output")) {
1008 count
= atoi(arg
+15);
1011 revs
->topo_order
= 1;
1012 revs
->early_output
= count
;
1016 if (!strcmp(arg
, "--parents")) {
1020 if (!strcmp(arg
, "--dense")) {
1024 if (!strcmp(arg
, "--sparse")) {
1028 if (!strcmp(arg
, "--remove-empty")) {
1029 revs
->remove_empty_trees
= 1;
1032 if (!strcmp(arg
, "--no-merges")) {
1033 revs
->no_merges
= 1;
1036 if (!strcmp(arg
, "--boundary")) {
1040 if (!strcmp(arg
, "--left-right")) {
1041 revs
->left_right
= 1;
1044 if (!strcmp(arg
, "--cherry-pick")) {
1045 revs
->cherry_pick
= 1;
1049 if (!strcmp(arg
, "--objects")) {
1050 revs
->tag_objects
= 1;
1051 revs
->tree_objects
= 1;
1052 revs
->blob_objects
= 1;
1055 if (!strcmp(arg
, "--objects-edge")) {
1056 revs
->tag_objects
= 1;
1057 revs
->tree_objects
= 1;
1058 revs
->blob_objects
= 1;
1059 revs
->edge_hint
= 1;
1062 if (!strcmp(arg
, "--unpacked")) {
1064 free(revs
->ignore_packed
);
1065 revs
->ignore_packed
= NULL
;
1066 revs
->num_ignore_packed
= 0;
1069 if (!prefixcmp(arg
, "--unpacked=")) {
1071 add_ignore_packed(revs
, arg
+11);
1074 if (!strcmp(arg
, "-r")) {
1076 revs
->diffopt
.recursive
= 1;
1079 if (!strcmp(arg
, "-t")) {
1081 revs
->diffopt
.recursive
= 1;
1082 revs
->diffopt
.tree_in_recursive
= 1;
1085 if (!strcmp(arg
, "-m")) {
1086 revs
->ignore_merges
= 0;
1089 if (!strcmp(arg
, "-c")) {
1091 revs
->dense_combined_merges
= 0;
1092 revs
->combine_merges
= 1;
1095 if (!strcmp(arg
, "--cc")) {
1097 revs
->dense_combined_merges
= 1;
1098 revs
->combine_merges
= 1;
1101 if (!strcmp(arg
, "-v")) {
1102 revs
->verbose_header
= 1;
1105 if (!prefixcmp(arg
, "--pretty")) {
1106 revs
->verbose_header
= 1;
1107 revs
->commit_format
= get_commit_format(arg
+8);
1110 if (!strcmp(arg
, "--root")) {
1111 revs
->show_root_diff
= 1;
1114 if (!strcmp(arg
, "--no-commit-id")) {
1115 revs
->no_commit_id
= 1;
1118 if (!strcmp(arg
, "--always")) {
1119 revs
->always_show_header
= 1;
1122 if (!strcmp(arg
, "--no-abbrev")) {
1126 if (!strcmp(arg
, "--abbrev")) {
1127 revs
->abbrev
= DEFAULT_ABBREV
;
1130 if (!prefixcmp(arg
, "--abbrev=")) {
1131 revs
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
1132 if (revs
->abbrev
< MINIMUM_ABBREV
)
1133 revs
->abbrev
= MINIMUM_ABBREV
;
1134 else if (revs
->abbrev
> 40)
1138 if (!strcmp(arg
, "--abbrev-commit")) {
1139 revs
->abbrev_commit
= 1;
1142 if (!strcmp(arg
, "--full-diff")) {
1144 revs
->full_diff
= 1;
1147 if (!strcmp(arg
, "--full-history")) {
1148 revs
->simplify_history
= 0;
1151 if (!strcmp(arg
, "--relative-date")) {
1152 revs
->date_mode
= DATE_RELATIVE
;
1155 if (!strncmp(arg
, "--date=", 7)) {
1156 revs
->date_mode
= parse_date_format(arg
+ 7);
1159 if (!strcmp(arg
, "--log-size")) {
1160 revs
->show_log_size
= 1;
1165 * Grepping the commit log
1167 if (!prefixcmp(arg
, "--author=")) {
1168 add_header_grep(revs
, "author", arg
+9);
1171 if (!prefixcmp(arg
, "--committer=")) {
1172 add_header_grep(revs
, "committer", arg
+12);
1175 if (!prefixcmp(arg
, "--grep=")) {
1176 add_message_grep(revs
, arg
+7);
1179 if (!strcmp(arg
, "--extended-regexp") ||
1180 !strcmp(arg
, "-E")) {
1181 regflags
|= REG_EXTENDED
;
1184 if (!strcmp(arg
, "--regexp-ignore-case") ||
1185 !strcmp(arg
, "-i")) {
1186 regflags
|= REG_ICASE
;
1189 if (!strcmp(arg
, "--all-match")) {
1193 if (!prefixcmp(arg
, "--encoding=")) {
1195 if (strcmp(arg
, "none"))
1196 git_log_output_encoding
= xstrdup(arg
);
1198 git_log_output_encoding
= "";
1201 if (!strcmp(arg
, "--reverse")) {
1205 if (!strcmp(arg
, "--no-walk")) {
1209 if (!strcmp(arg
, "--do-walk")) {
1214 opts
= diff_opt_parse(&revs
->diffopt
, argv
+i
, argc
-i
);
1219 *unrecognized
++ = arg
;
1224 if (handle_revision_arg(arg
, revs
, flags
, seen_dashdash
)) {
1226 if (seen_dashdash
|| *arg
== '^')
1227 die("bad revision '%s'", arg
);
1229 /* If we didn't have a "--":
1230 * (1) all filenames must exist;
1231 * (2) all rev-args must not be interpretable
1232 * as a valid filename.
1233 * but the latter we have checked in the main loop.
1235 for (j
= i
; j
< argc
; j
++)
1236 verify_filename(revs
->prefix
, argv
[j
]);
1238 revs
->prune_data
= get_pathspec(revs
->prefix
,
1244 if (revs
->grep_filter
)
1245 revs
->grep_filter
->regflags
|= regflags
;
1248 prepare_show_merge(revs
);
1249 if (def
&& !revs
->pending
.nr
) {
1250 unsigned char sha1
[20];
1251 struct object
*object
;
1253 if (get_sha1_with_mode(def
, sha1
, &mode
))
1254 die("bad default revision '%s'", def
);
1255 object
= get_reference(revs
, def
, sha1
, 0);
1256 add_pending_object_with_mode(revs
, object
, def
, mode
);
1259 /* Did the user ask for any diff output? Run the diff! */
1260 if (revs
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
)
1263 /* Pickaxe and rename following needs diffs */
1264 if (revs
->diffopt
.pickaxe
|| revs
->diffopt
.follow_renames
)
1267 if (revs
->topo_order
)
1270 if (revs
->prune_data
) {
1271 diff_tree_setup_paths(revs
->prune_data
, &revs
->pruning
);
1272 /* Can't prune commits with rename following: the paths change.. */
1273 if (!revs
->diffopt
.follow_renames
)
1274 revs
->prune_fn
= try_to_simplify_commit
;
1275 if (!revs
->full_diff
)
1276 diff_tree_setup_paths(revs
->prune_data
, &revs
->diffopt
);
1278 if (revs
->combine_merges
) {
1279 revs
->ignore_merges
= 0;
1280 if (revs
->dense_combined_merges
&& !revs
->diffopt
.output_format
)
1281 revs
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
1283 revs
->diffopt
.abbrev
= revs
->abbrev
;
1284 if (diff_setup_done(&revs
->diffopt
) < 0)
1285 die("diff_setup_done failed");
1287 if (revs
->grep_filter
) {
1288 revs
->grep_filter
->all_match
= all_match
;
1289 compile_grep_patterns(revs
->grep_filter
);
1292 if (revs
->reverse
&& revs
->reflog_info
)
1293 die("cannot combine --reverse with --walk-reflogs");
1298 int prepare_revision_walk(struct rev_info
*revs
)
1300 int nr
= revs
->pending
.nr
;
1301 struct object_array_entry
*e
, *list
;
1303 e
= list
= revs
->pending
.objects
;
1304 revs
->pending
.nr
= 0;
1305 revs
->pending
.alloc
= 0;
1306 revs
->pending
.objects
= NULL
;
1308 struct commit
*commit
= handle_commit(revs
, e
->item
, e
->name
);
1310 if (!(commit
->object
.flags
& SEEN
)) {
1311 commit
->object
.flags
|= SEEN
;
1312 insert_by_date(commit
, &revs
->commits
);
1322 if (limit_list(revs
) < 0)
1324 if (revs
->topo_order
)
1325 sort_in_topological_order(&revs
->commits
, revs
->lifo
);
1329 enum rewrite_result
{
1331 rewrite_one_noparents
,
1335 static enum rewrite_result
rewrite_one(struct rev_info
*revs
, struct commit
**pp
)
1338 struct commit
*p
= *pp
;
1340 if (add_parents_to_list(revs
, p
, &revs
->commits
) < 0)
1341 return rewrite_one_error
;
1342 if (p
->parents
&& p
->parents
->next
)
1343 return rewrite_one_ok
;
1344 if (p
->object
.flags
& (TREECHANGE
| UNINTERESTING
))
1345 return rewrite_one_ok
;
1347 return rewrite_one_noparents
;
1348 *pp
= p
->parents
->item
;
1352 static void remove_duplicate_parents(struct commit
*commit
)
1354 struct commit_list
**pp
, *p
;
1356 /* Examine existing parents while marking ones we have seen... */
1357 pp
= &commit
->parents
;
1358 while ((p
= *pp
) != NULL
) {
1359 struct commit
*parent
= p
->item
;
1360 if (parent
->object
.flags
& TMP_MARK
) {
1364 parent
->object
.flags
|= TMP_MARK
;
1367 /* ... and clear the temporary mark */
1368 for (p
= commit
->parents
; p
; p
= p
->next
)
1369 p
->item
->object
.flags
&= ~TMP_MARK
;
1372 static int rewrite_parents(struct rev_info
*revs
, struct commit
*commit
)
1374 struct commit_list
**pp
= &commit
->parents
;
1376 struct commit_list
*parent
= *pp
;
1377 switch (rewrite_one(revs
, &parent
->item
)) {
1378 case rewrite_one_ok
:
1380 case rewrite_one_noparents
:
1383 case rewrite_one_error
:
1388 remove_duplicate_parents(commit
);
1392 static int commit_match(struct commit
*commit
, struct rev_info
*opt
)
1394 if (!opt
->grep_filter
)
1396 return grep_buffer(opt
->grep_filter
,
1397 NULL
, /* we say nothing, not even filename */
1398 commit
->buffer
, strlen(commit
->buffer
));
1401 static struct commit
*get_revision_1(struct rev_info
*revs
)
1407 struct commit_list
*entry
= revs
->commits
;
1408 struct commit
*commit
= entry
->item
;
1410 revs
->commits
= entry
->next
;
1413 if (revs
->reflog_info
)
1414 fake_reflog_parent(revs
->reflog_info
, commit
);
1417 * If we haven't done the list limiting, we need to look at
1418 * the parents here. We also need to do the date-based limiting
1419 * that we'd otherwise have done in limit_list().
1421 if (!revs
->limited
) {
1422 if (revs
->max_age
!= -1 &&
1423 (commit
->date
< revs
->max_age
))
1425 if (add_parents_to_list(revs
, commit
, &revs
->commits
) < 0)
1428 if (commit
->object
.flags
& SHOWN
)
1431 if (revs
->unpacked
&& has_sha1_pack(commit
->object
.sha1
,
1432 revs
->ignore_packed
))
1435 if (commit
->object
.flags
& UNINTERESTING
)
1437 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
1439 if (revs
->no_merges
&&
1440 commit
->parents
&& commit
->parents
->next
)
1442 if (!commit_match(commit
, revs
))
1444 if (revs
->prune_fn
&& revs
->dense
) {
1445 /* Commit without changes? */
1446 if (!(commit
->object
.flags
& TREECHANGE
)) {
1447 /* drop merges unless we want parenthood */
1450 /* non-merge - always ignore it */
1451 if (!commit
->parents
|| !commit
->parents
->next
)
1454 if (revs
->parents
&& rewrite_parents(revs
, commit
) < 0)
1458 } while (revs
->commits
);
1462 static void gc_boundary(struct object_array
*array
)
1464 unsigned nr
= array
->nr
;
1465 unsigned alloc
= array
->alloc
;
1466 struct object_array_entry
*objects
= array
->objects
;
1470 for (i
= j
= 0; i
< nr
; i
++) {
1471 if (objects
[i
].item
->flags
& SHOWN
)
1474 objects
[j
] = objects
[i
];
1477 for (i
= j
; i
< nr
; i
++)
1478 objects
[i
].item
= NULL
;
1483 struct commit
*get_revision(struct rev_info
*revs
)
1485 struct commit
*c
= NULL
;
1486 struct commit_list
*l
;
1488 if (revs
->boundary
== 2) {
1490 struct object_array
*array
= &revs
->boundary_commits
;
1491 struct object_array_entry
*objects
= array
->objects
;
1492 for (i
= 0; i
< array
->nr
; i
++) {
1493 c
= (struct commit
*)(objects
[i
].item
);
1496 if (!(c
->object
.flags
& CHILD_SHOWN
))
1498 if (!(c
->object
.flags
& SHOWN
))
1504 c
->object
.flags
|= SHOWN
| BOUNDARY
;
1508 if (revs
->reverse
) {
1511 if (0 <= revs
->max_count
) {
1512 limit
= revs
->max_count
;
1513 if (0 < revs
->skip_count
)
1514 limit
+= revs
->skip_count
;
1517 while ((c
= get_revision_1(revs
))) {
1518 commit_list_insert(c
, &l
);
1519 if ((0 < limit
) && !--limit
)
1524 revs
->max_count
= -1;
1529 * Now pick up what they want to give us
1531 c
= get_revision_1(revs
);
1533 while (0 < revs
->skip_count
) {
1535 c
= get_revision_1(revs
);
1542 * Check the max_count.
1544 switch (revs
->max_count
) {
1555 c
->object
.flags
|= SHOWN
;
1557 if (!revs
->boundary
) {
1563 * get_revision_1() runs out the commits, and
1564 * we are done computing the boundaries.
1565 * switch to boundary commits output mode.
1568 return get_revision(revs
);
1572 * boundary commits are the commits that are parents of the
1573 * ones we got from get_revision_1() but they themselves are
1574 * not returned from get_revision_1(). Before returning
1575 * 'c', we need to mark its parents that they could be boundaries.
1578 for (l
= c
->parents
; l
; l
= l
->next
) {
1580 p
= &(l
->item
->object
);
1581 if (p
->flags
& (CHILD_SHOWN
| SHOWN
))
1583 p
->flags
|= CHILD_SHOWN
;
1584 gc_boundary(&revs
->boundary_commits
);
1585 add_object_array(p
, NULL
, &revs
->boundary_commits
);