]>
Commit | Line | Data |
---|---|---|
f2dc849e JS |
1 | /* |
2 | * "git fast-export" builtin command | |
3 | * | |
4 | * Copyright (C) 2007 Johannes E. Schindelin | |
5 | */ | |
6 | #include "builtin.h" | |
7 | #include "cache.h" | |
8 | #include "commit.h" | |
9 | #include "object.h" | |
10 | #include "tag.h" | |
11 | #include "diff.h" | |
12 | #include "diffcore.h" | |
13 | #include "log-tree.h" | |
14 | #include "revision.h" | |
15 | #include "decorate.h" | |
c455c87c | 16 | #include "string-list.h" |
f2dc849e JS |
17 | #include "utf8.h" |
18 | #include "parse-options.h" | |
6280dfdc | 19 | #include "quote.h" |
f2dc849e JS |
20 | |
21 | static const char *fast_export_usage[] = { | |
3b787b96 | 22 | N_("git fast-export [rev-list-opts]"), |
f2dc849e JS |
23 | NULL |
24 | }; | |
25 | ||
26 | static int progress; | |
cd16c59b | 27 | static enum { ABORT, VERBATIM, WARN, WARN_STRIP, STRIP } signed_tag_mode = ABORT; |
d7a10c31 | 28 | static enum { ERROR, DROP, REWRITE } tag_of_filtered_mode = ERROR; |
4e46a8d6 | 29 | static int fake_missing_tagger; |
82670a5c | 30 | static int use_done_feature; |
79559f27 | 31 | static int no_data; |
7f40ab09 | 32 | static int full_tree; |
f2dc849e JS |
33 | |
34 | static int parse_opt_signed_tag_mode(const struct option *opt, | |
35 | const char *arg, int unset) | |
36 | { | |
37 | if (unset || !strcmp(arg, "abort")) | |
38 | signed_tag_mode = ABORT; | |
ee4bc371 JS |
39 | else if (!strcmp(arg, "verbatim") || !strcmp(arg, "ignore")) |
40 | signed_tag_mode = VERBATIM; | |
f2dc849e JS |
41 | else if (!strcmp(arg, "warn")) |
42 | signed_tag_mode = WARN; | |
cd16c59b JK |
43 | else if (!strcmp(arg, "warn-strip")) |
44 | signed_tag_mode = WARN_STRIP; | |
f2dc849e JS |
45 | else if (!strcmp(arg, "strip")) |
46 | signed_tag_mode = STRIP; | |
47 | else | |
04a74b6c | 48 | return error("Unknown signed-tags mode: %s", arg); |
f2dc849e JS |
49 | return 0; |
50 | } | |
51 | ||
2d8ad469 EN |
52 | static int parse_opt_tag_of_filtered_mode(const struct option *opt, |
53 | const char *arg, int unset) | |
54 | { | |
55 | if (unset || !strcmp(arg, "abort")) | |
d7a10c31 | 56 | tag_of_filtered_mode = ERROR; |
2d8ad469 EN |
57 | else if (!strcmp(arg, "drop")) |
58 | tag_of_filtered_mode = DROP; | |
59 | else if (!strcmp(arg, "rewrite")) | |
60 | tag_of_filtered_mode = REWRITE; | |
61 | else | |
62 | return error("Unknown tag-of-filtered mode: %s", arg); | |
63 | return 0; | |
64 | } | |
65 | ||
f2dc849e JS |
66 | static struct decoration idnums; |
67 | static uint32_t last_idnum; | |
68 | ||
69 | static int has_unshown_parent(struct commit *commit) | |
70 | { | |
71 | struct commit_list *parent; | |
72 | ||
73 | for (parent = commit->parents; parent; parent = parent->next) | |
74 | if (!(parent->item->object.flags & SHOWN) && | |
75 | !(parent->item->object.flags & UNINTERESTING)) | |
76 | return 1; | |
77 | return 0; | |
78 | } | |
79 | ||
80 | /* Since intptr_t is C99, we do not use it here */ | |
df6a7ff7 | 81 | static inline uint32_t *mark_to_ptr(uint32_t mark) |
f2dc849e | 82 | { |
df6a7ff7 PB |
83 | return ((uint32_t *)NULL) + mark; |
84 | } | |
85 | ||
86 | static inline uint32_t ptr_to_mark(void * mark) | |
87 | { | |
88 | return (uint32_t *)mark - (uint32_t *)NULL; | |
89 | } | |
90 | ||
91 | static inline void mark_object(struct object *object, uint32_t mark) | |
92 | { | |
93 | add_decoration(&idnums, object, mark_to_ptr(mark)); | |
94 | } | |
95 | ||
96 | static inline void mark_next_object(struct object *object) | |
97 | { | |
98 | mark_object(object, ++last_idnum); | |
f2dc849e JS |
99 | } |
100 | ||
101 | static int get_object_mark(struct object *object) | |
102 | { | |
103 | void *decoration = lookup_decoration(&idnums, object); | |
104 | if (!decoration) | |
105 | return 0; | |
df6a7ff7 | 106 | return ptr_to_mark(decoration); |
f2dc849e JS |
107 | } |
108 | ||
109 | static void show_progress(void) | |
110 | { | |
111 | static int counter = 0; | |
112 | if (!progress) | |
113 | return; | |
114 | if ((++counter % progress) == 0) | |
115 | printf("progress %d objects\n", counter); | |
116 | } | |
117 | ||
f9b54e26 | 118 | static void export_blob(const unsigned char *sha1) |
f2dc849e JS |
119 | { |
120 | unsigned long size; | |
121 | enum object_type type; | |
122 | char *buf; | |
123 | struct object *object; | |
30b939c3 | 124 | int eaten; |
f2dc849e | 125 | |
79559f27 GI |
126 | if (no_data) |
127 | return; | |
128 | ||
f2dc849e JS |
129 | if (is_null_sha1(sha1)) |
130 | return; | |
131 | ||
30b939c3 JK |
132 | object = lookup_object(sha1); |
133 | if (object && object->flags & SHOWN) | |
f2dc849e JS |
134 | return; |
135 | ||
136 | buf = read_sha1_file(sha1, &type, &size); | |
137 | if (!buf) | |
138 | die ("Could not read blob %s", sha1_to_hex(sha1)); | |
30b939c3 JK |
139 | if (check_sha1_signature(sha1, buf, size, typename(type)) < 0) |
140 | die("sha1 mismatch in blob %s", sha1_to_hex(sha1)); | |
141 | object = parse_object_buffer(sha1, type, size, buf, &eaten); | |
142 | if (!object) | |
143 | die("Could not read blob %s", sha1_to_hex(sha1)); | |
f2dc849e | 144 | |
df6a7ff7 | 145 | mark_next_object(object); |
f2dc849e | 146 | |
6e1c2344 | 147 | printf("blob\nmark :%"PRIu32"\ndata %lu\n", last_idnum, size); |
b0fe0d72 | 148 | if (size && fwrite(buf, size, 1, stdout) != 1) |
0721c314 | 149 | die_errno ("Could not write blob '%s'", sha1_to_hex(sha1)); |
f2dc849e JS |
150 | printf("\n"); |
151 | ||
152 | show_progress(); | |
153 | ||
154 | object->flags |= SHOWN; | |
30b939c3 JK |
155 | if (!eaten) |
156 | free(buf); | |
f2dc849e JS |
157 | } |
158 | ||
060df624 EN |
159 | static int depth_first(const void *a_, const void *b_) |
160 | { | |
161 | const struct diff_filepair *a = *((const struct diff_filepair **)a_); | |
162 | const struct diff_filepair *b = *((const struct diff_filepair **)b_); | |
163 | const char *name_a, *name_b; | |
164 | int len_a, len_b, len; | |
165 | int cmp; | |
166 | ||
167 | name_a = a->one ? a->one->path : a->two->path; | |
168 | name_b = b->one ? b->one->path : b->two->path; | |
169 | ||
170 | len_a = strlen(name_a); | |
171 | len_b = strlen(name_b); | |
172 | len = (len_a < len_b) ? len_a : len_b; | |
173 | ||
174 | /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */ | |
175 | cmp = memcmp(name_a, name_b, len); | |
176 | if (cmp) | |
177 | return cmp; | |
4ce6fb80 JS |
178 | cmp = len_b - len_a; |
179 | if (cmp) | |
180 | return cmp; | |
181 | /* | |
182 | * Move 'R'ename entries last so that all references of the file | |
183 | * appear in the output before it is renamed (e.g., when a file | |
184 | * was copied and renamed in the same commit). | |
185 | */ | |
186 | return (a->status == 'R') - (b->status == 'R'); | |
060df624 EN |
187 | } |
188 | ||
6280dfdc JK |
189 | static void print_path(const char *path) |
190 | { | |
191 | int need_quote = quote_c_style(path, NULL, NULL, 0); | |
192 | if (need_quote) | |
193 | quote_c_style(path, NULL, stdout, 0); | |
ff59f6da JS |
194 | else if (strchr(path, ' ')) |
195 | printf("\"%s\"", path); | |
6280dfdc JK |
196 | else |
197 | printf("%s", path); | |
198 | } | |
199 | ||
f2dc849e JS |
200 | static void show_filemodify(struct diff_queue_struct *q, |
201 | struct diff_options *options, void *data) | |
202 | { | |
203 | int i; | |
060df624 EN |
204 | |
205 | /* | |
206 | * Handle files below a directory first, in case they are all deleted | |
207 | * and the directory changes to a file or symlink. | |
208 | */ | |
209 | qsort(q->queue, q->nr, sizeof(q->queue[0]), depth_first); | |
210 | ||
f2dc849e | 211 | for (i = 0; i < q->nr; i++) { |
ae7c5dce | 212 | struct diff_filespec *ospec = q->queue[i]->one; |
f2dc849e | 213 | struct diff_filespec *spec = q->queue[i]->two; |
ae7c5dce AG |
214 | |
215 | switch (q->queue[i]->status) { | |
216 | case DIFF_STATUS_DELETED: | |
6280dfdc JK |
217 | printf("D "); |
218 | print_path(spec->path); | |
219 | putchar('\n'); | |
ae7c5dce AG |
220 | break; |
221 | ||
222 | case DIFF_STATUS_COPIED: | |
223 | case DIFF_STATUS_RENAMED: | |
6280dfdc JK |
224 | printf("%c ", q->queue[i]->status); |
225 | print_path(ospec->path); | |
226 | putchar(' '); | |
227 | print_path(spec->path); | |
228 | putchar('\n'); | |
ae7c5dce AG |
229 | |
230 | if (!hashcmp(ospec->sha1, spec->sha1) && | |
231 | ospec->mode == spec->mode) | |
232 | break; | |
233 | /* fallthrough */ | |
234 | ||
235 | case DIFF_STATUS_TYPE_CHANGED: | |
236 | case DIFF_STATUS_MODIFIED: | |
237 | case DIFF_STATUS_ADDED: | |
03db4525 AG |
238 | /* |
239 | * Links refer to objects in another repositories; | |
240 | * output the SHA-1 verbatim. | |
241 | */ | |
79559f27 | 242 | if (no_data || S_ISGITLINK(spec->mode)) |
6280dfdc JK |
243 | printf("M %06o %s ", spec->mode, |
244 | sha1_to_hex(spec->sha1)); | |
03db4525 AG |
245 | else { |
246 | struct object *object = lookup_object(spec->sha1); | |
6280dfdc JK |
247 | printf("M %06o :%d ", spec->mode, |
248 | get_object_mark(object)); | |
03db4525 | 249 | } |
6280dfdc JK |
250 | print_path(spec->path); |
251 | putchar('\n'); | |
ae7c5dce AG |
252 | break; |
253 | ||
254 | default: | |
255 | die("Unexpected comparison status '%c' for %s, %s", | |
256 | q->queue[i]->status, | |
257 | ospec->path ? ospec->path : "none", | |
258 | spec->path ? spec->path : "none"); | |
f2dc849e JS |
259 | } |
260 | } | |
261 | } | |
262 | ||
263 | static const char *find_encoding(const char *begin, const char *end) | |
264 | { | |
265 | const char *needle = "\nencoding "; | |
266 | char *bol, *eol; | |
267 | ||
268 | bol = memmem(begin, end ? end - begin : strlen(begin), | |
269 | needle, strlen(needle)); | |
270 | if (!bol) | |
271 | return git_commit_encoding; | |
272 | bol += strlen(needle); | |
273 | eol = strchrnul(bol, '\n'); | |
274 | *eol = '\0'; | |
275 | return bol; | |
276 | } | |
277 | ||
278 | static void handle_commit(struct commit *commit, struct rev_info *rev) | |
279 | { | |
280 | int saved_output_format = rev->diffopt.output_format; | |
281 | const char *author, *author_end, *committer, *committer_end; | |
282 | const char *encoding, *message; | |
283 | char *reencoded = NULL; | |
284 | struct commit_list *p; | |
285 | int i; | |
286 | ||
287 | rev->diffopt.output_format = DIFF_FORMAT_CALLBACK; | |
288 | ||
289 | parse_commit(commit); | |
290 | author = strstr(commit->buffer, "\nauthor "); | |
291 | if (!author) | |
292 | die ("Could not find author in commit %s", | |
293 | sha1_to_hex(commit->object.sha1)); | |
294 | author++; | |
295 | author_end = strchrnul(author, '\n'); | |
296 | committer = strstr(author_end, "\ncommitter "); | |
297 | if (!committer) | |
298 | die ("Could not find committer in commit %s", | |
299 | sha1_to_hex(commit->object.sha1)); | |
300 | committer++; | |
301 | committer_end = strchrnul(committer, '\n'); | |
302 | message = strstr(committer_end, "\n\n"); | |
303 | encoding = find_encoding(committer_end, message); | |
304 | if (message) | |
305 | message += 2; | |
306 | ||
ebeec7db | 307 | if (commit->parents && |
4087a02e EN |
308 | get_object_mark(&commit->parents->item->object) != 0 && |
309 | !full_tree) { | |
f2dc849e JS |
310 | parse_commit(commit->parents->item); |
311 | diff_tree_sha1(commit->parents->item->tree->object.sha1, | |
312 | commit->tree->object.sha1, "", &rev->diffopt); | |
313 | } | |
314 | else | |
315 | diff_root_tree_sha1(commit->tree->object.sha1, | |
316 | "", &rev->diffopt); | |
317 | ||
03db4525 | 318 | /* Export the referenced blobs, and remember the marks. */ |
f2dc849e | 319 | for (i = 0; i < diff_queued_diff.nr; i++) |
03db4525 | 320 | if (!S_ISGITLINK(diff_queued_diff.queue[i]->two->mode)) |
f9b54e26 | 321 | export_blob(diff_queued_diff.queue[i]->two->sha1); |
f2dc849e | 322 | |
df6a7ff7 | 323 | mark_next_object(&commit->object); |
f2dc849e JS |
324 | if (!is_encoding_utf8(encoding)) |
325 | reencoded = reencode_string(message, "UTF-8", encoding); | |
d8933f01 SP |
326 | if (!commit->parents) |
327 | printf("reset %s\n", (const char*)commit->util); | |
6e1c2344 | 328 | printf("commit %s\nmark :%"PRIu32"\n%.*s\n%.*s\ndata %u\n%s", |
f2dc849e JS |
329 | (const char *)commit->util, last_idnum, |
330 | (int)(author_end - author), author, | |
331 | (int)(committer_end - committer), committer, | |
332 | (unsigned)(reencoded | |
333 | ? strlen(reencoded) : message | |
334 | ? strlen(message) : 0), | |
335 | reencoded ? reencoded : message ? message : ""); | |
8e0f7003 | 336 | free(reencoded); |
f2dc849e JS |
337 | |
338 | for (i = 0, p = commit->parents; p; p = p->next) { | |
339 | int mark = get_object_mark(&p->item->object); | |
340 | if (!mark) | |
341 | continue; | |
342 | if (i == 0) | |
343 | printf("from :%d\n", mark); | |
f2dc849e | 344 | else |
5070b49e | 345 | printf("merge :%d\n", mark); |
f2dc849e JS |
346 | i++; |
347 | } | |
f2dc849e | 348 | |
4087a02e EN |
349 | if (full_tree) |
350 | printf("deleteall\n"); | |
f2dc849e JS |
351 | log_tree_diff_flush(rev); |
352 | rev->diffopt.output_format = saved_output_format; | |
353 | ||
354 | printf("\n"); | |
355 | ||
356 | show_progress(); | |
357 | } | |
358 | ||
359 | static void handle_tail(struct object_array *commits, struct rev_info *revs) | |
360 | { | |
361 | struct commit *commit; | |
362 | while (commits->nr) { | |
363 | commit = (struct commit *)commits->objects[commits->nr - 1].item; | |
364 | if (has_unshown_parent(commit)) | |
365 | return; | |
366 | handle_commit(commit, revs); | |
367 | commits->nr--; | |
368 | } | |
369 | } | |
370 | ||
371 | static void handle_tag(const char *name, struct tag *tag) | |
372 | { | |
373 | unsigned long size; | |
374 | enum object_type type; | |
375 | char *buf; | |
376 | const char *tagger, *tagger_end, *message; | |
377 | size_t message_size = 0; | |
02c48cd6 | 378 | struct object *tagged; |
2d8ad469 EN |
379 | int tagged_mark; |
380 | struct commit *p; | |
02c48cd6 EN |
381 | |
382 | /* Trees have no identifer in fast-export output, thus we have no way | |
383 | * to output tags of trees, tags of tags of trees, etc. Simply omit | |
384 | * such tags. | |
385 | */ | |
386 | tagged = tag->tagged; | |
387 | while (tagged->type == OBJ_TAG) { | |
388 | tagged = ((struct tag *)tagged)->tagged; | |
389 | } | |
390 | if (tagged->type == OBJ_TREE) { | |
391 | warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.", | |
392 | sha1_to_hex(tag->object.sha1)); | |
393 | return; | |
394 | } | |
f2dc849e JS |
395 | |
396 | buf = read_sha1_file(tag->object.sha1, &type, &size); | |
397 | if (!buf) | |
398 | die ("Could not read tag %s", sha1_to_hex(tag->object.sha1)); | |
399 | message = memmem(buf, size, "\n\n", 2); | |
400 | if (message) { | |
401 | message += 2; | |
402 | message_size = strlen(message); | |
403 | } | |
404 | tagger = memmem(buf, message ? message - buf : size, "\ntagger ", 8); | |
4e46a8d6 JS |
405 | if (!tagger) { |
406 | if (fake_missing_tagger) | |
407 | tagger = "tagger Unspecified Tagger " | |
408 | "<unspecified-tagger> 0 +0000"; | |
409 | else | |
410 | tagger = ""; | |
411 | tagger_end = tagger + strlen(tagger); | |
412 | } else { | |
413 | tagger++; | |
414 | tagger_end = strchrnul(tagger, '\n'); | |
415 | } | |
f2dc849e JS |
416 | |
417 | /* handle signed tags */ | |
418 | if (message) { | |
419 | const char *signature = strstr(message, | |
420 | "\n-----BEGIN PGP SIGNATURE-----\n"); | |
421 | if (signature) | |
422 | switch(signed_tag_mode) { | |
423 | case ABORT: | |
424 | die ("Encountered signed tag %s; use " | |
04a74b6c | 425 | "--signed-tags=<mode> to handle it.", |
f2dc849e JS |
426 | sha1_to_hex(tag->object.sha1)); |
427 | case WARN: | |
428 | warning ("Exporting signed tag %s", | |
429 | sha1_to_hex(tag->object.sha1)); | |
430 | /* fallthru */ | |
ee4bc371 | 431 | case VERBATIM: |
f2dc849e | 432 | break; |
cd16c59b JK |
433 | case WARN_STRIP: |
434 | warning ("Stripping signature from tag %s", | |
435 | sha1_to_hex(tag->object.sha1)); | |
436 | /* fallthru */ | |
f2dc849e JS |
437 | case STRIP: |
438 | message_size = signature + 1 - message; | |
439 | break; | |
440 | } | |
441 | } | |
442 | ||
2d8ad469 EN |
443 | /* handle tag->tagged having been filtered out due to paths specified */ |
444 | tagged = tag->tagged; | |
445 | tagged_mark = get_object_mark(tagged); | |
446 | if (!tagged_mark) { | |
447 | switch(tag_of_filtered_mode) { | |
448 | case ABORT: | |
449 | die ("Tag %s tags unexported object; use " | |
450 | "--tag-of-filtered-object=<mode> to handle it.", | |
451 | sha1_to_hex(tag->object.sha1)); | |
452 | case DROP: | |
453 | /* Ignore this tag altogether */ | |
454 | return; | |
455 | case REWRITE: | |
456 | if (tagged->type != OBJ_COMMIT) { | |
457 | die ("Tag %s tags unexported %s!", | |
458 | sha1_to_hex(tag->object.sha1), | |
459 | typename(tagged->type)); | |
460 | } | |
461 | p = (struct commit *)tagged; | |
462 | for (;;) { | |
463 | if (p->parents && p->parents->next) | |
464 | break; | |
465 | if (p->object.flags & UNINTERESTING) | |
466 | break; | |
467 | if (!(p->object.flags & TREESAME)) | |
468 | break; | |
469 | if (!p->parents) | |
470 | die ("Can't find replacement commit for tag %s\n", | |
471 | sha1_to_hex(tag->object.sha1)); | |
472 | p = p->parents->item; | |
473 | } | |
474 | tagged_mark = get_object_mark(&p->object); | |
475 | } | |
476 | } | |
477 | ||
f2dc849e JS |
478 | if (!prefixcmp(name, "refs/tags/")) |
479 | name += 10; | |
4e46a8d6 | 480 | printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n", |
2d8ad469 | 481 | name, tagged_mark, |
f2dc849e | 482 | (int)(tagger_end - tagger), tagger, |
4e46a8d6 | 483 | tagger == tagger_end ? "" : "\n", |
f2dc849e JS |
484 | (int)message_size, (int)message_size, message ? message : ""); |
485 | } | |
486 | ||
49266e8a | 487 | static void get_tags_and_duplicates(struct rev_cmdline_info *info, |
c455c87c | 488 | struct string_list *extra_refs) |
f2dc849e JS |
489 | { |
490 | struct tag *tag; | |
491 | int i; | |
492 | ||
49266e8a FC |
493 | for (i = 0; i < info->nr; i++) { |
494 | struct rev_cmdline_entry *e = info->rev + i; | |
f2dc849e | 495 | unsigned char sha1[20]; |
2d242de4 | 496 | struct commit *commit; |
f2dc849e JS |
497 | char *full_name; |
498 | ||
49266e8a FC |
499 | if (e->flags & UNINTERESTING) |
500 | continue; | |
501 | ||
f2dc849e JS |
502 | if (dwim_ref(e->name, strlen(e->name), sha1, &full_name) != 1) |
503 | continue; | |
504 | ||
505 | switch (e->item->type) { | |
506 | case OBJ_COMMIT: | |
507 | commit = (struct commit *)e->item; | |
508 | break; | |
509 | case OBJ_TAG: | |
510 | tag = (struct tag *)e->item; | |
1982467d EFL |
511 | |
512 | /* handle nested tags */ | |
f2dc849e | 513 | while (tag && tag->object.type == OBJ_TAG) { |
1982467d | 514 | parse_object(tag->object.sha1); |
1d2f80fa | 515 | string_list_append(extra_refs, full_name)->util = tag; |
f2dc849e JS |
516 | tag = (struct tag *)tag->tagged; |
517 | } | |
518 | if (!tag) | |
519 | die ("Tag %s points nowhere?", e->name); | |
520 | switch(tag->object.type) { | |
521 | case OBJ_COMMIT: | |
522 | commit = (struct commit *)tag; | |
523 | break; | |
524 | case OBJ_BLOB: | |
f9b54e26 | 525 | export_blob(tag->object.sha1); |
f2dc849e | 526 | continue; |
1982467d | 527 | default: /* OBJ_TAG (nested tags) is already handled */ |
c0582c53 EFL |
528 | warning("Tag points to object of unexpected type %s, skipping.", |
529 | typename(tag->object.type)); | |
530 | continue; | |
f2dc849e JS |
531 | } |
532 | break; | |
533 | default: | |
2d07f6d4 EFL |
534 | warning("%s: Unexpected object of type %s, skipping.", |
535 | e->name, | |
536 | typename(e->item->type)); | |
537 | continue; | |
f2dc849e | 538 | } |
f28e7c90 FC |
539 | |
540 | /* | |
541 | * This ref will not be updated through a commit, lets make | |
542 | * sure it gets properly updated eventually. | |
543 | */ | |
544 | if (commit->util || commit->object.flags & SHOWN) | |
1d2f80fa | 545 | string_list_append(extra_refs, full_name)->util = commit; |
f28e7c90 | 546 | if (!commit->util) |
f2dc849e JS |
547 | commit->util = full_name; |
548 | } | |
549 | } | |
550 | ||
c455c87c | 551 | static void handle_tags_and_duplicates(struct string_list *extra_refs) |
f2dc849e JS |
552 | { |
553 | struct commit *commit; | |
554 | int i; | |
555 | ||
556 | for (i = extra_refs->nr - 1; i >= 0; i--) { | |
c455c87c | 557 | const char *name = extra_refs->items[i].string; |
f2dc849e JS |
558 | struct object *object = extra_refs->items[i].util; |
559 | switch (object->type) { | |
560 | case OBJ_TAG: | |
561 | handle_tag(name, (struct tag *)object); | |
562 | break; | |
563 | case OBJ_COMMIT: | |
564 | /* create refs pointing to already seen commits */ | |
565 | commit = (struct commit *)object; | |
566 | printf("reset %s\nfrom :%d\n\n", name, | |
567 | get_object_mark(&commit->object)); | |
568 | show_progress(); | |
569 | break; | |
570 | } | |
571 | } | |
572 | } | |
573 | ||
df6a7ff7 PB |
574 | static void export_marks(char *file) |
575 | { | |
576 | unsigned int i; | |
577 | uint32_t mark; | |
578 | struct object_decoration *deco = idnums.hash; | |
579 | FILE *f; | |
96d69b55 | 580 | int e = 0; |
df6a7ff7 PB |
581 | |
582 | f = fopen(file, "w"); | |
583 | if (!f) | |
bb6ad28c | 584 | die_errno("Unable to open marks file %s for writing.", file); |
df6a7ff7 | 585 | |
69913575 JH |
586 | for (i = 0; i < idnums.size; i++) { |
587 | if (deco->base && deco->base->type == 1) { | |
df6a7ff7 | 588 | mark = ptr_to_mark(deco->decoration); |
96d69b55 MA |
589 | if (fprintf(f, ":%"PRIu32" %s\n", mark, |
590 | sha1_to_hex(deco->base->sha1)) < 0) { | |
591 | e = 1; | |
592 | break; | |
593 | } | |
df6a7ff7 | 594 | } |
69913575 | 595 | deco++; |
df6a7ff7 PB |
596 | } |
597 | ||
96d69b55 MA |
598 | e |= ferror(f); |
599 | e |= fclose(f); | |
600 | if (e) | |
df6a7ff7 PB |
601 | error("Unable to write marks file %s.", file); |
602 | } | |
603 | ||
69913575 | 604 | static void import_marks(char *input_file) |
df6a7ff7 PB |
605 | { |
606 | char line[512]; | |
607 | FILE *f = fopen(input_file, "r"); | |
608 | if (!f) | |
d824cbba | 609 | die_errno("cannot read '%s'", input_file); |
df6a7ff7 PB |
610 | |
611 | while (fgets(line, sizeof(line), f)) { | |
612 | uint32_t mark; | |
613 | char *line_end, *mark_end; | |
614 | unsigned char sha1[20]; | |
615 | struct object *object; | |
616 | ||
617 | line_end = strchr(line, '\n'); | |
618 | if (line[0] != ':' || !line_end) | |
619 | die("corrupt mark line: %s", line); | |
69913575 | 620 | *line_end = '\0'; |
df6a7ff7 PB |
621 | |
622 | mark = strtoumax(line + 1, &mark_end, 10); | |
623 | if (!mark || mark_end == line + 1 | |
624 | || *mark_end != ' ' || get_sha1(mark_end + 1, sha1)) | |
625 | die("corrupt mark line: %s", line); | |
626 | ||
c4458ecd AP |
627 | if (last_idnum < mark) |
628 | last_idnum = mark; | |
629 | ||
df6a7ff7 PB |
630 | object = parse_object(sha1); |
631 | if (!object) | |
c4458ecd | 632 | continue; |
df6a7ff7 PB |
633 | |
634 | if (object->flags & SHOWN) | |
43bc2302 | 635 | error("Object %s already has a mark", sha1_to_hex(sha1)); |
df6a7ff7 | 636 | |
5d3698ff FC |
637 | if (object->type != OBJ_COMMIT) |
638 | /* only commits */ | |
639 | continue; | |
640 | ||
df6a7ff7 | 641 | mark_object(object, mark); |
df6a7ff7 PB |
642 | |
643 | object->flags |= SHOWN; | |
644 | } | |
645 | fclose(f); | |
646 | } | |
647 | ||
f2dc849e JS |
648 | int cmd_fast_export(int argc, const char **argv, const char *prefix) |
649 | { | |
650 | struct rev_info revs; | |
3cd47459 | 651 | struct object_array commits = OBJECT_ARRAY_INIT; |
183113a5 | 652 | struct string_list extra_refs = STRING_LIST_INIT_NODUP; |
f2dc849e | 653 | struct commit *commit; |
df6a7ff7 | 654 | char *export_filename = NULL, *import_filename = NULL; |
c4458ecd | 655 | uint32_t lastimportid; |
f2dc849e JS |
656 | struct option options[] = { |
657 | OPT_INTEGER(0, "progress", &progress, | |
3b787b96 NTND |
658 | N_("show progress after <n> objects")), |
659 | OPT_CALLBACK(0, "signed-tags", &signed_tag_mode, N_("mode"), | |
660 | N_("select handling of signed tags"), | |
f2dc849e | 661 | parse_opt_signed_tag_mode), |
3b787b96 NTND |
662 | OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode, N_("mode"), |
663 | N_("select handling of tags that tag filtered objects"), | |
2d8ad469 | 664 | parse_opt_tag_of_filtered_mode), |
3b787b96 NTND |
665 | OPT_STRING(0, "export-marks", &export_filename, N_("file"), |
666 | N_("Dump marks to this file")), | |
667 | OPT_STRING(0, "import-marks", &import_filename, N_("file"), | |
668 | N_("Import marks from this file")), | |
4e46a8d6 | 669 | OPT_BOOLEAN(0, "fake-missing-tagger", &fake_missing_tagger, |
3b787b96 | 670 | N_("Fake a tagger when tags lack one")), |
7f40ab09 | 671 | OPT_BOOLEAN(0, "full-tree", &full_tree, |
3b787b96 | 672 | N_("Output full tree for each commit")), |
82670a5c | 673 | OPT_BOOLEAN(0, "use-done-feature", &use_done_feature, |
3b787b96 NTND |
674 | N_("Use the done feature to terminate the stream")), |
675 | OPT_BOOL(0, "no-data", &no_data, N_("Skip output of blob data")), | |
f2dc849e JS |
676 | OPT_END() |
677 | }; | |
678 | ||
dcfdbdf0 MV |
679 | if (argc == 1) |
680 | usage_with_options (fast_export_usage, options); | |
681 | ||
f2dc849e | 682 | /* we handle encodings */ |
ef90d6d4 | 683 | git_config(git_default_config, NULL); |
f2dc849e JS |
684 | |
685 | init_revisions(&revs, prefix); | |
668f3aa7 | 686 | revs.topo_order = 1; |
2374502c | 687 | revs.show_source = 1; |
32164131 | 688 | revs.rewrite_parents = 1; |
f2dc849e | 689 | argc = setup_revisions(argc, argv, &revs, NULL); |
37782920 | 690 | argc = parse_options(argc, argv, prefix, options, fast_export_usage, 0); |
f2dc849e JS |
691 | if (argc > 1) |
692 | usage_with_options (fast_export_usage, options); | |
693 | ||
82670a5c SR |
694 | if (use_done_feature) |
695 | printf("feature done\n"); | |
696 | ||
df6a7ff7 PB |
697 | if (import_filename) |
698 | import_marks(import_filename); | |
c4458ecd | 699 | lastimportid = last_idnum; |
df6a7ff7 | 700 | |
afe069d1 | 701 | if (import_filename && revs.prune_data.nr) |
4087a02e EN |
702 | full_tree = 1; |
703 | ||
49266e8a | 704 | get_tags_and_duplicates(&revs.cmdline, &extra_refs); |
f2dc849e | 705 | |
3d51e1b5 MK |
706 | if (prepare_revision_walk(&revs)) |
707 | die("revision walk setup failed"); | |
f2dc849e JS |
708 | revs.diffopt.format_callback = show_filemodify; |
709 | DIFF_OPT_SET(&revs.diffopt, RECURSIVE); | |
710 | while ((commit = get_revision(&revs))) { | |
711 | if (has_unshown_parent(commit)) { | |
f2dc849e | 712 | add_object_array(&commit->object, NULL, &commits); |
f2dc849e JS |
713 | } |
714 | else { | |
715 | handle_commit(commit, &revs); | |
716 | handle_tail(&commits, &revs); | |
717 | } | |
718 | } | |
719 | ||
720 | handle_tags_and_duplicates(&extra_refs); | |
721 | ||
c4458ecd | 722 | if (export_filename && lastimportid != last_idnum) |
df6a7ff7 PB |
723 | export_marks(export_filename); |
724 | ||
82670a5c SR |
725 | if (use_done_feature) |
726 | printf("done\n"); | |
727 | ||
f2dc849e JS |
728 | return 0; |
729 | } |