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