2 * "git fast-export" builtin command
4 * Copyright (C) 2007 Johannes E. Schindelin
16 #include "string-list.h"
18 #include "parse-options.h"
20 static const char *fast_export_usage
[] = {
21 "git fast-export [rev-list-opts]",
26 static enum { VERBATIM
, WARN
, STRIP
, ABORT
} signed_tag_mode
= ABORT
;
27 static int fake_missing_tagger
;
29 static int parse_opt_signed_tag_mode(const struct option
*opt
,
30 const char *arg
, int unset
)
32 if (unset
|| !strcmp(arg
, "abort"))
33 signed_tag_mode
= ABORT
;
34 else if (!strcmp(arg
, "verbatim") || !strcmp(arg
, "ignore"))
35 signed_tag_mode
= VERBATIM
;
36 else if (!strcmp(arg
, "warn"))
37 signed_tag_mode
= WARN
;
38 else if (!strcmp(arg
, "strip"))
39 signed_tag_mode
= STRIP
;
41 return error("Unknown signed-tag mode: %s", arg
);
45 static struct decoration idnums
;
46 static uint32_t last_idnum
;
48 static int has_unshown_parent(struct commit
*commit
)
50 struct commit_list
*parent
;
52 for (parent
= commit
->parents
; parent
; parent
= parent
->next
)
53 if (!(parent
->item
->object
.flags
& SHOWN
) &&
54 !(parent
->item
->object
.flags
& UNINTERESTING
))
59 /* Since intptr_t is C99, we do not use it here */
60 static inline uint32_t *mark_to_ptr(uint32_t mark
)
62 return ((uint32_t *)NULL
) + mark
;
65 static inline uint32_t ptr_to_mark(void * mark
)
67 return (uint32_t *)mark
- (uint32_t *)NULL
;
70 static inline void mark_object(struct object
*object
, uint32_t mark
)
72 add_decoration(&idnums
, object
, mark_to_ptr(mark
));
75 static inline void mark_next_object(struct object
*object
)
77 mark_object(object
, ++last_idnum
);
80 static int get_object_mark(struct object
*object
)
82 void *decoration
= lookup_decoration(&idnums
, object
);
85 return ptr_to_mark(decoration
);
88 static void show_progress(void)
90 static int counter
= 0;
93 if ((++counter
% progress
) == 0)
94 printf("progress %d objects\n", counter
);
97 static void handle_object(const unsigned char *sha1
)
100 enum object_type type
;
102 struct object
*object
;
104 if (is_null_sha1(sha1
))
107 object
= parse_object(sha1
);
109 die ("Could not read blob %s", sha1_to_hex(sha1
));
111 if (object
->flags
& SHOWN
)
114 buf
= read_sha1_file(sha1
, &type
, &size
);
116 die ("Could not read blob %s", sha1_to_hex(sha1
));
118 mark_next_object(object
);
120 printf("blob\nmark :%"PRIu32
"\ndata %lu\n", last_idnum
, size
);
121 if (size
&& fwrite(buf
, size
, 1, stdout
) != 1)
122 die ("Could not write blob %s", sha1_to_hex(sha1
));
127 object
->flags
|= SHOWN
;
131 static void show_filemodify(struct diff_queue_struct
*q
,
132 struct diff_options
*options
, void *data
)
135 for (i
= 0; i
< q
->nr
; i
++) {
136 struct diff_filespec
*ospec
= q
->queue
[i
]->one
;
137 struct diff_filespec
*spec
= q
->queue
[i
]->two
;
139 switch (q
->queue
[i
]->status
) {
140 case DIFF_STATUS_DELETED
:
141 printf("D %s\n", spec
->path
);
144 case DIFF_STATUS_COPIED
:
145 case DIFF_STATUS_RENAMED
:
146 printf("%c \"%s\" \"%s\"\n", q
->queue
[i
]->status
,
147 ospec
->path
, spec
->path
);
149 if (!hashcmp(ospec
->sha1
, spec
->sha1
) &&
150 ospec
->mode
== spec
->mode
)
154 case DIFF_STATUS_TYPE_CHANGED
:
155 case DIFF_STATUS_MODIFIED
:
156 case DIFF_STATUS_ADDED
:
158 * Links refer to objects in another repositories;
159 * output the SHA-1 verbatim.
161 if (S_ISGITLINK(spec
->mode
))
162 printf("M %06o %s %s\n", spec
->mode
,
163 sha1_to_hex(spec
->sha1
), spec
->path
);
165 struct object
*object
= lookup_object(spec
->sha1
);
166 printf("M %06o :%d %s\n", spec
->mode
,
167 get_object_mark(object
), spec
->path
);
172 die("Unexpected comparison status '%c' for %s, %s",
174 ospec
->path
? ospec
->path
: "none",
175 spec
->path
? spec
->path
: "none");
180 static const char *find_encoding(const char *begin
, const char *end
)
182 const char *needle
= "\nencoding ";
185 bol
= memmem(begin
, end
? end
- begin
: strlen(begin
),
186 needle
, strlen(needle
));
188 return git_commit_encoding
;
189 bol
+= strlen(needle
);
190 eol
= strchrnul(bol
, '\n');
195 static void handle_commit(struct commit
*commit
, struct rev_info
*rev
)
197 int saved_output_format
= rev
->diffopt
.output_format
;
198 const char *author
, *author_end
, *committer
, *committer_end
;
199 const char *encoding
, *message
;
200 char *reencoded
= NULL
;
201 struct commit_list
*p
;
204 rev
->diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
206 parse_commit(commit
);
207 author
= strstr(commit
->buffer
, "\nauthor ");
209 die ("Could not find author in commit %s",
210 sha1_to_hex(commit
->object
.sha1
));
212 author_end
= strchrnul(author
, '\n');
213 committer
= strstr(author_end
, "\ncommitter ");
215 die ("Could not find committer in commit %s",
216 sha1_to_hex(commit
->object
.sha1
));
218 committer_end
= strchrnul(committer
, '\n');
219 message
= strstr(committer_end
, "\n\n");
220 encoding
= find_encoding(committer_end
, message
);
224 if (commit
->parents
) {
225 parse_commit(commit
->parents
->item
);
226 diff_tree_sha1(commit
->parents
->item
->tree
->object
.sha1
,
227 commit
->tree
->object
.sha1
, "", &rev
->diffopt
);
230 diff_root_tree_sha1(commit
->tree
->object
.sha1
,
233 /* Export the referenced blobs, and remember the marks. */
234 for (i
= 0; i
< diff_queued_diff
.nr
; i
++)
235 if (!S_ISGITLINK(diff_queued_diff
.queue
[i
]->two
->mode
))
236 handle_object(diff_queued_diff
.queue
[i
]->two
->sha1
);
238 mark_next_object(&commit
->object
);
239 if (!is_encoding_utf8(encoding
))
240 reencoded
= reencode_string(message
, "UTF-8", encoding
);
241 if (!commit
->parents
)
242 printf("reset %s\n", (const char*)commit
->util
);
243 printf("commit %s\nmark :%"PRIu32
"\n%.*s\n%.*s\ndata %u\n%s",
244 (const char *)commit
->util
, last_idnum
,
245 (int)(author_end
- author
), author
,
246 (int)(committer_end
- committer
), committer
,
248 ? strlen(reencoded
) : message
249 ? strlen(message
) : 0),
250 reencoded
? reencoded
: message
? message
: "");
253 for (i
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
254 int mark
= get_object_mark(&p
->item
->object
);
258 printf("from :%d\n", mark
);
260 printf("merge :%d\n", mark
);
264 log_tree_diff_flush(rev
);
265 rev
->diffopt
.output_format
= saved_output_format
;
272 static void handle_tail(struct object_array
*commits
, struct rev_info
*revs
)
274 struct commit
*commit
;
275 while (commits
->nr
) {
276 commit
= (struct commit
*)commits
->objects
[commits
->nr
- 1].item
;
277 if (has_unshown_parent(commit
))
279 handle_commit(commit
, revs
);
284 static void handle_tag(const char *name
, struct tag
*tag
)
287 enum object_type type
;
289 const char *tagger
, *tagger_end
, *message
;
290 size_t message_size
= 0;
292 buf
= read_sha1_file(tag
->object
.sha1
, &type
, &size
);
294 die ("Could not read tag %s", sha1_to_hex(tag
->object
.sha1
));
295 message
= memmem(buf
, size
, "\n\n", 2);
298 message_size
= strlen(message
);
300 tagger
= memmem(buf
, message
? message
- buf
: size
, "\ntagger ", 8);
302 if (fake_missing_tagger
)
303 tagger
= "tagger Unspecified Tagger "
304 "<unspecified-tagger> 0 +0000";
307 tagger_end
= tagger
+ strlen(tagger
);
310 tagger_end
= strchrnul(tagger
, '\n');
313 /* handle signed tags */
315 const char *signature
= strstr(message
,
316 "\n-----BEGIN PGP SIGNATURE-----\n");
318 switch(signed_tag_mode
) {
320 die ("Encountered signed tag %s; use "
321 "--signed-tag=<mode> to handle it.",
322 sha1_to_hex(tag
->object
.sha1
));
324 warning ("Exporting signed tag %s",
325 sha1_to_hex(tag
->object
.sha1
));
330 message_size
= signature
+ 1 - message
;
335 if (!prefixcmp(name
, "refs/tags/"))
337 printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n",
338 name
, get_object_mark(tag
->tagged
),
339 (int)(tagger_end
- tagger
), tagger
,
340 tagger
== tagger_end
? "" : "\n",
341 (int)message_size
, (int)message_size
, message
? message
: "");
344 static void get_tags_and_duplicates(struct object_array
*pending
,
345 struct string_list
*extra_refs
)
350 for (i
= 0; i
< pending
->nr
; i
++) {
351 struct object_array_entry
*e
= pending
->objects
+ i
;
352 unsigned char sha1
[20];
353 struct commit
*commit
= commit
;
356 if (dwim_ref(e
->name
, strlen(e
->name
), sha1
, &full_name
) != 1)
359 switch (e
->item
->type
) {
361 commit
= (struct commit
*)e
->item
;
364 tag
= (struct tag
*)e
->item
;
365 while (tag
&& tag
->object
.type
== OBJ_TAG
) {
366 string_list_append(full_name
, extra_refs
)->util
= tag
;
367 tag
= (struct tag
*)tag
->tagged
;
370 die ("Tag %s points nowhere?", e
->name
);
371 switch(tag
->object
.type
) {
373 commit
= (struct commit
*)tag
;
376 handle_object(tag
->object
.sha1
);
379 warning("Tag points to object of unexpected type %s, skipping.",
380 typename(tag
->object
.type
));
385 warning("%s: Unexpected object of type %s, skipping.",
387 typename(e
->item
->type
));
391 /* more than one name for the same object */
392 string_list_append(full_name
, extra_refs
)->util
= commit
;
394 commit
->util
= full_name
;
398 static void handle_tags_and_duplicates(struct string_list
*extra_refs
)
400 struct commit
*commit
;
403 for (i
= extra_refs
->nr
- 1; i
>= 0; i
--) {
404 const char *name
= extra_refs
->items
[i
].string
;
405 struct object
*object
= extra_refs
->items
[i
].util
;
406 switch (object
->type
) {
408 handle_tag(name
, (struct tag
*)object
);
411 /* create refs pointing to already seen commits */
412 commit
= (struct commit
*)object
;
413 printf("reset %s\nfrom :%d\n\n", name
,
414 get_object_mark(&commit
->object
));
421 static void export_marks(char *file
)
425 struct object_decoration
*deco
= idnums
.hash
;
428 f
= fopen(file
, "w");
430 error("Unable to open marks file %s for writing", file
);
432 for (i
= 0; i
< idnums
.size
; i
++) {
433 if (deco
->base
&& deco
->base
->type
== 1) {
434 mark
= ptr_to_mark(deco
->decoration
);
435 fprintf(f
, ":%"PRIu32
" %s\n", mark
,
436 sha1_to_hex(deco
->base
->sha1
));
441 if (ferror(f
) || fclose(f
))
442 error("Unable to write marks file %s.", file
);
445 static void import_marks(char *input_file
)
448 FILE *f
= fopen(input_file
, "r");
450 die("cannot read %s: %s", input_file
, strerror(errno
));
452 while (fgets(line
, sizeof(line
), f
)) {
454 char *line_end
, *mark_end
;
455 unsigned char sha1
[20];
456 struct object
*object
;
458 line_end
= strchr(line
, '\n');
459 if (line
[0] != ':' || !line_end
)
460 die("corrupt mark line: %s", line
);
463 mark
= strtoumax(line
+ 1, &mark_end
, 10);
464 if (!mark
|| mark_end
== line
+ 1
465 || *mark_end
!= ' ' || get_sha1(mark_end
+ 1, sha1
))
466 die("corrupt mark line: %s", line
);
468 object
= parse_object(sha1
);
470 die ("Could not read blob %s", sha1_to_hex(sha1
));
472 if (object
->flags
& SHOWN
)
473 error("Object %s already has a mark", sha1
);
475 mark_object(object
, mark
);
476 if (last_idnum
< mark
)
479 object
->flags
|= SHOWN
;
484 int cmd_fast_export(int argc
, const char **argv
, const char *prefix
)
486 struct rev_info revs
;
487 struct object_array commits
= { 0, 0, NULL
};
488 struct string_list extra_refs
= { NULL
, 0, 0, 0 };
489 struct commit
*commit
;
490 char *export_filename
= NULL
, *import_filename
= NULL
;
491 struct option options
[] = {
492 OPT_INTEGER(0, "progress", &progress
,
493 "show progress after <n> objects"),
494 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode
, "mode",
495 "select handling of signed tags",
496 parse_opt_signed_tag_mode
),
497 OPT_STRING(0, "export-marks", &export_filename
, "FILE",
498 "Dump marks to this file"),
499 OPT_STRING(0, "import-marks", &import_filename
, "FILE",
500 "Import marks from this file"),
501 OPT_BOOLEAN(0, "fake-missing-tagger", &fake_missing_tagger
,
502 "Fake a tagger when tags lack one"),
507 usage_with_options (fast_export_usage
, options
);
509 /* we handle encodings */
510 git_config(git_default_config
, NULL
);
512 init_revisions(&revs
, prefix
);
513 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
514 argc
= parse_options(argc
, argv
, options
, fast_export_usage
, 0);
516 usage_with_options (fast_export_usage
, options
);
519 import_marks(import_filename
);
521 get_tags_and_duplicates(&revs
.pending
, &extra_refs
);
524 if (prepare_revision_walk(&revs
))
525 die("revision walk setup failed");
526 revs
.diffopt
.format_callback
= show_filemodify
;
527 DIFF_OPT_SET(&revs
.diffopt
, RECURSIVE
);
528 while ((commit
= get_revision(&revs
))) {
529 if (has_unshown_parent(commit
)) {
530 struct commit_list
*parent
= commit
->parents
;
531 add_object_array(&commit
->object
, NULL
, &commits
);
532 for (; parent
; parent
= parent
->next
)
533 if (!parent
->item
->util
)
534 parent
->item
->util
= commit
->util
;
537 handle_commit(commit
, &revs
);
538 handle_tail(&commits
, &revs
);
542 handle_tags_and_duplicates(&extra_refs
);
545 export_marks(export_filename
);