2 * "git fast-export" builtin command
4 * Copyright (C) 2007 Johannes E. Schindelin
16 #include "path-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 void mark_object(struct object
*object
)
62 add_decoration(&idnums
, object
, ((uint32_t *)NULL
) + last_idnum
);
65 static int get_object_mark(struct object
*object
)
67 void *decoration
= lookup_decoration(&idnums
, object
);
70 return (uint32_t *)decoration
- (uint32_t *)NULL
;
73 static void show_progress(void)
75 static int counter
= 0;
78 if ((++counter
% progress
) == 0)
79 printf("progress %d objects\n", counter
);
82 static void handle_object(const unsigned char *sha1
)
85 enum object_type type
;
87 struct object
*object
;
89 if (is_null_sha1(sha1
))
92 object
= parse_object(sha1
);
94 die ("Could not read blob %s", sha1_to_hex(sha1
));
96 if (object
->flags
& SHOWN
)
99 buf
= read_sha1_file(sha1
, &type
, &size
);
101 die ("Could not read blob %s", sha1_to_hex(sha1
));
105 printf("blob\nmark :%d\ndata %lu\n", last_idnum
, size
);
106 if (size
&& fwrite(buf
, size
, 1, stdout
) != 1)
107 die ("Could not write blob %s", sha1_to_hex(sha1
));
112 object
->flags
|= SHOWN
;
116 static void show_filemodify(struct diff_queue_struct
*q
,
117 struct diff_options
*options
, void *data
)
120 for (i
= 0; i
< q
->nr
; i
++) {
121 struct diff_filespec
*spec
= q
->queue
[i
]->two
;
122 if (is_null_sha1(spec
->sha1
))
123 printf("D %s\n", spec
->path
);
125 struct object
*object
= lookup_object(spec
->sha1
);
126 printf("M %06o :%d %s\n", spec
->mode
,
127 get_object_mark(object
), spec
->path
);
132 static const char *find_encoding(const char *begin
, const char *end
)
134 const char *needle
= "\nencoding ";
137 bol
= memmem(begin
, end
? end
- begin
: strlen(begin
),
138 needle
, strlen(needle
));
140 return git_commit_encoding
;
141 bol
+= strlen(needle
);
142 eol
= strchrnul(bol
, '\n');
147 static void handle_commit(struct commit
*commit
, struct rev_info
*rev
)
149 int saved_output_format
= rev
->diffopt
.output_format
;
150 const char *author
, *author_end
, *committer
, *committer_end
;
151 const char *encoding
, *message
;
152 char *reencoded
= NULL
;
153 struct commit_list
*p
;
156 rev
->diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
158 parse_commit(commit
);
159 author
= strstr(commit
->buffer
, "\nauthor ");
161 die ("Could not find author in commit %s",
162 sha1_to_hex(commit
->object
.sha1
));
164 author_end
= strchrnul(author
, '\n');
165 committer
= strstr(author_end
, "\ncommitter ");
167 die ("Could not find committer in commit %s",
168 sha1_to_hex(commit
->object
.sha1
));
170 committer_end
= strchrnul(committer
, '\n');
171 message
= strstr(committer_end
, "\n\n");
172 encoding
= find_encoding(committer_end
, message
);
176 if (commit
->parents
) {
177 parse_commit(commit
->parents
->item
);
178 diff_tree_sha1(commit
->parents
->item
->tree
->object
.sha1
,
179 commit
->tree
->object
.sha1
, "", &rev
->diffopt
);
182 diff_root_tree_sha1(commit
->tree
->object
.sha1
,
185 for (i
= 0; i
< diff_queued_diff
.nr
; i
++)
186 handle_object(diff_queued_diff
.queue
[i
]->two
->sha1
);
188 mark_object(&commit
->object
);
189 if (!is_encoding_utf8(encoding
))
190 reencoded
= reencode_string(message
, "UTF-8", encoding
);
191 printf("commit %s\nmark :%d\n%.*s\n%.*s\ndata %u\n%s",
192 (const char *)commit
->util
, last_idnum
,
193 (int)(author_end
- author
), author
,
194 (int)(committer_end
- committer
), committer
,
196 ? strlen(reencoded
) : message
197 ? strlen(message
) : 0),
198 reencoded
? reencoded
: message
? message
: "");
201 for (i
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
202 int mark
= get_object_mark(&p
->item
->object
);
206 printf("from :%d\n", mark
);
208 printf("merge :%d", mark
);
210 printf(" :%d", mark
);
216 log_tree_diff_flush(rev
);
217 rev
->diffopt
.output_format
= saved_output_format
;
224 static void handle_tail(struct object_array
*commits
, struct rev_info
*revs
)
226 struct commit
*commit
;
227 while (commits
->nr
) {
228 commit
= (struct commit
*)commits
->objects
[commits
->nr
- 1].item
;
229 if (has_unshown_parent(commit
))
231 handle_commit(commit
, revs
);
236 static void handle_tag(const char *name
, struct tag
*tag
)
239 enum object_type type
;
241 const char *tagger
, *tagger_end
, *message
;
242 size_t message_size
= 0;
244 buf
= read_sha1_file(tag
->object
.sha1
, &type
, &size
);
246 die ("Could not read tag %s", sha1_to_hex(tag
->object
.sha1
));
247 message
= memmem(buf
, size
, "\n\n", 2);
250 message_size
= strlen(message
);
252 tagger
= memmem(buf
, message
? message
- buf
: size
, "\ntagger ", 8);
254 die ("No tagger for tag %s", sha1_to_hex(tag
->object
.sha1
));
256 tagger_end
= strchrnul(tagger
, '\n');
258 /* handle signed tags */
260 const char *signature
= strstr(message
,
261 "\n-----BEGIN PGP SIGNATURE-----\n");
263 switch(signed_tag_mode
) {
265 die ("Encountered signed tag %s; use "
266 "--signed-tag=<mode> to handle it.",
267 sha1_to_hex(tag
->object
.sha1
));
269 warning ("Exporting signed tag %s",
270 sha1_to_hex(tag
->object
.sha1
));
275 message_size
= signature
+ 1 - message
;
280 if (!prefixcmp(name
, "refs/tags/"))
282 printf("tag %s\nfrom :%d\n%.*s\ndata %d\n%.*s\n",
283 name
, get_object_mark(tag
->tagged
),
284 (int)(tagger_end
- tagger
), tagger
,
285 (int)message_size
, (int)message_size
, message
? message
: "");
288 static void get_tags_and_duplicates(struct object_array
*pending
,
289 struct path_list
*extra_refs
)
294 for (i
= 0; i
< pending
->nr
; i
++) {
295 struct object_array_entry
*e
= pending
->objects
+ i
;
296 unsigned char sha1
[20];
297 struct commit
*commit
= commit
;
300 if (dwim_ref(e
->name
, strlen(e
->name
), sha1
, &full_name
) != 1)
303 switch (e
->item
->type
) {
305 commit
= (struct commit
*)e
->item
;
308 tag
= (struct tag
*)e
->item
;
309 while (tag
&& tag
->object
.type
== OBJ_TAG
) {
310 path_list_insert(full_name
, extra_refs
)->util
= tag
;
311 tag
= (struct tag
*)tag
->tagged
;
314 die ("Tag %s points nowhere?", e
->name
);
315 switch(tag
->object
.type
) {
317 commit
= (struct commit
*)tag
;
320 handle_object(tag
->object
.sha1
);
325 die ("Unexpected object of type %s",
326 typename(e
->item
->type
));
329 /* more than one name for the same object */
330 path_list_insert(full_name
, extra_refs
)->util
= commit
;
332 commit
->util
= full_name
;
336 static void handle_tags_and_duplicates(struct path_list
*extra_refs
)
338 struct commit
*commit
;
341 for (i
= extra_refs
->nr
- 1; i
>= 0; i
--) {
342 const char *name
= extra_refs
->items
[i
].path
;
343 struct object
*object
= extra_refs
->items
[i
].util
;
344 switch (object
->type
) {
346 handle_tag(name
, (struct tag
*)object
);
349 /* create refs pointing to already seen commits */
350 commit
= (struct commit
*)object
;
351 printf("reset %s\nfrom :%d\n\n", name
,
352 get_object_mark(&commit
->object
));
359 int cmd_fast_export(int argc
, const char **argv
, const char *prefix
)
361 struct rev_info revs
;
362 struct object_array commits
= { 0, 0, NULL
};
363 struct path_list extra_refs
= { NULL
, 0, 0, 0 };
364 struct commit
*commit
;
365 struct option options
[] = {
366 OPT_INTEGER(0, "progress", &progress
,
367 "show progress after <n> objects"),
368 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode
, "mode",
369 "select handling of signed tags",
370 parse_opt_signed_tag_mode
),
374 /* we handle encodings */
375 git_config(git_default_config
);
377 init_revisions(&revs
, prefix
);
378 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
379 argc
= parse_options(argc
, argv
, options
, fast_export_usage
, 0);
381 usage_with_options (fast_export_usage
, options
);
383 get_tags_and_duplicates(&revs
.pending
, &extra_refs
);
385 if (prepare_revision_walk(&revs
))
386 die("revision walk setup failed");
387 revs
.diffopt
.format_callback
= show_filemodify
;
388 DIFF_OPT_SET(&revs
.diffopt
, RECURSIVE
);
389 while ((commit
= get_revision(&revs
))) {
390 if (has_unshown_parent(commit
)) {
391 struct commit_list
*parent
= commit
->parents
;
392 add_object_array(&commit
->object
, NULL
, &commits
);
393 for (; parent
; parent
= parent
->next
)
394 if (!parent
->item
->util
)
395 parent
->item
->util
= commit
->util
;
398 handle_commit(commit
, &revs
);
399 handle_tail(&commits
, &revs
);
403 handle_tags_and_duplicates(&extra_refs
);