12 /* bits #0-15 in revision.h */
14 #define COUNTED (1u<<16)
16 static const char rev_list_usage
[] =
17 "git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
29 " formatting output:\n"
31 " --objects | --objects-edge\n"
33 " --header | --pretty\n"
34 " --abbrev=nr | --no-abbrev\n"
40 static struct rev_info revs
;
42 static int bisect_list
= 0;
43 static int show_timestamp
= 0;
44 static int hdr_termination
= 0;
45 static const char *header_prefix
;
47 static void show_commit(struct commit
*commit
)
50 printf("%lu ", commit
->date
);
52 fputs(header_prefix
, stdout
);
53 if (commit
->object
.flags
& BOUNDARY
)
55 if (revs
.abbrev_commit
&& revs
.abbrev
)
56 fputs(find_unique_abbrev(commit
->object
.sha1
, revs
.abbrev
),
59 fputs(sha1_to_hex(commit
->object
.sha1
), stdout
);
61 struct commit_list
*parents
= commit
->parents
;
63 struct object
*o
= &(parents
->item
->object
);
64 parents
= parents
->next
;
65 if (o
->flags
& TMP_MARK
)
67 printf(" %s", sha1_to_hex(o
->sha1
));
70 /* TMP_MARK is a general purpose flag that can
71 * be used locally, but the user should clean
72 * things up after it is done with them.
74 for (parents
= commit
->parents
;
76 parents
= parents
->next
)
77 parents
->item
->object
.flags
&= ~TMP_MARK
;
79 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
84 if (revs
.verbose_header
) {
85 static char pretty_header
[16384];
86 pretty_print_commit(revs
.commit_format
, commit
, ~0,
87 pretty_header
, sizeof(pretty_header
),
88 revs
.abbrev
, NULL
, NULL
);
89 printf("%s%c", pretty_header
, hdr_termination
);
94 static struct object_list
**process_blob(struct blob
*blob
,
95 struct object_list
**p
,
96 struct name_path
*path
,
99 struct object
*obj
= &blob
->object
;
101 if (!revs
.blob_objects
)
103 if (obj
->flags
& (UNINTERESTING
| SEEN
))
107 return add_object(obj
, p
, path
, name
);
110 static struct object_list
**process_tree(struct tree
*tree
,
111 struct object_list
**p
,
112 struct name_path
*path
,
115 struct object
*obj
= &tree
->object
;
116 struct tree_entry_list
*entry
;
119 if (!revs
.tree_objects
)
121 if (obj
->flags
& (UNINTERESTING
| SEEN
))
123 if (parse_tree(tree
) < 0)
124 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
127 p
= add_object(obj
, p
, path
, name
);
130 me
.elem_len
= strlen(name
);
131 entry
= tree
->entries
;
132 tree
->entries
= NULL
;
134 struct tree_entry_list
*next
= entry
->next
;
135 if (entry
->directory
)
136 p
= process_tree(entry
->item
.tree
, p
, &me
, entry
->name
);
138 p
= process_blob(entry
->item
.blob
, p
, &me
, entry
->name
);
146 static void show_commit_list(struct rev_info
*revs
)
148 struct commit
*commit
;
149 struct object_list
*objects
= NULL
, **p
= &objects
, *pending
;
151 while ((commit
= get_revision(revs
)) != NULL
) {
152 p
= process_tree(commit
->tree
, p
, NULL
, "");
155 for (pending
= revs
->pending_objects
; pending
; pending
= pending
->next
) {
156 struct object
*obj
= pending
->item
;
157 const char *name
= pending
->name
;
158 if (obj
->flags
& (UNINTERESTING
| SEEN
))
160 if (obj
->type
== tag_type
) {
162 p
= add_object(obj
, p
, NULL
, name
);
165 if (obj
->type
== tree_type
) {
166 p
= process_tree((struct tree
*)obj
, p
, NULL
, name
);
169 if (obj
->type
== blob_type
) {
170 p
= process_blob((struct blob
*)obj
, p
, NULL
, name
);
173 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
176 /* An object with name "foo\n0000000..." can be used to
177 * confuse downstream git-pack-objects very badly.
179 const char *ep
= strchr(objects
->name
, '\n');
181 printf("%s %.*s\n", sha1_to_hex(objects
->item
->sha1
),
182 (int) (ep
- objects
->name
),
186 printf("%s %s\n", sha1_to_hex(objects
->item
->sha1
), objects
->name
);
187 objects
= objects
->next
;
192 * This is a truly stupid algorithm, but it's only
193 * used for bisection, and we just don't care enough.
195 * We care just barely enough to avoid recursing for
198 static int count_distance(struct commit_list
*entry
)
203 struct commit
*commit
= entry
->item
;
204 struct commit_list
*p
;
206 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
208 if (!revs
.prune_fn
|| (commit
->object
.flags
& TREECHANGE
))
210 commit
->object
.flags
|= COUNTED
;
216 nr
+= count_distance(p
);
225 static void clear_distance(struct commit_list
*list
)
228 struct commit
*commit
= list
->item
;
229 commit
->object
.flags
&= ~COUNTED
;
234 static struct commit_list
*find_bisection(struct commit_list
*list
)
237 struct commit_list
*p
, *best
;
242 if (!revs
.prune_fn
|| (p
->item
->object
.flags
& TREECHANGE
))
249 for (p
= list
; p
; p
= p
->next
) {
252 if (revs
.prune_fn
&& !(p
->item
->object
.flags
& TREECHANGE
))
255 distance
= count_distance(p
);
256 clear_distance(list
);
257 if (nr
- distance
< distance
)
258 distance
= nr
- distance
;
259 if (distance
> closest
) {
269 static void mark_edge_parents_uninteresting(struct commit
*commit
)
271 struct commit_list
*parents
;
273 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
274 struct commit
*parent
= parents
->item
;
275 if (!(parent
->object
.flags
& UNINTERESTING
))
277 mark_tree_uninteresting(parent
->tree
);
278 if (revs
.edge_hint
&& !(parent
->object
.flags
& SHOWN
)) {
279 parent
->object
.flags
|= SHOWN
;
280 printf("-%s\n", sha1_to_hex(parent
->object
.sha1
));
285 static void mark_edges_uninteresting(struct commit_list
*list
)
287 for ( ; list
; list
= list
->next
) {
288 struct commit
*commit
= list
->item
;
290 if (commit
->object
.flags
& UNINTERESTING
) {
291 mark_tree_uninteresting(commit
->tree
);
294 mark_edge_parents_uninteresting(commit
);
298 int cmd_rev_list(int argc
, const char **argv
, char **envp
)
300 struct commit_list
*list
;
303 init_revisions(&revs
);
305 revs
.commit_format
= CMIT_FMT_UNSPECIFIED
;
306 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
308 for (i
= 1 ; i
< argc
; i
++) {
309 const char *arg
= argv
[i
];
311 if (!strcmp(arg
, "--header")) {
312 revs
.verbose_header
= 1;
315 if (!strcmp(arg
, "--timestamp")) {
319 if (!strcmp(arg
, "--bisect")) {
323 usage(rev_list_usage
);
326 if (revs
.commit_format
!= CMIT_FMT_UNSPECIFIED
) {
327 /* The command line has a --pretty */
328 hdr_termination
= '\n';
329 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
332 header_prefix
= "commit ";
334 else if (revs
.verbose_header
)
335 /* Only --header was specified */
336 revs
.commit_format
= CMIT_FMT_RAW
;
341 (!(revs
.tag_objects
||revs
.tree_objects
||revs
.blob_objects
) &&
342 !revs
.pending_objects
)) ||
344 usage(rev_list_usage
);
346 save_commit_buffer
= revs
.verbose_header
;
347 track_object_refs
= 0;
351 prepare_revision_walk(&revs
);
352 if (revs
.tree_objects
)
353 mark_edges_uninteresting(revs
.commits
);
356 revs
.commits
= find_bisection(revs
.commits
);
358 show_commit_list(&revs
);