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