11 /* bits #0-2 in revision.h */
13 #define COUNTED (1u << 3)
14 #define SHOWN (1u << 4)
15 #define TMP_MARK (1u << 5) /* for isolated cases; clean after use */
17 static const char rev_list_usage
[] =
18 "git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
28 " --merge-order [ --show-breaks ]\n"
31 " formatting output:\n"
33 " --objects | --objects-edge\n"
35 " --header | --pretty\n"
36 " --abbrev=nr | --no-abbrev\n"
43 static int bisect_list
= 0;
44 static int verbose_header
= 0;
45 static int abbrev
= DEFAULT_ABBREV
;
46 static int show_parents
= 0;
47 static int hdr_termination
= 0;
48 static const char *commit_prefix
= "";
49 static enum cmit_fmt commit_format
= CMIT_FMT_RAW
;
50 static int merge_order
= 0;
51 static int show_breaks
= 0;
52 static int stop_traversal
= 0;
53 static int no_merges
= 0;
55 static void show_commit(struct commit
*commit
)
57 commit
->object
.flags
|= SHOWN
;
60 if (commit
->object
.flags
& DISCONTINUITY
) {
62 } else if (commit
->object
.flags
& BOUNDARY
) {
66 printf("%s%s", commit_prefix
, sha1_to_hex(commit
->object
.sha1
));
68 struct commit_list
*parents
= commit
->parents
;
70 struct object
*o
= &(parents
->item
->object
);
71 parents
= parents
->next
;
72 if (o
->flags
& TMP_MARK
)
74 printf(" %s", sha1_to_hex(o
->sha1
));
77 /* TMP_MARK is a general purpose flag that can
78 * be used locally, but the user should clean
79 * things up after it is done with them.
81 for (parents
= commit
->parents
;
83 parents
= parents
->next
)
84 parents
->item
->object
.flags
&= ~TMP_MARK
;
86 if (commit_format
== CMIT_FMT_ONELINE
)
92 static char pretty_header
[16384];
93 pretty_print_commit(commit_format
, commit
, ~0, pretty_header
, sizeof(pretty_header
), abbrev
);
94 printf("%s%c", pretty_header
, hdr_termination
);
99 static int rewrite_one(struct commit
**pp
)
102 struct commit
*p
= *pp
;
103 if (p
->object
.flags
& (TREECHANGE
| UNINTERESTING
))
107 *pp
= p
->parents
->item
;
111 static void rewrite_parents(struct commit
*commit
)
113 struct commit_list
**pp
= &commit
->parents
;
115 struct commit_list
*parent
= *pp
;
116 if (rewrite_one(&parent
->item
) < 0) {
124 static int filter_commit(struct commit
* commit
)
126 if (stop_traversal
&& (commit
->object
.flags
& BOUNDARY
))
128 if (commit
->object
.flags
& (UNINTERESTING
|SHOWN
))
130 if (revs
.min_age
!= -1 && (commit
->date
> revs
.min_age
))
132 if (revs
.max_age
!= -1 && (commit
->date
< revs
.max_age
)) {
136 if (no_merges
&& (commit
->parents
&& commit
->parents
->next
))
138 if (revs
.paths
&& revs
.dense
) {
139 if (!(commit
->object
.flags
& TREECHANGE
))
141 rewrite_parents(commit
);
146 static int process_commit(struct commit
* commit
)
148 int action
=filter_commit(commit
);
150 if (action
== STOP
) {
154 if (action
== CONTINUE
) {
158 if (revs
.max_count
!= -1 && !revs
.max_count
--)
166 static struct object_list
**process_blob(struct blob
*blob
,
167 struct object_list
**p
,
168 struct name_path
*path
,
171 struct object
*obj
= &blob
->object
;
173 if (!revs
.blob_objects
)
175 if (obj
->flags
& (UNINTERESTING
| SEEN
))
178 return add_object(obj
, p
, path
, name
);
181 static struct object_list
**process_tree(struct tree
*tree
,
182 struct object_list
**p
,
183 struct name_path
*path
,
186 struct object
*obj
= &tree
->object
;
187 struct tree_entry_list
*entry
;
190 if (!revs
.tree_objects
)
192 if (obj
->flags
& (UNINTERESTING
| SEEN
))
194 if (parse_tree(tree
) < 0)
195 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
197 p
= add_object(obj
, p
, path
, name
);
200 me
.elem_len
= strlen(name
);
201 entry
= tree
->entries
;
202 tree
->entries
= NULL
;
204 struct tree_entry_list
*next
= entry
->next
;
205 if (entry
->directory
)
206 p
= process_tree(entry
->item
.tree
, p
, &me
, entry
->name
);
208 p
= process_blob(entry
->item
.blob
, p
, &me
, entry
->name
);
215 static void show_commit_list(struct rev_info
*revs
)
217 struct commit
*commit
;
218 struct object_list
*objects
= NULL
, **p
= &objects
, *pending
;
220 while ((commit
= get_revision(revs
)) != NULL
) {
221 p
= process_tree(commit
->tree
, p
, NULL
, "");
222 if (process_commit(commit
) == STOP
)
225 for (pending
= revs
->pending_objects
; pending
; pending
= pending
->next
) {
226 struct object
*obj
= pending
->item
;
227 const char *name
= pending
->name
;
228 if (obj
->flags
& (UNINTERESTING
| SEEN
))
230 if (obj
->type
== tag_type
) {
232 p
= add_object(obj
, p
, NULL
, name
);
235 if (obj
->type
== tree_type
) {
236 p
= process_tree((struct tree
*)obj
, p
, NULL
, name
);
239 if (obj
->type
== blob_type
) {
240 p
= process_blob((struct blob
*)obj
, p
, NULL
, name
);
243 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
246 /* An object with name "foo\n0000000..." can be used to
247 * confuse downstream git-pack-objects very badly.
249 const char *ep
= strchr(objects
->name
, '\n');
251 printf("%s %.*s\n", sha1_to_hex(objects
->item
->sha1
),
252 (int) (ep
- objects
->name
),
256 printf("%s %s\n", sha1_to_hex(objects
->item
->sha1
), objects
->name
);
257 objects
= objects
->next
;
262 * This is a truly stupid algorithm, but it's only
263 * used for bisection, and we just don't care enough.
265 * We care just barely enough to avoid recursing for
268 static int count_distance(struct commit_list
*entry
)
273 struct commit
*commit
= entry
->item
;
274 struct commit_list
*p
;
276 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
278 if (!revs
.paths
|| (commit
->object
.flags
& TREECHANGE
))
280 commit
->object
.flags
|= COUNTED
;
286 nr
+= count_distance(p
);
295 static void clear_distance(struct commit_list
*list
)
298 struct commit
*commit
= list
->item
;
299 commit
->object
.flags
&= ~COUNTED
;
304 static struct commit_list
*find_bisection(struct commit_list
*list
)
307 struct commit_list
*p
, *best
;
312 if (!revs
.paths
|| (p
->item
->object
.flags
& TREECHANGE
))
319 for (p
= list
; p
; p
= p
->next
) {
322 if (revs
.paths
&& !(p
->item
->object
.flags
& TREECHANGE
))
325 distance
= count_distance(p
);
326 clear_distance(list
);
327 if (nr
- distance
< distance
)
328 distance
= nr
- distance
;
329 if (distance
> closest
) {
339 static void mark_edge_parents_uninteresting(struct commit
*commit
)
341 struct commit_list
*parents
;
343 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
344 struct commit
*parent
= parents
->item
;
345 if (!(parent
->object
.flags
& UNINTERESTING
))
347 mark_tree_uninteresting(parent
->tree
);
348 if (revs
.edge_hint
&& !(parent
->object
.flags
& SHOWN
)) {
349 parent
->object
.flags
|= SHOWN
;
350 printf("-%s\n", sha1_to_hex(parent
->object
.sha1
));
355 static void mark_edges_uninteresting(struct commit_list
*list
)
357 for ( ; list
; list
= list
->next
) {
358 struct commit
*commit
= list
->item
;
360 if (commit
->object
.flags
& UNINTERESTING
) {
361 mark_tree_uninteresting(commit
->tree
);
364 mark_edge_parents_uninteresting(commit
);
368 int main(int argc
, const char **argv
)
370 struct commit_list
*list
;
373 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
375 for (i
= 1 ; i
< argc
; i
++) {
376 const char *arg
= argv
[i
];
378 /* accept -<digit>, like traditilnal "head" */
379 if ((*arg
== '-') && isdigit(arg
[1])) {
380 revs
.max_count
= atoi(arg
+ 1);
383 if (!strcmp(arg
, "-n")) {
385 die("-n requires an argument");
386 revs
.max_count
= atoi(argv
[i
]);
389 if (!strncmp(arg
,"-n",2)) {
390 revs
.max_count
= atoi(arg
+ 2);
393 if (!strcmp(arg
, "--header")) {
397 if (!strcmp(arg
, "--no-abbrev")) {
401 if (!strncmp(arg
, "--abbrev=", 9)) {
402 abbrev
= strtoul(arg
+ 9, NULL
, 10);
403 if (abbrev
&& abbrev
< MINIMUM_ABBREV
)
404 abbrev
= MINIMUM_ABBREV
;
405 else if (40 < abbrev
)
409 if (!strncmp(arg
, "--pretty", 8)) {
410 commit_format
= get_commit_format(arg
+8);
412 hdr_termination
= '\n';
413 if (commit_format
== CMIT_FMT_ONELINE
)
416 commit_prefix
= "commit ";
419 if (!strncmp(arg
, "--no-merges", 11)) {
423 if (!strcmp(arg
, "--parents")) {
427 if (!strcmp(arg
, "--bisect")) {
431 if (!strcmp(arg
, "--merge-order")) {
435 if (!strcmp(arg
, "--show-breaks")) {
439 usage(rev_list_usage
);
446 (!(revs
.tag_objects
||revs
.tree_objects
||revs
.blob_objects
) && !revs
.pending_objects
))
447 usage(rev_list_usage
);
449 prepare_revision_walk(&revs
);
450 if (revs
.tree_objects
)
451 mark_edges_uninteresting(revs
.commits
);
454 revs
.commits
= find_bisection(revs
.commits
);
456 save_commit_buffer
= verbose_header
;
457 track_object_refs
= 0;
460 show_commit_list(&revs
);
463 if (sort_list_in_merge_order(list
, &process_commit
)) {
464 die("merge order sort failed\n");
467 die("merge order sort unsupported, OpenSSL not linked");