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);
92 if (commit->parents) {
93 free_commit_list(commit->parents);
94 commit->parents = NULL;
98 commit->buffer = NULL;
102 static struct object_list **process_blob(struct blob *blob,
103 struct object_list **p,
104 struct name_path *path,
107 struct object *obj = &blob->object;
109 if (!revs.blob_objects)
111 if (obj->flags & (UNINTERESTING | SEEN))
115 return add_object(obj, p, path, name);
118 static struct object_list **process_tree(struct tree *tree,
119 struct object_list **p,
120 struct name_path *path,
123 struct object *obj = &tree->object;
124 struct tree_desc desc;
125 struct name_entry entry;
128 if (!revs.tree_objects)
130 if (obj->flags & (UNINTERESTING | SEEN))
132 if (parse_tree(tree) < 0)
133 die("bad tree object %s", sha1_to_hex(obj->sha1));
136 p = add_object(obj, p, path, name);
139 me.elem_len = strlen(name);
141 desc.buf = tree->buffer;
142 desc.size = tree->size;
144 while (tree_entry(&desc, &entry)) {
145 if (S_ISDIR(entry.mode))
146 p = process_tree(lookup_tree(entry.sha1), p, &me, entry.path);
148 p = process_blob(lookup_blob(entry.sha1), p, &me, entry.path);
155 static void show_commit_list(struct rev_info *revs)
157 struct commit *commit;
158 struct object_list *objects = NULL, **p = &objects, *pending;
160 while ((commit = get_revision(revs)) != NULL) {
161 p = process_tree(commit->tree, p, NULL, "");
164 for (pending = revs->pending_objects; pending; pending = pending->next) {
165 struct object *obj = pending->item;
166 const char *name = pending->name;
167 if (obj->flags & (UNINTERESTING | SEEN))
169 if (obj->type == TYPE_TAG) {
171 p = add_object(obj, p, NULL, name);
174 if (obj->type == TYPE_TREE) {
175 p = process_tree((struct tree *)obj, p, NULL, name);
178 if (obj->type == TYPE_BLOB) {
179 p = process_blob((struct blob *)obj, p, NULL, name);
182 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
185 /* An object with name "foo\n0000000..." can be used to
186 * confuse downstream git-pack-objects very badly.
188 const char *ep = strchr(objects->name, '\n');
190 printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
191 (int) (ep - objects->name),
195 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
196 objects = objects->next;
201 * This is a truly stupid algorithm, but it's only
202 * used for bisection, and we just don't care enough.
204 * We care just barely enough to avoid recursing for
207 static int count_distance(struct commit_list *entry)
212 struct commit *commit = entry->item;
213 struct commit_list *p;
215 if (commit->object.flags & (UNINTERESTING | COUNTED))
217 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
219 commit->object.flags |= COUNTED;
225 nr += count_distance(p);
234 static void clear_distance(struct commit_list *list)
237 struct commit *commit = list->item;
238 commit->object.flags &= ~COUNTED;
243 static struct commit_list *find_bisection(struct commit_list *list)
246 struct commit_list *p, *best;
251 if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
258 for (p = list; p; p = p->next) {
261 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
264 distance = count_distance(p);
265 clear_distance(list);
266 if (nr - distance < distance)
267 distance = nr - distance;
268 if (distance > closest) {
278 static void mark_edge_parents_uninteresting(struct commit *commit)
280 struct commit_list *parents;
282 for (parents = commit->parents; parents; parents = parents->next) {
283 struct commit *parent = parents->item;
284 if (!(parent->object.flags & UNINTERESTING))
286 mark_tree_uninteresting(parent->tree);
287 if (revs.edge_hint && !(parent->object.flags & SHOWN)) {
288 parent->object.flags |= SHOWN;
289 printf("-%s\n", sha1_to_hex(parent->object.sha1));
294 static void mark_edges_uninteresting(struct commit_list *list)
296 for ( ; list; list = list->next) {
297 struct commit *commit = list->item;
299 if (commit->object.flags & UNINTERESTING) {
300 mark_tree_uninteresting(commit->tree);
303 mark_edge_parents_uninteresting(commit);
307 int cmd_rev_list(int argc, const char **argv, char **envp)
309 struct commit_list *list;
312 init_revisions(&revs);
314 revs.commit_format = CMIT_FMT_UNSPECIFIED;
315 argc = setup_revisions(argc, argv, &revs, NULL);
317 for (i = 1 ; i < argc; i++) {
318 const char *arg = argv[i];
320 if (!strcmp(arg, "--header")) {
321 revs.verbose_header = 1;
324 if (!strcmp(arg, "--timestamp")) {
328 if (!strcmp(arg, "--bisect")) {
332 usage(rev_list_usage);
335 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
336 /* The command line has a --pretty */
337 hdr_termination = '\n';
338 if (revs.commit_format == CMIT_FMT_ONELINE)
341 header_prefix = "commit ";
343 else if (revs.verbose_header)
344 /* Only --header was specified */
345 revs.commit_format = CMIT_FMT_RAW;
350 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
351 !revs.pending_objects)) ||
353 usage(rev_list_usage);
355 save_commit_buffer = revs.verbose_header;
356 track_object_refs = 0;
360 prepare_revision_walk(&revs);
361 if (revs.tree_objects)
362 mark_edges_uninteresting(revs.commits);
365 revs.commits = find_bisection(revs.commits);
367 show_commit_list(&revs);