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