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
;
28 static int parse_opt_signed_tag_mode(const struct option
*opt
,
29 const char *arg
, int unset
)
31 if (unset
|| !strcmp(arg
, "abort"))
32 signed_tag_mode
= ABORT
;
33 else if (!strcmp(arg
, "verbatim") || !strcmp(arg
, "ignore"))
34 signed_tag_mode
= VERBATIM
;
35 else if (!strcmp(arg
, "warn"))
36 signed_tag_mode
= WARN
;
37 else if (!strcmp(arg
, "strip"))
38 signed_tag_mode
= STRIP
;
40 return error("Unknown signed-tag mode: %s", arg
);
44 static struct decoration idnums
;
45 static uint32_t last_idnum
;
47 static int has_unshown_parent(struct commit
*commit
)
49 struct commit_list
*parent
;
51 for (parent
= commit
->parents
; parent
; parent
= parent
->next
)
52 if (!(parent
->item
->object
.flags
& SHOWN
) &&
53 !(parent
->item
->object
.flags
& UNINTERESTING
))
58 /* Since intptr_t is C99, we do not use it here */
59 static inline uint32_t *mark_to_ptr(uint32_t mark
)
61 return ((uint32_t *)NULL
) + mark
;
64 static inline uint32_t ptr_to_mark(void * mark
)
66 return (uint32_t *)mark
- (uint32_t *)NULL
;
69 static inline void mark_object(struct object
*object
, uint32_t mark
)
71 add_decoration(&idnums
, object
, mark_to_ptr(mark
));
74 static inline void mark_next_object(struct object
*object
)
76 mark_object(object
, ++last_idnum
);
79 static int get_object_mark(struct object
*object
)
81 void *decoration
= lookup_decoration(&idnums
, object
);
84 return ptr_to_mark(decoration
);
87 static void show_progress(void)
89 static int counter
= 0;
92 if ((++counter
% progress
) == 0)
93 printf("progress %d objects\n", counter
);
96 static void handle_object(const unsigned char *sha1
)
99 enum object_type type
;
101 struct object
*object
;
103 if (is_null_sha1(sha1
))
106 object
= parse_object(sha1
);
108 die ("Could not read blob %s", sha1_to_hex(sha1
));
110 if (object
->flags
& SHOWN
)
113 buf
= read_sha1_file(sha1
, &type
, &size
);
115 die ("Could not read blob %s", sha1_to_hex(sha1
));
117 mark_next_object(object
);
119 printf("blob\nmark :%"PRIu32
"\ndata %lu\n", last_idnum
, size
);
120 if (size
&& fwrite(buf
, size
, 1, stdout
) != 1)
121 die ("Could not write blob %s", sha1_to_hex(sha1
));
126 object
->flags
|= SHOWN
;
130 static void show_filemodify(struct diff_queue_struct
*q
,
131 struct diff_options
*options
, void *data
)
134 for (i
= 0; i
< q
->nr
; i
++) {
135 struct diff_filespec
*ospec
= q
->queue
[i
]->one
;
136 struct diff_filespec
*spec
= q
->queue
[i
]->two
;
138 switch (q
->queue
[i
]->status
) {
139 case DIFF_STATUS_DELETED
:
140 printf("D %s\n", spec
->path
);
143 case DIFF_STATUS_COPIED
:
144 case DIFF_STATUS_RENAMED
:
145 printf("%c \"%s\" \"%s\"\n", q
->queue
[i
]->status
,
146 ospec
->path
, spec
->path
);
148 if (!hashcmp(ospec
->sha1
, spec
->sha1
) &&
149 ospec
->mode
== spec
->mode
)
153 case DIFF_STATUS_TYPE_CHANGED
:
154 case DIFF_STATUS_MODIFIED
:
155 case DIFF_STATUS_ADDED
:
157 * Links refer to objects in another repositories;
158 * output the SHA-1 verbatim.
160 if (S_ISGITLINK(spec
->mode
))
161 printf("M %06o %s %s\n", spec
->mode
,
162 sha1_to_hex(spec
->sha1
), spec
->path
);
164 struct object
*object
= lookup_object(spec
->sha1
);
165 printf("M %06o :%d %s\n", spec
->mode
,
166 get_object_mark(object
), spec
->path
);
171 die("Unexpected comparison status '%c' for %s, %s",
173 ospec
->path
? ospec
->path
: "none",
174 spec
->path
? spec
->path
: "none");
179 static const char *find_encoding(const char *begin
, const char *end
)
181 const char *needle
= "\nencoding ";
184 bol
= memmem(begin
, end
? end
- begin
: strlen(begin
),
185 needle
, strlen(needle
));
187 return git_commit_encoding
;
188 bol
+= strlen(needle
);
189 eol
= strchrnul(bol
, '\n');
194 static void handle_commit(struct commit
*commit
, struct rev_info
*rev
)
196 int saved_output_format
= rev
->diffopt
.output_format
;
197 const char *author
, *author_end
, *committer
, *committer_end
;
198 const char *encoding
, *message
;
199 char *reencoded
= NULL
;
200 struct commit_list
*p
;
203 rev
->diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
205 parse_commit(commit
);
206 author
= strstr(commit
->buffer
, "\nauthor ");
208 die ("Could not find author in commit %s",
209 sha1_to_hex(commit
->object
.sha1
));
211 author_end
= strchrnul(author
, '\n');
212 committer
= strstr(author_end
, "\ncommitter ");
214 die ("Could not find committer in commit %s",
215 sha1_to_hex(commit
->object
.sha1
));
217 committer_end
= strchrnul(committer
, '\n');
218 message
= strstr(committer_end
, "\n\n");
219 encoding
= find_encoding(committer_end
, message
);
223 if (commit
->parents
) {
224 parse_commit(commit
->parents
->item
);
225 diff_tree_sha1(commit
->parents
->item
->tree
->object
.sha1
,
226 commit
->tree
->object
.sha1
, "", &rev
->diffopt
);
229 diff_root_tree_sha1(commit
->tree
->object
.sha1
,
232 /* Export the referenced blobs, and remember the marks. */
233 for (i
= 0; i
< diff_queued_diff
.nr
; i
++)
234 if (!S_ISGITLINK(diff_queued_diff
.queue
[i
]->two
->mode
))
235 handle_object(diff_queued_diff
.queue
[i
]->two
->sha1
);
237 mark_next_object(&commit
->object
);
238 if (!is_encoding_utf8(encoding
))
239 reencoded
= reencode_string(message
, "UTF-8", encoding
);
240 if (!commit
->parents
)
241 printf("reset %s\n", (const char*)commit
->util
);
242 printf("commit %s\nmark :%"PRIu32
"\n%.*s\n%.*s\ndata %u\n%s",
243 (const char *)commit
->util
, last_idnum
,
244 (int)(author_end
- author
), author
,
245 (int)(committer_end
- committer
), committer
,
247 ? strlen(reencoded
) : message
248 ? strlen(message
) : 0),
249 reencoded
? reencoded
: message
? message
: "");
252 for (i
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
253 int mark
= get_object_mark(&p
->item
->object
);
257 printf("from :%d\n", mark
);
259 printf("merge :%d\n", mark
);
263 log_tree_diff_flush(rev
);
264 rev
->diffopt
.output_format
= saved_output_format
;
271 static void handle_tail(struct object_array
*commits
, struct rev_info
*revs
)
273 struct commit
*commit
;
274 while (commits
->nr
) {
275 commit
= (struct commit
*)commits
->objects
[commits
->nr
- 1].item
;
276 if (has_unshown_parent(commit
))
278 handle_commit(commit
, revs
);
283 static void handle_tag(const char *name
, struct tag
*tag
)
286 enum object_type type
;
288 const char *tagger
, *tagger_end
, *message
;
289 size_t message_size
= 0;
291 buf
= read_sha1_file(tag
->object
.sha1
, &type
, &size
);
293 die ("Could not read tag %s", sha1_to_hex(tag
->object
.sha1
));
294 message
= memmem(buf
, size
, "\n\n", 2);
297 message_size
= strlen(message
);
299 tagger
= memmem(buf
, message
? message
- buf
: size
, "\ntagger ", 8);
301 die ("No tagger for tag %s", sha1_to_hex(tag
->object
.sha1
));
303 tagger_end
= strchrnul(tagger
, '\n');
305 /* handle signed tags */
307 const char *signature
= strstr(message
,
308 "\n-----BEGIN PGP SIGNATURE-----\n");
310 switch(signed_tag_mode
) {
312 die ("Encountered signed tag %s; use "
313 "--signed-tag=<mode> to handle it.",
314 sha1_to_hex(tag
->object
.sha1
));
316 warning ("Exporting signed tag %s",
317 sha1_to_hex(tag
->object
.sha1
));
322 message_size
= signature
+ 1 - message
;
327 if (!prefixcmp(name
, "refs/tags/"))
329 printf("tag %s\nfrom :%d\n%.*s\ndata %d\n%.*s\n",
330 name
, get_object_mark(tag
->tagged
),
331 (int)(tagger_end
- tagger
), tagger
,
332 (int)message_size
, (int)message_size
, message
? message
: "");
335 static void get_tags_and_duplicates(struct object_array
*pending
,
336 struct string_list
*extra_refs
)
341 for (i
= 0; i
< pending
->nr
; i
++) {
342 struct object_array_entry
*e
= pending
->objects
+ i
;
343 unsigned char sha1
[20];
344 struct commit
*commit
= commit
;
347 if (dwim_ref(e
->name
, strlen(e
->name
), sha1
, &full_name
) != 1)
350 switch (e
->item
->type
) {
352 commit
= (struct commit
*)e
->item
;
355 tag
= (struct tag
*)e
->item
;
356 while (tag
&& tag
->object
.type
== OBJ_TAG
) {
357 string_list_insert(full_name
, extra_refs
)->util
= tag
;
358 tag
= (struct tag
*)tag
->tagged
;
361 die ("Tag %s points nowhere?", e
->name
);
362 switch(tag
->object
.type
) {
364 commit
= (struct commit
*)tag
;
367 handle_object(tag
->object
.sha1
);
372 die ("Unexpected object of type %s",
373 typename(e
->item
->type
));
376 /* more than one name for the same object */
377 string_list_insert(full_name
, extra_refs
)->util
= commit
;
379 commit
->util
= full_name
;
383 static void handle_tags_and_duplicates(struct string_list
*extra_refs
)
385 struct commit
*commit
;
388 for (i
= extra_refs
->nr
- 1; i
>= 0; i
--) {
389 const char *name
= extra_refs
->items
[i
].string
;
390 struct object
*object
= extra_refs
->items
[i
].util
;
391 switch (object
->type
) {
393 handle_tag(name
, (struct tag
*)object
);
396 /* create refs pointing to already seen commits */
397 commit
= (struct commit
*)object
;
398 printf("reset %s\nfrom :%d\n\n", name
,
399 get_object_mark(&commit
->object
));
406 static void export_marks(char *file
)
410 struct object_decoration
*deco
= idnums
.hash
;
413 f
= fopen(file
, "w");
415 error("Unable to open marks file %s for writing", file
);
417 for (i
= 0; i
< idnums
.size
; i
++) {
418 if (deco
->base
&& deco
->base
->type
== 1) {
419 mark
= ptr_to_mark(deco
->decoration
);
420 fprintf(f
, ":%u %s\n", mark
, sha1_to_hex(deco
->base
->sha1
));
425 if (ferror(f
) || fclose(f
))
426 error("Unable to write marks file %s.", file
);
429 static void import_marks(char *input_file
)
432 FILE *f
= fopen(input_file
, "r");
434 die("cannot read %s: %s", input_file
, strerror(errno
));
436 while (fgets(line
, sizeof(line
), f
)) {
438 char *line_end
, *mark_end
;
439 unsigned char sha1
[20];
440 struct object
*object
;
442 line_end
= strchr(line
, '\n');
443 if (line
[0] != ':' || !line_end
)
444 die("corrupt mark line: %s", line
);
447 mark
= strtoumax(line
+ 1, &mark_end
, 10);
448 if (!mark
|| mark_end
== line
+ 1
449 || *mark_end
!= ' ' || get_sha1(mark_end
+ 1, sha1
))
450 die("corrupt mark line: %s", line
);
452 object
= parse_object(sha1
);
454 die ("Could not read blob %s", sha1_to_hex(sha1
));
456 if (object
->flags
& SHOWN
)
457 error("Object %s already has a mark", sha1
);
459 mark_object(object
, mark
);
460 if (last_idnum
< mark
)
463 object
->flags
|= SHOWN
;
468 int cmd_fast_export(int argc
, const char **argv
, const char *prefix
)
470 struct rev_info revs
;
471 struct object_array commits
= { 0, 0, NULL
};
472 struct string_list extra_refs
= { NULL
, 0, 0, 0 };
473 struct commit
*commit
;
474 char *export_filename
= NULL
, *import_filename
= NULL
;
475 struct option options
[] = {
476 OPT_INTEGER(0, "progress", &progress
,
477 "show progress after <n> objects"),
478 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode
, "mode",
479 "select handling of signed tags",
480 parse_opt_signed_tag_mode
),
481 OPT_STRING(0, "export-marks", &export_filename
, "FILE",
482 "Dump marks to this file"),
483 OPT_STRING(0, "import-marks", &import_filename
, "FILE",
484 "Import marks from this file"),
488 /* we handle encodings */
489 git_config(git_default_config
, NULL
);
491 init_revisions(&revs
, prefix
);
492 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
493 argc
= parse_options(argc
, argv
, options
, fast_export_usage
, 0);
495 usage_with_options (fast_export_usage
, options
);
498 import_marks(import_filename
);
500 get_tags_and_duplicates(&revs
.pending
, &extra_refs
);
502 if (prepare_revision_walk(&revs
))
503 die("revision walk setup failed");
504 revs
.diffopt
.format_callback
= show_filemodify
;
505 DIFF_OPT_SET(&revs
.diffopt
, RECURSIVE
);
506 while ((commit
= get_revision(&revs
))) {
507 if (has_unshown_parent(commit
)) {
508 struct commit_list
*parent
= commit
->parents
;
509 add_object_array(&commit
->object
, NULL
, &commits
);
510 for (; parent
; parent
= parent
->next
)
511 if (!parent
->item
->util
)
512 parent
->item
->util
= commit
->util
;
515 handle_commit(commit
, &revs
);
516 handle_tail(&commits
, &revs
);
520 handle_tags_and_duplicates(&extra_refs
);
523 export_marks(export_filename
);