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