1#include "cache.h"
2#include "refs.h"
3#include "tag.h"
4#include "commit.h"
5#include "tree.h"
6#include "blob.h"
7#include "diff.h"
8#include "revision.h"
9
10/* bits #0-4 in revision.h */
11
12#define COUNTED (1u<<5)
13
14static const char rev_list_usage[] =
15"git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
16" limiting output:\n"
17" --max-count=nr\n"
18" --max-age=epoch\n"
19" --min-age=epoch\n"
20" --sparse\n"
21" --no-merges\n"
22" --remove-empty\n"
23" --all\n"
24" ordering output:\n"
25" --topo-order\n"
26" --date-order\n"
27" formatting output:\n"
28" --parents\n"
29" --objects | --objects-edge\n"
30" --unpacked\n"
31" --header | --pretty\n"
32" --abbrev=nr | --no-abbrev\n"
33" special purpose:\n"
34" --bisect"
35;
36
37struct rev_info revs;
38
39static int bisect_list = 0;
40static int verbose_header = 0;
41static int abbrev = DEFAULT_ABBREV;
42static int show_parents = 0;
43static int show_timestamp = 0;
44static int hdr_termination = 0;
45static const char *commit_prefix = "";
46static enum cmit_fmt commit_format = CMIT_FMT_RAW;
47
48static void show_commit(struct commit *commit)
49{
50 if (show_timestamp)
51 printf("%lu ", commit->date);
52 if (commit_prefix[0])
53 fputs(commit_prefix, stdout);
54 fputs(sha1_to_hex(commit->object.sha1), stdout);
55 if (show_parents) {
56 struct commit_list *parents = commit->parents;
57 while (parents) {
58 struct object *o = &(parents->item->object);
59 parents = parents->next;
60 if (o->flags & TMP_MARK)
61 continue;
62 printf(" %s", sha1_to_hex(o->sha1));
63 o->flags |= TMP_MARK;
64 }
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.
68 */
69 for (parents = commit->parents;
70 parents;
71 parents = parents->next)
72 parents->item->object.flags &= ~TMP_MARK;
73 }
74 if (commit_format == CMIT_FMT_ONELINE)
75 putchar(' ');
76 else
77 putchar('\n');
78
79 if (verbose_header) {
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);
83 }
84 fflush(stdout);
85}
86
87static struct object_list **process_blob(struct blob *blob,
88 struct object_list **p,
89 struct name_path *path,
90 const char *name)
91{
92 struct object *obj = &blob->object;
93
94 if (!revs.blob_objects)
95 return p;
96 if (obj->flags & (UNINTERESTING | SEEN))
97 return p;
98 obj->flags |= SEEN;
99 return add_object(obj, p, path, name);
100}
101
102static struct object_list **process_tree(struct tree *tree,
103 struct object_list **p,
104 struct name_path *path,
105 const char *name)
106{
107 struct object *obj = &tree->object;
108 struct tree_entry_list *entry;
109 struct name_path me;
110
111 if (!revs.tree_objects)
112 return p;
113 if (obj->flags & (UNINTERESTING | SEEN))
114 return p;
115 if (parse_tree(tree) < 0)
116 die("bad tree object %s", sha1_to_hex(obj->sha1));
117 obj->flags |= SEEN;
118 p = add_object(obj, p, path, name);
119 me.up = path;
120 me.elem = name;
121 me.elem_len = strlen(name);
122 entry = tree->entries;
123 tree->entries = NULL;
124 while (entry) {
125 struct tree_entry_list *next = entry->next;
126 if (entry->directory)
127 p = process_tree(entry->item.tree, p, &me, entry->name);
128 else
129 p = process_blob(entry->item.blob, p, &me, entry->name);
130 free(entry);
131 entry = next;
132 }
133 return p;
134}
135
136static void show_commit_list(struct rev_info *revs)
137{
138 struct commit *commit;
139 struct object_list *objects = NULL, **p = &objects, *pending;
140
141 while ((commit = get_revision(revs)) != NULL) {
142 p = process_tree(commit->tree, p, NULL, "");
143 show_commit(commit);
144 }
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))
149 continue;
150 if (obj->type == tag_type) {
151 obj->flags |= SEEN;
152 p = add_object(obj, p, NULL, name);
153 continue;
154 }
155 if (obj->type == tree_type) {
156 p = process_tree((struct tree *)obj, p, NULL, name);
157 continue;
158 }
159 if (obj->type == blob_type) {
160 p = process_blob((struct blob *)obj, p, NULL, name);
161 continue;
162 }
163 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
164 }
165 while (objects) {
166 /* An object with name "foo\n0000000..." can be used to
167 * confuse downstream git-pack-objects very badly.
168 */
169 const char *ep = strchr(objects->name, '\n');
170 if (ep) {
171 printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
172 (int) (ep - objects->name),
173 objects->name);
174 }
175 else
176 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
177 objects = objects->next;
178 }
179}
180
181/*
182 * This is a truly stupid algorithm, but it's only
183 * used for bisection, and we just don't care enough.
184 *
185 * We care just barely enough to avoid recursing for
186 * non-merge entries.
187 */
188static int count_distance(struct commit_list *entry)
189{
190 int nr = 0;
191
192 while (entry) {
193 struct commit *commit = entry->item;
194 struct commit_list *p;
195
196 if (commit->object.flags & (UNINTERESTING | COUNTED))
197 break;
198 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
199 nr++;
200 commit->object.flags |= COUNTED;
201 p = commit->parents;
202 entry = p;
203 if (p) {
204 p = p->next;
205 while (p) {
206 nr += count_distance(p);
207 p = p->next;
208 }
209 }
210 }
211
212 return nr;
213}
214
215static void clear_distance(struct commit_list *list)
216{
217 while (list) {
218 struct commit *commit = list->item;
219 commit->object.flags &= ~COUNTED;
220 list = list->next;
221 }
222}
223
224static struct commit_list *find_bisection(struct commit_list *list)
225{
226 int nr, closest;
227 struct commit_list *p, *best;
228
229 nr = 0;
230 p = list;
231 while (p) {
232 if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
233 nr++;
234 p = p->next;
235 }
236 closest = 0;
237 best = list;
238
239 for (p = list; p; p = p->next) {
240 int distance;
241
242 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
243 continue;
244
245 distance = count_distance(p);
246 clear_distance(list);
247 if (nr - distance < distance)
248 distance = nr - distance;
249 if (distance > closest) {
250 best = p;
251 closest = distance;
252 }
253 }
254 if (best)
255 best->next = NULL;
256 return best;
257}
258
259static void mark_edge_parents_uninteresting(struct commit *commit)
260{
261 struct commit_list *parents;
262
263 for (parents = commit->parents; parents; parents = parents->next) {
264 struct commit *parent = parents->item;
265 if (!(parent->object.flags & UNINTERESTING))
266 continue;
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));
271 }
272 }
273}
274
275static void mark_edges_uninteresting(struct commit_list *list)
276{
277 for ( ; list; list = list->next) {
278 struct commit *commit = list->item;
279
280 if (commit->object.flags & UNINTERESTING) {
281 mark_tree_uninteresting(commit->tree);
282 continue;
283 }
284 mark_edge_parents_uninteresting(commit);
285 }
286}
287
288int main(int argc, const char **argv)
289{
290 struct commit_list *list;
291 int i;
292
293 argc = setup_revisions(argc, argv, &revs, NULL);
294
295 for (i = 1 ; i < argc; i++) {
296 const char *arg = argv[i];
297
298 /* accept -<digit>, like traditilnal "head" */
299 if ((*arg == '-') && isdigit(arg[1])) {
300 revs.max_count = atoi(arg + 1);
301 continue;
302 }
303 if (!strcmp(arg, "-n")) {
304 if (++i >= argc)
305 die("-n requires an argument");
306 revs.max_count = atoi(argv[i]);
307 continue;
308 }
309 if (!strncmp(arg,"-n",2)) {
310 revs.max_count = atoi(arg + 2);
311 continue;
312 }
313 if (!strcmp(arg, "--header")) {
314 verbose_header = 1;
315 continue;
316 }
317 if (!strcmp(arg, "--no-abbrev")) {
318 abbrev = 0;
319 continue;
320 }
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)
326 abbrev = 40;
327 continue;
328 }
329 if (!strncmp(arg, "--pretty", 8)) {
330 commit_format = get_commit_format(arg+8);
331 verbose_header = 1;
332 hdr_termination = '\n';
333 if (commit_format == CMIT_FMT_ONELINE)
334 commit_prefix = "";
335 else
336 commit_prefix = "commit ";
337 continue;
338 }
339 if (!strcmp(arg, "--parents")) {
340 show_parents = 1;
341 continue;
342 }
343 if (!strcmp(arg, "--timestamp")) {
344 show_timestamp = 1;
345 continue;
346 }
347 if (!strcmp(arg, "--bisect")) {
348 bisect_list = 1;
349 continue;
350 }
351 usage(rev_list_usage);
352
353 }
354
355 list = revs.commits;
356
357 if (!list &&
358 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) && !revs.pending_objects))
359 usage(rev_list_usage);
360
361 prepare_revision_walk(&revs);
362 if (revs.tree_objects)
363 mark_edges_uninteresting(revs.commits);
364
365 if (bisect_list)
366 revs.commits = find_bisection(revs.commits);
367
368 save_commit_buffer = verbose_header;
369 track_object_refs = 0;
370
371 show_commit_list(&revs);
372
373 return 0;
374}