10 #include "list-objects.h"
13 /* bits #0-15 in revision.h */
15 #define COUNTED (1u<<16)
17 static const char rev_list_usage[] =
18 "git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
31 " formatting output:\n"
33 " --objects | --objects-edge\n"
35 " --header | --pretty\n"
36 " --abbrev=nr | --no-abbrev\n"
42 static struct rev_info revs;
44 static int bisect_list;
45 static int show_timestamp;
46 static int hdr_termination;
47 static const char *header_prefix;
49 static void show_commit(struct commit *commit)
52 printf("%lu ", commit->date);
54 fputs(header_prefix, stdout);
55 if (commit->object.flags & BOUNDARY)
57 else if (revs.left_right) {
58 if (commit->object.flags & SYMMETRIC_LEFT)
63 if (revs.abbrev_commit && revs.abbrev)
64 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
67 fputs(sha1_to_hex(commit->object.sha1), stdout);
69 struct commit_list *parents = commit->parents;
71 struct object *o = &(parents->item->object);
72 parents = parents->next;
73 if (o->flags & TMP_MARK)
75 printf(" %s", sha1_to_hex(o->sha1));
78 /* TMP_MARK is a general purpose flag that can
79 * be used locally, but the user should clean
80 * things up after it is done with them.
82 for (parents = commit->parents;
84 parents = parents->next)
85 parents->item->object.flags &= ~TMP_MARK;
87 if (revs.commit_format == CMIT_FMT_ONELINE)
92 if (revs.verbose_header) {
93 static char pretty_header[16384];
94 pretty_print_commit(revs.commit_format, commit, ~0,
95 pretty_header, sizeof(pretty_header),
96 revs.abbrev, NULL, NULL, revs.relative_date);
97 printf("%s%c", pretty_header, hdr_termination);
100 if (commit->parents) {
101 free_commit_list(commit->parents);
102 commit->parents = NULL;
104 free(commit->buffer);
105 commit->buffer = NULL;
108 static void show_object(struct object_array_entry *p)
110 /* An object with name "foo\n0000000..." can be used to
111 * confuse downstream git-pack-objects very badly.
113 const char *ep = strchr(p->name, '\n');
115 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
116 (int) (ep - p->name),
120 printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
123 static void show_edge(struct commit *commit)
125 printf("-%s\n", sha1_to_hex(commit->object.sha1));
129 * This is a truly stupid algorithm, but it's only
130 * used for bisection, and we just don't care enough.
132 * We care just barely enough to avoid recursing for
135 static int count_distance(struct commit_list *entry)
140 struct commit *commit = entry->item;
141 struct commit_list *p;
143 if (commit->object.flags & (UNINTERESTING | COUNTED))
145 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
147 commit->object.flags |= COUNTED;
153 nr += count_distance(p);
162 static void clear_distance(struct commit_list *list)
165 struct commit *commit = list->item;
166 commit->object.flags &= ~COUNTED;
171 static struct commit_list *find_bisection(struct commit_list *list)
174 struct commit_list *p, *best;
179 if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
186 for (p = list; p; p = p->next) {
189 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
192 distance = count_distance(p);
193 clear_distance(list);
194 if (nr - distance < distance)
195 distance = nr - distance;
196 if (distance > closest) {
206 static void read_revisions_from_stdin(struct rev_info *revs)
210 while (fgets(line, sizeof(line), stdin) != NULL) {
211 int len = strlen(line);
212 if (line[len - 1] == '\n')
217 die("options not supported in --stdin mode");
218 if (handle_revision_arg(line, revs, 0, 1))
219 die("bad revision '%s'", line);
223 int cmd_rev_list(int argc, const char **argv, const char *prefix)
225 struct commit_list *list;
227 int read_from_stdin = 0;
229 git_config(git_default_config);
230 init_revisions(&revs, prefix);
232 revs.commit_format = CMIT_FMT_UNSPECIFIED;
233 argc = setup_revisions(argc, argv, &revs, NULL);
235 for (i = 1 ; i < argc; i++) {
236 const char *arg = argv[i];
238 if (!strcmp(arg, "--header")) {
239 revs.verbose_header = 1;
242 if (!strcmp(arg, "--timestamp")) {
246 if (!strcmp(arg, "--bisect")) {
250 if (!strcmp(arg, "--stdin")) {
251 if (read_from_stdin++)
252 die("--stdin given twice?");
253 read_revisions_from_stdin(&revs);
256 usage(rev_list_usage);
259 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
260 /* The command line has a --pretty */
261 hdr_termination = '\n';
262 if (revs.commit_format == CMIT_FMT_ONELINE)
265 header_prefix = "commit ";
267 else if (revs.verbose_header)
268 /* Only --header was specified */
269 revs.commit_format = CMIT_FMT_RAW;
274 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
275 !revs.pending.nr)) ||
277 usage(rev_list_usage);
279 save_commit_buffer = revs.verbose_header || revs.grep_filter;
280 track_object_refs = 0;
284 prepare_revision_walk(&revs);
285 if (revs.tree_objects)
286 mark_edges_uninteresting(revs.commits, &revs, show_edge);
289 revs.commits = find_bisection(revs.commits);
291 traverse_commit_list(&revs, show_commit, show_object);