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