Use xcalloc instead of calloc
[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),
698ce6f8 88 revs.abbrev, NULL, NULL);
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 }
96 if (commit->buffer) {
97 free(commit->buffer);
98 commit->buffer = NULL;
99 }
a3437b8c
JS
100}
101
1f1e895f
LT
102static void process_blob(struct blob *blob,
103 struct object_array *p,
104 struct name_path *path,
105 const char *name)
9de48752
LT
106{
107 struct object *obj = &blob->object;
108
ae563542 109 if (!revs.blob_objects)
1f1e895f 110 return;
9de48752 111 if (obj->flags & (UNINTERESTING | SEEN))
1f1e895f 112 return;
9de48752 113 obj->flags |= SEEN;
91b452cb 114 name = strdup(name);
1f1e895f 115 add_object(obj, p, path, name);
9de48752
LT
116}
117
1f1e895f
LT
118static void process_tree(struct tree *tree,
119 struct object_array *p,
120 struct name_path *path,
121 const char *name)
9de48752
LT
122{
123 struct object *obj = &tree->object;
2d9c58c6 124 struct tree_desc desc;
4c068a98 125 struct name_entry entry;
e646de0d 126 struct name_path me;
9de48752 127
ae563542 128 if (!revs.tree_objects)
1f1e895f 129 return;
9de48752 130 if (obj->flags & (UNINTERESTING | SEEN))
1f1e895f 131 return;
9de48752
LT
132 if (parse_tree(tree) < 0)
133 die("bad tree object %s", sha1_to_hex(obj->sha1));
134 obj->flags |= SEEN;
91b452cb 135 name = strdup(name);
1f1e895f 136 add_object(obj, p, path, name);
e646de0d
JH
137 me.up = path;
138 me.elem = name;
139 me.elem_len = strlen(name);
2d9c58c6
LT
140
141 desc.buf = tree->buffer;
142 desc.size = tree->size;
143
4c068a98
LT
144 while (tree_entry(&desc, &entry)) {
145 if (S_ISDIR(entry.mode))
1f1e895f 146 process_tree(lookup_tree(entry.sha1), p, &me, entry.path);
9de48752 147 else
1f1e895f 148 process_blob(lookup_blob(entry.sha1), p, &me, entry.path);
9de48752 149 }
136f2e54
LT
150 free(tree->buffer);
151 tree->buffer = NULL;
9de48752
LT
152}
153
a4a88b2b 154static void show_commit_list(struct rev_info *revs)
81f2bb1f 155{
1f1e895f 156 int i;
a4a88b2b 157 struct commit *commit;
1f1e895f 158 struct object_array objects = { 0, 0, NULL };
81f2bb1f 159
a4a88b2b 160 while ((commit = get_revision(revs)) != NULL) {
1f1e895f 161 process_tree(commit->tree, &objects, NULL, "");
765ac8ec 162 show_commit(commit);
81f2bb1f 163 }
1f1e895f
LT
164 for (i = 0; i < revs->pending.nr; i++) {
165 struct object_array_entry *pending = revs->pending.objects + i;
36f8d174
LT
166 struct object *obj = pending->item;
167 const char *name = pending->name;
168 if (obj->flags & (UNINTERESTING | SEEN))
169 continue;
1974632c 170 if (obj->type == OBJ_TAG) {
36f8d174 171 obj->flags |= SEEN;
1f1e895f 172 add_object_array(obj, name, &objects);
36f8d174
LT
173 continue;
174 }
1974632c 175 if (obj->type == OBJ_TREE) {
1f1e895f 176 process_tree((struct tree *)obj, &objects, NULL, name);
36f8d174
LT
177 continue;
178 }
1974632c 179 if (obj->type == OBJ_BLOB) {
1f1e895f 180 process_blob((struct blob *)obj, &objects, NULL, name);
36f8d174
LT
181 continue;
182 }
183 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
184 }
1f1e895f
LT
185 for (i = 0; i < objects.nr; i++) {
186 struct object_array_entry *p = objects.objects + i;
187
50319850
JH
188 /* An object with name "foo\n0000000..." can be used to
189 * confuse downstream git-pack-objects very badly.
c807f771 190 */
1f1e895f 191 const char *ep = strchr(p->name, '\n');
c807f771 192 if (ep) {
1f1e895f
LT
193 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
194 (int) (ep - p->name),
195 p->name);
c807f771
JH
196 }
197 else
1f1e895f 198 printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
9de48752
LT
199 }
200}
201
8b3a1e05
LT
202/*
203 * This is a truly stupid algorithm, but it's only
204 * used for bisection, and we just don't care enough.
205 *
206 * We care just barely enough to avoid recursing for
207 * non-merge entries.
208 */
209static int count_distance(struct commit_list *entry)
210{
211 int nr = 0;
212
213 while (entry) {
214 struct commit *commit = entry->item;
215 struct commit_list *p;
216
217 if (commit->object.flags & (UNINTERESTING | COUNTED))
218 break;
8efdc326 219 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
b3cfd939 220 nr++;
8b3a1e05
LT
221 commit->object.flags |= COUNTED;
222 p = commit->parents;
223 entry = p;
224 if (p) {
225 p = p->next;
226 while (p) {
227 nr += count_distance(p);
228 p = p->next;
229 }
230 }
231 }
b3cfd939 232
8b3a1e05
LT
233 return nr;
234}
235
3d958064 236static void clear_distance(struct commit_list *list)
8b3a1e05
LT
237{
238 while (list) {
239 struct commit *commit = list->item;
240 commit->object.flags &= ~COUNTED;
241 list = list->next;
242 }
243}
244
245static struct commit_list *find_bisection(struct commit_list *list)
246{
247 int nr, closest;
248 struct commit_list *p, *best;
249
250 nr = 0;
251 p = list;
252 while (p) {
8efdc326 253 if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
b3cfd939 254 nr++;
8b3a1e05
LT
255 p = p->next;
256 }
257 closest = 0;
258 best = list;
259
b3cfd939
LT
260 for (p = list; p; p = p->next) {
261 int distance;
262
8efdc326 263 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
b3cfd939
LT
264 continue;
265
266 distance = count_distance(p);
8b3a1e05
LT
267 clear_distance(list);
268 if (nr - distance < distance)
269 distance = nr - distance;
270 if (distance > closest) {
271 best = p;
272 closest = distance;
273 }
8b3a1e05
LT
274 }
275 if (best)
276 best->next = NULL;
277 return best;
278}
279
c6496575
JH
280static void mark_edge_parents_uninteresting(struct commit *commit)
281{
282 struct commit_list *parents;
283
284 for (parents = commit->parents; parents; parents = parents->next) {
285 struct commit *parent = parents->item;
286 if (!(parent->object.flags & UNINTERESTING))
287 continue;
288 mark_tree_uninteresting(parent->tree);
ae563542 289 if (revs.edge_hint && !(parent->object.flags & SHOWN)) {
eb38cc68 290 parent->object.flags |= SHOWN;
c6496575 291 printf("-%s\n", sha1_to_hex(parent->object.sha1));
eb38cc68 292 }
c6496575
JH
293 }
294}
295
5bdbaaa4
LT
296static void mark_edges_uninteresting(struct commit_list *list)
297{
298 for ( ; list; list = list->next) {
c6496575 299 struct commit *commit = list->item;
5bdbaaa4 300
c6496575
JH
301 if (commit->object.flags & UNINTERESTING) {
302 mark_tree_uninteresting(commit->tree);
303 continue;
5bdbaaa4 304 }
c6496575 305 mark_edge_parents_uninteresting(commit);
5bdbaaa4
LT
306 }
307}
308
a633fca0 309int cmd_rev_list(int argc, const char **argv, const char *prefix)
64745109 310{
ae563542 311 struct commit_list *list;
d9a83684 312 int i;
64745109 313
a633fca0 314 init_revisions(&revs, prefix);
7594c4b2
JH
315 revs.abbrev = 0;
316 revs.commit_format = CMIT_FMT_UNSPECIFIED;
a4a88b2b 317 argc = setup_revisions(argc, argv, &revs, NULL);
ae563542 318
fcfda02b 319 for (i = 1 ; i < argc; i++) {
cf484544 320 const char *arg = argv[i];
fcfda02b 321
a6f68d47 322 if (!strcmp(arg, "--header")) {
7594c4b2 323 revs.verbose_header = 1;
9d97aa64
LT
324 continue;
325 }
dc68c4ff
JH
326 if (!strcmp(arg, "--timestamp")) {
327 show_timestamp = 1;
328 continue;
329 }
8b3a1e05
LT
330 if (!strcmp(arg, "--bisect")) {
331 bisect_list = 1;
332 continue;
333 }
ae563542 334 usage(rev_list_usage);
a6f68d47 335
fcfda02b 336 }
7594c4b2
JH
337 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
338 /* The command line has a --pretty */
339 hdr_termination = '\n';
340 if (revs.commit_format == CMIT_FMT_ONELINE)
91539833 341 header_prefix = "";
7594c4b2 342 else
91539833 343 header_prefix = "commit ";
7594c4b2 344 }
db89665f
JH
345 else if (revs.verbose_header)
346 /* Only --header was specified */
347 revs.commit_format = CMIT_FMT_RAW;
fcfda02b 348
ae563542 349 list = revs.commits;
ae563542 350
8c1f0b44
JH
351 if ((!list &&
352 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
1f1e895f 353 !revs.pending.nr)) ||
8c1f0b44 354 revs.diff)
7b34c2fa
LT
355 usage(rev_list_usage);
356
7594c4b2 357 save_commit_buffer = revs.verbose_header;
9181ca2c 358 track_object_refs = 0;
4e1dc640
JH
359 if (bisect_list)
360 revs.limited = 1;
9181ca2c 361
a4a88b2b
LT
362 prepare_revision_walk(&revs);
363 if (revs.tree_objects)
364 mark_edges_uninteresting(revs.commits);
365
366 if (bisect_list)
367 revs.commits = find_bisection(revs.commits);
7b34c2fa 368
765ac8ec 369 show_commit_list(&revs);
8906300f 370
64745109
LT
371 return 0;
372}