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