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 static void add_pending_object_with_mode(struct rev_info
*revs
, struct object
*obj
, const char *name
, unsigned mode
)
119 if (revs
->no_walk
&& (obj
->flags
& UNINTERESTING
))
120 die("object ranges do not make sense when not walking revisions");
121 add_object_array_with_mode(obj
, name
, &revs
->pending
, mode
);
122 if (revs
->reflog_info
&& obj
->type
== OBJ_COMMIT
)
123 add_reflog_for_walk(revs
->reflog_info
,
124 (struct commit
*)obj
, name
);
127 void add_pending_object(struct rev_info
*revs
, struct object
*obj
, const char *name
)
129 add_pending_object_with_mode(revs
, obj
, name
, S_IFINVALID
);
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 static 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 static 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
;
321 if (parse_commit(p
) < 0)
322 die("cannot simplify commit %s (because of %s)",
323 sha1_to_hex(commit
->object
.sha1
),
324 sha1_to_hex(p
->object
.sha1
));
325 switch (rev_compare_tree(revs
, p
->tree
, commit
->tree
)) {
328 if (!revs
->simplify_history
|| (p
->object
.flags
& UNINTERESTING
)) {
329 /* Even if a merge with an uninteresting
330 * side branch brought the entire change
331 * we are interested in, we do not want
332 * to lose the other branches of this
333 * merge, so we just keep going.
339 commit
->parents
= parent
;
343 if (revs
->remove_empty_trees
&&
344 rev_same_tree_as_empty(revs
, p
->tree
)) {
345 /* We are adding all the specified
346 * paths from this parent, so the
347 * history beyond this parent is not
348 * interesting. Remove its parents
349 * (they are grandparents for us).
350 * IOW, we pretend this parent is a
353 if (parse_commit(p
) < 0)
354 die("cannot simplify commit %s (invalid %s)",
355 sha1_to_hex(commit
->object
.sha1
),
356 sha1_to_hex(p
->object
.sha1
));
360 case REV_TREE_DIFFERENT
:
365 die("bad tree compare for commit %s", sha1_to_hex(commit
->object
.sha1
));
367 if (tree_changed
&& !tree_same
)
368 commit
->object
.flags
|= TREECHANGE
;
371 static int add_parents_to_list(struct rev_info
*revs
, struct commit
*commit
, struct commit_list
**list
)
373 struct commit_list
*parent
= commit
->parents
;
377 if (commit
->object
.flags
& ADDED
)
379 commit
->object
.flags
|= ADDED
;
382 * If the commit is uninteresting, don't try to
383 * prune parents - we want the maximal uninteresting
386 * Normally we haven't parsed the parent
387 * yet, so we won't have a parent of a parent
388 * here. However, it may turn out that we've
389 * reached this commit some other way (where it
390 * wasn't uninteresting), in which case we need
391 * to mark its parents recursively too..
393 if (commit
->object
.flags
& UNINTERESTING
) {
395 struct commit
*p
= parent
->item
;
396 parent
= parent
->next
;
397 if (parse_commit(p
) < 0)
399 p
->object
.flags
|= UNINTERESTING
;
401 mark_parents_uninteresting(p
);
402 if (p
->object
.flags
& SEEN
)
404 p
->object
.flags
|= SEEN
;
405 insert_by_date(p
, list
);
411 * Ok, the commit wasn't uninteresting. Try to
412 * simplify the commit history and find the parent
413 * that has no differences in the path set if one exists.
416 revs
->prune_fn(revs
, commit
);
421 left_flag
= (commit
->object
.flags
& SYMMETRIC_LEFT
);
423 rest
= !revs
->first_parent_only
;
424 for (parent
= commit
->parents
, add
= 1; parent
; add
= rest
) {
425 struct commit
*p
= parent
->item
;
427 parent
= parent
->next
;
428 if (parse_commit(p
) < 0)
430 p
->object
.flags
|= left_flag
;
431 if (p
->object
.flags
& SEEN
)
433 p
->object
.flags
|= SEEN
;
435 insert_by_date(p
, list
);
440 static void cherry_pick_list(struct commit_list
*list
, struct rev_info
*revs
)
442 struct commit_list
*p
;
443 int left_count
= 0, right_count
= 0;
445 struct patch_ids ids
;
447 /* First count the commits on the left and on the right */
448 for (p
= list
; p
; p
= p
->next
) {
449 struct commit
*commit
= p
->item
;
450 unsigned flags
= commit
->object
.flags
;
451 if (flags
& BOUNDARY
)
453 else if (flags
& SYMMETRIC_LEFT
)
459 left_first
= left_count
< right_count
;
460 init_patch_ids(&ids
);
461 if (revs
->diffopt
.nr_paths
) {
462 ids
.diffopts
.nr_paths
= revs
->diffopt
.nr_paths
;
463 ids
.diffopts
.paths
= revs
->diffopt
.paths
;
464 ids
.diffopts
.pathlens
= revs
->diffopt
.pathlens
;
467 /* Compute patch-ids for one side */
468 for (p
= list
; p
; p
= p
->next
) {
469 struct commit
*commit
= p
->item
;
470 unsigned flags
= commit
->object
.flags
;
472 if (flags
& BOUNDARY
)
475 * If we have fewer left, left_first is set and we omit
476 * commits on the right branch in this loop. If we have
477 * fewer right, we skip the left ones.
479 if (left_first
!= !!(flags
& SYMMETRIC_LEFT
))
481 commit
->util
= add_commit_patch_id(commit
, &ids
);
484 /* Check the other side */
485 for (p
= list
; p
; p
= p
->next
) {
486 struct commit
*commit
= p
->item
;
488 unsigned flags
= commit
->object
.flags
;
490 if (flags
& BOUNDARY
)
493 * If we have fewer left, left_first is set and we omit
494 * commits on the left branch in this loop.
496 if (left_first
== !!(flags
& SYMMETRIC_LEFT
))
500 * Have we seen the same patch id?
502 id
= has_commit_patch_id(commit
, &ids
);
506 commit
->object
.flags
|= SHOWN
;
509 /* Now check the original side for seen ones */
510 for (p
= list
; p
; p
= p
->next
) {
511 struct commit
*commit
= p
->item
;
512 struct patch_id
*ent
;
518 commit
->object
.flags
|= SHOWN
;
522 free_patch_ids(&ids
);
525 static int limit_list(struct rev_info
*revs
)
527 struct commit_list
*list
= revs
->commits
;
528 struct commit_list
*newlist
= NULL
;
529 struct commit_list
**p
= &newlist
;
532 struct commit_list
*entry
= list
;
533 struct commit
*commit
= list
->item
;
534 struct object
*obj
= &commit
->object
;
539 if (revs
->max_age
!= -1 && (commit
->date
< revs
->max_age
))
540 obj
->flags
|= UNINTERESTING
;
541 if (add_parents_to_list(revs
, commit
, &list
) < 0)
543 if (obj
->flags
& UNINTERESTING
) {
544 mark_parents_uninteresting(commit
);
545 if (everybody_uninteresting(list
))
549 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
551 p
= &commit_list_insert(commit
, p
)->next
;
553 if (revs
->cherry_pick
)
554 cherry_pick_list(newlist
, revs
);
556 revs
->commits
= newlist
;
562 int warned_bad_reflog
;
563 struct rev_info
*all_revs
;
564 const char *name_for_errormsg
;
567 static int handle_one_ref(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
569 struct all_refs_cb
*cb
= cb_data
;
570 struct object
*object
= get_reference(cb
->all_revs
, path
, sha1
,
572 add_pending_object(cb
->all_revs
, object
, path
);
576 static void handle_all(struct rev_info
*revs
, unsigned flags
)
578 struct all_refs_cb cb
;
580 cb
.all_flags
= flags
;
581 for_each_ref(handle_one_ref
, &cb
);
584 static void handle_one_reflog_commit(unsigned char *sha1
, void *cb_data
)
586 struct all_refs_cb
*cb
= cb_data
;
587 if (!is_null_sha1(sha1
)) {
588 struct object
*o
= parse_object(sha1
);
590 o
->flags
|= cb
->all_flags
;
591 add_pending_object(cb
->all_revs
, o
, "");
593 else if (!cb
->warned_bad_reflog
) {
594 warning("reflog of '%s' references pruned commits",
595 cb
->name_for_errormsg
);
596 cb
->warned_bad_reflog
= 1;
601 static int handle_one_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
602 const char *email
, unsigned long timestamp
, int tz
,
603 const char *message
, void *cb_data
)
605 handle_one_reflog_commit(osha1
, cb_data
);
606 handle_one_reflog_commit(nsha1
, cb_data
);
610 static int handle_one_reflog(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
612 struct all_refs_cb
*cb
= cb_data
;
613 cb
->warned_bad_reflog
= 0;
614 cb
->name_for_errormsg
= path
;
615 for_each_reflog_ent(path
, handle_one_reflog_ent
, cb_data
);
619 static void handle_reflog(struct rev_info
*revs
, unsigned flags
)
621 struct all_refs_cb cb
;
623 cb
.all_flags
= flags
;
624 for_each_reflog(handle_one_reflog
, &cb
);
627 static int add_parents_only(struct rev_info
*revs
, const char *arg
, int flags
)
629 unsigned char sha1
[20];
631 struct commit
*commit
;
632 struct commit_list
*parents
;
635 flags
^= UNINTERESTING
;
638 if (get_sha1(arg
, sha1
))
641 it
= get_reference(revs
, arg
, sha1
, 0);
642 if (it
->type
!= OBJ_TAG
)
644 hashcpy(sha1
, ((struct tag
*)it
)->tagged
->sha1
);
646 if (it
->type
!= OBJ_COMMIT
)
648 commit
= (struct commit
*)it
;
649 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
650 it
= &parents
->item
->object
;
652 add_pending_object(revs
, it
, arg
);
657 void init_revisions(struct rev_info
*revs
, const char *prefix
)
659 memset(revs
, 0, sizeof(*revs
));
661 revs
->abbrev
= DEFAULT_ABBREV
;
662 revs
->ignore_merges
= 1;
663 revs
->simplify_history
= 1;
664 revs
->pruning
.recursive
= 1;
665 revs
->pruning
.quiet
= 1;
666 revs
->pruning
.add_remove
= file_add_remove
;
667 revs
->pruning
.change
= file_change
;
670 revs
->prefix
= prefix
;
673 revs
->skip_count
= -1;
674 revs
->max_count
= -1;
676 revs
->prune_fn
= NULL
;
677 revs
->prune_data
= NULL
;
679 revs
->topo_setter
= topo_sort_default_setter
;
680 revs
->topo_getter
= topo_sort_default_getter
;
682 revs
->commit_format
= CMIT_FMT_DEFAULT
;
684 diff_setup(&revs
->diffopt
);
687 static void add_pending_commit_list(struct rev_info
*revs
,
688 struct commit_list
*commit_list
,
691 while (commit_list
) {
692 struct object
*object
= &commit_list
->item
->object
;
693 object
->flags
|= flags
;
694 add_pending_object(revs
, object
, sha1_to_hex(object
->sha1
));
695 commit_list
= commit_list
->next
;
699 static void prepare_show_merge(struct rev_info
*revs
)
701 struct commit_list
*bases
;
702 struct commit
*head
, *other
;
703 unsigned char sha1
[20];
704 const char **prune
= NULL
;
705 int i
, prune_num
= 1; /* counting terminating NULL */
707 if (get_sha1("HEAD", sha1
) || !(head
= lookup_commit(sha1
)))
708 die("--merge without HEAD?");
709 if (get_sha1("MERGE_HEAD", sha1
) || !(other
= lookup_commit(sha1
)))
710 die("--merge without MERGE_HEAD?");
711 add_pending_object(revs
, &head
->object
, "HEAD");
712 add_pending_object(revs
, &other
->object
, "MERGE_HEAD");
713 bases
= get_merge_bases(head
, other
, 1);
715 struct commit
*it
= bases
->item
;
716 struct commit_list
*n
= bases
->next
;
719 it
->object
.flags
|= UNINTERESTING
;
720 add_pending_object(revs
, &it
->object
, "(merge-base)");
725 for (i
= 0; i
< active_nr
; i
++) {
726 struct cache_entry
*ce
= active_cache
[i
];
729 if (ce_path_match(ce
, revs
->prune_data
)) {
731 prune
= xrealloc(prune
, sizeof(*prune
) * prune_num
);
732 prune
[prune_num
-2] = ce
->name
;
733 prune
[prune_num
-1] = NULL
;
735 while ((i
+1 < active_nr
) &&
736 ce_same_name(ce
, active_cache
[i
+1]))
739 revs
->prune_data
= prune
;
742 int handle_revision_arg(const char *arg
, struct rev_info
*revs
,
744 int cant_be_filename
)
748 struct object
*object
;
749 unsigned char sha1
[20];
752 dotdot
= strstr(arg
, "..");
754 unsigned char from_sha1
[20];
755 const char *next
= dotdot
+ 2;
756 const char *this = arg
;
757 int symmetric
= *next
== '.';
758 unsigned int flags_exclude
= flags
^ UNINTERESTING
;
767 if (!get_sha1(this, from_sha1
) &&
768 !get_sha1(next
, sha1
)) {
769 struct commit
*a
, *b
;
770 struct commit_list
*exclude
;
772 a
= lookup_commit_reference(from_sha1
);
773 b
= lookup_commit_reference(sha1
);
776 "Invalid symmetric difference expression %s...%s" :
777 "Invalid revision range %s..%s",
781 if (!cant_be_filename
) {
783 verify_non_filename(revs
->prefix
, arg
);
787 exclude
= get_merge_bases(a
, b
, 1);
788 add_pending_commit_list(revs
, exclude
,
790 free_commit_list(exclude
);
791 a
->object
.flags
|= flags
| SYMMETRIC_LEFT
;
793 a
->object
.flags
|= flags_exclude
;
794 b
->object
.flags
|= flags
;
795 add_pending_object(revs
, &a
->object
, this);
796 add_pending_object(revs
, &b
->object
, next
);
801 dotdot
= strstr(arg
, "^@");
802 if (dotdot
&& !dotdot
[2]) {
804 if (add_parents_only(revs
, arg
, flags
))
808 dotdot
= strstr(arg
, "^!");
809 if (dotdot
&& !dotdot
[2]) {
811 if (!add_parents_only(revs
, arg
, flags
^ UNINTERESTING
))
817 local_flags
= UNINTERESTING
;
820 if (get_sha1_with_mode(arg
, sha1
, &mode
))
822 if (!cant_be_filename
)
823 verify_non_filename(revs
->prefix
, arg
);
824 object
= get_reference(revs
, arg
, sha1
, flags
^ local_flags
);
825 add_pending_object_with_mode(revs
, object
, arg
, mode
);
829 static void add_grep(struct rev_info
*revs
, const char *ptn
, enum grep_pat_token what
)
831 if (!revs
->grep_filter
) {
832 struct grep_opt
*opt
= xcalloc(1, sizeof(*opt
));
833 opt
->status_only
= 1;
834 opt
->pattern_tail
= &(opt
->pattern_list
);
835 opt
->regflags
= REG_NEWLINE
;
836 revs
->grep_filter
= opt
;
838 append_grep_pattern(revs
->grep_filter
, ptn
,
839 "command line", 0, what
);
842 static void add_header_grep(struct rev_info
*revs
, const char *field
, const char *pattern
)
848 fldlen
= strlen(field
);
849 patlen
= strlen(pattern
);
850 pat
= xmalloc(patlen
+ fldlen
+ 10);
852 if (*pattern
== '^') {
856 sprintf(pat
, "^%s %s%s", field
, prefix
, pattern
);
857 add_grep(revs
, pat
, GREP_PATTERN_HEAD
);
860 static void add_message_grep(struct rev_info
*revs
, const char *pattern
)
862 add_grep(revs
, pattern
, GREP_PATTERN_BODY
);
865 static void add_ignore_packed(struct rev_info
*revs
, const char *name
)
867 int num
= ++revs
->num_ignore_packed
;
869 revs
->ignore_packed
= xrealloc(revs
->ignore_packed
,
870 sizeof(const char **) * (num
+ 1));
871 revs
->ignore_packed
[num
-1] = name
;
872 revs
->ignore_packed
[num
] = NULL
;
876 * Parse revision information, filling in the "rev_info" structure,
877 * and removing the used arguments from the argument list.
879 * Returns the number of arguments left that weren't recognized
880 * (which are also moved to the head of the argument list)
882 int setup_revisions(int argc
, const char **argv
, struct rev_info
*revs
, const char *def
)
884 int i
, flags
, seen_dashdash
, show_merge
;
885 const char **unrecognized
= argv
+ 1;
890 /* First, search for "--" */
892 for (i
= 1; i
< argc
; i
++) {
893 const char *arg
= argv
[i
];
894 if (strcmp(arg
, "--"))
898 revs
->prune_data
= get_pathspec(revs
->prefix
, argv
+ i
+ 1);
903 flags
= show_merge
= 0;
904 for (i
= 1; i
< argc
; i
++) {
905 const char *arg
= argv
[i
];
908 if (!prefixcmp(arg
, "--max-count=")) {
909 revs
->max_count
= atoi(arg
+ 12);
912 if (!prefixcmp(arg
, "--skip=")) {
913 revs
->skip_count
= atoi(arg
+ 7);
916 /* accept -<digit>, like traditional "head" */
917 if ((*arg
== '-') && isdigit(arg
[1])) {
918 revs
->max_count
= atoi(arg
+ 1);
921 if (!strcmp(arg
, "-n")) {
923 die("-n requires an argument");
924 revs
->max_count
= atoi(argv
[++i
]);
927 if (!prefixcmp(arg
, "-n")) {
928 revs
->max_count
= atoi(arg
+ 2);
931 if (!prefixcmp(arg
, "--max-age=")) {
932 revs
->max_age
= atoi(arg
+ 10);
935 if (!prefixcmp(arg
, "--since=")) {
936 revs
->max_age
= approxidate(arg
+ 8);
939 if (!prefixcmp(arg
, "--after=")) {
940 revs
->max_age
= approxidate(arg
+ 8);
943 if (!prefixcmp(arg
, "--min-age=")) {
944 revs
->min_age
= atoi(arg
+ 10);
947 if (!prefixcmp(arg
, "--before=")) {
948 revs
->min_age
= approxidate(arg
+ 9);
951 if (!prefixcmp(arg
, "--until=")) {
952 revs
->min_age
= approxidate(arg
+ 8);
955 if (!strcmp(arg
, "--all")) {
956 handle_all(revs
, flags
);
959 if (!strcmp(arg
, "--first-parent")) {
960 revs
->first_parent_only
= 1;
963 if (!strcmp(arg
, "--reflog")) {
964 handle_reflog(revs
, flags
);
967 if (!strcmp(arg
, "-g") ||
968 !strcmp(arg
, "--walk-reflogs")) {
969 init_reflog_walk(&revs
->reflog_info
);
972 if (!strcmp(arg
, "--not")) {
973 flags
^= UNINTERESTING
;
976 if (!strcmp(arg
, "--default")) {
978 die("bad --default argument");
982 if (!strcmp(arg
, "--merge")) {
986 if (!strcmp(arg
, "--topo-order")) {
987 revs
->topo_order
= 1;
990 if (!strcmp(arg
, "--date-order")) {
992 revs
->topo_order
= 1;
995 if (!strcmp(arg
, "--parents")) {
999 if (!strcmp(arg
, "--dense")) {
1003 if (!strcmp(arg
, "--sparse")) {
1007 if (!strcmp(arg
, "--remove-empty")) {
1008 revs
->remove_empty_trees
= 1;
1011 if (!strcmp(arg
, "--no-merges")) {
1012 revs
->no_merges
= 1;
1015 if (!strcmp(arg
, "--boundary")) {
1019 if (!strcmp(arg
, "--left-right")) {
1020 revs
->left_right
= 1;
1023 if (!strcmp(arg
, "--cherry-pick")) {
1024 revs
->cherry_pick
= 1;
1027 if (!strcmp(arg
, "--objects")) {
1028 revs
->tag_objects
= 1;
1029 revs
->tree_objects
= 1;
1030 revs
->blob_objects
= 1;
1033 if (!strcmp(arg
, "--objects-edge")) {
1034 revs
->tag_objects
= 1;
1035 revs
->tree_objects
= 1;
1036 revs
->blob_objects
= 1;
1037 revs
->edge_hint
= 1;
1040 if (!strcmp(arg
, "--unpacked")) {
1042 free(revs
->ignore_packed
);
1043 revs
->ignore_packed
= NULL
;
1044 revs
->num_ignore_packed
= 0;
1047 if (!prefixcmp(arg
, "--unpacked=")) {
1049 add_ignore_packed(revs
, arg
+11);
1052 if (!strcmp(arg
, "-r")) {
1054 revs
->diffopt
.recursive
= 1;
1057 if (!strcmp(arg
, "-t")) {
1059 revs
->diffopt
.recursive
= 1;
1060 revs
->diffopt
.tree_in_recursive
= 1;
1063 if (!strcmp(arg
, "-m")) {
1064 revs
->ignore_merges
= 0;
1067 if (!strcmp(arg
, "-c")) {
1069 revs
->dense_combined_merges
= 0;
1070 revs
->combine_merges
= 1;
1073 if (!strcmp(arg
, "--cc")) {
1075 revs
->dense_combined_merges
= 1;
1076 revs
->combine_merges
= 1;
1079 if (!strcmp(arg
, "-v")) {
1080 revs
->verbose_header
= 1;
1083 if (!prefixcmp(arg
, "--pretty")) {
1084 revs
->verbose_header
= 1;
1085 revs
->commit_format
= get_commit_format(arg
+8);
1088 if (!strcmp(arg
, "--root")) {
1089 revs
->show_root_diff
= 1;
1092 if (!strcmp(arg
, "--no-commit-id")) {
1093 revs
->no_commit_id
= 1;
1096 if (!strcmp(arg
, "--always")) {
1097 revs
->always_show_header
= 1;
1100 if (!strcmp(arg
, "--no-abbrev")) {
1104 if (!strcmp(arg
, "--abbrev")) {
1105 revs
->abbrev
= DEFAULT_ABBREV
;
1108 if (!prefixcmp(arg
, "--abbrev=")) {
1109 revs
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
1110 if (revs
->abbrev
< MINIMUM_ABBREV
)
1111 revs
->abbrev
= MINIMUM_ABBREV
;
1112 else if (revs
->abbrev
> 40)
1116 if (!strcmp(arg
, "--abbrev-commit")) {
1117 revs
->abbrev_commit
= 1;
1120 if (!strcmp(arg
, "--full-diff")) {
1122 revs
->full_diff
= 1;
1125 if (!strcmp(arg
, "--full-history")) {
1126 revs
->simplify_history
= 0;
1129 if (!strcmp(arg
, "--relative-date")) {
1130 revs
->date_mode
= DATE_RELATIVE
;
1133 if (!strncmp(arg
, "--date=", 7)) {
1134 if (!strcmp(arg
+ 7, "relative"))
1135 revs
->date_mode
= DATE_RELATIVE
;
1136 else if (!strcmp(arg
+ 7, "local"))
1137 revs
->date_mode
= DATE_LOCAL
;
1138 else if (!strcmp(arg
+ 7, "default"))
1139 revs
->date_mode
= DATE_NORMAL
;
1141 die("unknown date format %s", arg
);
1146 * Grepping the commit log
1148 if (!prefixcmp(arg
, "--author=")) {
1149 add_header_grep(revs
, "author", arg
+9);
1152 if (!prefixcmp(arg
, "--committer=")) {
1153 add_header_grep(revs
, "committer", arg
+12);
1156 if (!prefixcmp(arg
, "--grep=")) {
1157 add_message_grep(revs
, arg
+7);
1160 if (!prefixcmp(arg
, "--extended-regexp")) {
1161 regflags
|= REG_EXTENDED
;
1164 if (!prefixcmp(arg
, "--regexp-ignore-case")) {
1165 regflags
|= REG_ICASE
;
1168 if (!strcmp(arg
, "--all-match")) {
1172 if (!prefixcmp(arg
, "--encoding=")) {
1174 if (strcmp(arg
, "none"))
1175 git_log_output_encoding
= xstrdup(arg
);
1177 git_log_output_encoding
= "";
1180 if (!strcmp(arg
, "--reverse")) {
1185 opts
= diff_opt_parse(&revs
->diffopt
, argv
+i
, argc
-i
);
1187 if (strcmp(argv
[i
], "-z"))
1192 *unrecognized
++ = arg
;
1197 if (handle_revision_arg(arg
, revs
, flags
, seen_dashdash
)) {
1199 if (seen_dashdash
|| *arg
== '^')
1200 die("bad revision '%s'", arg
);
1202 /* If we didn't have a "--":
1203 * (1) all filenames must exist;
1204 * (2) all rev-args must not be interpretable
1205 * as a valid filename.
1206 * but the latter we have checked in the main loop.
1208 for (j
= i
; j
< argc
; j
++)
1209 verify_filename(revs
->prefix
, argv
[j
]);
1211 revs
->prune_data
= get_pathspec(revs
->prefix
,
1217 if (revs
->grep_filter
)
1218 revs
->grep_filter
->regflags
|= regflags
;
1221 prepare_show_merge(revs
);
1222 if (def
&& !revs
->pending
.nr
) {
1223 unsigned char sha1
[20];
1224 struct object
*object
;
1226 if (get_sha1_with_mode(def
, sha1
, &mode
))
1227 die("bad default revision '%s'", def
);
1228 object
= get_reference(revs
, def
, sha1
, 0);
1229 add_pending_object_with_mode(revs
, object
, def
, mode
);
1232 if (revs
->topo_order
)
1235 if (revs
->prune_data
) {
1236 diff_tree_setup_paths(revs
->prune_data
, &revs
->pruning
);
1237 /* Can't prune commits with rename following: the paths change.. */
1238 if (!revs
->diffopt
.follow_renames
)
1239 revs
->prune_fn
= try_to_simplify_commit
;
1240 if (!revs
->full_diff
)
1241 diff_tree_setup_paths(revs
->prune_data
, &revs
->diffopt
);
1243 if (revs
->combine_merges
) {
1244 revs
->ignore_merges
= 0;
1245 if (revs
->dense_combined_merges
&& !revs
->diffopt
.output_format
)
1246 revs
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
1248 revs
->diffopt
.abbrev
= revs
->abbrev
;
1249 if (diff_setup_done(&revs
->diffopt
) < 0)
1250 die("diff_setup_done failed");
1252 if (revs
->grep_filter
) {
1253 revs
->grep_filter
->all_match
= all_match
;
1254 compile_grep_patterns(revs
->grep_filter
);
1260 int prepare_revision_walk(struct rev_info
*revs
)
1262 int nr
= revs
->pending
.nr
;
1263 struct object_array_entry
*e
, *list
;
1265 e
= list
= revs
->pending
.objects
;
1266 revs
->pending
.nr
= 0;
1267 revs
->pending
.alloc
= 0;
1268 revs
->pending
.objects
= NULL
;
1270 struct commit
*commit
= handle_commit(revs
, e
->item
, e
->name
);
1272 if (!(commit
->object
.flags
& SEEN
)) {
1273 commit
->object
.flags
|= SEEN
;
1274 insert_by_date(commit
, &revs
->commits
);
1284 if (limit_list(revs
) < 0)
1286 if (revs
->topo_order
)
1287 sort_in_topological_order_fn(&revs
->commits
, revs
->lifo
,
1293 enum rewrite_result
{
1295 rewrite_one_noparents
,
1299 static enum rewrite_result
rewrite_one(struct rev_info
*revs
, struct commit
**pp
)
1302 struct commit
*p
= *pp
;
1304 if (add_parents_to_list(revs
, p
, &revs
->commits
) < 0)
1305 return rewrite_one_error
;
1306 if (p
->parents
&& p
->parents
->next
)
1307 return rewrite_one_ok
;
1308 if (p
->object
.flags
& (TREECHANGE
| UNINTERESTING
))
1309 return rewrite_one_ok
;
1311 return rewrite_one_noparents
;
1312 *pp
= p
->parents
->item
;
1316 static void remove_duplicate_parents(struct commit
*commit
)
1318 struct commit_list
*p
;
1319 struct commit_list
**pp
= &commit
->parents
;
1321 /* Examine existing parents while marking ones we have seen... */
1322 for (p
= commit
->parents
; p
; p
= p
->next
) {
1323 struct commit
*parent
= p
->item
;
1324 if (parent
->object
.flags
& TMP_MARK
)
1326 parent
->object
.flags
|= TMP_MARK
;
1330 /* ... and clear the temporary mark */
1331 for (p
= commit
->parents
; p
; p
= p
->next
)
1332 p
->item
->object
.flags
&= ~TMP_MARK
;
1335 static int rewrite_parents(struct rev_info
*revs
, struct commit
*commit
)
1337 struct commit_list
**pp
= &commit
->parents
;
1339 struct commit_list
*parent
= *pp
;
1340 switch (rewrite_one(revs
, &parent
->item
)) {
1341 case rewrite_one_ok
:
1343 case rewrite_one_noparents
:
1346 case rewrite_one_error
:
1351 remove_duplicate_parents(commit
);
1355 static int commit_match(struct commit
*commit
, struct rev_info
*opt
)
1357 if (!opt
->grep_filter
)
1359 return grep_buffer(opt
->grep_filter
,
1360 NULL
, /* we say nothing, not even filename */
1361 commit
->buffer
, strlen(commit
->buffer
));
1364 static struct commit
*get_revision_1(struct rev_info
*revs
)
1370 struct commit_list
*entry
= revs
->commits
;
1371 struct commit
*commit
= entry
->item
;
1373 revs
->commits
= entry
->next
;
1376 if (revs
->reflog_info
)
1377 fake_reflog_parent(revs
->reflog_info
, commit
);
1380 * If we haven't done the list limiting, we need to look at
1381 * the parents here. We also need to do the date-based limiting
1382 * that we'd otherwise have done in limit_list().
1384 if (!revs
->limited
) {
1385 if (revs
->max_age
!= -1 &&
1386 (commit
->date
< revs
->max_age
))
1388 if (add_parents_to_list(revs
, commit
, &revs
->commits
) < 0)
1391 if (commit
->object
.flags
& SHOWN
)
1394 if (revs
->unpacked
&& has_sha1_pack(commit
->object
.sha1
,
1395 revs
->ignore_packed
))
1398 if (commit
->object
.flags
& UNINTERESTING
)
1400 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
1402 if (revs
->no_merges
&&
1403 commit
->parents
&& commit
->parents
->next
)
1405 if (!commit_match(commit
, revs
))
1407 if (revs
->prune_fn
&& revs
->dense
) {
1408 /* Commit without changes? */
1409 if (!(commit
->object
.flags
& TREECHANGE
)) {
1410 /* drop merges unless we want parenthood */
1413 /* non-merge - always ignore it */
1414 if (!commit
->parents
|| !commit
->parents
->next
)
1417 if (revs
->parents
&& rewrite_parents(revs
, commit
) < 0)
1421 } while (revs
->commits
);
1425 static void gc_boundary(struct object_array
*array
)
1427 unsigned nr
= array
->nr
;
1428 unsigned alloc
= array
->alloc
;
1429 struct object_array_entry
*objects
= array
->objects
;
1433 for (i
= j
= 0; i
< nr
; i
++) {
1434 if (objects
[i
].item
->flags
& SHOWN
)
1437 objects
[j
] = objects
[i
];
1440 for (i
= j
; i
< nr
; i
++)
1441 objects
[i
].item
= NULL
;
1446 struct commit
*get_revision(struct rev_info
*revs
)
1448 struct commit
*c
= NULL
;
1449 struct commit_list
*l
;
1451 if (revs
->boundary
== 2) {
1453 struct object_array
*array
= &revs
->boundary_commits
;
1454 struct object_array_entry
*objects
= array
->objects
;
1455 for (i
= 0; i
< array
->nr
; i
++) {
1456 c
= (struct commit
*)(objects
[i
].item
);
1459 if (!(c
->object
.flags
& CHILD_SHOWN
))
1461 if (!(c
->object
.flags
& SHOWN
))
1467 c
->object
.flags
|= SHOWN
| BOUNDARY
;
1471 if (revs
->reverse
) {
1474 if (0 <= revs
->max_count
) {
1475 limit
= revs
->max_count
;
1476 if (0 < revs
->skip_count
)
1477 limit
+= revs
->skip_count
;
1480 while ((c
= get_revision_1(revs
))) {
1481 commit_list_insert(c
, &l
);
1482 if ((0 < limit
) && !--limit
)
1487 revs
->max_count
= -1;
1492 * Now pick up what they want to give us
1494 c
= get_revision_1(revs
);
1496 while (0 < revs
->skip_count
) {
1498 c
= get_revision_1(revs
);
1505 * Check the max_count.
1507 switch (revs
->max_count
) {
1518 c
->object
.flags
|= SHOWN
;
1520 if (!revs
->boundary
) {
1526 * get_revision_1() runs out the commits, and
1527 * we are done computing the boundaries.
1528 * switch to boundary commits output mode.
1531 return get_revision(revs
);
1535 * boundary commits are the commits that are parents of the
1536 * ones we got from get_revision_1() but they themselves are
1537 * not returned from get_revision_1(). Before returning
1538 * 'c', we need to mark its parents that they could be boundaries.
1541 for (l
= c
->parents
; l
; l
= l
->next
) {
1543 p
= &(l
->item
->object
);
1544 if (p
->flags
& (CHILD_SHOWN
| SHOWN
))
1546 p
->flags
|= CHILD_SHOWN
;
1547 gc_boundary(&revs
->boundary_commits
);
1548 add_object_array(p
, NULL
, &revs
->boundary_commits
);