10 #include "reflog-walk.h"
12 static char *path_name(struct name_path
*path
, const char *name
)
16 int nlen
= strlen(name
);
19 for (p
= path
; p
; p
= p
->up
) {
21 len
+= p
->elem_len
+ 1;
24 m
= n
+ len
- (nlen
+ 1);
26 for (p
= path
; p
; p
= p
->up
) {
29 memcpy(m
, p
->elem
, p
->elem_len
);
36 void add_object(struct object
*obj
,
37 struct object_array
*p
,
38 struct name_path
*path
,
41 add_object_array(obj
, path_name(path
, name
), p
);
44 static void mark_blob_uninteresting(struct blob
*blob
)
46 if (blob
->object
.flags
& UNINTERESTING
)
48 blob
->object
.flags
|= UNINTERESTING
;
51 void mark_tree_uninteresting(struct tree
*tree
)
53 struct tree_desc desc
;
54 struct name_entry entry
;
55 struct object
*obj
= &tree
->object
;
57 if (obj
->flags
& UNINTERESTING
)
59 obj
->flags
|= UNINTERESTING
;
60 if (!has_sha1_file(obj
->sha1
))
62 if (parse_tree(tree
) < 0)
63 die("bad tree %s", sha1_to_hex(obj
->sha1
));
65 desc
.buf
= tree
->buffer
;
66 desc
.size
= tree
->size
;
67 while (tree_entry(&desc
, &entry
)) {
68 if (S_ISDIR(entry
.mode
))
69 mark_tree_uninteresting(lookup_tree(entry
.sha1
));
71 mark_blob_uninteresting(lookup_blob(entry
.sha1
));
75 * We don't care about the tree any more
76 * after it has been marked uninteresting.
82 void mark_parents_uninteresting(struct commit
*commit
)
84 struct commit_list
*parents
= commit
->parents
;
87 struct commit
*commit
= parents
->item
;
88 if (!(commit
->object
.flags
& UNINTERESTING
)) {
89 commit
->object
.flags
|= UNINTERESTING
;
92 * Normally we haven't parsed the parent
93 * yet, so we won't have a parent of a parent
94 * here. However, it may turn out that we've
95 * reached this commit some other way (where it
96 * wasn't uninteresting), in which case we need
97 * to mark its parents recursively too..
100 mark_parents_uninteresting(commit
);
104 * A missing commit is ok iff its parent is marked
107 * We just mark such a thing parsed, so that when
108 * it is popped next time around, we won't be trying
109 * to parse it and get an error.
111 if (!has_sha1_file(commit
->object
.sha1
))
112 commit
->object
.parsed
= 1;
113 parents
= parents
->next
;
117 void add_pending_object(struct rev_info
*revs
, struct object
*obj
, const char *name
)
119 if (revs
->no_walk
&& (obj
->flags
& UNINTERESTING
))
120 die("object ranges do not make sense when not walking revisions");
121 add_object_array(obj
, name
, &revs
->pending
);
122 if (revs
->reflog_info
&& obj
->type
== OBJ_COMMIT
)
123 add_reflog_for_walk(revs
->reflog_info
,
124 (struct commit
*)obj
, name
);
127 static struct object
*get_reference(struct rev_info
*revs
, const char *name
, const unsigned char *sha1
, unsigned int flags
)
129 struct object
*object
;
131 object
= parse_object(sha1
);
133 die("bad object %s", name
);
134 object
->flags
|= flags
;
138 static struct commit
*handle_commit(struct rev_info
*revs
, struct object
*object
, const char *name
)
140 unsigned long flags
= object
->flags
;
143 * Tag object? Look what it points to..
145 while (object
->type
== OBJ_TAG
) {
146 struct tag
*tag
= (struct tag
*) object
;
147 if (revs
->tag_objects
&& !(flags
& UNINTERESTING
))
148 add_pending_object(revs
, object
, tag
->tag
);
149 object
= parse_object(tag
->tagged
->sha1
);
151 die("bad object %s", sha1_to_hex(tag
->tagged
->sha1
));
155 * Commit object? Just return it, we'll do all the complex
158 if (object
->type
== OBJ_COMMIT
) {
159 struct commit
*commit
= (struct commit
*)object
;
160 if (parse_commit(commit
) < 0)
161 die("unable to parse commit %s", name
);
162 if (flags
& UNINTERESTING
) {
163 commit
->object
.flags
|= UNINTERESTING
;
164 mark_parents_uninteresting(commit
);
171 * Tree object? Either mark it uniniteresting, or add it
172 * to the list of objects to look at later..
174 if (object
->type
== OBJ_TREE
) {
175 struct tree
*tree
= (struct tree
*)object
;
176 if (!revs
->tree_objects
)
178 if (flags
& UNINTERESTING
) {
179 mark_tree_uninteresting(tree
);
182 add_pending_object(revs
, object
, "");
187 * Blob object? You know the drill by now..
189 if (object
->type
== OBJ_BLOB
) {
190 struct blob
*blob
= (struct blob
*)object
;
191 if (!revs
->blob_objects
)
193 if (flags
& UNINTERESTING
) {
194 mark_blob_uninteresting(blob
);
197 add_pending_object(revs
, object
, "");
200 die("%s is unknown object", name
);
203 static int everybody_uninteresting(struct commit_list
*orig
)
205 struct commit_list
*list
= orig
;
207 struct commit
*commit
= list
->item
;
209 if (commit
->object
.flags
& UNINTERESTING
)
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
;
248 static void file_change(struct diff_options
*options
,
249 unsigned old_mode
, unsigned new_mode
,
250 const unsigned char *old_sha1
,
251 const unsigned char *new_sha1
,
252 const char *base
, const char *path
)
254 tree_difference
= REV_TREE_DIFFERENT
;
257 int rev_compare_tree(struct rev_info
*revs
, struct tree
*t1
, struct tree
*t2
)
262 return REV_TREE_DIFFERENT
;
263 tree_difference
= REV_TREE_SAME
;
264 if (diff_tree_sha1(t1
->object
.sha1
, t2
->object
.sha1
, "",
266 return REV_TREE_DIFFERENT
;
267 return tree_difference
;
270 int rev_same_tree_as_empty(struct rev_info
*revs
, struct tree
*t1
)
274 struct tree_desc empty
, real
;
279 tree
= read_object_with_reference(t1
->object
.sha1
, tree_type
, &real
.size
, NULL
);
287 tree_difference
= REV_TREE_SAME
;
288 retval
= diff_tree(&empty
, &real
, "", &revs
->pruning
);
291 return retval
>= 0 && (tree_difference
== REV_TREE_SAME
);
294 static void try_to_simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
296 struct commit_list
**pp
, *parent
;
297 int tree_changed
= 0, tree_same
= 0;
302 if (!commit
->parents
) {
303 if (!rev_same_tree_as_empty(revs
, commit
->tree
))
304 commit
->object
.flags
|= TREECHANGE
;
308 pp
= &commit
->parents
;
309 while ((parent
= *pp
) != NULL
) {
310 struct commit
*p
= parent
->item
;
313 switch (rev_compare_tree(revs
, p
->tree
, commit
->tree
)) {
316 if (!revs
->simplify_history
|| (p
->object
.flags
& UNINTERESTING
)) {
317 /* Even if a merge with an uninteresting
318 * side branch brought the entire change
319 * we are interested in, we do not want
320 * to lose the other branches of this
321 * merge, so we just keep going.
327 commit
->parents
= parent
;
331 if (revs
->remove_empty_trees
&&
332 rev_same_tree_as_empty(revs
, p
->tree
)) {
333 /* We are adding all the specified
334 * paths from this parent, so the
335 * history beyond this parent is not
336 * interesting. Remove its parents
337 * (they are grandparents for us).
338 * IOW, we pretend this parent is a
345 case REV_TREE_DIFFERENT
:
350 die("bad tree compare for commit %s", sha1_to_hex(commit
->object
.sha1
));
352 if (tree_changed
&& !tree_same
)
353 commit
->object
.flags
|= TREECHANGE
;
356 static void add_parents_to_list(struct rev_info
*revs
, struct commit
*commit
, struct commit_list
**list
)
358 struct commit_list
*parent
= commit
->parents
;
361 if (commit
->object
.flags
& ADDED
)
363 commit
->object
.flags
|= ADDED
;
366 * If the commit is uninteresting, don't try to
367 * prune parents - we want the maximal uninteresting
370 * Normally we haven't parsed the parent
371 * yet, so we won't have a parent of a parent
372 * here. However, it may turn out that we've
373 * reached this commit some other way (where it
374 * wasn't uninteresting), in which case we need
375 * to mark its parents recursively too..
377 if (commit
->object
.flags
& UNINTERESTING
) {
379 struct commit
*p
= parent
->item
;
380 parent
= parent
->next
;
382 p
->object
.flags
|= UNINTERESTING
;
384 mark_parents_uninteresting(p
);
385 if (p
->object
.flags
& SEEN
)
387 p
->object
.flags
|= SEEN
;
388 insert_by_date(p
, list
);
394 * Ok, the commit wasn't uninteresting. Try to
395 * simplify the commit history and find the parent
396 * that has no differences in the path set if one exists.
399 revs
->prune_fn(revs
, commit
);
404 left_flag
= (commit
->object
.flags
& SYMMETRIC_LEFT
);
405 parent
= commit
->parents
;
407 struct commit
*p
= parent
->item
;
409 parent
= parent
->next
;
412 p
->object
.flags
|= left_flag
;
413 if (p
->object
.flags
& SEEN
)
415 p
->object
.flags
|= SEEN
;
416 insert_by_date(p
, list
);
420 static void limit_list(struct rev_info
*revs
)
422 struct commit_list
*list
= revs
->commits
;
423 struct commit_list
*newlist
= NULL
;
424 struct commit_list
**p
= &newlist
;
427 struct commit_list
*entry
= list
;
428 struct commit
*commit
= list
->item
;
429 struct object
*obj
= &commit
->object
;
434 if (revs
->max_age
!= -1 && (commit
->date
< revs
->max_age
))
435 obj
->flags
|= UNINTERESTING
;
436 add_parents_to_list(revs
, commit
, &list
);
437 if (obj
->flags
& UNINTERESTING
) {
438 mark_parents_uninteresting(commit
);
439 if (everybody_uninteresting(list
))
443 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
445 p
= &commit_list_insert(commit
, p
)->next
;
447 revs
->commits
= newlist
;
452 int warned_bad_reflog
;
453 struct rev_info
*all_revs
;
454 const char *name_for_errormsg
;
457 static int handle_one_ref(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
459 struct all_refs_cb
*cb
= cb_data
;
460 struct object
*object
= get_reference(cb
->all_revs
, path
, sha1
,
462 add_pending_object(cb
->all_revs
, object
, path
);
466 static void handle_all(struct rev_info
*revs
, unsigned flags
)
468 struct all_refs_cb cb
;
470 cb
.all_flags
= flags
;
471 for_each_ref(handle_one_ref
, &cb
);
474 static void handle_one_reflog_commit(unsigned char *sha1
, void *cb_data
)
476 struct all_refs_cb
*cb
= cb_data
;
477 if (!is_null_sha1(sha1
)) {
478 struct object
*o
= parse_object(sha1
);
480 o
->flags
|= cb
->all_flags
;
481 add_pending_object(cb
->all_revs
, o
, "");
483 else if (!cb
->warned_bad_reflog
) {
484 warn("reflog of '%s' references pruned commits",
485 cb
->name_for_errormsg
);
486 cb
->warned_bad_reflog
= 1;
491 static int handle_one_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
492 const char *email
, unsigned long timestamp
, int tz
,
493 const char *message
, void *cb_data
)
495 handle_one_reflog_commit(osha1
, cb_data
);
496 handle_one_reflog_commit(nsha1
, cb_data
);
500 static int handle_one_reflog(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
502 struct all_refs_cb
*cb
= cb_data
;
503 cb
->warned_bad_reflog
= 0;
504 cb
->name_for_errormsg
= path
;
505 for_each_reflog_ent(path
, handle_one_reflog_ent
, cb_data
);
509 static void handle_reflog(struct rev_info
*revs
, unsigned flags
)
511 struct all_refs_cb cb
;
513 cb
.all_flags
= flags
;
514 for_each_reflog(handle_one_reflog
, &cb
);
517 static int add_parents_only(struct rev_info
*revs
, const char *arg
, int flags
)
519 unsigned char sha1
[20];
521 struct commit
*commit
;
522 struct commit_list
*parents
;
525 flags
^= UNINTERESTING
;
528 if (get_sha1(arg
, sha1
))
531 it
= get_reference(revs
, arg
, sha1
, 0);
532 if (it
->type
!= OBJ_TAG
)
534 hashcpy(sha1
, ((struct tag
*)it
)->tagged
->sha1
);
536 if (it
->type
!= OBJ_COMMIT
)
538 commit
= (struct commit
*)it
;
539 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
540 it
= &parents
->item
->object
;
542 add_pending_object(revs
, it
, arg
);
547 void init_revisions(struct rev_info
*revs
, const char *prefix
)
549 memset(revs
, 0, sizeof(*revs
));
551 revs
->abbrev
= DEFAULT_ABBREV
;
552 revs
->ignore_merges
= 1;
553 revs
->simplify_history
= 1;
554 revs
->pruning
.recursive
= 1;
555 revs
->pruning
.add_remove
= file_add_remove
;
556 revs
->pruning
.change
= file_change
;
559 revs
->prefix
= prefix
;
562 revs
->skip_count
= -1;
563 revs
->max_count
= -1;
565 revs
->prune_fn
= NULL
;
566 revs
->prune_data
= NULL
;
568 revs
->topo_setter
= topo_sort_default_setter
;
569 revs
->topo_getter
= topo_sort_default_getter
;
571 revs
->commit_format
= CMIT_FMT_DEFAULT
;
573 diff_setup(&revs
->diffopt
);
576 static void add_pending_commit_list(struct rev_info
*revs
,
577 struct commit_list
*commit_list
,
580 while (commit_list
) {
581 struct object
*object
= &commit_list
->item
->object
;
582 object
->flags
|= flags
;
583 add_pending_object(revs
, object
, sha1_to_hex(object
->sha1
));
584 commit_list
= commit_list
->next
;
588 static void prepare_show_merge(struct rev_info
*revs
)
590 struct commit_list
*bases
;
591 struct commit
*head
, *other
;
592 unsigned char sha1
[20];
593 const char **prune
= NULL
;
594 int i
, prune_num
= 1; /* counting terminating NULL */
596 if (get_sha1("HEAD", sha1
) || !(head
= lookup_commit(sha1
)))
597 die("--merge without HEAD?");
598 if (get_sha1("MERGE_HEAD", sha1
) || !(other
= lookup_commit(sha1
)))
599 die("--merge without MERGE_HEAD?");
600 add_pending_object(revs
, &head
->object
, "HEAD");
601 add_pending_object(revs
, &other
->object
, "MERGE_HEAD");
602 bases
= get_merge_bases(head
, other
, 1);
604 struct commit
*it
= bases
->item
;
605 struct commit_list
*n
= bases
->next
;
608 it
->object
.flags
|= UNINTERESTING
;
609 add_pending_object(revs
, &it
->object
, "(merge-base)");
614 for (i
= 0; i
< active_nr
; i
++) {
615 struct cache_entry
*ce
= active_cache
[i
];
618 if (ce_path_match(ce
, revs
->prune_data
)) {
620 prune
= xrealloc(prune
, sizeof(*prune
) * prune_num
);
621 prune
[prune_num
-2] = ce
->name
;
622 prune
[prune_num
-1] = NULL
;
624 while ((i
+1 < active_nr
) &&
625 ce_same_name(ce
, active_cache
[i
+1]))
628 revs
->prune_data
= prune
;
631 int handle_revision_arg(const char *arg
, struct rev_info
*revs
,
633 int cant_be_filename
)
636 struct object
*object
;
637 unsigned char sha1
[20];
640 dotdot
= strstr(arg
, "..");
642 unsigned char from_sha1
[20];
643 const char *next
= dotdot
+ 2;
644 const char *this = arg
;
645 int symmetric
= *next
== '.';
646 unsigned int flags_exclude
= flags
^ UNINTERESTING
;
655 if (!get_sha1(this, from_sha1
) &&
656 !get_sha1(next
, sha1
)) {
657 struct commit
*a
, *b
;
658 struct commit_list
*exclude
;
660 a
= lookup_commit_reference(from_sha1
);
661 b
= lookup_commit_reference(sha1
);
664 "Invalid symmetric difference expression %s...%s" :
665 "Invalid revision range %s..%s",
669 if (!cant_be_filename
) {
671 verify_non_filename(revs
->prefix
, arg
);
675 exclude
= get_merge_bases(a
, b
, 1);
676 add_pending_commit_list(revs
, exclude
,
678 free_commit_list(exclude
);
679 a
->object
.flags
|= flags
| SYMMETRIC_LEFT
;
681 a
->object
.flags
|= flags_exclude
;
682 b
->object
.flags
|= flags
;
683 add_pending_object(revs
, &a
->object
, this);
684 add_pending_object(revs
, &b
->object
, next
);
689 dotdot
= strstr(arg
, "^@");
690 if (dotdot
&& !dotdot
[2]) {
692 if (add_parents_only(revs
, arg
, flags
))
696 dotdot
= strstr(arg
, "^!");
697 if (dotdot
&& !dotdot
[2]) {
699 if (!add_parents_only(revs
, arg
, flags
^ UNINTERESTING
))
705 local_flags
= UNINTERESTING
;
708 if (get_sha1(arg
, sha1
))
710 if (!cant_be_filename
)
711 verify_non_filename(revs
->prefix
, arg
);
712 object
= get_reference(revs
, arg
, sha1
, flags
^ local_flags
);
713 add_pending_object(revs
, object
, arg
);
717 static void add_grep(struct rev_info
*revs
, const char *ptn
, enum grep_pat_token what
)
719 if (!revs
->grep_filter
) {
720 struct grep_opt
*opt
= xcalloc(1, sizeof(*opt
));
721 opt
->status_only
= 1;
722 opt
->pattern_tail
= &(opt
->pattern_list
);
723 opt
->regflags
= REG_NEWLINE
;
724 revs
->grep_filter
= opt
;
726 append_grep_pattern(revs
->grep_filter
, ptn
,
727 "command line", 0, what
);
730 static void add_header_grep(struct rev_info
*revs
, const char *field
, const char *pattern
)
736 fldlen
= strlen(field
);
737 patlen
= strlen(pattern
);
738 pat
= xmalloc(patlen
+ fldlen
+ 10);
740 if (*pattern
== '^') {
744 sprintf(pat
, "^%s %s%s", field
, prefix
, pattern
);
745 add_grep(revs
, pat
, GREP_PATTERN_HEAD
);
748 static void add_message_grep(struct rev_info
*revs
, const char *pattern
)
750 add_grep(revs
, pattern
, GREP_PATTERN_BODY
);
753 static void add_ignore_packed(struct rev_info
*revs
, const char *name
)
755 int num
= ++revs
->num_ignore_packed
;
757 revs
->ignore_packed
= xrealloc(revs
->ignore_packed
,
758 sizeof(const char **) * (num
+ 1));
759 revs
->ignore_packed
[num
-1] = name
;
760 revs
->ignore_packed
[num
] = NULL
;
764 * Parse revision information, filling in the "rev_info" structure,
765 * and removing the used arguments from the argument list.
767 * Returns the number of arguments left that weren't recognized
768 * (which are also moved to the head of the argument list)
770 int setup_revisions(int argc
, const char **argv
, struct rev_info
*revs
, const char *def
)
772 int i
, flags
, seen_dashdash
, show_merge
;
773 const char **unrecognized
= argv
+ 1;
777 /* First, search for "--" */
779 for (i
= 1; i
< argc
; i
++) {
780 const char *arg
= argv
[i
];
781 if (strcmp(arg
, "--"))
785 revs
->prune_data
= get_pathspec(revs
->prefix
, argv
+ i
+ 1);
790 flags
= show_merge
= 0;
791 for (i
= 1; i
< argc
; i
++) {
792 const char *arg
= argv
[i
];
795 if (!prefixcmp(arg
, "--max-count=")) {
796 revs
->max_count
= atoi(arg
+ 12);
799 if (!prefixcmp(arg
, "--skip=")) {
800 revs
->skip_count
= atoi(arg
+ 7);
803 /* accept -<digit>, like traditional "head" */
804 if ((*arg
== '-') && isdigit(arg
[1])) {
805 revs
->max_count
= atoi(arg
+ 1);
808 if (!strcmp(arg
, "-n")) {
810 die("-n requires an argument");
811 revs
->max_count
= atoi(argv
[++i
]);
814 if (!prefixcmp(arg
, "-n")) {
815 revs
->max_count
= atoi(arg
+ 2);
818 if (!prefixcmp(arg
, "--max-age=")) {
819 revs
->max_age
= atoi(arg
+ 10);
822 if (!prefixcmp(arg
, "--since=")) {
823 revs
->max_age
= approxidate(arg
+ 8);
826 if (!prefixcmp(arg
, "--after=")) {
827 revs
->max_age
= approxidate(arg
+ 8);
830 if (!prefixcmp(arg
, "--min-age=")) {
831 revs
->min_age
= atoi(arg
+ 10);
834 if (!prefixcmp(arg
, "--before=")) {
835 revs
->min_age
= approxidate(arg
+ 9);
838 if (!prefixcmp(arg
, "--until=")) {
839 revs
->min_age
= approxidate(arg
+ 8);
842 if (!strcmp(arg
, "--all")) {
843 handle_all(revs
, flags
);
846 if (!strcmp(arg
, "--reflog")) {
847 handle_reflog(revs
, flags
);
850 if (!strcmp(arg
, "-g") ||
851 !strcmp(arg
, "--walk-reflogs")) {
852 init_reflog_walk(&revs
->reflog_info
);
855 if (!strcmp(arg
, "--not")) {
856 flags
^= UNINTERESTING
;
859 if (!strcmp(arg
, "--default")) {
861 die("bad --default argument");
865 if (!strcmp(arg
, "--merge")) {
869 if (!strcmp(arg
, "--topo-order")) {
870 revs
->topo_order
= 1;
873 if (!strcmp(arg
, "--date-order")) {
875 revs
->topo_order
= 1;
878 if (!strcmp(arg
, "--parents")) {
882 if (!strcmp(arg
, "--dense")) {
886 if (!strcmp(arg
, "--sparse")) {
890 if (!strcmp(arg
, "--remove-empty")) {
891 revs
->remove_empty_trees
= 1;
894 if (!strcmp(arg
, "--no-merges")) {
898 if (!strcmp(arg
, "--boundary")) {
902 if (!strcmp(arg
, "--left-right")) {
903 revs
->left_right
= 1;
906 if (!strcmp(arg
, "--objects")) {
907 revs
->tag_objects
= 1;
908 revs
->tree_objects
= 1;
909 revs
->blob_objects
= 1;
912 if (!strcmp(arg
, "--objects-edge")) {
913 revs
->tag_objects
= 1;
914 revs
->tree_objects
= 1;
915 revs
->blob_objects
= 1;
919 if (!strcmp(arg
, "--unpacked")) {
921 free(revs
->ignore_packed
);
922 revs
->ignore_packed
= NULL
;
923 revs
->num_ignore_packed
= 0;
926 if (!prefixcmp(arg
, "--unpacked=")) {
928 add_ignore_packed(revs
, arg
+11);
931 if (!strcmp(arg
, "-r")) {
933 revs
->diffopt
.recursive
= 1;
936 if (!strcmp(arg
, "-t")) {
938 revs
->diffopt
.recursive
= 1;
939 revs
->diffopt
.tree_in_recursive
= 1;
942 if (!strcmp(arg
, "-m")) {
943 revs
->ignore_merges
= 0;
946 if (!strcmp(arg
, "-c")) {
948 revs
->dense_combined_merges
= 0;
949 revs
->combine_merges
= 1;
952 if (!strcmp(arg
, "--cc")) {
954 revs
->dense_combined_merges
= 1;
955 revs
->combine_merges
= 1;
958 if (!strcmp(arg
, "-v")) {
959 revs
->verbose_header
= 1;
962 if (!prefixcmp(arg
, "--pretty")) {
963 revs
->verbose_header
= 1;
964 revs
->commit_format
= get_commit_format(arg
+8);
967 if (!strcmp(arg
, "--root")) {
968 revs
->show_root_diff
= 1;
971 if (!strcmp(arg
, "--no-commit-id")) {
972 revs
->no_commit_id
= 1;
975 if (!strcmp(arg
, "--always")) {
976 revs
->always_show_header
= 1;
979 if (!strcmp(arg
, "--no-abbrev")) {
983 if (!strcmp(arg
, "--abbrev")) {
984 revs
->abbrev
= DEFAULT_ABBREV
;
987 if (!prefixcmp(arg
, "--abbrev=")) {
988 revs
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
989 if (revs
->abbrev
< MINIMUM_ABBREV
)
990 revs
->abbrev
= MINIMUM_ABBREV
;
991 else if (revs
->abbrev
> 40)
995 if (!strcmp(arg
, "--abbrev-commit")) {
996 revs
->abbrev_commit
= 1;
999 if (!strcmp(arg
, "--full-diff")) {
1001 revs
->full_diff
= 1;
1004 if (!strcmp(arg
, "--full-history")) {
1005 revs
->simplify_history
= 0;
1008 if (!strcmp(arg
, "--relative-date")) {
1009 revs
->relative_date
= 1;
1014 * Grepping the commit log
1016 if (!prefixcmp(arg
, "--author=")) {
1017 add_header_grep(revs
, "author", arg
+9);
1020 if (!prefixcmp(arg
, "--committer=")) {
1021 add_header_grep(revs
, "committer", arg
+12);
1024 if (!prefixcmp(arg
, "--grep=")) {
1025 add_message_grep(revs
, arg
+7);
1028 if (!strcmp(arg
, "--all-match")) {
1032 if (!prefixcmp(arg
, "--encoding=")) {
1034 if (strcmp(arg
, "none"))
1035 git_log_output_encoding
= strdup(arg
);
1037 git_log_output_encoding
= "";
1040 if (!strcmp(arg
, "--reverse")) {
1045 opts
= diff_opt_parse(&revs
->diffopt
, argv
+i
, argc
-i
);
1051 *unrecognized
++ = arg
;
1056 if (handle_revision_arg(arg
, revs
, flags
, seen_dashdash
)) {
1058 if (seen_dashdash
|| *arg
== '^')
1059 die("bad revision '%s'", arg
);
1061 /* If we didn't have a "--":
1062 * (1) all filenames must exist;
1063 * (2) all rev-args must not be interpretable
1064 * as a valid filename.
1065 * but the latter we have checked in the main loop.
1067 for (j
= i
; j
< argc
; j
++)
1068 verify_filename(revs
->prefix
, argv
[j
]);
1070 revs
->prune_data
= get_pathspec(revs
->prefix
,
1077 prepare_show_merge(revs
);
1078 if (def
&& !revs
->pending
.nr
) {
1079 unsigned char sha1
[20];
1080 struct object
*object
;
1081 if (get_sha1(def
, sha1
))
1082 die("bad default revision '%s'", def
);
1083 object
= get_reference(revs
, def
, sha1
, 0);
1084 add_pending_object(revs
, object
, def
);
1087 if (revs
->topo_order
)
1090 if (revs
->prune_data
) {
1091 diff_tree_setup_paths(revs
->prune_data
, &revs
->pruning
);
1092 revs
->prune_fn
= try_to_simplify_commit
;
1093 if (!revs
->full_diff
)
1094 diff_tree_setup_paths(revs
->prune_data
, &revs
->diffopt
);
1096 if (revs
->combine_merges
) {
1097 revs
->ignore_merges
= 0;
1098 if (revs
->dense_combined_merges
&& !revs
->diffopt
.output_format
)
1099 revs
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
1101 revs
->diffopt
.abbrev
= revs
->abbrev
;
1102 if (diff_setup_done(&revs
->diffopt
) < 0)
1103 die("diff_setup_done failed");
1105 if (revs
->grep_filter
) {
1106 revs
->grep_filter
->all_match
= all_match
;
1107 compile_grep_patterns(revs
->grep_filter
);
1113 void prepare_revision_walk(struct rev_info
*revs
)
1115 int nr
= revs
->pending
.nr
;
1116 struct object_array_entry
*e
, *list
;
1118 e
= list
= revs
->pending
.objects
;
1119 revs
->pending
.nr
= 0;
1120 revs
->pending
.alloc
= 0;
1121 revs
->pending
.objects
= NULL
;
1123 struct commit
*commit
= handle_commit(revs
, e
->item
, e
->name
);
1125 if (!(commit
->object
.flags
& SEEN
)) {
1126 commit
->object
.flags
|= SEEN
;
1127 insert_by_date(commit
, &revs
->commits
);
1138 if (revs
->topo_order
)
1139 sort_in_topological_order_fn(&revs
->commits
, revs
->lifo
,
1144 static int rewrite_one(struct rev_info
*revs
, struct commit
**pp
)
1147 struct commit
*p
= *pp
;
1149 add_parents_to_list(revs
, p
, &revs
->commits
);
1150 if (p
->parents
&& p
->parents
->next
)
1152 if (p
->object
.flags
& (TREECHANGE
| UNINTERESTING
))
1156 *pp
= p
->parents
->item
;
1160 static void rewrite_parents(struct rev_info
*revs
, struct commit
*commit
)
1162 struct commit_list
**pp
= &commit
->parents
;
1164 struct commit_list
*parent
= *pp
;
1165 if (rewrite_one(revs
, &parent
->item
) < 0) {
1173 static int commit_match(struct commit
*commit
, struct rev_info
*opt
)
1175 if (!opt
->grep_filter
)
1177 return grep_buffer(opt
->grep_filter
,
1178 NULL
, /* we say nothing, not even filename */
1179 commit
->buffer
, strlen(commit
->buffer
));
1182 static struct commit
*get_revision_1(struct rev_info
*revs
)
1188 struct commit_list
*entry
= revs
->commits
;
1189 struct commit
*commit
= entry
->item
;
1191 revs
->commits
= entry
->next
;
1194 if (revs
->reflog_info
)
1195 fake_reflog_parent(revs
->reflog_info
, commit
);
1198 * If we haven't done the list limiting, we need to look at
1199 * the parents here. We also need to do the date-based limiting
1200 * that we'd otherwise have done in limit_list().
1202 if (!revs
->limited
) {
1203 if (revs
->max_age
!= -1 &&
1204 (commit
->date
< revs
->max_age
))
1206 add_parents_to_list(revs
, commit
, &revs
->commits
);
1208 if (commit
->object
.flags
& SHOWN
)
1211 if (revs
->unpacked
&& has_sha1_pack(commit
->object
.sha1
,
1212 revs
->ignore_packed
))
1215 if (commit
->object
.flags
& UNINTERESTING
)
1217 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
1219 if (revs
->no_merges
&&
1220 commit
->parents
&& commit
->parents
->next
)
1222 if (!commit_match(commit
, revs
))
1224 if (revs
->prune_fn
&& revs
->dense
) {
1225 /* Commit without changes? */
1226 if (!(commit
->object
.flags
& TREECHANGE
)) {
1227 /* drop merges unless we want parenthood */
1230 /* non-merge - always ignore it */
1231 if (!commit
->parents
|| !commit
->parents
->next
)
1235 rewrite_parents(revs
, commit
);
1238 } while (revs
->commits
);
1242 static void gc_boundary(struct object_array
*array
)
1244 unsigned nr
= array
->nr
;
1245 unsigned alloc
= array
->alloc
;
1246 struct object_array_entry
*objects
= array
->objects
;
1250 for (i
= j
= 0; i
< nr
; i
++) {
1251 if (objects
[i
].item
->flags
& SHOWN
)
1254 objects
[j
] = objects
[i
];
1257 for (i
= j
; i
< nr
; i
++)
1258 objects
[i
].item
= NULL
;
1263 struct commit
*get_revision(struct rev_info
*revs
)
1265 struct commit
*c
= NULL
;
1266 struct commit_list
*l
;
1268 if (revs
->boundary
== 2) {
1270 struct object_array
*array
= &revs
->boundary_commits
;
1271 struct object_array_entry
*objects
= array
->objects
;
1272 for (i
= 0; i
< array
->nr
; i
++) {
1273 c
= (struct commit
*)(objects
[i
].item
);
1276 if (!(c
->object
.flags
& CHILD_SHOWN
))
1278 if (!(c
->object
.flags
& SHOWN
))
1284 c
->object
.flags
|= SHOWN
| BOUNDARY
;
1288 if (revs
->reverse
) {
1291 if (0 <= revs
->max_count
) {
1292 limit
= revs
->max_count
;
1293 if (0 < revs
->skip_count
)
1294 limit
+= revs
->skip_count
;
1297 while ((c
= get_revision_1(revs
))) {
1298 commit_list_insert(c
, &l
);
1299 if ((0 < limit
) && !--limit
)
1304 revs
->max_count
= -1;
1309 * Now pick up what they want to give us
1311 c
= get_revision_1(revs
);
1313 while (0 < revs
->skip_count
) {
1315 c
= get_revision_1(revs
);
1322 * Check the max_count.
1324 switch (revs
->max_count
) {
1335 c
->object
.flags
|= SHOWN
;
1337 if (!revs
->boundary
) {
1343 * get_revision_1() runs out the commits, and
1344 * we are done computing the boundaries.
1345 * switch to boundary commits output mode.
1348 return get_revision(revs
);
1352 * boundary commits are the commits that are parents of the
1353 * ones we got from get_revision_1() but they themselves are
1354 * not returned from get_revision_1(). Before returning
1355 * 'c', we need to mark its parents that they could be boundaries.
1358 for (l
= c
->parents
; l
; l
= l
->next
) {
1360 p
= &(l
->item
->object
);
1361 if (p
->flags
& (CHILD_SHOWN
| SHOWN
))
1363 p
->flags
|= CHILD_SHOWN
;
1364 gc_boundary(&revs
->boundary_commits
);
1365 add_object_array(p
, NULL
, &revs
->boundary_commits
);