]> git.ipfire.org Git - thirdparty/git.git/blame - rev-list.c
Fixes for option parsing
[thirdparty/git.git] / 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
LT
9#include "revision.h"
10
3381c790 11/* bits #0-6 in revision.h */
64745109 12
3381c790 13#define COUNTED (1u<<7)
8906300f 14
a6f68d47 15static const char rev_list_usage[] =
69e0c256
JH
16"git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
17" limiting output:\n"
18" --max-count=nr\n"
19" --max-age=epoch\n"
20" --min-age=epoch\n"
21" --sparse\n"
22" --no-merges\n"
93b74bca 23" --remove-empty\n"
69e0c256
JH
24" --all\n"
25" ordering output:\n"
69e0c256 26" --topo-order\n"
4c8725f1 27" --date-order\n"
69e0c256
JH
28" formatting output:\n"
29" --parents\n"
c6496575 30" --objects | --objects-edge\n"
69e0c256
JH
31" --unpacked\n"
32" --header | --pretty\n"
9da5c2f0 33" --abbrev=nr | --no-abbrev\n"
5c51c985 34" --abbrev-commit\n"
69e0c256
JH
35" special purpose:\n"
36" --bisect"
37;
a6f68d47 38
ae563542
LT
39struct rev_info revs;
40
8b3a1e05 41static int bisect_list = 0;
dc68c4ff 42static int show_timestamp = 0;
81f2bb1f 43static int hdr_termination = 0;
e646de0d 44
81f2bb1f
LT
45static void show_commit(struct commit *commit)
46{
dc68c4ff
JH
47 if (show_timestamp)
48 printf("%lu ", commit->date);
7594c4b2
JH
49 if (*revs.header_prefix)
50 fputs(revs.header_prefix, stdout);
384e99a4
JH
51 if (commit->object.flags & BOUNDARY)
52 putchar('-');
7594c4b2
JH
53 if (revs.abbrev_commit && revs.abbrev)
54 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
55 stdout);
5c51c985
JH
56 else
57 fputs(sha1_to_hex(commit->object.sha1), stdout);
7b0c9966 58 if (revs.parents) {
81f2bb1f
LT
59 struct commit_list *parents = commit->parents;
60 while (parents) {
88494423 61 struct object *o = &(parents->item->object);
81f2bb1f 62 parents = parents->next;
88494423
JH
63 if (o->flags & TMP_MARK)
64 continue;
65 printf(" %s", sha1_to_hex(o->sha1));
66 o->flags |= TMP_MARK;
81f2bb1f 67 }
88494423
JH
68 /* TMP_MARK is a general purpose flag that can
69 * be used locally, but the user should clean
70 * things up after it is done with them.
71 */
72 for (parents = commit->parents;
73 parents;
74 parents = parents->next)
75 parents->item->object.flags &= ~TMP_MARK;
81f2bb1f 76 }
7594c4b2 77 if (revs.commit_format == CMIT_FMT_ONELINE)
d87449c5
JH
78 putchar(' ');
79 else
80 putchar('\n');
81
7594c4b2 82 if (revs.verbose_header) {
000182ea 83 static char pretty_header[16384];
7594c4b2
JH
84 pretty_print_commit(revs.commit_format, commit, ~0,
85 pretty_header, sizeof(pretty_header),
86 revs.abbrev);
000182ea 87 printf("%s%c", pretty_header, hdr_termination);
7620d39f
LT
88 }
89 fflush(stdout);
a3437b8c
JS
90}
91
e646de0d
JH
92static struct object_list **process_blob(struct blob *blob,
93 struct object_list **p,
94 struct name_path *path,
95 const char *name)
9de48752
LT
96{
97 struct object *obj = &blob->object;
98
ae563542 99 if (!revs.blob_objects)
9de48752
LT
100 return p;
101 if (obj->flags & (UNINTERESTING | SEEN))
102 return p;
103 obj->flags |= SEEN;
e646de0d 104 return add_object(obj, p, path, name);
9de48752
LT
105}
106
e646de0d
JH
107static struct object_list **process_tree(struct tree *tree,
108 struct object_list **p,
109 struct name_path *path,
110 const char *name)
9de48752
LT
111{
112 struct object *obj = &tree->object;
113 struct tree_entry_list *entry;
e646de0d 114 struct name_path me;
9de48752 115
ae563542 116 if (!revs.tree_objects)
9de48752
LT
117 return p;
118 if (obj->flags & (UNINTERESTING | SEEN))
119 return p;
120 if (parse_tree(tree) < 0)
121 die("bad tree object %s", sha1_to_hex(obj->sha1));
122 obj->flags |= SEEN;
e646de0d
JH
123 p = add_object(obj, p, path, name);
124 me.up = path;
125 me.elem = name;
126 me.elem_len = strlen(name);
b0d8923e
LT
127 entry = tree->entries;
128 tree->entries = NULL;
129 while (entry) {
130 struct tree_entry_list *next = entry->next;
9de48752 131 if (entry->directory)
e646de0d 132 p = process_tree(entry->item.tree, p, &me, entry->name);
9de48752 133 else
e646de0d 134 p = process_blob(entry->item.blob, p, &me, entry->name);
b0d8923e
LT
135 free(entry);
136 entry = next;
9de48752
LT
137 }
138 return p;
139}
140
a4a88b2b 141static void show_commit_list(struct rev_info *revs)
81f2bb1f 142{
a4a88b2b 143 struct commit *commit;
36f8d174 144 struct object_list *objects = NULL, **p = &objects, *pending;
81f2bb1f 145
a4a88b2b 146 while ((commit = get_revision(revs)) != NULL) {
e646de0d 147 p = process_tree(commit->tree, p, NULL, "");
765ac8ec 148 show_commit(commit);
81f2bb1f 149 }
a4a88b2b 150 for (pending = revs->pending_objects; pending; pending = pending->next) {
36f8d174
LT
151 struct object *obj = pending->item;
152 const char *name = pending->name;
153 if (obj->flags & (UNINTERESTING | SEEN))
154 continue;
155 if (obj->type == tag_type) {
156 obj->flags |= SEEN;
e646de0d 157 p = add_object(obj, p, NULL, name);
36f8d174
LT
158 continue;
159 }
160 if (obj->type == tree_type) {
e646de0d 161 p = process_tree((struct tree *)obj, p, NULL, name);
36f8d174
LT
162 continue;
163 }
164 if (obj->type == blob_type) {
e646de0d 165 p = process_blob((struct blob *)obj, p, NULL, name);
36f8d174
LT
166 continue;
167 }
168 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
169 }
9de48752 170 while (objects) {
50319850
JH
171 /* An object with name "foo\n0000000..." can be used to
172 * confuse downstream git-pack-objects very badly.
c807f771
JH
173 */
174 const char *ep = strchr(objects->name, '\n');
175 if (ep) {
176 printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
177 (int) (ep - objects->name),
178 objects->name);
179 }
180 else
181 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
9de48752
LT
182 objects = objects->next;
183 }
184}
185
8b3a1e05
LT
186/*
187 * This is a truly stupid algorithm, but it's only
188 * used for bisection, and we just don't care enough.
189 *
190 * We care just barely enough to avoid recursing for
191 * non-merge entries.
192 */
193static int count_distance(struct commit_list *entry)
194{
195 int nr = 0;
196
197 while (entry) {
198 struct commit *commit = entry->item;
199 struct commit_list *p;
200
201 if (commit->object.flags & (UNINTERESTING | COUNTED))
202 break;
8efdc326 203 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
b3cfd939 204 nr++;
8b3a1e05
LT
205 commit->object.flags |= COUNTED;
206 p = commit->parents;
207 entry = p;
208 if (p) {
209 p = p->next;
210 while (p) {
211 nr += count_distance(p);
212 p = p->next;
213 }
214 }
215 }
b3cfd939 216
8b3a1e05
LT
217 return nr;
218}
219
3d958064 220static void clear_distance(struct commit_list *list)
8b3a1e05
LT
221{
222 while (list) {
223 struct commit *commit = list->item;
224 commit->object.flags &= ~COUNTED;
225 list = list->next;
226 }
227}
228
229static struct commit_list *find_bisection(struct commit_list *list)
230{
231 int nr, closest;
232 struct commit_list *p, *best;
233
234 nr = 0;
235 p = list;
236 while (p) {
8efdc326 237 if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
b3cfd939 238 nr++;
8b3a1e05
LT
239 p = p->next;
240 }
241 closest = 0;
242 best = list;
243
b3cfd939
LT
244 for (p = list; p; p = p->next) {
245 int distance;
246
8efdc326 247 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
b3cfd939
LT
248 continue;
249
250 distance = count_distance(p);
8b3a1e05
LT
251 clear_distance(list);
252 if (nr - distance < distance)
253 distance = nr - distance;
254 if (distance > closest) {
255 best = p;
256 closest = distance;
257 }
8b3a1e05
LT
258 }
259 if (best)
260 best->next = NULL;
261 return best;
262}
263
c6496575
JH
264static void mark_edge_parents_uninteresting(struct commit *commit)
265{
266 struct commit_list *parents;
267
268 for (parents = commit->parents; parents; parents = parents->next) {
269 struct commit *parent = parents->item;
270 if (!(parent->object.flags & UNINTERESTING))
271 continue;
272 mark_tree_uninteresting(parent->tree);
ae563542 273 if (revs.edge_hint && !(parent->object.flags & SHOWN)) {
eb38cc68 274 parent->object.flags |= SHOWN;
c6496575 275 printf("-%s\n", sha1_to_hex(parent->object.sha1));
eb38cc68 276 }
c6496575
JH
277 }
278}
279
5bdbaaa4
LT
280static void mark_edges_uninteresting(struct commit_list *list)
281{
282 for ( ; list; list = list->next) {
c6496575 283 struct commit *commit = list->item;
5bdbaaa4 284
c6496575
JH
285 if (commit->object.flags & UNINTERESTING) {
286 mark_tree_uninteresting(commit->tree);
287 continue;
5bdbaaa4 288 }
c6496575 289 mark_edge_parents_uninteresting(commit);
5bdbaaa4
LT
290 }
291}
292
cf484544 293int main(int argc, const char **argv)
64745109 294{
ae563542 295 struct commit_list *list;
d9a83684 296 int i;
64745109 297
7594c4b2
JH
298 init_revisions(&revs);
299 revs.abbrev = 0;
300 revs.commit_format = CMIT_FMT_UNSPECIFIED;
a4a88b2b 301 argc = setup_revisions(argc, argv, &revs, NULL);
ae563542 302
fcfda02b 303 for (i = 1 ; i < argc; i++) {
cf484544 304 const char *arg = argv[i];
fcfda02b 305
a6f68d47 306 if (!strcmp(arg, "--header")) {
7594c4b2 307 revs.verbose_header = 1;
9d97aa64
LT
308 continue;
309 }
dc68c4ff
JH
310 if (!strcmp(arg, "--timestamp")) {
311 show_timestamp = 1;
312 continue;
313 }
8b3a1e05
LT
314 if (!strcmp(arg, "--bisect")) {
315 bisect_list = 1;
316 continue;
317 }
ae563542 318 usage(rev_list_usage);
a6f68d47 319
fcfda02b 320 }
7594c4b2
JH
321 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
322 /* The command line has a --pretty */
323 hdr_termination = '\n';
324 if (revs.commit_format == CMIT_FMT_ONELINE)
325 revs.header_prefix = "";
326 else
327 revs.header_prefix = "commit ";
328 }
fcfda02b 329
ae563542 330 list = revs.commits;
ae563542 331
8c1f0b44
JH
332 if ((!list &&
333 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
334 !revs.pending_objects)) ||
335 revs.diff)
7b34c2fa
LT
336 usage(rev_list_usage);
337
7594c4b2 338 save_commit_buffer = revs.verbose_header;
9181ca2c
JH
339 track_object_refs = 0;
340
a4a88b2b
LT
341 prepare_revision_walk(&revs);
342 if (revs.tree_objects)
343 mark_edges_uninteresting(revs.commits);
344
345 if (bisect_list)
346 revs.commits = find_bisection(revs.commits);
7b34c2fa 347
765ac8ec 348 show_commit_list(&revs);
8906300f 349
64745109
LT
350 return 0;
351}