Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 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" |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 16 | #include "string-list.h" |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 17 | #include "utf8.h" |
| 18 | #include "parse-options.h" |
| 19 | |
| 20 | static const char *fast_export_usage[] = { |
Stephan Beyer | 1b1dd23 | 2008-07-13 15:36:15 +0200 | [diff] [blame] | 21 | "git fast-export [rev-list-opts]", |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 22 | NULL |
| 23 | }; |
| 24 | |
| 25 | static int progress; |
Johannes Schindelin | ee4bc37 | 2007-12-03 22:44:39 +0000 | [diff] [blame] | 26 | static enum { VERBATIM, WARN, STRIP, ABORT } signed_tag_mode = ABORT; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 27 | |
| 28 | static int parse_opt_signed_tag_mode(const struct option *opt, |
| 29 | const char *arg, int unset) |
| 30 | { |
| 31 | if (unset || !strcmp(arg, "abort")) |
| 32 | signed_tag_mode = ABORT; |
Johannes Schindelin | ee4bc37 | 2007-12-03 22:44:39 +0000 | [diff] [blame] | 33 | else if (!strcmp(arg, "verbatim") || !strcmp(arg, "ignore")) |
| 34 | signed_tag_mode = VERBATIM; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 35 | else if (!strcmp(arg, "warn")) |
| 36 | signed_tag_mode = WARN; |
| 37 | else if (!strcmp(arg, "strip")) |
| 38 | signed_tag_mode = STRIP; |
| 39 | else |
| 40 | return error("Unknown signed-tag mode: %s", arg); |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | static struct decoration idnums; |
| 45 | static uint32_t last_idnum; |
| 46 | |
| 47 | static int has_unshown_parent(struct commit *commit) |
| 48 | { |
| 49 | struct commit_list *parent; |
| 50 | |
| 51 | for (parent = commit->parents; parent; parent = parent->next) |
| 52 | if (!(parent->item->object.flags & SHOWN) && |
| 53 | !(parent->item->object.flags & UNINTERESTING)) |
| 54 | return 1; |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | /* Since intptr_t is C99, we do not use it here */ |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 59 | static inline uint32_t *mark_to_ptr(uint32_t mark) |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 60 | { |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 61 | return ((uint32_t *)NULL) + mark; |
| 62 | } |
| 63 | |
| 64 | static inline uint32_t ptr_to_mark(void * mark) |
| 65 | { |
| 66 | return (uint32_t *)mark - (uint32_t *)NULL; |
| 67 | } |
| 68 | |
| 69 | static inline void mark_object(struct object *object, uint32_t mark) |
| 70 | { |
| 71 | add_decoration(&idnums, object, mark_to_ptr(mark)); |
| 72 | } |
| 73 | |
| 74 | static inline void mark_next_object(struct object *object) |
| 75 | { |
| 76 | mark_object(object, ++last_idnum); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static int get_object_mark(struct object *object) |
| 80 | { |
| 81 | void *decoration = lookup_decoration(&idnums, object); |
| 82 | if (!decoration) |
| 83 | return 0; |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 84 | return ptr_to_mark(decoration); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | static void show_progress(void) |
| 88 | { |
| 89 | static int counter = 0; |
| 90 | if (!progress) |
| 91 | return; |
| 92 | if ((++counter % progress) == 0) |
| 93 | printf("progress %d objects\n", counter); |
| 94 | } |
| 95 | |
| 96 | static void handle_object(const unsigned char *sha1) |
| 97 | { |
| 98 | unsigned long size; |
| 99 | enum object_type type; |
| 100 | char *buf; |
| 101 | struct object *object; |
| 102 | |
| 103 | if (is_null_sha1(sha1)) |
| 104 | return; |
| 105 | |
| 106 | object = parse_object(sha1); |
| 107 | if (!object) |
| 108 | die ("Could not read blob %s", sha1_to_hex(sha1)); |
| 109 | |
| 110 | if (object->flags & SHOWN) |
| 111 | return; |
| 112 | |
| 113 | buf = read_sha1_file(sha1, &type, &size); |
| 114 | if (!buf) |
| 115 | die ("Could not read blob %s", sha1_to_hex(sha1)); |
| 116 | |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 117 | mark_next_object(object); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 118 | |
Ramsay Jones | 6e1c234 | 2008-07-03 16:52:09 +0100 | [diff] [blame] | 119 | printf("blob\nmark :%"PRIu32"\ndata %lu\n", last_idnum, size); |
Alex Riesen | b0fe0d7 | 2007-12-11 23:01:28 +0100 | [diff] [blame] | 120 | if (size && fwrite(buf, size, 1, stdout) != 1) |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 121 | die ("Could not write blob %s", sha1_to_hex(sha1)); |
| 122 | printf("\n"); |
| 123 | |
| 124 | show_progress(); |
| 125 | |
| 126 | object->flags |= SHOWN; |
| 127 | free(buf); |
| 128 | } |
| 129 | |
| 130 | static void show_filemodify(struct diff_queue_struct *q, |
| 131 | struct diff_options *options, void *data) |
| 132 | { |
| 133 | int i; |
| 134 | for (i = 0; i < q->nr; i++) { |
Alexander Gavrilov | ae7c5dc | 2008-07-27 00:52:54 +0400 | [diff] [blame] | 135 | struct diff_filespec *ospec = q->queue[i]->one; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 136 | struct diff_filespec *spec = q->queue[i]->two; |
Alexander Gavrilov | ae7c5dc | 2008-07-27 00:52:54 +0400 | [diff] [blame] | 137 | |
| 138 | switch (q->queue[i]->status) { |
| 139 | case DIFF_STATUS_DELETED: |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 140 | printf("D %s\n", spec->path); |
Alexander Gavrilov | ae7c5dc | 2008-07-27 00:52:54 +0400 | [diff] [blame] | 141 | break; |
| 142 | |
| 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); |
| 147 | |
| 148 | if (!hashcmp(ospec->sha1, spec->sha1) && |
| 149 | ospec->mode == spec->mode) |
| 150 | break; |
| 151 | /* fallthrough */ |
| 152 | |
| 153 | case DIFF_STATUS_TYPE_CHANGED: |
| 154 | case DIFF_STATUS_MODIFIED: |
| 155 | case DIFF_STATUS_ADDED: |
Alexander Gavrilov | 03db452 | 2008-07-19 16:21:24 +0400 | [diff] [blame] | 156 | /* |
| 157 | * Links refer to objects in another repositories; |
| 158 | * output the SHA-1 verbatim. |
| 159 | */ |
| 160 | if (S_ISGITLINK(spec->mode)) |
| 161 | printf("M %06o %s %s\n", spec->mode, |
| 162 | sha1_to_hex(spec->sha1), spec->path); |
| 163 | else { |
| 164 | struct object *object = lookup_object(spec->sha1); |
| 165 | printf("M %06o :%d %s\n", spec->mode, |
| 166 | get_object_mark(object), spec->path); |
| 167 | } |
Alexander Gavrilov | ae7c5dc | 2008-07-27 00:52:54 +0400 | [diff] [blame] | 168 | break; |
| 169 | |
| 170 | default: |
| 171 | die("Unexpected comparison status '%c' for %s, %s", |
| 172 | q->queue[i]->status, |
| 173 | ospec->path ? ospec->path : "none", |
| 174 | spec->path ? spec->path : "none"); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | static const char *find_encoding(const char *begin, const char *end) |
| 180 | { |
| 181 | const char *needle = "\nencoding "; |
| 182 | char *bol, *eol; |
| 183 | |
| 184 | bol = memmem(begin, end ? end - begin : strlen(begin), |
| 185 | needle, strlen(needle)); |
| 186 | if (!bol) |
| 187 | return git_commit_encoding; |
| 188 | bol += strlen(needle); |
| 189 | eol = strchrnul(bol, '\n'); |
| 190 | *eol = '\0'; |
| 191 | return bol; |
| 192 | } |
| 193 | |
| 194 | static void handle_commit(struct commit *commit, struct rev_info *rev) |
| 195 | { |
| 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; |
| 201 | int i; |
| 202 | |
| 203 | rev->diffopt.output_format = DIFF_FORMAT_CALLBACK; |
| 204 | |
| 205 | parse_commit(commit); |
| 206 | author = strstr(commit->buffer, "\nauthor "); |
| 207 | if (!author) |
| 208 | die ("Could not find author in commit %s", |
| 209 | sha1_to_hex(commit->object.sha1)); |
| 210 | author++; |
| 211 | author_end = strchrnul(author, '\n'); |
| 212 | committer = strstr(author_end, "\ncommitter "); |
| 213 | if (!committer) |
| 214 | die ("Could not find committer in commit %s", |
| 215 | sha1_to_hex(commit->object.sha1)); |
| 216 | committer++; |
| 217 | committer_end = strchrnul(committer, '\n'); |
| 218 | message = strstr(committer_end, "\n\n"); |
| 219 | encoding = find_encoding(committer_end, message); |
| 220 | if (message) |
| 221 | message += 2; |
| 222 | |
| 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); |
| 227 | } |
| 228 | else |
| 229 | diff_root_tree_sha1(commit->tree->object.sha1, |
| 230 | "", &rev->diffopt); |
| 231 | |
Alexander Gavrilov | 03db452 | 2008-07-19 16:21:24 +0400 | [diff] [blame] | 232 | /* Export the referenced blobs, and remember the marks. */ |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 233 | for (i = 0; i < diff_queued_diff.nr; i++) |
Alexander Gavrilov | 03db452 | 2008-07-19 16:21:24 +0400 | [diff] [blame] | 234 | if (!S_ISGITLINK(diff_queued_diff.queue[i]->two->mode)) |
| 235 | handle_object(diff_queued_diff.queue[i]->two->sha1); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 236 | |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 237 | mark_next_object(&commit->object); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 238 | if (!is_encoding_utf8(encoding)) |
| 239 | reencoded = reencode_string(message, "UTF-8", encoding); |
Shawn O. Pearce | d8933f0 | 2008-06-13 00:38:55 -0400 | [diff] [blame] | 240 | if (!commit->parents) |
| 241 | printf("reset %s\n", (const char*)commit->util); |
Ramsay Jones | 6e1c234 | 2008-07-03 16:52:09 +0100 | [diff] [blame] | 242 | printf("commit %s\nmark :%"PRIu32"\n%.*s\n%.*s\ndata %u\n%s", |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 243 | (const char *)commit->util, last_idnum, |
| 244 | (int)(author_end - author), author, |
| 245 | (int)(committer_end - committer), committer, |
| 246 | (unsigned)(reencoded |
| 247 | ? strlen(reencoded) : message |
| 248 | ? strlen(message) : 0), |
| 249 | reencoded ? reencoded : message ? message : ""); |
Jim Meyering | 8e0f700 | 2008-01-31 18:26:32 +0100 | [diff] [blame] | 250 | free(reencoded); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 251 | |
| 252 | for (i = 0, p = commit->parents; p; p = p->next) { |
| 253 | int mark = get_object_mark(&p->item->object); |
| 254 | if (!mark) |
| 255 | continue; |
| 256 | if (i == 0) |
| 257 | printf("from :%d\n", mark); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 258 | else |
Pieter de Bie | 5070b49 | 2008-05-25 01:21:53 +0200 | [diff] [blame] | 259 | printf("merge :%d\n", mark); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 260 | i++; |
| 261 | } |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 262 | |
| 263 | log_tree_diff_flush(rev); |
| 264 | rev->diffopt.output_format = saved_output_format; |
| 265 | |
| 266 | printf("\n"); |
| 267 | |
| 268 | show_progress(); |
| 269 | } |
| 270 | |
| 271 | static void handle_tail(struct object_array *commits, struct rev_info *revs) |
| 272 | { |
| 273 | struct commit *commit; |
| 274 | while (commits->nr) { |
| 275 | commit = (struct commit *)commits->objects[commits->nr - 1].item; |
| 276 | if (has_unshown_parent(commit)) |
| 277 | return; |
| 278 | handle_commit(commit, revs); |
| 279 | commits->nr--; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | static void handle_tag(const char *name, struct tag *tag) |
| 284 | { |
| 285 | unsigned long size; |
| 286 | enum object_type type; |
| 287 | char *buf; |
| 288 | const char *tagger, *tagger_end, *message; |
| 289 | size_t message_size = 0; |
| 290 | |
| 291 | buf = read_sha1_file(tag->object.sha1, &type, &size); |
| 292 | if (!buf) |
| 293 | die ("Could not read tag %s", sha1_to_hex(tag->object.sha1)); |
| 294 | message = memmem(buf, size, "\n\n", 2); |
| 295 | if (message) { |
| 296 | message += 2; |
| 297 | message_size = strlen(message); |
| 298 | } |
| 299 | tagger = memmem(buf, message ? message - buf : size, "\ntagger ", 8); |
| 300 | if (!tagger) |
| 301 | die ("No tagger for tag %s", sha1_to_hex(tag->object.sha1)); |
| 302 | tagger++; |
| 303 | tagger_end = strchrnul(tagger, '\n'); |
| 304 | |
| 305 | /* handle signed tags */ |
| 306 | if (message) { |
| 307 | const char *signature = strstr(message, |
| 308 | "\n-----BEGIN PGP SIGNATURE-----\n"); |
| 309 | if (signature) |
| 310 | switch(signed_tag_mode) { |
| 311 | case ABORT: |
| 312 | die ("Encountered signed tag %s; use " |
| 313 | "--signed-tag=<mode> to handle it.", |
| 314 | sha1_to_hex(tag->object.sha1)); |
| 315 | case WARN: |
| 316 | warning ("Exporting signed tag %s", |
| 317 | sha1_to_hex(tag->object.sha1)); |
| 318 | /* fallthru */ |
Johannes Schindelin | ee4bc37 | 2007-12-03 22:44:39 +0000 | [diff] [blame] | 319 | case VERBATIM: |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 320 | break; |
| 321 | case STRIP: |
| 322 | message_size = signature + 1 - message; |
| 323 | break; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | if (!prefixcmp(name, "refs/tags/")) |
| 328 | name += 10; |
| 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 : ""); |
| 333 | } |
| 334 | |
| 335 | static void get_tags_and_duplicates(struct object_array *pending, |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 336 | struct string_list *extra_refs) |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 337 | { |
| 338 | struct tag *tag; |
| 339 | int i; |
| 340 | |
| 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; |
| 345 | char *full_name; |
| 346 | |
| 347 | if (dwim_ref(e->name, strlen(e->name), sha1, &full_name) != 1) |
| 348 | continue; |
| 349 | |
| 350 | switch (e->item->type) { |
| 351 | case OBJ_COMMIT: |
| 352 | commit = (struct commit *)e->item; |
| 353 | break; |
| 354 | case OBJ_TAG: |
| 355 | tag = (struct tag *)e->item; |
| 356 | while (tag && tag->object.type == OBJ_TAG) { |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 357 | string_list_insert(full_name, extra_refs)->util = tag; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 358 | tag = (struct tag *)tag->tagged; |
| 359 | } |
| 360 | if (!tag) |
| 361 | die ("Tag %s points nowhere?", e->name); |
| 362 | switch(tag->object.type) { |
| 363 | case OBJ_COMMIT: |
| 364 | commit = (struct commit *)tag; |
| 365 | break; |
| 366 | case OBJ_BLOB: |
| 367 | handle_object(tag->object.sha1); |
| 368 | continue; |
| 369 | } |
| 370 | break; |
| 371 | default: |
| 372 | die ("Unexpected object of type %s", |
| 373 | typename(e->item->type)); |
| 374 | } |
| 375 | if (commit->util) |
| 376 | /* more than one name for the same object */ |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 377 | string_list_insert(full_name, extra_refs)->util = commit; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 378 | else |
| 379 | commit->util = full_name; |
| 380 | } |
| 381 | } |
| 382 | |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 383 | static void handle_tags_and_duplicates(struct string_list *extra_refs) |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 384 | { |
| 385 | struct commit *commit; |
| 386 | int i; |
| 387 | |
| 388 | for (i = extra_refs->nr - 1; i >= 0; i--) { |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 389 | const char *name = extra_refs->items[i].string; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 390 | struct object *object = extra_refs->items[i].util; |
| 391 | switch (object->type) { |
| 392 | case OBJ_TAG: |
| 393 | handle_tag(name, (struct tag *)object); |
| 394 | break; |
| 395 | case OBJ_COMMIT: |
| 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)); |
| 400 | show_progress(); |
| 401 | break; |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 406 | static void export_marks(char *file) |
| 407 | { |
| 408 | unsigned int i; |
| 409 | uint32_t mark; |
| 410 | struct object_decoration *deco = idnums.hash; |
| 411 | FILE *f; |
| 412 | |
| 413 | f = fopen(file, "w"); |
| 414 | if (!f) |
| 415 | error("Unable to open marks file %s for writing", file); |
| 416 | |
Junio C Hamano | 6991357 | 2008-07-03 00:25:23 -0700 | [diff] [blame] | 417 | for (i = 0; i < idnums.size; i++) { |
| 418 | if (deco->base && deco->base->type == 1) { |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 419 | mark = ptr_to_mark(deco->decoration); |
Ramsay Jones | d47fb8b | 2008-08-26 18:50:37 +0100 | [diff] [blame] | 420 | fprintf(f, ":%"PRIu32" %s\n", mark, |
| 421 | sha1_to_hex(deco->base->sha1)); |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 422 | } |
Junio C Hamano | 6991357 | 2008-07-03 00:25:23 -0700 | [diff] [blame] | 423 | deco++; |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | if (ferror(f) || fclose(f)) |
| 427 | error("Unable to write marks file %s.", file); |
| 428 | } |
| 429 | |
Junio C Hamano | 6991357 | 2008-07-03 00:25:23 -0700 | [diff] [blame] | 430 | static void import_marks(char *input_file) |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 431 | { |
| 432 | char line[512]; |
| 433 | FILE *f = fopen(input_file, "r"); |
| 434 | if (!f) |
| 435 | die("cannot read %s: %s", input_file, strerror(errno)); |
| 436 | |
| 437 | while (fgets(line, sizeof(line), f)) { |
| 438 | uint32_t mark; |
| 439 | char *line_end, *mark_end; |
| 440 | unsigned char sha1[20]; |
| 441 | struct object *object; |
| 442 | |
| 443 | line_end = strchr(line, '\n'); |
| 444 | if (line[0] != ':' || !line_end) |
| 445 | die("corrupt mark line: %s", line); |
Junio C Hamano | 6991357 | 2008-07-03 00:25:23 -0700 | [diff] [blame] | 446 | *line_end = '\0'; |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 447 | |
| 448 | mark = strtoumax(line + 1, &mark_end, 10); |
| 449 | if (!mark || mark_end == line + 1 |
| 450 | || *mark_end != ' ' || get_sha1(mark_end + 1, sha1)) |
| 451 | die("corrupt mark line: %s", line); |
| 452 | |
| 453 | object = parse_object(sha1); |
| 454 | if (!object) |
| 455 | die ("Could not read blob %s", sha1_to_hex(sha1)); |
| 456 | |
| 457 | if (object->flags & SHOWN) |
| 458 | error("Object %s already has a mark", sha1); |
| 459 | |
| 460 | mark_object(object, mark); |
| 461 | if (last_idnum < mark) |
| 462 | last_idnum = mark; |
| 463 | |
| 464 | object->flags |= SHOWN; |
| 465 | } |
| 466 | fclose(f); |
| 467 | } |
| 468 | |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 469 | int cmd_fast_export(int argc, const char **argv, const char *prefix) |
| 470 | { |
| 471 | struct rev_info revs; |
| 472 | struct object_array commits = { 0, 0, NULL }; |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 473 | struct string_list extra_refs = { NULL, 0, 0, 0 }; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 474 | struct commit *commit; |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 475 | char *export_filename = NULL, *import_filename = NULL; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 476 | struct option options[] = { |
| 477 | OPT_INTEGER(0, "progress", &progress, |
| 478 | "show progress after <n> objects"), |
| 479 | OPT_CALLBACK(0, "signed-tags", &signed_tag_mode, "mode", |
| 480 | "select handling of signed tags", |
| 481 | parse_opt_signed_tag_mode), |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 482 | OPT_STRING(0, "export-marks", &export_filename, "FILE", |
| 483 | "Dump marks to this file"), |
| 484 | OPT_STRING(0, "import-marks", &import_filename, "FILE", |
| 485 | "Import marks from this file"), |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 486 | OPT_END() |
| 487 | }; |
| 488 | |
| 489 | /* we handle encodings */ |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 490 | git_config(git_default_config, NULL); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 491 | |
| 492 | init_revisions(&revs, prefix); |
| 493 | argc = setup_revisions(argc, argv, &revs, NULL); |
| 494 | argc = parse_options(argc, argv, options, fast_export_usage, 0); |
| 495 | if (argc > 1) |
| 496 | usage_with_options (fast_export_usage, options); |
| 497 | |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 498 | if (import_filename) |
| 499 | import_marks(import_filename); |
| 500 | |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 501 | get_tags_and_duplicates(&revs.pending, &extra_refs); |
| 502 | |
Martin Koegler | 3d51e1b | 2008-02-18 08:31:56 +0100 | [diff] [blame] | 503 | if (prepare_revision_walk(&revs)) |
| 504 | die("revision walk setup failed"); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 505 | revs.diffopt.format_callback = show_filemodify; |
| 506 | DIFF_OPT_SET(&revs.diffopt, RECURSIVE); |
| 507 | while ((commit = get_revision(&revs))) { |
| 508 | if (has_unshown_parent(commit)) { |
| 509 | struct commit_list *parent = commit->parents; |
| 510 | add_object_array(&commit->object, NULL, &commits); |
| 511 | for (; parent; parent = parent->next) |
| 512 | if (!parent->item->util) |
| 513 | parent->item->util = commit->util; |
| 514 | } |
| 515 | else { |
| 516 | handle_commit(commit, &revs); |
| 517 | handle_tail(&commits, &revs); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | handle_tags_and_duplicates(&extra_refs); |
| 522 | |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 523 | if (export_filename) |
| 524 | export_marks(export_filename); |
| 525 | |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 526 | return 0; |
| 527 | } |