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 add_pending_object_with_mode(revs
, obj
, name
, S_IFINVALID
);
122 void add_pending_object_with_mode(struct rev_info
*revs
, struct object
*obj
, const char *name
, unsigned mode
)
124 if (revs
->no_walk
&& (obj
->flags
& UNINTERESTING
))
125 die("object ranges do not make sense when not walking revisions");
126 add_object_array_with_mode(obj
, name
, &revs
->pending
, mode
);
127 if (revs
->reflog_info
&& obj
->type
== OBJ_COMMIT
)
128 add_reflog_for_walk(revs
->reflog_info
,
129 (struct commit
*)obj
, name
);
132 static struct object
*get_reference(struct rev_info
*revs
, const char *name
, const unsigned char *sha1
, unsigned int flags
)
134 struct object
*object
;
136 object
= parse_object(sha1
);
138 die("bad object %s", name
);
139 object
->flags
|= flags
;
143 static struct commit
*handle_commit(struct rev_info
*revs
, struct object
*object
, const char *name
)
145 unsigned long flags
= object
->flags
;
148 * Tag object? Look what it points to..
150 while (object
->type
== OBJ_TAG
) {
151 struct tag
*tag
= (struct tag
*) object
;
152 if (revs
->tag_objects
&& !(flags
& UNINTERESTING
))
153 add_pending_object(revs
, object
, tag
->tag
);
154 object
= parse_object(tag
->tagged
->sha1
);
156 die("bad object %s", sha1_to_hex(tag
->tagged
->sha1
));
160 * Commit object? Just return it, we'll do all the complex
163 if (object
->type
== OBJ_COMMIT
) {
164 struct commit
*commit
= (struct commit
*)object
;
165 if (parse_commit(commit
) < 0)
166 die("unable to parse commit %s", name
);
167 if (flags
& UNINTERESTING
) {
168 commit
->object
.flags
|= UNINTERESTING
;
169 mark_parents_uninteresting(commit
);
176 * Tree object? Either mark it uniniteresting, or add it
177 * to the list of objects to look at later..
179 if (object
->type
== OBJ_TREE
) {
180 struct tree
*tree
= (struct tree
*)object
;
181 if (!revs
->tree_objects
)
183 if (flags
& UNINTERESTING
) {
184 mark_tree_uninteresting(tree
);
187 add_pending_object(revs
, object
, "");
192 * Blob object? You know the drill by now..
194 if (object
->type
== OBJ_BLOB
) {
195 struct blob
*blob
= (struct blob
*)object
;
196 if (!revs
->blob_objects
)
198 if (flags
& UNINTERESTING
) {
199 mark_blob_uninteresting(blob
);
202 add_pending_object(revs
, object
, "");
205 die("%s is unknown object", name
);
208 static int everybody_uninteresting(struct commit_list
*orig
)
210 struct commit_list
*list
= orig
;
212 struct commit
*commit
= list
->item
;
214 if (commit
->object
.flags
& UNINTERESTING
)
222 * The goal is to get REV_TREE_NEW as the result only if the
223 * diff consists of all '+' (and no other changes), and
224 * REV_TREE_DIFFERENT otherwise (of course if the trees are
225 * the same we want REV_TREE_SAME). That means that once we
226 * get to REV_TREE_DIFFERENT, we do not have to look any further.
228 static int tree_difference
= REV_TREE_SAME
;
230 static void file_add_remove(struct diff_options
*options
,
231 int addremove
, unsigned mode
,
232 const unsigned char *sha1
,
233 const char *base
, const char *path
)
235 int diff
= REV_TREE_DIFFERENT
;
238 * Is it an add of a new file? It means that the old tree
239 * didn't have it at all, so we will turn "REV_TREE_SAME" ->
240 * "REV_TREE_NEW", but leave any "REV_TREE_DIFFERENT" alone
241 * (and if it already was "REV_TREE_NEW", we'll keep it
242 * "REV_TREE_NEW" of course).
244 if (addremove
== '+') {
245 diff
= tree_difference
;
246 if (diff
!= REV_TREE_SAME
)
250 tree_difference
= diff
;
251 if (tree_difference
== REV_TREE_DIFFERENT
)
252 options
->has_changes
= 1;
255 static void file_change(struct diff_options
*options
,
256 unsigned old_mode
, unsigned new_mode
,
257 const unsigned char *old_sha1
,
258 const unsigned char *new_sha1
,
259 const char *base
, const char *path
)
261 tree_difference
= REV_TREE_DIFFERENT
;
262 options
->has_changes
= 1;
265 int rev_compare_tree(struct rev_info
*revs
, struct tree
*t1
, struct tree
*t2
)
270 return REV_TREE_DIFFERENT
;
271 tree_difference
= REV_TREE_SAME
;
272 revs
->pruning
.has_changes
= 0;
273 if (diff_tree_sha1(t1
->object
.sha1
, t2
->object
.sha1
, "",
275 return REV_TREE_DIFFERENT
;
276 return tree_difference
;
279 int rev_same_tree_as_empty(struct rev_info
*revs
, struct tree
*t1
)
284 struct tree_desc empty
, real
;
289 tree
= read_object_with_reference(t1
->object
.sha1
, tree_type
, &size
, NULL
);
292 init_tree_desc(&real
, tree
, size
);
293 init_tree_desc(&empty
, "", 0);
295 tree_difference
= REV_TREE_SAME
;
296 revs
->pruning
.has_changes
= 0;
297 retval
= diff_tree(&empty
, &real
, "", &revs
->pruning
);
300 return retval
>= 0 && (tree_difference
== REV_TREE_SAME
);
303 static void try_to_simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
305 struct commit_list
**pp
, *parent
;
306 int tree_changed
= 0, tree_same
= 0;
311 if (!commit
->parents
) {
312 if (!rev_same_tree_as_empty(revs
, commit
->tree
))
313 commit
->object
.flags
|= TREECHANGE
;
317 pp
= &commit
->parents
;
318 while ((parent
= *pp
) != NULL
) {
319 struct commit
*p
= parent
->item
;
322 switch (rev_compare_tree(revs
, p
->tree
, commit
->tree
)) {
325 if (!revs
->simplify_history
|| (p
->object
.flags
& UNINTERESTING
)) {
326 /* Even if a merge with an uninteresting
327 * side branch brought the entire change
328 * we are interested in, we do not want
329 * to lose the other branches of this
330 * merge, so we just keep going.
336 commit
->parents
= parent
;
340 if (revs
->remove_empty_trees
&&
341 rev_same_tree_as_empty(revs
, p
->tree
)) {
342 /* We are adding all the specified
343 * paths from this parent, so the
344 * history beyond this parent is not
345 * interesting. Remove its parents
346 * (they are grandparents for us).
347 * IOW, we pretend this parent is a
354 case REV_TREE_DIFFERENT
:
359 die("bad tree compare for commit %s", sha1_to_hex(commit
->object
.sha1
));
361 if (tree_changed
&& !tree_same
)
362 commit
->object
.flags
|= TREECHANGE
;
365 static void add_parents_to_list(struct rev_info
*revs
, struct commit
*commit
, struct commit_list
**list
)
367 struct commit_list
*parent
= commit
->parents
;
371 if (commit
->object
.flags
& ADDED
)
373 commit
->object
.flags
|= ADDED
;
376 * If the commit is uninteresting, don't try to
377 * prune parents - we want the maximal uninteresting
380 * Normally we haven't parsed the parent
381 * yet, so we won't have a parent of a parent
382 * here. However, it may turn out that we've
383 * reached this commit some other way (where it
384 * wasn't uninteresting), in which case we need
385 * to mark its parents recursively too..
387 if (commit
->object
.flags
& UNINTERESTING
) {
389 struct commit
*p
= parent
->item
;
390 parent
= parent
->next
;
392 p
->object
.flags
|= UNINTERESTING
;
394 mark_parents_uninteresting(p
);
395 if (p
->object
.flags
& SEEN
)
397 p
->object
.flags
|= SEEN
;
398 insert_by_date(p
, list
);
404 * Ok, the commit wasn't uninteresting. Try to
405 * simplify the commit history and find the parent
406 * that has no differences in the path set if one exists.
409 revs
->prune_fn(revs
, commit
);
414 left_flag
= (commit
->object
.flags
& SYMMETRIC_LEFT
);
416 rest
= !revs
->first_parent_only
;
417 for (parent
= commit
->parents
, add
= 1; parent
; add
= rest
) {
418 struct commit
*p
= parent
->item
;
420 parent
= parent
->next
;
422 p
->object
.flags
|= left_flag
;
423 if (p
->object
.flags
& SEEN
)
425 p
->object
.flags
|= SEEN
;
427 insert_by_date(p
, list
);
431 static void cherry_pick_list(struct commit_list
*list
)
433 struct commit_list
*p
;
434 int left_count
= 0, right_count
= 0;
436 struct patch_ids ids
;
438 /* First count the commits on the left and on the right */
439 for (p
= list
; p
; p
= p
->next
) {
440 struct commit
*commit
= p
->item
;
441 unsigned flags
= commit
->object
.flags
;
442 if (flags
& BOUNDARY
)
444 else if (flags
& SYMMETRIC_LEFT
)
450 left_first
= left_count
< right_count
;
451 init_patch_ids(&ids
);
453 /* Compute patch-ids for one side */
454 for (p
= list
; p
; p
= p
->next
) {
455 struct commit
*commit
= p
->item
;
456 unsigned flags
= commit
->object
.flags
;
458 if (flags
& BOUNDARY
)
461 * If we have fewer left, left_first is set and we omit
462 * commits on the right branch in this loop. If we have
463 * fewer right, we skip the left ones.
465 if (left_first
!= !!(flags
& SYMMETRIC_LEFT
))
467 commit
->util
= add_commit_patch_id(commit
, &ids
);
470 /* Check the other side */
471 for (p
= list
; p
; p
= p
->next
) {
472 struct commit
*commit
= p
->item
;
474 unsigned flags
= commit
->object
.flags
;
476 if (flags
& BOUNDARY
)
479 * If we have fewer left, left_first is set and we omit
480 * commits on the left branch in this loop.
482 if (left_first
== !!(flags
& SYMMETRIC_LEFT
))
486 * Have we seen the same patch id?
488 id
= has_commit_patch_id(commit
, &ids
);
492 commit
->object
.flags
|= SHOWN
;
495 /* Now check the original side for seen ones */
496 for (p
= list
; p
; p
= p
->next
) {
497 struct commit
*commit
= p
->item
;
498 struct patch_id
*ent
;
504 commit
->object
.flags
|= SHOWN
;
508 free_patch_ids(&ids
);
511 static void limit_list(struct rev_info
*revs
)
513 struct commit_list
*list
= revs
->commits
;
514 struct commit_list
*newlist
= NULL
;
515 struct commit_list
**p
= &newlist
;
518 struct commit_list
*entry
= list
;
519 struct commit
*commit
= list
->item
;
520 struct object
*obj
= &commit
->object
;
525 if (revs
->max_age
!= -1 && (commit
->date
< revs
->max_age
))
526 obj
->flags
|= UNINTERESTING
;
527 add_parents_to_list(revs
, commit
, &list
);
528 if (obj
->flags
& UNINTERESTING
) {
529 mark_parents_uninteresting(commit
);
530 if (everybody_uninteresting(list
))
534 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
536 p
= &commit_list_insert(commit
, p
)->next
;
538 if (revs
->cherry_pick
)
539 cherry_pick_list(newlist
);
541 revs
->commits
= newlist
;
546 int warned_bad_reflog
;
547 struct rev_info
*all_revs
;
548 const char *name_for_errormsg
;
551 static int handle_one_ref(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
553 struct all_refs_cb
*cb
= cb_data
;
554 struct object
*object
= get_reference(cb
->all_revs
, path
, sha1
,
556 add_pending_object(cb
->all_revs
, object
, path
);
560 static void handle_all(struct rev_info
*revs
, unsigned flags
)
562 struct all_refs_cb cb
;
564 cb
.all_flags
= flags
;
565 for_each_ref(handle_one_ref
, &cb
);
568 static void handle_one_reflog_commit(unsigned char *sha1
, void *cb_data
)
570 struct all_refs_cb
*cb
= cb_data
;
571 if (!is_null_sha1(sha1
)) {
572 struct object
*o
= parse_object(sha1
);
574 o
->flags
|= cb
->all_flags
;
575 add_pending_object(cb
->all_revs
, o
, "");
577 else if (!cb
->warned_bad_reflog
) {
578 warning("reflog of '%s' references pruned commits",
579 cb
->name_for_errormsg
);
580 cb
->warned_bad_reflog
= 1;
585 static int handle_one_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
586 const char *email
, unsigned long timestamp
, int tz
,
587 const char *message
, void *cb_data
)
589 handle_one_reflog_commit(osha1
, cb_data
);
590 handle_one_reflog_commit(nsha1
, cb_data
);
594 static int handle_one_reflog(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
596 struct all_refs_cb
*cb
= cb_data
;
597 cb
->warned_bad_reflog
= 0;
598 cb
->name_for_errormsg
= path
;
599 for_each_reflog_ent(path
, handle_one_reflog_ent
, cb_data
);
603 static void handle_reflog(struct rev_info
*revs
, unsigned flags
)
605 struct all_refs_cb cb
;
607 cb
.all_flags
= flags
;
608 for_each_reflog(handle_one_reflog
, &cb
);
611 static int add_parents_only(struct rev_info
*revs
, const char *arg
, int flags
)
613 unsigned char sha1
[20];
615 struct commit
*commit
;
616 struct commit_list
*parents
;
619 flags
^= UNINTERESTING
;
622 if (get_sha1(arg
, sha1
))
625 it
= get_reference(revs
, arg
, sha1
, 0);
626 if (it
->type
!= OBJ_TAG
)
628 hashcpy(sha1
, ((struct tag
*)it
)->tagged
->sha1
);
630 if (it
->type
!= OBJ_COMMIT
)
632 commit
= (struct commit
*)it
;
633 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
634 it
= &parents
->item
->object
;
636 add_pending_object(revs
, it
, arg
);
641 void init_revisions(struct rev_info
*revs
, const char *prefix
)
643 memset(revs
, 0, sizeof(*revs
));
645 revs
->abbrev
= DEFAULT_ABBREV
;
646 revs
->ignore_merges
= 1;
647 revs
->simplify_history
= 1;
648 revs
->pruning
.recursive
= 1;
649 revs
->pruning
.quiet
= 1;
650 revs
->pruning
.add_remove
= file_add_remove
;
651 revs
->pruning
.change
= file_change
;
654 revs
->prefix
= prefix
;
657 revs
->skip_count
= -1;
658 revs
->max_count
= -1;
659 revs
->subject_prefix
= "PATCH";
661 revs
->prune_fn
= NULL
;
662 revs
->prune_data
= NULL
;
664 revs
->topo_setter
= topo_sort_default_setter
;
665 revs
->topo_getter
= topo_sort_default_getter
;
667 revs
->commit_format
= CMIT_FMT_DEFAULT
;
669 diff_setup(&revs
->diffopt
);
672 static void add_pending_commit_list(struct rev_info
*revs
,
673 struct commit_list
*commit_list
,
676 while (commit_list
) {
677 struct object
*object
= &commit_list
->item
->object
;
678 object
->flags
|= flags
;
679 add_pending_object(revs
, object
, sha1_to_hex(object
->sha1
));
680 commit_list
= commit_list
->next
;
684 static void prepare_show_merge(struct rev_info
*revs
)
686 struct commit_list
*bases
;
687 struct commit
*head
, *other
;
688 unsigned char sha1
[20];
689 const char **prune
= NULL
;
690 int i
, prune_num
= 1; /* counting terminating NULL */
692 if (get_sha1("HEAD", sha1
) || !(head
= lookup_commit(sha1
)))
693 die("--merge without HEAD?");
694 if (get_sha1("MERGE_HEAD", sha1
) || !(other
= lookup_commit(sha1
)))
695 die("--merge without MERGE_HEAD?");
696 add_pending_object(revs
, &head
->object
, "HEAD");
697 add_pending_object(revs
, &other
->object
, "MERGE_HEAD");
698 bases
= get_merge_bases(head
, other
, 1);
700 struct commit
*it
= bases
->item
;
701 struct commit_list
*n
= bases
->next
;
704 it
->object
.flags
|= UNINTERESTING
;
705 add_pending_object(revs
, &it
->object
, "(merge-base)");
710 for (i
= 0; i
< active_nr
; i
++) {
711 struct cache_entry
*ce
= active_cache
[i
];
714 if (ce_path_match(ce
, revs
->prune_data
)) {
716 prune
= xrealloc(prune
, sizeof(*prune
) * prune_num
);
717 prune
[prune_num
-2] = ce
->name
;
718 prune
[prune_num
-1] = NULL
;
720 while ((i
+1 < active_nr
) &&
721 ce_same_name(ce
, active_cache
[i
+1]))
724 revs
->prune_data
= prune
;
727 int handle_revision_arg(const char *arg
, struct rev_info
*revs
,
729 int cant_be_filename
)
733 struct object
*object
;
734 unsigned char sha1
[20];
737 dotdot
= strstr(arg
, "..");
739 unsigned char from_sha1
[20];
740 const char *next
= dotdot
+ 2;
741 const char *this = arg
;
742 int symmetric
= *next
== '.';
743 unsigned int flags_exclude
= flags
^ UNINTERESTING
;
752 if (!get_sha1(this, from_sha1
) &&
753 !get_sha1(next
, sha1
)) {
754 struct commit
*a
, *b
;
755 struct commit_list
*exclude
;
757 a
= lookup_commit_reference(from_sha1
);
758 b
= lookup_commit_reference(sha1
);
761 "Invalid symmetric difference expression %s...%s" :
762 "Invalid revision range %s..%s",
766 if (!cant_be_filename
) {
768 verify_non_filename(revs
->prefix
, arg
);
772 exclude
= get_merge_bases(a
, b
, 1);
773 add_pending_commit_list(revs
, exclude
,
775 free_commit_list(exclude
);
776 a
->object
.flags
|= flags
| SYMMETRIC_LEFT
;
778 a
->object
.flags
|= flags_exclude
;
779 b
->object
.flags
|= flags
;
780 add_pending_object(revs
, &a
->object
, this);
781 add_pending_object(revs
, &b
->object
, next
);
786 dotdot
= strstr(arg
, "^@");
787 if (dotdot
&& !dotdot
[2]) {
789 if (add_parents_only(revs
, arg
, flags
))
793 dotdot
= strstr(arg
, "^!");
794 if (dotdot
&& !dotdot
[2]) {
796 if (!add_parents_only(revs
, arg
, flags
^ UNINTERESTING
))
802 local_flags
= UNINTERESTING
;
805 if (get_sha1_with_mode(arg
, sha1
, &mode
))
807 if (!cant_be_filename
)
808 verify_non_filename(revs
->prefix
, arg
);
809 object
= get_reference(revs
, arg
, sha1
, flags
^ local_flags
);
810 add_pending_object_with_mode(revs
, object
, arg
, mode
);
814 static void add_grep(struct rev_info
*revs
, const char *ptn
, enum grep_pat_token what
)
816 if (!revs
->grep_filter
) {
817 struct grep_opt
*opt
= xcalloc(1, sizeof(*opt
));
818 opt
->status_only
= 1;
819 opt
->pattern_tail
= &(opt
->pattern_list
);
820 opt
->regflags
= REG_NEWLINE
;
821 revs
->grep_filter
= opt
;
823 append_grep_pattern(revs
->grep_filter
, ptn
,
824 "command line", 0, what
);
827 static void add_header_grep(struct rev_info
*revs
, const char *field
, const char *pattern
)
833 fldlen
= strlen(field
);
834 patlen
= strlen(pattern
);
835 pat
= xmalloc(patlen
+ fldlen
+ 10);
837 if (*pattern
== '^') {
841 sprintf(pat
, "^%s %s%s", field
, prefix
, pattern
);
842 add_grep(revs
, pat
, GREP_PATTERN_HEAD
);
845 static void add_message_grep(struct rev_info
*revs
, const char *pattern
)
847 add_grep(revs
, pattern
, GREP_PATTERN_BODY
);
850 static void add_ignore_packed(struct rev_info
*revs
, const char *name
)
852 int num
= ++revs
->num_ignore_packed
;
854 revs
->ignore_packed
= xrealloc(revs
->ignore_packed
,
855 sizeof(const char **) * (num
+ 1));
856 revs
->ignore_packed
[num
-1] = name
;
857 revs
->ignore_packed
[num
] = NULL
;
861 * Parse revision information, filling in the "rev_info" structure,
862 * and removing the used arguments from the argument list.
864 * Returns the number of arguments left that weren't recognized
865 * (which are also moved to the head of the argument list)
867 int setup_revisions(int argc
, const char **argv
, struct rev_info
*revs
, const char *def
)
869 int i
, flags
, seen_dashdash
, show_merge
;
870 const char **unrecognized
= argv
+ 1;
874 /* First, search for "--" */
876 for (i
= 1; i
< argc
; i
++) {
877 const char *arg
= argv
[i
];
878 if (strcmp(arg
, "--"))
882 revs
->prune_data
= get_pathspec(revs
->prefix
, argv
+ i
+ 1);
887 flags
= show_merge
= 0;
888 for (i
= 1; i
< argc
; i
++) {
889 const char *arg
= argv
[i
];
892 if (!prefixcmp(arg
, "--max-count=")) {
893 revs
->max_count
= atoi(arg
+ 12);
896 if (!prefixcmp(arg
, "--skip=")) {
897 revs
->skip_count
= atoi(arg
+ 7);
900 /* accept -<digit>, like traditional "head" */
901 if ((*arg
== '-') && isdigit(arg
[1])) {
902 revs
->max_count
= atoi(arg
+ 1);
905 if (!strcmp(arg
, "-n")) {
907 die("-n requires an argument");
908 revs
->max_count
= atoi(argv
[++i
]);
911 if (!prefixcmp(arg
, "-n")) {
912 revs
->max_count
= atoi(arg
+ 2);
915 if (!prefixcmp(arg
, "--max-age=")) {
916 revs
->max_age
= atoi(arg
+ 10);
919 if (!prefixcmp(arg
, "--since=")) {
920 revs
->max_age
= approxidate(arg
+ 8);
923 if (!prefixcmp(arg
, "--after=")) {
924 revs
->max_age
= approxidate(arg
+ 8);
927 if (!prefixcmp(arg
, "--min-age=")) {
928 revs
->min_age
= atoi(arg
+ 10);
931 if (!prefixcmp(arg
, "--before=")) {
932 revs
->min_age
= approxidate(arg
+ 9);
935 if (!prefixcmp(arg
, "--until=")) {
936 revs
->min_age
= approxidate(arg
+ 8);
939 if (!strcmp(arg
, "--all")) {
940 handle_all(revs
, flags
);
943 if (!strcmp(arg
, "--first-parent")) {
944 revs
->first_parent_only
= 1;
947 if (!strcmp(arg
, "--reflog")) {
948 handle_reflog(revs
, flags
);
951 if (!strcmp(arg
, "-g") ||
952 !strcmp(arg
, "--walk-reflogs")) {
953 init_reflog_walk(&revs
->reflog_info
);
956 if (!strcmp(arg
, "--not")) {
957 flags
^= UNINTERESTING
;
960 if (!strcmp(arg
, "--default")) {
962 die("bad --default argument");
966 if (!strcmp(arg
, "--merge")) {
970 if (!strcmp(arg
, "--topo-order")) {
971 revs
->topo_order
= 1;
974 if (!strcmp(arg
, "--date-order")) {
976 revs
->topo_order
= 1;
979 if (!strcmp(arg
, "--parents")) {
983 if (!strcmp(arg
, "--dense")) {
987 if (!strcmp(arg
, "--sparse")) {
991 if (!strcmp(arg
, "--remove-empty")) {
992 revs
->remove_empty_trees
= 1;
995 if (!strcmp(arg
, "--no-merges")) {
999 if (!strcmp(arg
, "--boundary")) {
1003 if (!strcmp(arg
, "--left-right")) {
1004 revs
->left_right
= 1;
1007 if (!strcmp(arg
, "--cherry-pick")) {
1008 revs
->cherry_pick
= 1;
1011 if (!strcmp(arg
, "--objects")) {
1012 revs
->tag_objects
= 1;
1013 revs
->tree_objects
= 1;
1014 revs
->blob_objects
= 1;
1017 if (!strcmp(arg
, "--objects-edge")) {
1018 revs
->tag_objects
= 1;
1019 revs
->tree_objects
= 1;
1020 revs
->blob_objects
= 1;
1021 revs
->edge_hint
= 1;
1024 if (!strcmp(arg
, "--unpacked")) {
1026 free(revs
->ignore_packed
);
1027 revs
->ignore_packed
= NULL
;
1028 revs
->num_ignore_packed
= 0;
1031 if (!prefixcmp(arg
, "--unpacked=")) {
1033 add_ignore_packed(revs
, arg
+11);
1036 if (!strcmp(arg
, "-r")) {
1038 revs
->diffopt
.recursive
= 1;
1041 if (!strcmp(arg
, "-t")) {
1043 revs
->diffopt
.recursive
= 1;
1044 revs
->diffopt
.tree_in_recursive
= 1;
1047 if (!strcmp(arg
, "-m")) {
1048 revs
->ignore_merges
= 0;
1051 if (!strcmp(arg
, "-c")) {
1053 revs
->dense_combined_merges
= 0;
1054 revs
->combine_merges
= 1;
1057 if (!strcmp(arg
, "--cc")) {
1059 revs
->dense_combined_merges
= 1;
1060 revs
->combine_merges
= 1;
1063 if (!strcmp(arg
, "-v")) {
1064 revs
->verbose_header
= 1;
1067 if (!prefixcmp(arg
, "--pretty")) {
1068 revs
->verbose_header
= 1;
1069 revs
->commit_format
= get_commit_format(arg
+8);
1072 if (!strcmp(arg
, "--root")) {
1073 revs
->show_root_diff
= 1;
1076 if (!strcmp(arg
, "--no-commit-id")) {
1077 revs
->no_commit_id
= 1;
1080 if (!strcmp(arg
, "--always")) {
1081 revs
->always_show_header
= 1;
1084 if (!strcmp(arg
, "--no-abbrev")) {
1088 if (!strcmp(arg
, "--abbrev")) {
1089 revs
->abbrev
= DEFAULT_ABBREV
;
1092 if (!prefixcmp(arg
, "--abbrev=")) {
1093 revs
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
1094 if (revs
->abbrev
< MINIMUM_ABBREV
)
1095 revs
->abbrev
= MINIMUM_ABBREV
;
1096 else if (revs
->abbrev
> 40)
1100 if (!strcmp(arg
, "--abbrev-commit")) {
1101 revs
->abbrev_commit
= 1;
1104 if (!strcmp(arg
, "--full-diff")) {
1106 revs
->full_diff
= 1;
1109 if (!strcmp(arg
, "--full-history")) {
1110 revs
->simplify_history
= 0;
1113 if (!strcmp(arg
, "--relative-date")) {
1114 revs
->date_mode
= DATE_RELATIVE
;
1117 if (!strncmp(arg
, "--date=", 7)) {
1118 if (!strcmp(arg
+ 7, "relative"))
1119 revs
->date_mode
= DATE_RELATIVE
;
1120 else if (!strcmp(arg
+ 7, "local"))
1121 revs
->date_mode
= DATE_LOCAL
;
1122 else if (!strcmp(arg
+ 7, "default"))
1123 revs
->date_mode
= DATE_NORMAL
;
1125 die("unknown date format %s", arg
);
1130 * Grepping the commit log
1132 if (!prefixcmp(arg
, "--author=")) {
1133 add_header_grep(revs
, "author", arg
+9);
1136 if (!prefixcmp(arg
, "--committer=")) {
1137 add_header_grep(revs
, "committer", arg
+12);
1140 if (!prefixcmp(arg
, "--grep=")) {
1141 add_message_grep(revs
, arg
+7);
1144 if (!strcmp(arg
, "--all-match")) {
1148 if (!prefixcmp(arg
, "--encoding=")) {
1150 if (strcmp(arg
, "none"))
1151 git_log_output_encoding
= xstrdup(arg
);
1153 git_log_output_encoding
= "";
1156 if (!strcmp(arg
, "--reverse")) {
1161 opts
= diff_opt_parse(&revs
->diffopt
, argv
+i
, argc
-i
);
1167 *unrecognized
++ = arg
;
1172 if (handle_revision_arg(arg
, revs
, flags
, seen_dashdash
)) {
1174 if (seen_dashdash
|| *arg
== '^')
1175 die("bad revision '%s'", arg
);
1177 /* If we didn't have a "--":
1178 * (1) all filenames must exist;
1179 * (2) all rev-args must not be interpretable
1180 * as a valid filename.
1181 * but the latter we have checked in the main loop.
1183 for (j
= i
; j
< argc
; j
++)
1184 verify_filename(revs
->prefix
, argv
[j
]);
1186 revs
->prune_data
= get_pathspec(revs
->prefix
,
1193 prepare_show_merge(revs
);
1194 if (def
&& !revs
->pending
.nr
) {
1195 unsigned char sha1
[20];
1196 struct object
*object
;
1198 if (get_sha1_with_mode(def
, sha1
, &mode
))
1199 die("bad default revision '%s'", def
);
1200 object
= get_reference(revs
, def
, sha1
, 0);
1201 add_pending_object_with_mode(revs
, object
, def
, mode
);
1204 if (revs
->topo_order
)
1207 if (revs
->prune_data
) {
1208 diff_tree_setup_paths(revs
->prune_data
, &revs
->pruning
);
1209 revs
->prune_fn
= try_to_simplify_commit
;
1210 if (!revs
->full_diff
)
1211 diff_tree_setup_paths(revs
->prune_data
, &revs
->diffopt
);
1213 if (revs
->combine_merges
) {
1214 revs
->ignore_merges
= 0;
1215 if (revs
->dense_combined_merges
&& !revs
->diffopt
.output_format
)
1216 revs
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
1218 revs
->diffopt
.abbrev
= revs
->abbrev
;
1219 if (diff_setup_done(&revs
->diffopt
) < 0)
1220 die("diff_setup_done failed");
1222 if (revs
->grep_filter
) {
1223 revs
->grep_filter
->all_match
= all_match
;
1224 compile_grep_patterns(revs
->grep_filter
);
1230 void prepare_revision_walk(struct rev_info
*revs
)
1232 int nr
= revs
->pending
.nr
;
1233 struct object_array_entry
*e
, *list
;
1235 e
= list
= revs
->pending
.objects
;
1236 revs
->pending
.nr
= 0;
1237 revs
->pending
.alloc
= 0;
1238 revs
->pending
.objects
= NULL
;
1240 struct commit
*commit
= handle_commit(revs
, e
->item
, e
->name
);
1242 if (!(commit
->object
.flags
& SEEN
)) {
1243 commit
->object
.flags
|= SEEN
;
1244 insert_by_date(commit
, &revs
->commits
);
1255 if (revs
->topo_order
)
1256 sort_in_topological_order_fn(&revs
->commits
, revs
->lifo
,
1261 static int rewrite_one(struct rev_info
*revs
, struct commit
**pp
)
1264 struct commit
*p
= *pp
;
1266 add_parents_to_list(revs
, p
, &revs
->commits
);
1267 if (p
->parents
&& p
->parents
->next
)
1269 if (p
->object
.flags
& (TREECHANGE
| UNINTERESTING
))
1273 *pp
= p
->parents
->item
;
1277 static void rewrite_parents(struct rev_info
*revs
, struct commit
*commit
)
1279 struct commit_list
**pp
= &commit
->parents
;
1281 struct commit_list
*parent
= *pp
;
1282 if (rewrite_one(revs
, &parent
->item
) < 0) {
1290 static int commit_match(struct commit
*commit
, struct rev_info
*opt
)
1292 if (!opt
->grep_filter
)
1294 return grep_buffer(opt
->grep_filter
,
1295 NULL
, /* we say nothing, not even filename */
1296 commit
->buffer
, strlen(commit
->buffer
));
1299 static struct commit
*get_revision_1(struct rev_info
*revs
)
1305 struct commit_list
*entry
= revs
->commits
;
1306 struct commit
*commit
= entry
->item
;
1308 revs
->commits
= entry
->next
;
1311 if (revs
->reflog_info
)
1312 fake_reflog_parent(revs
->reflog_info
, commit
);
1315 * If we haven't done the list limiting, we need to look at
1316 * the parents here. We also need to do the date-based limiting
1317 * that we'd otherwise have done in limit_list().
1319 if (!revs
->limited
) {
1320 if (revs
->max_age
!= -1 &&
1321 (commit
->date
< revs
->max_age
))
1323 add_parents_to_list(revs
, commit
, &revs
->commits
);
1325 if (commit
->object
.flags
& SHOWN
)
1328 if (revs
->unpacked
&& has_sha1_pack(commit
->object
.sha1
,
1329 revs
->ignore_packed
))
1332 if (commit
->object
.flags
& UNINTERESTING
)
1334 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
1336 if (revs
->no_merges
&&
1337 commit
->parents
&& commit
->parents
->next
)
1339 if (!commit_match(commit
, revs
))
1341 if (revs
->prune_fn
&& revs
->dense
) {
1342 /* Commit without changes? */
1343 if (!(commit
->object
.flags
& TREECHANGE
)) {
1344 /* drop merges unless we want parenthood */
1347 /* non-merge - always ignore it */
1348 if (!commit
->parents
|| !commit
->parents
->next
)
1352 rewrite_parents(revs
, commit
);
1355 } while (revs
->commits
);
1359 static void gc_boundary(struct object_array
*array
)
1361 unsigned nr
= array
->nr
;
1362 unsigned alloc
= array
->alloc
;
1363 struct object_array_entry
*objects
= array
->objects
;
1367 for (i
= j
= 0; i
< nr
; i
++) {
1368 if (objects
[i
].item
->flags
& SHOWN
)
1371 objects
[j
] = objects
[i
];
1374 for (i
= j
; i
< nr
; i
++)
1375 objects
[i
].item
= NULL
;
1380 struct commit
*get_revision(struct rev_info
*revs
)
1382 struct commit
*c
= NULL
;
1383 struct commit_list
*l
;
1385 if (revs
->boundary
== 2) {
1387 struct object_array
*array
= &revs
->boundary_commits
;
1388 struct object_array_entry
*objects
= array
->objects
;
1389 for (i
= 0; i
< array
->nr
; i
++) {
1390 c
= (struct commit
*)(objects
[i
].item
);
1393 if (!(c
->object
.flags
& CHILD_SHOWN
))
1395 if (!(c
->object
.flags
& SHOWN
))
1401 c
->object
.flags
|= SHOWN
| BOUNDARY
;
1405 if (revs
->reverse
) {
1408 if (0 <= revs
->max_count
) {
1409 limit
= revs
->max_count
;
1410 if (0 < revs
->skip_count
)
1411 limit
+= revs
->skip_count
;
1414 while ((c
= get_revision_1(revs
))) {
1415 commit_list_insert(c
, &l
);
1416 if ((0 < limit
) && !--limit
)
1421 revs
->max_count
= -1;
1426 * Now pick up what they want to give us
1428 c
= get_revision_1(revs
);
1430 while (0 < revs
->skip_count
) {
1432 c
= get_revision_1(revs
);
1439 * Check the max_count.
1441 switch (revs
->max_count
) {
1452 c
->object
.flags
|= SHOWN
;
1454 if (!revs
->boundary
) {
1460 * get_revision_1() runs out the commits, and
1461 * we are done computing the boundaries.
1462 * switch to boundary commits output mode.
1465 return get_revision(revs
);
1469 * boundary commits are the commits that are parents of the
1470 * ones we got from get_revision_1() but they themselves are
1471 * not returned from get_revision_1(). Before returning
1472 * 'c', we need to mark its parents that they could be boundaries.
1475 for (l
= c
->parents
; l
; l
= l
->next
) {
1477 p
= &(l
->item
->object
);
1478 if (p
->flags
& (CHILD_SHOWN
| SHOWN
))
1480 p
->flags
|= CHILD_SHOWN
;
1481 gc_boundary(&revs
->boundary_commits
);
1482 add_object_array(p
, NULL
, &revs
->boundary_commits
);