git-rev-list libification: rev-list walking
[git/git.git] / rev-list.c
CommitLineData
64745109 1#include "cache.h"
e091eb93 2#include "refs.h"
36f8d174 3#include "tag.h"
64745109 4#include "commit.h"
9de48752
LT
5#include "tree.h"
6#include "blob.h"
a3437b8c 7#include "epoch.h"
cf484544 8#include "diff.h"
ae563542
LT
9#include "revision.h"
10
a4a88b2b 11/* bits #0-2 in revision.h */
64745109 12
a4a88b2b
LT
13#define COUNTED (1u << 3)
14#define SHOWN (1u << 4)
88494423 15#define TMP_MARK (1u << 5) /* for isolated cases; clean after use */
8906300f 16
a6f68d47 17static const char rev_list_usage[] =
69e0c256
JH
18"git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
19" limiting output:\n"
20" --max-count=nr\n"
21" --max-age=epoch\n"
22" --min-age=epoch\n"
23" --sparse\n"
24" --no-merges\n"
93b74bca 25" --remove-empty\n"
69e0c256
JH
26" --all\n"
27" ordering output:\n"
28" --merge-order [ --show-breaks ]\n"
29" --topo-order\n"
4c8725f1 30" --date-order\n"
69e0c256
JH
31" formatting output:\n"
32" --parents\n"
c6496575 33" --objects | --objects-edge\n"
69e0c256
JH
34" --unpacked\n"
35" --header | --pretty\n"
9da5c2f0 36" --abbrev=nr | --no-abbrev\n"
69e0c256
JH
37" special purpose:\n"
38" --bisect"
39;
a6f68d47 40
ae563542
LT
41struct rev_info revs;
42
8b3a1e05 43static int bisect_list = 0;
81f2bb1f 44static int verbose_header = 0;
9da5c2f0 45static int abbrev = DEFAULT_ABBREV;
81f2bb1f 46static int show_parents = 0;
81f2bb1f 47static int hdr_termination = 0;
d998a089 48static const char *commit_prefix = "";
000182ea 49static enum cmit_fmt commit_format = CMIT_FMT_RAW;
a3437b8c
JS
50static int merge_order = 0;
51static int show_breaks = 0;
5e749e25 52static int stop_traversal = 0;
76cd8eb6 53static int no_merges = 0;
e646de0d 54
81f2bb1f
LT
55static void show_commit(struct commit *commit)
56{
51b1e171 57 commit->object.flags |= SHOWN;
a3437b8c 58 if (show_breaks) {
d998a089 59 commit_prefix = "| ";
a3437b8c 60 if (commit->object.flags & DISCONTINUITY) {
d998a089 61 commit_prefix = "^ ";
a3437b8c 62 } else if (commit->object.flags & BOUNDARY) {
d998a089 63 commit_prefix = "= ";
a3437b8c
JS
64 }
65 }
d998a089 66 printf("%s%s", commit_prefix, sha1_to_hex(commit->object.sha1));
81f2bb1f
LT
67 if (show_parents) {
68 struct commit_list *parents = commit->parents;
69 while (parents) {
88494423 70 struct object *o = &(parents->item->object);
81f2bb1f 71 parents = parents->next;
88494423
JH
72 if (o->flags & TMP_MARK)
73 continue;
74 printf(" %s", sha1_to_hex(o->sha1));
75 o->flags |= TMP_MARK;
81f2bb1f 76 }
88494423
JH
77 /* TMP_MARK is a general purpose flag that can
78 * be used locally, but the user should clean
79 * things up after it is done with them.
80 */
81 for (parents = commit->parents;
82 parents;
83 parents = parents->next)
84 parents->item->object.flags &= ~TMP_MARK;
81f2bb1f 85 }
d87449c5
JH
86 if (commit_format == CMIT_FMT_ONELINE)
87 putchar(' ');
88 else
89 putchar('\n');
90
81f2bb1f 91 if (verbose_header) {
000182ea 92 static char pretty_header[16384];
9da5c2f0 93 pretty_print_commit(commit_format, commit, ~0, pretty_header, sizeof(pretty_header), abbrev);
000182ea 94 printf("%s%c", pretty_header, hdr_termination);
7620d39f
LT
95 }
96 fflush(stdout);
a3437b8c
JS
97}
98
129adf4d 99static int rewrite_one(struct commit **pp)
1b9e059d
LT
100{
101 for (;;) {
102 struct commit *p = *pp;
103 if (p->object.flags & (TREECHANGE | UNINTERESTING))
129adf4d
LT
104 return 0;
105 if (!p->parents)
106 return -1;
1b9e059d
LT
107 *pp = p->parents->item;
108 }
109}
110
111static void rewrite_parents(struct commit *commit)
112{
129adf4d
LT
113 struct commit_list **pp = &commit->parents;
114 while (*pp) {
115 struct commit_list *parent = *pp;
116 if (rewrite_one(&parent->item) < 0) {
117 *pp = parent->next;
118 continue;
119 }
120 pp = &parent->next;
1b9e059d
LT
121 }
122}
123
a3437b8c
JS
124static int filter_commit(struct commit * commit)
125{
d2775a81 126 if (stop_traversal && (commit->object.flags & BOUNDARY))
5e749e25 127 return STOP;
51b1e171 128 if (commit->object.flags & (UNINTERESTING|SHOWN))
a3437b8c 129 return CONTINUE;
ae563542 130 if (revs.min_age != -1 && (commit->date > revs.min_age))
a3437b8c 131 return CONTINUE;
ae563542 132 if (revs.max_age != -1 && (commit->date < revs.max_age)) {
d2775a81 133 stop_traversal=1;
27cfe2e2 134 return CONTINUE;
5e749e25 135 }
76cd8eb6
JS
136 if (no_merges && (commit->parents && commit->parents->next))
137 return CONTINUE;
ae563542 138 if (revs.paths && revs.dense) {
1b9e059d
LT
139 if (!(commit->object.flags & TREECHANGE))
140 return CONTINUE;
141 rewrite_parents(commit);
142 }
a3437b8c
JS
143 return DO;
144}
145
146static int process_commit(struct commit * commit)
147{
148 int action=filter_commit(commit);
149
150 if (action == STOP) {
151 return STOP;
152 }
153
154 if (action == CONTINUE) {
155 return CONTINUE;
81f2bb1f 156 }
a3437b8c 157
ae563542 158 if (revs.max_count != -1 && !revs.max_count--)
07f92477
LT
159 return STOP;
160
a3437b8c
JS
161 show_commit(commit);
162
163 return CONTINUE;
81f2bb1f
LT
164}
165
e646de0d
JH
166static struct object_list **process_blob(struct blob *blob,
167 struct object_list **p,
168 struct name_path *path,
169 const char *name)
9de48752
LT
170{
171 struct object *obj = &blob->object;
172
ae563542 173 if (!revs.blob_objects)
9de48752
LT
174 return p;
175 if (obj->flags & (UNINTERESTING | SEEN))
176 return p;
177 obj->flags |= SEEN;
e646de0d 178 return add_object(obj, p, path, name);
9de48752
LT
179}
180
e646de0d
JH
181static struct object_list **process_tree(struct tree *tree,
182 struct object_list **p,
183 struct name_path *path,
184 const char *name)
9de48752
LT
185{
186 struct object *obj = &tree->object;
187 struct tree_entry_list *entry;
e646de0d 188 struct name_path me;
9de48752 189
ae563542 190 if (!revs.tree_objects)
9de48752
LT
191 return p;
192 if (obj->flags & (UNINTERESTING | SEEN))
193 return p;
194 if (parse_tree(tree) < 0)
195 die("bad tree object %s", sha1_to_hex(obj->sha1));
196 obj->flags |= SEEN;
e646de0d
JH
197 p = add_object(obj, p, path, name);
198 me.up = path;
199 me.elem = name;
200 me.elem_len = strlen(name);
b0d8923e
LT
201 entry = tree->entries;
202 tree->entries = NULL;
203 while (entry) {
204 struct tree_entry_list *next = entry->next;
9de48752 205 if (entry->directory)
e646de0d 206 p = process_tree(entry->item.tree, p, &me, entry->name);
9de48752 207 else
e646de0d 208 p = process_blob(entry->item.blob, p, &me, entry->name);
b0d8923e
LT
209 free(entry);
210 entry = next;
9de48752
LT
211 }
212 return p;
213}
214
a4a88b2b 215static void show_commit_list(struct rev_info *revs)
81f2bb1f 216{
a4a88b2b 217 struct commit *commit;
36f8d174 218 struct object_list *objects = NULL, **p = &objects, *pending;
81f2bb1f 219
a4a88b2b 220 while ((commit = get_revision(revs)) != NULL) {
e646de0d 221 p = process_tree(commit->tree, p, NULL, "");
a3437b8c 222 if (process_commit(commit) == STOP)
81f2bb1f 223 break;
81f2bb1f 224 }
a4a88b2b 225 for (pending = revs->pending_objects; pending; pending = pending->next) {
36f8d174
LT
226 struct object *obj = pending->item;
227 const char *name = pending->name;
228 if (obj->flags & (UNINTERESTING | SEEN))
229 continue;
230 if (obj->type == tag_type) {
231 obj->flags |= SEEN;
e646de0d 232 p = add_object(obj, p, NULL, name);
36f8d174
LT
233 continue;
234 }
235 if (obj->type == tree_type) {
e646de0d 236 p = process_tree((struct tree *)obj, p, NULL, name);
36f8d174
LT
237 continue;
238 }
239 if (obj->type == blob_type) {
e646de0d 240 p = process_blob((struct blob *)obj, p, NULL, name);
36f8d174
LT
241 continue;
242 }
243 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
244 }
9de48752 245 while (objects) {
50319850
JH
246 /* An object with name "foo\n0000000..." can be used to
247 * confuse downstream git-pack-objects very badly.
c807f771
JH
248 */
249 const char *ep = strchr(objects->name, '\n');
250 if (ep) {
251 printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
252 (int) (ep - objects->name),
253 objects->name);
254 }
255 else
256 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
9de48752
LT
257 objects = objects->next;
258 }
259}
260
8b3a1e05
LT
261/*
262 * This is a truly stupid algorithm, but it's only
263 * used for bisection, and we just don't care enough.
264 *
265 * We care just barely enough to avoid recursing for
266 * non-merge entries.
267 */
268static int count_distance(struct commit_list *entry)
269{
270 int nr = 0;
271
272 while (entry) {
273 struct commit *commit = entry->item;
274 struct commit_list *p;
275
276 if (commit->object.flags & (UNINTERESTING | COUNTED))
277 break;
ae563542 278 if (!revs.paths || (commit->object.flags & TREECHANGE))
b3cfd939 279 nr++;
8b3a1e05
LT
280 commit->object.flags |= COUNTED;
281 p = commit->parents;
282 entry = p;
283 if (p) {
284 p = p->next;
285 while (p) {
286 nr += count_distance(p);
287 p = p->next;
288 }
289 }
290 }
b3cfd939 291
8b3a1e05
LT
292 return nr;
293}
294
3d958064 295static void clear_distance(struct commit_list *list)
8b3a1e05
LT
296{
297 while (list) {
298 struct commit *commit = list->item;
299 commit->object.flags &= ~COUNTED;
300 list = list->next;
301 }
302}
303
304static struct commit_list *find_bisection(struct commit_list *list)
305{
306 int nr, closest;
307 struct commit_list *p, *best;
308
309 nr = 0;
310 p = list;
311 while (p) {
ae563542 312 if (!revs.paths || (p->item->object.flags & TREECHANGE))
b3cfd939 313 nr++;
8b3a1e05
LT
314 p = p->next;
315 }
316 closest = 0;
317 best = list;
318
b3cfd939
LT
319 for (p = list; p; p = p->next) {
320 int distance;
321
ae563542 322 if (revs.paths && !(p->item->object.flags & TREECHANGE))
b3cfd939
LT
323 continue;
324
325 distance = count_distance(p);
8b3a1e05
LT
326 clear_distance(list);
327 if (nr - distance < distance)
328 distance = nr - distance;
329 if (distance > closest) {
330 best = p;
331 closest = distance;
332 }
8b3a1e05
LT
333 }
334 if (best)
335 best->next = NULL;
336 return best;
337}
338
c6496575
JH
339static void mark_edge_parents_uninteresting(struct commit *commit)
340{
341 struct commit_list *parents;
342
343 for (parents = commit->parents; parents; parents = parents->next) {
344 struct commit *parent = parents->item;
345 if (!(parent->object.flags & UNINTERESTING))
346 continue;
347 mark_tree_uninteresting(parent->tree);
ae563542 348 if (revs.edge_hint && !(parent->object.flags & SHOWN)) {
eb38cc68 349 parent->object.flags |= SHOWN;
c6496575 350 printf("-%s\n", sha1_to_hex(parent->object.sha1));
eb38cc68 351 }
c6496575
JH
352 }
353}
354
5bdbaaa4
LT
355static void mark_edges_uninteresting(struct commit_list *list)
356{
357 for ( ; list; list = list->next) {
c6496575 358 struct commit *commit = list->item;
5bdbaaa4 359
c6496575
JH
360 if (commit->object.flags & UNINTERESTING) {
361 mark_tree_uninteresting(commit->tree);
362 continue;
5bdbaaa4 363 }
c6496575 364 mark_edge_parents_uninteresting(commit);
5bdbaaa4
LT
365 }
366}
367
cf484544 368int main(int argc, const char **argv)
64745109 369{
ae563542 370 struct commit_list *list;
d9a83684 371 int i;
64745109 372
a4a88b2b 373 argc = setup_revisions(argc, argv, &revs, NULL);
ae563542 374
fcfda02b 375 for (i = 1 ; i < argc; i++) {
cf484544 376 const char *arg = argv[i];
fcfda02b 377
8233340c
EW
378 /* accept -<digit>, like traditilnal "head" */
379 if ((*arg == '-') && isdigit(arg[1])) {
ae563542 380 revs.max_count = atoi(arg + 1);
8233340c
EW
381 continue;
382 }
3af06987
EW
383 if (!strcmp(arg, "-n")) {
384 if (++i >= argc)
385 die("-n requires an argument");
ae563542 386 revs.max_count = atoi(argv[i]);
3af06987
EW
387 continue;
388 }
389 if (!strncmp(arg,"-n",2)) {
ae563542 390 revs.max_count = atoi(arg + 2);
a6f68d47 391 continue;
fcfda02b 392 }
a6f68d47
LT
393 if (!strcmp(arg, "--header")) {
394 verbose_header = 1;
395 continue;
396 }
9da5c2f0
JH
397 if (!strcmp(arg, "--no-abbrev")) {
398 abbrev = 0;
399 continue;
400 }
401 if (!strncmp(arg, "--abbrev=", 9)) {
402 abbrev = strtoul(arg + 9, NULL, 10);
403 if (abbrev && abbrev < MINIMUM_ABBREV)
404 abbrev = MINIMUM_ABBREV;
405 else if (40 < abbrev)
406 abbrev = 40;
407 continue;
408 }
000182ea
LT
409 if (!strncmp(arg, "--pretty", 8)) {
410 commit_format = get_commit_format(arg+8);
9d97aa64 411 verbose_header = 1;
9d97aa64 412 hdr_termination = '\n';
d87449c5 413 if (commit_format == CMIT_FMT_ONELINE)
d998a089 414 commit_prefix = "";
d87449c5 415 else
d998a089 416 commit_prefix = "commit ";
9d97aa64
LT
417 continue;
418 }
76cd8eb6
JS
419 if (!strncmp(arg, "--no-merges", 11)) {
420 no_merges = 1;
421 continue;
422 }
97658004
LT
423 if (!strcmp(arg, "--parents")) {
424 show_parents = 1;
425 continue;
426 }
8b3a1e05
LT
427 if (!strcmp(arg, "--bisect")) {
428 bisect_list = 1;
429 continue;
430 }
12ba7eaf 431 if (!strcmp(arg, "--merge-order")) {
a3437b8c
JS
432 merge_order = 1;
433 continue;
434 }
12ba7eaf 435 if (!strcmp(arg, "--show-breaks")) {
a3437b8c
JS
436 show_breaks = 1;
437 continue;
438 }
ae563542 439 usage(rev_list_usage);
a6f68d47 440
fcfda02b
KS
441 }
442
ae563542 443 list = revs.commits;
ae563542 444
ef1cc2cc 445 if (!list &&
ae563542 446 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) && !revs.pending_objects))
7b34c2fa
LT
447 usage(rev_list_usage);
448
a4a88b2b
LT
449 prepare_revision_walk(&revs);
450 if (revs.tree_objects)
451 mark_edges_uninteresting(revs.commits);
452
453 if (bisect_list)
454 revs.commits = find_bisection(revs.commits);
7b34c2fa 455
60ab26de 456 save_commit_buffer = verbose_header;
8805ccac 457 track_object_refs = 0;
60ab26de 458
a4a88b2b
LT
459 if (!merge_order) {
460 show_commit_list(&revs);
a3437b8c 461 } else {
dd53c7ab 462#ifndef NO_OPENSSL
a3437b8c 463 if (sort_list_in_merge_order(list, &process_commit)) {
dd53c7ab 464 die("merge order sort failed\n");
a3437b8c 465 }
dd53c7ab
PB
466#else
467 die("merge order sort unsupported, OpenSSL not linked");
468#endif
a3437b8c 469 }
8906300f 470
64745109
LT
471 return 0;
472}