revision.c: allow injecting revision parameters after setup_revisions().
[git/git.git] / builtin-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"
1b0c7174 7#include "tree-walk.h"
c4e05b1a 8#include "diff.h"
ae563542 9#include "revision.h"
5fb61b8d 10#include "builtin.h"
ae563542 11
1b65a5aa 12/* bits #0-15 in revision.h */
64745109 13
1b65a5aa 14#define COUNTED (1u<<16)
8906300f 15
a6f68d47 16static const char rev_list_usage[] =
69e0c256
JH
17"git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
18" limiting output:\n"
19" --max-count=nr\n"
20" --max-age=epoch\n"
21" --min-age=epoch\n"
22" --sparse\n"
23" --no-merges\n"
93b74bca 24" --remove-empty\n"
69e0c256
JH
25" --all\n"
26" ordering output:\n"
69e0c256 27" --topo-order\n"
4c8725f1 28" --date-order\n"
69e0c256
JH
29" formatting output:\n"
30" --parents\n"
c6496575 31" --objects | --objects-edge\n"
69e0c256
JH
32" --unpacked\n"
33" --header | --pretty\n"
9da5c2f0 34" --abbrev=nr | --no-abbrev\n"
5c51c985 35" --abbrev-commit\n"
69e0c256
JH
36" special purpose:\n"
37" --bisect"
38;
a6f68d47 39
5fb61b8d 40static struct rev_info revs;
ae563542 41
96f1e58f
DR
42static int bisect_list;
43static int show_timestamp;
44static int hdr_termination;
91539833 45static const char *header_prefix;
e646de0d 46
81f2bb1f
LT
47static void show_commit(struct commit *commit)
48{
dc68c4ff
JH
49 if (show_timestamp)
50 printf("%lu ", commit->date);
91539833
LT
51 if (header_prefix)
52 fputs(header_prefix, stdout);
384e99a4
JH
53 if (commit->object.flags & BOUNDARY)
54 putchar('-');
7594c4b2
JH
55 if (revs.abbrev_commit && revs.abbrev)
56 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
57 stdout);
5c51c985
JH
58 else
59 fputs(sha1_to_hex(commit->object.sha1), stdout);
7b0c9966 60 if (revs.parents) {
81f2bb1f
LT
61 struct commit_list *parents = commit->parents;
62 while (parents) {
88494423 63 struct object *o = &(parents->item->object);
81f2bb1f 64 parents = parents->next;
88494423
JH
65 if (o->flags & TMP_MARK)
66 continue;
67 printf(" %s", sha1_to_hex(o->sha1));
68 o->flags |= TMP_MARK;
81f2bb1f 69 }
88494423
JH
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.
73 */
74 for (parents = commit->parents;
75 parents;
76 parents = parents->next)
77 parents->item->object.flags &= ~TMP_MARK;
81f2bb1f 78 }
7594c4b2 79 if (revs.commit_format == CMIT_FMT_ONELINE)
d87449c5
JH
80 putchar(' ');
81 else
82 putchar('\n');
83
7594c4b2 84 if (revs.verbose_header) {
000182ea 85 static char pretty_header[16384];
7594c4b2
JH
86 pretty_print_commit(revs.commit_format, commit, ~0,
87 pretty_header, sizeof(pretty_header),
3dfb9278 88 revs.abbrev, NULL, NULL, revs.relative_date);
000182ea 89 printf("%s%c", pretty_header, hdr_termination);
7620d39f
LT
90 }
91 fflush(stdout);
cb115748
LT
92 if (commit->parents) {
93 free_commit_list(commit->parents);
94 commit->parents = NULL;
95 }
4cac42b1
JH
96 free(commit->buffer);
97 commit->buffer = NULL;
a3437b8c
JS
98}
99
1f1e895f
LT
100static void process_blob(struct blob *blob,
101 struct object_array *p,
102 struct name_path *path,
103 const char *name)
9de48752
LT
104{
105 struct object *obj = &blob->object;
106
ae563542 107 if (!revs.blob_objects)
1f1e895f 108 return;
9de48752 109 if (obj->flags & (UNINTERESTING | SEEN))
1f1e895f 110 return;
9de48752 111 obj->flags |= SEEN;
9befac47 112 name = xstrdup(name);
1f1e895f 113 add_object(obj, p, path, name);
9de48752
LT
114}
115
1f1e895f
LT
116static void process_tree(struct tree *tree,
117 struct object_array *p,
118 struct name_path *path,
119 const char *name)
9de48752
LT
120{
121 struct object *obj = &tree->object;
2d9c58c6 122 struct tree_desc desc;
4c068a98 123 struct name_entry entry;
e646de0d 124 struct name_path me;
9de48752 125
ae563542 126 if (!revs.tree_objects)
1f1e895f 127 return;
9de48752 128 if (obj->flags & (UNINTERESTING | SEEN))
1f1e895f 129 return;
9de48752
LT
130 if (parse_tree(tree) < 0)
131 die("bad tree object %s", sha1_to_hex(obj->sha1));
132 obj->flags |= SEEN;
9befac47 133 name = xstrdup(name);
1f1e895f 134 add_object(obj, p, path, name);
e646de0d
JH
135 me.up = path;
136 me.elem = name;
137 me.elem_len = strlen(name);
2d9c58c6
LT
138
139 desc.buf = tree->buffer;
140 desc.size = tree->size;
141
4c068a98
LT
142 while (tree_entry(&desc, &entry)) {
143 if (S_ISDIR(entry.mode))
1f1e895f 144 process_tree(lookup_tree(entry.sha1), p, &me, entry.path);
9de48752 145 else
1f1e895f 146 process_blob(lookup_blob(entry.sha1), p, &me, entry.path);
9de48752 147 }
136f2e54
LT
148 free(tree->buffer);
149 tree->buffer = NULL;
9de48752
LT
150}
151
a4a88b2b 152static void show_commit_list(struct rev_info *revs)
81f2bb1f 153{
1f1e895f 154 int i;
a4a88b2b 155 struct commit *commit;
1f1e895f 156 struct object_array objects = { 0, 0, NULL };
81f2bb1f 157
a4a88b2b 158 while ((commit = get_revision(revs)) != NULL) {
1f1e895f 159 process_tree(commit->tree, &objects, NULL, "");
765ac8ec 160 show_commit(commit);
81f2bb1f 161 }
1f1e895f
LT
162 for (i = 0; i < revs->pending.nr; i++) {
163 struct object_array_entry *pending = revs->pending.objects + i;
36f8d174
LT
164 struct object *obj = pending->item;
165 const char *name = pending->name;
166 if (obj->flags & (UNINTERESTING | SEEN))
167 continue;
1974632c 168 if (obj->type == OBJ_TAG) {
36f8d174 169 obj->flags |= SEEN;
1f1e895f 170 add_object_array(obj, name, &objects);
36f8d174
LT
171 continue;
172 }
1974632c 173 if (obj->type == OBJ_TREE) {
1f1e895f 174 process_tree((struct tree *)obj, &objects, NULL, name);
36f8d174
LT
175 continue;
176 }
1974632c 177 if (obj->type == OBJ_BLOB) {
1f1e895f 178 process_blob((struct blob *)obj, &objects, NULL, name);
36f8d174
LT
179 continue;
180 }
181 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
182 }
1f1e895f
LT
183 for (i = 0; i < objects.nr; i++) {
184 struct object_array_entry *p = objects.objects + i;
185
50319850
JH
186 /* An object with name "foo\n0000000..." can be used to
187 * confuse downstream git-pack-objects very badly.
c807f771 188 */
1f1e895f 189 const char *ep = strchr(p->name, '\n');
c807f771 190 if (ep) {
1f1e895f
LT
191 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
192 (int) (ep - p->name),
193 p->name);
c807f771
JH
194 }
195 else
1f1e895f 196 printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
9de48752
LT
197 }
198}
199
8b3a1e05
LT
200/*
201 * This is a truly stupid algorithm, but it's only
202 * used for bisection, and we just don't care enough.
203 *
204 * We care just barely enough to avoid recursing for
205 * non-merge entries.
206 */
207static int count_distance(struct commit_list *entry)
208{
209 int nr = 0;
210
211 while (entry) {
212 struct commit *commit = entry->item;
213 struct commit_list *p;
214
215 if (commit->object.flags & (UNINTERESTING | COUNTED))
216 break;
8efdc326 217 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
b3cfd939 218 nr++;
8b3a1e05
LT
219 commit->object.flags |= COUNTED;
220 p = commit->parents;
221 entry = p;
222 if (p) {
223 p = p->next;
224 while (p) {
225 nr += count_distance(p);
226 p = p->next;
227 }
228 }
229 }
b3cfd939 230
8b3a1e05
LT
231 return nr;
232}
233
3d958064 234static void clear_distance(struct commit_list *list)
8b3a1e05
LT
235{
236 while (list) {
237 struct commit *commit = list->item;
238 commit->object.flags &= ~COUNTED;
239 list = list->next;
240 }
241}
242
243static struct commit_list *find_bisection(struct commit_list *list)
244{
245 int nr, closest;
246 struct commit_list *p, *best;
247
248 nr = 0;
249 p = list;
250 while (p) {
8efdc326 251 if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
b3cfd939 252 nr++;
8b3a1e05
LT
253 p = p->next;
254 }
255 closest = 0;
256 best = list;
257
b3cfd939
LT
258 for (p = list; p; p = p->next) {
259 int distance;
260
8efdc326 261 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
b3cfd939
LT
262 continue;
263
264 distance = count_distance(p);
8b3a1e05
LT
265 clear_distance(list);
266 if (nr - distance < distance)
267 distance = nr - distance;
268 if (distance > closest) {
269 best = p;
270 closest = distance;
271 }
8b3a1e05
LT
272 }
273 if (best)
274 best->next = NULL;
275 return best;
276}
277
c6496575
JH
278static void mark_edge_parents_uninteresting(struct commit *commit)
279{
280 struct commit_list *parents;
281
282 for (parents = commit->parents; parents; parents = parents->next) {
283 struct commit *parent = parents->item;
284 if (!(parent->object.flags & UNINTERESTING))
285 continue;
286 mark_tree_uninteresting(parent->tree);
ae563542 287 if (revs.edge_hint && !(parent->object.flags & SHOWN)) {
eb38cc68 288 parent->object.flags |= SHOWN;
c6496575 289 printf("-%s\n", sha1_to_hex(parent->object.sha1));
eb38cc68 290 }
c6496575
JH
291 }
292}
293
5bdbaaa4
LT
294static void mark_edges_uninteresting(struct commit_list *list)
295{
296 for ( ; list; list = list->next) {
c6496575 297 struct commit *commit = list->item;
5bdbaaa4 298
c6496575
JH
299 if (commit->object.flags & UNINTERESTING) {
300 mark_tree_uninteresting(commit->tree);
301 continue;
5bdbaaa4 302 }
c6496575 303 mark_edge_parents_uninteresting(commit);
5bdbaaa4
LT
304 }
305}
306
a633fca0 307int cmd_rev_list(int argc, const char **argv, const char *prefix)
64745109 308{
ae563542 309 struct commit_list *list;
d9a83684 310 int i;
64745109 311
a633fca0 312 init_revisions(&revs, prefix);
7594c4b2
JH
313 revs.abbrev = 0;
314 revs.commit_format = CMIT_FMT_UNSPECIFIED;
a4a88b2b 315 argc = setup_revisions(argc, argv, &revs, NULL);
ae563542 316
fcfda02b 317 for (i = 1 ; i < argc; i++) {
cf484544 318 const char *arg = argv[i];
fcfda02b 319
a6f68d47 320 if (!strcmp(arg, "--header")) {
7594c4b2 321 revs.verbose_header = 1;
9d97aa64
LT
322 continue;
323 }
dc68c4ff
JH
324 if (!strcmp(arg, "--timestamp")) {
325 show_timestamp = 1;
326 continue;
327 }
8b3a1e05
LT
328 if (!strcmp(arg, "--bisect")) {
329 bisect_list = 1;
330 continue;
331 }
ae563542 332 usage(rev_list_usage);
a6f68d47 333
fcfda02b 334 }
7594c4b2
JH
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)
91539833 339 header_prefix = "";
7594c4b2 340 else
91539833 341 header_prefix = "commit ";
7594c4b2 342 }
db89665f
JH
343 else if (revs.verbose_header)
344 /* Only --header was specified */
345 revs.commit_format = CMIT_FMT_RAW;
fcfda02b 346
ae563542 347 list = revs.commits;
ae563542 348
8c1f0b44
JH
349 if ((!list &&
350 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
1f1e895f 351 !revs.pending.nr)) ||
8c1f0b44 352 revs.diff)
7b34c2fa
LT
353 usage(rev_list_usage);
354
7594c4b2 355 save_commit_buffer = revs.verbose_header;
9181ca2c 356 track_object_refs = 0;
4e1dc640
JH
357 if (bisect_list)
358 revs.limited = 1;
9181ca2c 359
a4a88b2b
LT
360 prepare_revision_walk(&revs);
361 if (revs.tree_objects)
362 mark_edges_uninteresting(revs.commits);
363
364 if (bisect_list)
365 revs.commits = find_bisection(revs.commits);
7b34c2fa 366
765ac8ec 367 show_commit_list(&revs);
8906300f 368
64745109
LT
369 return 0;
370}