10 /* bits #0-4 in revision.h */
12 #define COUNTED (1u<<5)
14 static const char rev_list_usage
[] =
15 "git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
27 " formatting output:\n"
29 " --objects | --objects-edge\n"
31 " --header | --pretty\n"
32 " --abbrev=nr | --no-abbrev\n"
39 static int bisect_list
= 0;
40 static int verbose_header
= 0;
41 static int abbrev
= DEFAULT_ABBREV
;
42 static int show_parents
= 0;
43 static int show_timestamp
= 0;
44 static int hdr_termination
= 0;
45 static const char *commit_prefix
= "";
46 static enum cmit_fmt commit_format
= CMIT_FMT_RAW
;
48 static void show_commit(struct commit
*commit
)
51 printf("%lu ", commit
->date
);
53 fputs(commit_prefix
, stdout
);
54 fputs(sha1_to_hex(commit
->object
.sha1
), stdout
);
56 struct commit_list
*parents
= commit
->parents
;
58 struct object
*o
= &(parents
->item
->object
);
59 parents
= parents
->next
;
60 if (o
->flags
& TMP_MARK
)
62 printf(" %s", sha1_to_hex(o
->sha1
));
65 /* TMP_MARK is a general purpose flag that can
66 * be used locally, but the user should clean
67 * things up after it is done with them.
69 for (parents
= commit
->parents
;
71 parents
= parents
->next
)
72 parents
->item
->object
.flags
&= ~TMP_MARK
;
74 if (commit_format
== CMIT_FMT_ONELINE
)
80 static char pretty_header
[16384];
81 pretty_print_commit(commit_format
, commit
, ~0, pretty_header
, sizeof(pretty_header
), abbrev
);
82 printf("%s%c", pretty_header
, hdr_termination
);
87 static struct object_list
**process_blob(struct blob
*blob
,
88 struct object_list
**p
,
89 struct name_path
*path
,
92 struct object
*obj
= &blob
->object
;
94 if (!revs
.blob_objects
)
96 if (obj
->flags
& (UNINTERESTING
| SEEN
))
99 return add_object(obj
, p
, path
, name
);
102 static struct object_list
**process_tree(struct tree
*tree
,
103 struct object_list
**p
,
104 struct name_path
*path
,
107 struct object
*obj
= &tree
->object
;
108 struct tree_entry_list
*entry
;
111 if (!revs
.tree_objects
)
113 if (obj
->flags
& (UNINTERESTING
| SEEN
))
115 if (parse_tree(tree
) < 0)
116 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
118 p
= add_object(obj
, p
, path
, name
);
121 me
.elem_len
= strlen(name
);
122 entry
= tree
->entries
;
123 tree
->entries
= NULL
;
125 struct tree_entry_list
*next
= entry
->next
;
126 if (entry
->directory
)
127 p
= process_tree(entry
->item
.tree
, p
, &me
, entry
->name
);
129 p
= process_blob(entry
->item
.blob
, p
, &me
, entry
->name
);
136 static void show_commit_list(struct rev_info
*revs
)
138 struct commit
*commit
;
139 struct object_list
*objects
= NULL
, **p
= &objects
, *pending
;
141 while ((commit
= get_revision(revs
)) != NULL
) {
142 p
= process_tree(commit
->tree
, p
, NULL
, "");
145 for (pending
= revs
->pending_objects
; pending
; pending
= pending
->next
) {
146 struct object
*obj
= pending
->item
;
147 const char *name
= pending
->name
;
148 if (obj
->flags
& (UNINTERESTING
| SEEN
))
150 if (obj
->type
== tag_type
) {
152 p
= add_object(obj
, p
, NULL
, name
);
155 if (obj
->type
== tree_type
) {
156 p
= process_tree((struct tree
*)obj
, p
, NULL
, name
);
159 if (obj
->type
== blob_type
) {
160 p
= process_blob((struct blob
*)obj
, p
, NULL
, name
);
163 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
166 /* An object with name "foo\n0000000..." can be used to
167 * confuse downstream git-pack-objects very badly.
169 const char *ep
= strchr(objects
->name
, '\n');
171 printf("%s %.*s\n", sha1_to_hex(objects
->item
->sha1
),
172 (int) (ep
- objects
->name
),
176 printf("%s %s\n", sha1_to_hex(objects
->item
->sha1
), objects
->name
);
177 objects
= objects
->next
;
182 * This is a truly stupid algorithm, but it's only
183 * used for bisection, and we just don't care enough.
185 * We care just barely enough to avoid recursing for
188 static int count_distance(struct commit_list
*entry
)
193 struct commit
*commit
= entry
->item
;
194 struct commit_list
*p
;
196 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
198 if (!revs
.prune_fn
|| (commit
->object
.flags
& TREECHANGE
))
200 commit
->object
.flags
|= COUNTED
;
206 nr
+= count_distance(p
);
215 static void clear_distance(struct commit_list
*list
)
218 struct commit
*commit
= list
->item
;
219 commit
->object
.flags
&= ~COUNTED
;
224 static struct commit_list
*find_bisection(struct commit_list
*list
)
227 struct commit_list
*p
, *best
;
232 if (!revs
.prune_fn
|| (p
->item
->object
.flags
& TREECHANGE
))
239 for (p
= list
; p
; p
= p
->next
) {
242 if (revs
.prune_fn
&& !(p
->item
->object
.flags
& TREECHANGE
))
245 distance
= count_distance(p
);
246 clear_distance(list
);
247 if (nr
- distance
< distance
)
248 distance
= nr
- distance
;
249 if (distance
> closest
) {
259 static void mark_edge_parents_uninteresting(struct commit
*commit
)
261 struct commit_list
*parents
;
263 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
264 struct commit
*parent
= parents
->item
;
265 if (!(parent
->object
.flags
& UNINTERESTING
))
267 mark_tree_uninteresting(parent
->tree
);
268 if (revs
.edge_hint
&& !(parent
->object
.flags
& SHOWN
)) {
269 parent
->object
.flags
|= SHOWN
;
270 printf("-%s\n", sha1_to_hex(parent
->object
.sha1
));
275 static void mark_edges_uninteresting(struct commit_list
*list
)
277 for ( ; list
; list
= list
->next
) {
278 struct commit
*commit
= list
->item
;
280 if (commit
->object
.flags
& UNINTERESTING
) {
281 mark_tree_uninteresting(commit
->tree
);
284 mark_edge_parents_uninteresting(commit
);
288 int main(int argc
, const char **argv
)
290 struct commit_list
*list
;
293 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
295 for (i
= 1 ; i
< argc
; i
++) {
296 const char *arg
= argv
[i
];
298 /* accept -<digit>, like traditilnal "head" */
299 if ((*arg
== '-') && isdigit(arg
[1])) {
300 revs
.max_count
= atoi(arg
+ 1);
303 if (!strcmp(arg
, "-n")) {
305 die("-n requires an argument");
306 revs
.max_count
= atoi(argv
[i
]);
309 if (!strncmp(arg
,"-n",2)) {
310 revs
.max_count
= atoi(arg
+ 2);
313 if (!strcmp(arg
, "--header")) {
317 if (!strcmp(arg
, "--no-abbrev")) {
321 if (!strncmp(arg
, "--abbrev=", 9)) {
322 abbrev
= strtoul(arg
+ 9, NULL
, 10);
323 if (abbrev
&& abbrev
< MINIMUM_ABBREV
)
324 abbrev
= MINIMUM_ABBREV
;
325 else if (40 < abbrev
)
329 if (!strncmp(arg
, "--pretty", 8)) {
330 commit_format
= get_commit_format(arg
+8);
332 hdr_termination
= '\n';
333 if (commit_format
== CMIT_FMT_ONELINE
)
336 commit_prefix
= "commit ";
339 if (!strcmp(arg
, "--parents")) {
343 if (!strcmp(arg
, "--timestamp")) {
347 if (!strcmp(arg
, "--bisect")) {
351 usage(rev_list_usage
);
358 (!(revs
.tag_objects
||revs
.tree_objects
||revs
.blob_objects
) && !revs
.pending_objects
))
359 usage(rev_list_usage
);
361 prepare_revision_walk(&revs
);
362 if (revs
.tree_objects
)
363 mark_edges_uninteresting(revs
.commits
);
366 revs
.commits
= find_bisection(revs
.commits
);
368 save_commit_buffer
= verbose_header
;
369 track_object_refs
= 0;
371 show_commit_list(&revs
);