]> git.ipfire.org Git - thirdparty/git.git/blame - rev-list.c
builtin-diff: do not say files are renamed when blob and file are given
[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
1b65a5aa 11/* bits #0-15 in revision.h */
64745109 12
1b65a5aa 13#define COUNTED (1u<<16)
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;
91539833 44static const char *header_prefix;
e646de0d 45
81f2bb1f
LT
46static void show_commit(struct commit *commit)
47{
dc68c4ff
JH
48 if (show_timestamp)
49 printf("%lu ", commit->date);
91539833
LT
50 if (header_prefix)
51 fputs(header_prefix, stdout);
384e99a4
JH
52 if (commit->object.flags & BOUNDARY)
53 putchar('-');
7594c4b2
JH
54 if (revs.abbrev_commit && revs.abbrev)
55 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
56 stdout);
5c51c985
JH
57 else
58 fputs(sha1_to_hex(commit->object.sha1), stdout);
7b0c9966 59 if (revs.parents) {
81f2bb1f
LT
60 struct commit_list *parents = commit->parents;
61 while (parents) {
88494423 62 struct object *o = &(parents->item->object);
81f2bb1f 63 parents = parents->next;
88494423
JH
64 if (o->flags & TMP_MARK)
65 continue;
66 printf(" %s", sha1_to_hex(o->sha1));
67 o->flags |= TMP_MARK;
81f2bb1f 68 }
88494423
JH
69 /* TMP_MARK is a general purpose flag that can
70 * be used locally, but the user should clean
71 * things up after it is done with them.
72 */
73 for (parents = commit->parents;
74 parents;
75 parents = parents->next)
76 parents->item->object.flags &= ~TMP_MARK;
81f2bb1f 77 }
7594c4b2 78 if (revs.commit_format == CMIT_FMT_ONELINE)
d87449c5
JH
79 putchar(' ');
80 else
81 putchar('\n');
82
7594c4b2 83 if (revs.verbose_header) {
000182ea 84 static char pretty_header[16384];
7594c4b2
JH
85 pretty_print_commit(revs.commit_format, commit, ~0,
86 pretty_header, sizeof(pretty_header),
87 revs.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
7594c4b2
JH
299 init_revisions(&revs);
300 revs.abbrev = 0;
301 revs.commit_format = CMIT_FMT_UNSPECIFIED;
a4a88b2b 302 argc = setup_revisions(argc, argv, &revs, NULL);
ae563542 303
fcfda02b 304 for (i = 1 ; i < argc; i++) {
cf484544 305 const char *arg = argv[i];
fcfda02b 306
a6f68d47 307 if (!strcmp(arg, "--header")) {
7594c4b2 308 revs.verbose_header = 1;
9d97aa64
LT
309 continue;
310 }
dc68c4ff
JH
311 if (!strcmp(arg, "--timestamp")) {
312 show_timestamp = 1;
313 continue;
314 }
8b3a1e05
LT
315 if (!strcmp(arg, "--bisect")) {
316 bisect_list = 1;
317 continue;
318 }
ae563542 319 usage(rev_list_usage);
a6f68d47 320
fcfda02b 321 }
7594c4b2
JH
322 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
323 /* The command line has a --pretty */
324 hdr_termination = '\n';
325 if (revs.commit_format == CMIT_FMT_ONELINE)
91539833 326 header_prefix = "";
7594c4b2 327 else
91539833 328 header_prefix = "commit ";
7594c4b2 329 }
db89665f
JH
330 else if (revs.verbose_header)
331 /* Only --header was specified */
332 revs.commit_format = CMIT_FMT_RAW;
fcfda02b 333
ae563542 334 list = revs.commits;
ae563542 335
8c1f0b44
JH
336 if ((!list &&
337 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
338 !revs.pending_objects)) ||
339 revs.diff)
7b34c2fa
LT
340 usage(rev_list_usage);
341
7594c4b2 342 save_commit_buffer = revs.verbose_header;
9181ca2c 343 track_object_refs = 0;
4e1dc640
JH
344 if (bisect_list)
345 revs.limited = 1;
9181ca2c 346
a4a88b2b
LT
347 prepare_revision_walk(&revs);
348 if (revs.tree_objects)
349 mark_edges_uninteresting(revs.commits);
350
351 if (bisect_list)
352 revs.commits = find_bisection(revs.commits);
7b34c2fa 353
765ac8ec 354 show_commit_list(&revs);
8906300f 355
64745109
LT
356 return 0;
357}