11 /* bits #0-6 in revision.h */
13 #define COUNTED (1u<<7)
15 static const char rev_list_usage
[] =
16 "git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
28 " formatting output:\n"
30 " --objects | --objects-edge\n"
32 " --header | --pretty\n"
33 " --abbrev=nr | --no-abbrev\n"
41 static int bisect_list
= 0;
42 static int show_timestamp
= 0;
43 static int hdr_termination
= 0;
45 static void show_commit(struct commit
*commit
)
48 printf("%lu ", commit
->date
);
49 if (*revs
.header_prefix
)
50 fputs(revs
.header_prefix
, stdout
);
51 if (commit
->object
.flags
& BOUNDARY
)
53 if (revs
.abbrev_commit
&& revs
.abbrev
)
54 fputs(find_unique_abbrev(commit
->object
.sha1
, revs
.abbrev
),
57 fputs(sha1_to_hex(commit
->object
.sha1
), stdout
);
59 struct commit_list
*parents
= commit
->parents
;
61 struct object
*o
= &(parents
->item
->object
);
62 parents
= parents
->next
;
63 if (o
->flags
& TMP_MARK
)
65 printf(" %s", sha1_to_hex(o
->sha1
));
68 /* TMP_MARK is a general purpose flag that can
69 * be used locally, but the user should clean
70 * things up after it is done with them.
72 for (parents
= commit
->parents
;
74 parents
= parents
->next
)
75 parents
->item
->object
.flags
&= ~TMP_MARK
;
77 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
82 if (revs
.verbose_header
) {
83 static char pretty_header
[16384];
84 pretty_print_commit(revs
.commit_format
, commit
, ~0,
85 pretty_header
, sizeof(pretty_header
),
87 printf("%s%c", pretty_header
, hdr_termination
);
92 static struct object_list
**process_blob(struct blob
*blob
,
93 struct object_list
**p
,
94 struct name_path
*path
,
97 struct object
*obj
= &blob
->object
;
99 if (!revs
.blob_objects
)
101 if (obj
->flags
& (UNINTERESTING
| SEEN
))
104 return add_object(obj
, p
, path
, name
);
107 static struct object_list
**process_tree(struct tree
*tree
,
108 struct object_list
**p
,
109 struct name_path
*path
,
112 struct object
*obj
= &tree
->object
;
113 struct tree_entry_list
*entry
;
116 if (!revs
.tree_objects
)
118 if (obj
->flags
& (UNINTERESTING
| SEEN
))
120 if (parse_tree(tree
) < 0)
121 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
123 p
= add_object(obj
, p
, path
, name
);
126 me
.elem_len
= strlen(name
);
127 entry
= tree
->entries
;
128 tree
->entries
= NULL
;
130 struct tree_entry_list
*next
= entry
->next
;
131 if (entry
->directory
)
132 p
= process_tree(entry
->item
.tree
, p
, &me
, entry
->name
);
134 p
= process_blob(entry
->item
.blob
, p
, &me
, entry
->name
);
141 static void show_commit_list(struct rev_info
*revs
)
143 struct commit
*commit
;
144 struct object_list
*objects
= NULL
, **p
= &objects
, *pending
;
146 while ((commit
= get_revision(revs
)) != NULL
) {
147 p
= process_tree(commit
->tree
, p
, NULL
, "");
150 for (pending
= revs
->pending_objects
; pending
; pending
= pending
->next
) {
151 struct object
*obj
= pending
->item
;
152 const char *name
= pending
->name
;
153 if (obj
->flags
& (UNINTERESTING
| SEEN
))
155 if (obj
->type
== tag_type
) {
157 p
= add_object(obj
, p
, NULL
, name
);
160 if (obj
->type
== tree_type
) {
161 p
= process_tree((struct tree
*)obj
, p
, NULL
, name
);
164 if (obj
->type
== blob_type
) {
165 p
= process_blob((struct blob
*)obj
, p
, NULL
, name
);
168 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
171 /* An object with name "foo\n0000000..." can be used to
172 * confuse downstream git-pack-objects very badly.
174 const char *ep
= strchr(objects
->name
, '\n');
176 printf("%s %.*s\n", sha1_to_hex(objects
->item
->sha1
),
177 (int) (ep
- objects
->name
),
181 printf("%s %s\n", sha1_to_hex(objects
->item
->sha1
), objects
->name
);
182 objects
= objects
->next
;
187 * This is a truly stupid algorithm, but it's only
188 * used for bisection, and we just don't care enough.
190 * We care just barely enough to avoid recursing for
193 static int count_distance(struct commit_list
*entry
)
198 struct commit
*commit
= entry
->item
;
199 struct commit_list
*p
;
201 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
203 if (!revs
.prune_fn
|| (commit
->object
.flags
& TREECHANGE
))
205 commit
->object
.flags
|= COUNTED
;
211 nr
+= count_distance(p
);
220 static void clear_distance(struct commit_list
*list
)
223 struct commit
*commit
= list
->item
;
224 commit
->object
.flags
&= ~COUNTED
;
229 static struct commit_list
*find_bisection(struct commit_list
*list
)
232 struct commit_list
*p
, *best
;
237 if (!revs
.prune_fn
|| (p
->item
->object
.flags
& TREECHANGE
))
244 for (p
= list
; p
; p
= p
->next
) {
247 if (revs
.prune_fn
&& !(p
->item
->object
.flags
& TREECHANGE
))
250 distance
= count_distance(p
);
251 clear_distance(list
);
252 if (nr
- distance
< distance
)
253 distance
= nr
- distance
;
254 if (distance
> closest
) {
264 static void mark_edge_parents_uninteresting(struct commit
*commit
)
266 struct commit_list
*parents
;
268 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
269 struct commit
*parent
= parents
->item
;
270 if (!(parent
->object
.flags
& UNINTERESTING
))
272 mark_tree_uninteresting(parent
->tree
);
273 if (revs
.edge_hint
&& !(parent
->object
.flags
& SHOWN
)) {
274 parent
->object
.flags
|= SHOWN
;
275 printf("-%s\n", sha1_to_hex(parent
->object
.sha1
));
280 static void mark_edges_uninteresting(struct commit_list
*list
)
282 for ( ; list
; list
= list
->next
) {
283 struct commit
*commit
= list
->item
;
285 if (commit
->object
.flags
& UNINTERESTING
) {
286 mark_tree_uninteresting(commit
->tree
);
289 mark_edge_parents_uninteresting(commit
);
293 int main(int argc
, const char **argv
)
295 struct commit_list
*list
;
298 init_revisions(&revs
);
300 revs
.commit_format
= CMIT_FMT_UNSPECIFIED
;
301 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
303 for (i
= 1 ; i
< argc
; i
++) {
304 const char *arg
= argv
[i
];
306 if (!strcmp(arg
, "--header")) {
307 revs
.verbose_header
= 1;
310 if (!strcmp(arg
, "--timestamp")) {
314 if (!strcmp(arg
, "--bisect")) {
318 usage(rev_list_usage
);
321 if (revs
.commit_format
!= CMIT_FMT_UNSPECIFIED
) {
322 /* The command line has a --pretty */
323 hdr_termination
= '\n';
324 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
325 revs
.header_prefix
= "";
327 revs
.header_prefix
= "commit ";
333 (!(revs
.tag_objects
||revs
.tree_objects
||revs
.blob_objects
) &&
334 !revs
.pending_objects
)) ||
336 usage(rev_list_usage
);
338 save_commit_buffer
= revs
.verbose_header
;
339 track_object_refs
= 0;
341 prepare_revision_walk(&revs
);
342 if (revs
.tree_objects
)
343 mark_edges_uninteresting(revs
.commits
);
346 revs
.commits
= find_bisection(revs
.commits
);
348 show_commit_list(&revs
);