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