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" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 8 | #include "config.h" |
Michael Haggerty | fb58c8d | 2015-06-22 16:03:05 +0200 | [diff] [blame] | 9 | #include "refs.h" |
Brandon Williams | ec0cb49 | 2018-05-16 15:57:48 -0700 | [diff] [blame] | 10 | #include "refspec.h" |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 11 | #include "object-store.h" |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 12 | #include "commit.h" |
| 13 | #include "object.h" |
| 14 | #include "tag.h" |
| 15 | #include "diff.h" |
| 16 | #include "diffcore.h" |
| 17 | #include "log-tree.h" |
| 18 | #include "revision.h" |
| 19 | #include "decorate.h" |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 20 | #include "string-list.h" |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 21 | #include "utf8.h" |
| 22 | #include "parse-options.h" |
Jeff King | 6280dfd | 2011-08-05 16:36:22 -0600 | [diff] [blame] | 23 | #include "quote.h" |
Felipe Contreras | 03e9010 | 2014-04-20 13:59:24 -0500 | [diff] [blame] | 24 | #include "remote.h" |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 25 | #include "blob.h" |
Nguyễn Thái Ngọc Duy | 87be252 | 2018-05-19 07:28:24 +0200 | [diff] [blame] | 26 | #include "commit-slab.h" |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 27 | |
| 28 | static const char *fast_export_usage[] = { |
Nguyễn Thái Ngọc Duy | 3b787b9 | 2012-08-20 19:32:08 +0700 | [diff] [blame] | 29 | N_("git fast-export [rev-list-opts]"), |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 30 | NULL |
| 31 | }; |
| 32 | |
| 33 | static int progress; |
John Keeping | cd16c59 | 2013-04-14 11:57:06 +0100 | [diff] [blame] | 34 | static enum { ABORT, VERBATIM, WARN, WARN_STRIP, STRIP } signed_tag_mode = ABORT; |
Ævar Arnfjörð Bjarmason | d7a10c3 | 2011-12-21 01:18:19 +0000 | [diff] [blame] | 35 | static enum { ERROR, DROP, REWRITE } tag_of_filtered_mode = ERROR; |
Johannes Schindelin | 4e46a8d | 2008-12-20 01:00:27 +0100 | [diff] [blame] | 36 | static int fake_missing_tagger; |
Sverre Rabbelier | 82670a5 | 2011-07-16 15:03:33 +0200 | [diff] [blame] | 37 | static int use_done_feature; |
Geoffrey Irving | 79559f2 | 2009-07-27 22:20:22 -0400 | [diff] [blame] | 38 | static int no_data; |
Elijah Newren | 7f40ab0 | 2010-07-17 11:00:51 -0600 | [diff] [blame] | 39 | static int full_tree; |
Felipe Contreras | 1d844ee | 2013-09-01 02:05:47 -0500 | [diff] [blame] | 40 | static struct string_list extra_refs = STRING_LIST_INIT_NODUP; |
Brandon Williams | 16eefc8 | 2018-05-16 15:57:59 -0700 | [diff] [blame] | 41 | static struct refspec refspecs = REFSPEC_INIT_FETCH; |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 42 | static int anonymize; |
Nguyễn Thái Ngọc Duy | 87be252 | 2018-05-19 07:28:24 +0200 | [diff] [blame] | 43 | static struct revision_sources revision_sources; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 44 | |
| 45 | static int parse_opt_signed_tag_mode(const struct option *opt, |
| 46 | const char *arg, int unset) |
| 47 | { |
| 48 | if (unset || !strcmp(arg, "abort")) |
| 49 | signed_tag_mode = ABORT; |
Johannes Schindelin | ee4bc37 | 2007-12-03 22:44:39 +0000 | [diff] [blame] | 50 | else if (!strcmp(arg, "verbatim") || !strcmp(arg, "ignore")) |
| 51 | signed_tag_mode = VERBATIM; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 52 | else if (!strcmp(arg, "warn")) |
| 53 | signed_tag_mode = WARN; |
John Keeping | cd16c59 | 2013-04-14 11:57:06 +0100 | [diff] [blame] | 54 | else if (!strcmp(arg, "warn-strip")) |
| 55 | signed_tag_mode = WARN_STRIP; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 56 | else if (!strcmp(arg, "strip")) |
| 57 | signed_tag_mode = STRIP; |
| 58 | else |
Paul Price | 04a74b6 | 2013-04-12 10:05:55 -0400 | [diff] [blame] | 59 | return error("Unknown signed-tags mode: %s", arg); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 60 | return 0; |
| 61 | } |
| 62 | |
Elijah Newren | 2d8ad46 | 2009-06-25 22:48:31 -0600 | [diff] [blame] | 63 | static int parse_opt_tag_of_filtered_mode(const struct option *opt, |
| 64 | const char *arg, int unset) |
| 65 | { |
| 66 | if (unset || !strcmp(arg, "abort")) |
Ævar Arnfjörð Bjarmason | d7a10c3 | 2011-12-21 01:18:19 +0000 | [diff] [blame] | 67 | tag_of_filtered_mode = ERROR; |
Elijah Newren | 2d8ad46 | 2009-06-25 22:48:31 -0600 | [diff] [blame] | 68 | else if (!strcmp(arg, "drop")) |
| 69 | tag_of_filtered_mode = DROP; |
| 70 | else if (!strcmp(arg, "rewrite")) |
| 71 | tag_of_filtered_mode = REWRITE; |
| 72 | else |
| 73 | return error("Unknown tag-of-filtered mode: %s", arg); |
| 74 | return 0; |
| 75 | } |
| 76 | |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 77 | static struct decoration idnums; |
| 78 | static uint32_t last_idnum; |
| 79 | |
| 80 | static int has_unshown_parent(struct commit *commit) |
| 81 | { |
| 82 | struct commit_list *parent; |
| 83 | |
| 84 | for (parent = commit->parents; parent; parent = parent->next) |
| 85 | if (!(parent->item->object.flags & SHOWN) && |
| 86 | !(parent->item->object.flags & UNINTERESTING)) |
| 87 | return 1; |
| 88 | return 0; |
| 89 | } |
| 90 | |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 91 | struct anonymized_entry { |
| 92 | struct hashmap_entry hash; |
| 93 | const char *orig; |
| 94 | size_t orig_len; |
| 95 | const char *anon; |
| 96 | size_t anon_len; |
| 97 | }; |
| 98 | |
Stefan Beller | 7663cdc | 2017-06-30 12:14:05 -0700 | [diff] [blame] | 99 | static int anonymized_entry_cmp(const void *unused_cmp_data, |
| 100 | const void *va, const void *vb, |
| 101 | const void *unused_keydata) |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 102 | { |
| 103 | const struct anonymized_entry *a = va, *b = vb; |
| 104 | return a->orig_len != b->orig_len || |
| 105 | memcmp(a->orig, b->orig, a->orig_len); |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * Basically keep a cache of X->Y so that we can repeatedly replace |
| 110 | * the same anonymized string with another. The actual generation |
| 111 | * is farmed out to the generate function. |
| 112 | */ |
| 113 | static const void *anonymize_mem(struct hashmap *map, |
| 114 | void *(*generate)(const void *, size_t *), |
| 115 | const void *orig, size_t *len) |
| 116 | { |
| 117 | struct anonymized_entry key, *ret; |
| 118 | |
| 119 | if (!map->cmpfn) |
Stefan Beller | 7663cdc | 2017-06-30 12:14:05 -0700 | [diff] [blame] | 120 | hashmap_init(map, anonymized_entry_cmp, NULL, 0); |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 121 | |
| 122 | hashmap_entry_init(&key, memhash(orig, *len)); |
| 123 | key.orig = orig; |
| 124 | key.orig_len = *len; |
| 125 | ret = hashmap_get(map, &key, NULL); |
| 126 | |
| 127 | if (!ret) { |
| 128 | ret = xmalloc(sizeof(*ret)); |
| 129 | hashmap_entry_init(&ret->hash, key.hash.hash); |
| 130 | ret->orig = xstrdup(orig); |
| 131 | ret->orig_len = *len; |
| 132 | ret->anon = generate(orig, len); |
| 133 | ret->anon_len = *len; |
| 134 | hashmap_put(map, ret); |
| 135 | } |
| 136 | |
| 137 | *len = ret->anon_len; |
| 138 | return ret->anon; |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * We anonymize each component of a path individually, |
| 143 | * so that paths a/b and a/c will share a common root. |
| 144 | * The paths are cached via anonymize_mem so that repeated |
| 145 | * lookups for "a" will yield the same value. |
| 146 | */ |
| 147 | static void anonymize_path(struct strbuf *out, const char *path, |
| 148 | struct hashmap *map, |
| 149 | void *(*generate)(const void *, size_t *)) |
| 150 | { |
| 151 | while (*path) { |
| 152 | const char *end_of_component = strchrnul(path, '/'); |
| 153 | size_t len = end_of_component - path; |
| 154 | const char *c = anonymize_mem(map, generate, path, &len); |
| 155 | strbuf_add(out, c, len); |
| 156 | path = end_of_component; |
| 157 | if (*path) |
| 158 | strbuf_addch(out, *path++); |
| 159 | } |
| 160 | } |
| 161 | |
René Scharfe | c112084 | 2018-05-09 23:06:46 +0200 | [diff] [blame] | 162 | static inline void *mark_to_ptr(uint32_t mark) |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 163 | { |
René Scharfe | c112084 | 2018-05-09 23:06:46 +0200 | [diff] [blame] | 164 | return (void *)(uintptr_t)mark; |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static inline uint32_t ptr_to_mark(void * mark) |
| 168 | { |
René Scharfe | c112084 | 2018-05-09 23:06:46 +0200 | [diff] [blame] | 169 | return (uint32_t)(uintptr_t)mark; |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | static inline void mark_object(struct object *object, uint32_t mark) |
| 173 | { |
| 174 | add_decoration(&idnums, object, mark_to_ptr(mark)); |
| 175 | } |
| 176 | |
| 177 | static inline void mark_next_object(struct object *object) |
| 178 | { |
| 179 | mark_object(object, ++last_idnum); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static int get_object_mark(struct object *object) |
| 183 | { |
| 184 | void *decoration = lookup_decoration(&idnums, object); |
| 185 | if (!decoration) |
| 186 | return 0; |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 187 | return ptr_to_mark(decoration); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | static void show_progress(void) |
| 191 | { |
| 192 | static int counter = 0; |
| 193 | if (!progress) |
| 194 | return; |
| 195 | if ((++counter % progress) == 0) |
| 196 | printf("progress %d objects\n", counter); |
| 197 | } |
| 198 | |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 199 | /* |
| 200 | * Ideally we would want some transformation of the blob data here |
| 201 | * that is unreversible, but would still be the same size and have |
| 202 | * the same data relationship to other blobs (so that we get the same |
| 203 | * delta and packing behavior as the original). But the first and last |
| 204 | * requirements there are probably mutually exclusive, so let's take |
| 205 | * the easy way out for now, and just generate arbitrary content. |
| 206 | * |
| 207 | * There's no need to cache this result with anonymize_mem, since |
| 208 | * we already handle blob content caching with marks. |
| 209 | */ |
| 210 | static char *anonymize_blob(unsigned long *size) |
| 211 | { |
| 212 | static int counter; |
| 213 | struct strbuf out = STRBUF_INIT; |
| 214 | strbuf_addf(&out, "anonymous blob %d", counter++); |
| 215 | *size = out.len; |
| 216 | return strbuf_detach(&out, NULL); |
| 217 | } |
| 218 | |
brian m. carlson | 273f8ee | 2017-02-21 23:47:23 +0000 | [diff] [blame] | 219 | static void export_blob(const struct object_id *oid) |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 220 | { |
| 221 | unsigned long size; |
| 222 | enum object_type type; |
| 223 | char *buf; |
| 224 | struct object *object; |
Jeff King | 30b939c | 2013-03-17 04:38:57 -0400 | [diff] [blame] | 225 | int eaten; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 226 | |
Geoffrey Irving | 79559f2 | 2009-07-27 22:20:22 -0400 | [diff] [blame] | 227 | if (no_data) |
| 228 | return; |
| 229 | |
brian m. carlson | 273f8ee | 2017-02-21 23:47:23 +0000 | [diff] [blame] | 230 | if (is_null_oid(oid)) |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 231 | return; |
| 232 | |
Stefan Beller | 5abddd1 | 2018-06-28 18:21:52 -0700 | [diff] [blame] | 233 | object = lookup_object(the_repository, oid->hash); |
Jeff King | 30b939c | 2013-03-17 04:38:57 -0400 | [diff] [blame] | 234 | if (object && object->flags & SHOWN) |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 235 | return; |
| 236 | |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 237 | if (anonymize) { |
| 238 | buf = anonymize_blob(&size); |
Stefan Beller | da14a7f | 2018-06-28 18:21:55 -0700 | [diff] [blame] | 239 | object = (struct object *)lookup_blob(the_repository, oid); |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 240 | eaten = 0; |
| 241 | } else { |
brian m. carlson | b4f5aca | 2018-03-12 02:27:53 +0000 | [diff] [blame] | 242 | buf = read_object_file(oid, &type, &size); |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 243 | if (!buf) |
brian m. carlson | 273f8ee | 2017-02-21 23:47:23 +0000 | [diff] [blame] | 244 | die ("Could not read blob %s", oid_to_hex(oid)); |
brian m. carlson | 17e6545 | 2018-03-12 02:27:39 +0000 | [diff] [blame] | 245 | if (check_object_signature(oid, buf, size, type_name(type)) < 0) |
brian m. carlson | 273f8ee | 2017-02-21 23:47:23 +0000 | [diff] [blame] | 246 | die("sha1 mismatch in blob %s", oid_to_hex(oid)); |
Stefan Beller | 1ec5bfd | 2018-06-28 18:21:53 -0700 | [diff] [blame] | 247 | object = parse_object_buffer(the_repository, oid, type, |
| 248 | size, buf, &eaten); |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 249 | } |
| 250 | |
Jeff King | 30b939c | 2013-03-17 04:38:57 -0400 | [diff] [blame] | 251 | if (!object) |
brian m. carlson | 273f8ee | 2017-02-21 23:47:23 +0000 | [diff] [blame] | 252 | die("Could not read blob %s", oid_to_hex(oid)); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 253 | |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 254 | mark_next_object(object); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 255 | |
Ramsay Jones | 6e1c234 | 2008-07-03 16:52:09 +0100 | [diff] [blame] | 256 | printf("blob\nmark :%"PRIu32"\ndata %lu\n", last_idnum, size); |
Alex Riesen | b0fe0d7 | 2007-12-11 23:01:28 +0100 | [diff] [blame] | 257 | if (size && fwrite(buf, size, 1, stdout) != 1) |
brian m. carlson | 273f8ee | 2017-02-21 23:47:23 +0000 | [diff] [blame] | 258 | die_errno ("Could not write blob '%s'", oid_to_hex(oid)); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 259 | printf("\n"); |
| 260 | |
| 261 | show_progress(); |
| 262 | |
| 263 | object->flags |= SHOWN; |
Jeff King | 30b939c | 2013-03-17 04:38:57 -0400 | [diff] [blame] | 264 | if (!eaten) |
| 265 | free(buf); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Elijah Newren | 060df62 | 2010-07-09 07:10:55 -0600 | [diff] [blame] | 268 | static int depth_first(const void *a_, const void *b_) |
| 269 | { |
| 270 | const struct diff_filepair *a = *((const struct diff_filepair **)a_); |
| 271 | const struct diff_filepair *b = *((const struct diff_filepair **)b_); |
| 272 | const char *name_a, *name_b; |
| 273 | int len_a, len_b, len; |
| 274 | int cmp; |
| 275 | |
| 276 | name_a = a->one ? a->one->path : a->two->path; |
| 277 | name_b = b->one ? b->one->path : b->two->path; |
| 278 | |
| 279 | len_a = strlen(name_a); |
| 280 | len_b = strlen(name_b); |
| 281 | len = (len_a < len_b) ? len_a : len_b; |
| 282 | |
| 283 | /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */ |
| 284 | cmp = memcmp(name_a, name_b, len); |
| 285 | if (cmp) |
| 286 | return cmp; |
Johannes Sixt | 4ce6fb8 | 2010-09-07 21:33:02 +0200 | [diff] [blame] | 287 | cmp = len_b - len_a; |
| 288 | if (cmp) |
| 289 | return cmp; |
| 290 | /* |
| 291 | * Move 'R'ename entries last so that all references of the file |
| 292 | * appear in the output before it is renamed (e.g., when a file |
| 293 | * was copied and renamed in the same commit). |
| 294 | */ |
| 295 | return (a->status == 'R') - (b->status == 'R'); |
Elijah Newren | 060df62 | 2010-07-09 07:10:55 -0600 | [diff] [blame] | 296 | } |
| 297 | |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 298 | static void print_path_1(const char *path) |
Jeff King | 6280dfd | 2011-08-05 16:36:22 -0600 | [diff] [blame] | 299 | { |
| 300 | int need_quote = quote_c_style(path, NULL, NULL, 0); |
| 301 | if (need_quote) |
| 302 | quote_c_style(path, NULL, stdout, 0); |
Jay Soffian | ff59f6d | 2012-06-27 17:58:01 -0400 | [diff] [blame] | 303 | else if (strchr(path, ' ')) |
| 304 | printf("\"%s\"", path); |
Jeff King | 6280dfd | 2011-08-05 16:36:22 -0600 | [diff] [blame] | 305 | else |
| 306 | printf("%s", path); |
| 307 | } |
| 308 | |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 309 | static void *anonymize_path_component(const void *path, size_t *len) |
| 310 | { |
| 311 | static int counter; |
| 312 | struct strbuf out = STRBUF_INIT; |
| 313 | strbuf_addf(&out, "path%d", counter++); |
| 314 | return strbuf_detach(&out, len); |
| 315 | } |
| 316 | |
| 317 | static void print_path(const char *path) |
| 318 | { |
| 319 | if (!anonymize) |
| 320 | print_path_1(path); |
| 321 | else { |
| 322 | static struct hashmap paths; |
| 323 | static struct strbuf anon = STRBUF_INIT; |
| 324 | |
| 325 | anonymize_path(&anon, path, &paths, anonymize_path_component); |
| 326 | print_path_1(anon.buf); |
| 327 | strbuf_reset(&anon); |
| 328 | } |
| 329 | } |
| 330 | |
brian m. carlson | 273f8ee | 2017-02-21 23:47:23 +0000 | [diff] [blame] | 331 | static void *generate_fake_oid(const void *old, size_t *len) |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 332 | { |
| 333 | static uint32_t counter = 1; /* avoid null sha1 */ |
brian m. carlson | 273f8ee | 2017-02-21 23:47:23 +0000 | [diff] [blame] | 334 | unsigned char *out = xcalloc(GIT_SHA1_RAWSZ, 1); |
| 335 | put_be32(out + GIT_SHA1_RAWSZ - 4, counter++); |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 336 | return out; |
| 337 | } |
| 338 | |
brian m. carlson | 273f8ee | 2017-02-21 23:47:23 +0000 | [diff] [blame] | 339 | static const unsigned char *anonymize_sha1(const struct object_id *oid) |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 340 | { |
| 341 | static struct hashmap sha1s; |
brian m. carlson | 273f8ee | 2017-02-21 23:47:23 +0000 | [diff] [blame] | 342 | size_t len = GIT_SHA1_RAWSZ; |
| 343 | return anonymize_mem(&sha1s, generate_fake_oid, oid, &len); |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 344 | } |
| 345 | |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 346 | static void show_filemodify(struct diff_queue_struct *q, |
| 347 | struct diff_options *options, void *data) |
| 348 | { |
| 349 | int i; |
Jonathan Tan | b3e8ca8 | 2017-09-20 16:55:02 -0700 | [diff] [blame] | 350 | struct string_list *changed = data; |
Elijah Newren | 060df62 | 2010-07-09 07:10:55 -0600 | [diff] [blame] | 351 | |
| 352 | /* |
| 353 | * Handle files below a directory first, in case they are all deleted |
| 354 | * and the directory changes to a file or symlink. |
| 355 | */ |
René Scharfe | 9ed0d8d | 2016-09-29 17:27:31 +0200 | [diff] [blame] | 356 | QSORT(q->queue, q->nr, depth_first); |
Elijah Newren | 060df62 | 2010-07-09 07:10:55 -0600 | [diff] [blame] | 357 | |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 358 | for (i = 0; i < q->nr; i++) { |
Alexander Gavrilov | ae7c5dc | 2008-07-27 00:52:54 +0400 | [diff] [blame] | 359 | struct diff_filespec *ospec = q->queue[i]->one; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 360 | struct diff_filespec *spec = q->queue[i]->two; |
Alexander Gavrilov | ae7c5dc | 2008-07-27 00:52:54 +0400 | [diff] [blame] | 361 | |
| 362 | switch (q->queue[i]->status) { |
| 363 | case DIFF_STATUS_DELETED: |
Jeff King | 6280dfd | 2011-08-05 16:36:22 -0600 | [diff] [blame] | 364 | printf("D "); |
| 365 | print_path(spec->path); |
Jonathan Tan | b3e8ca8 | 2017-09-20 16:55:02 -0700 | [diff] [blame] | 366 | string_list_insert(changed, spec->path); |
Jeff King | 6280dfd | 2011-08-05 16:36:22 -0600 | [diff] [blame] | 367 | putchar('\n'); |
Alexander Gavrilov | ae7c5dc | 2008-07-27 00:52:54 +0400 | [diff] [blame] | 368 | break; |
| 369 | |
| 370 | case DIFF_STATUS_COPIED: |
| 371 | case DIFF_STATUS_RENAMED: |
Jonathan Tan | b3e8ca8 | 2017-09-20 16:55:02 -0700 | [diff] [blame] | 372 | /* |
| 373 | * If a change in the file corresponding to ospec->path |
| 374 | * has been observed, we cannot trust its contents |
| 375 | * because the diff is calculated based on the prior |
| 376 | * contents, not the current contents. So, declare a |
| 377 | * copy or rename only if there was no change observed. |
| 378 | */ |
| 379 | if (!string_list_has_string(changed, ospec->path)) { |
| 380 | printf("%c ", q->queue[i]->status); |
| 381 | print_path(ospec->path); |
| 382 | putchar(' '); |
| 383 | print_path(spec->path); |
| 384 | string_list_insert(changed, spec->path); |
| 385 | putchar('\n'); |
Alexander Gavrilov | ae7c5dc | 2008-07-27 00:52:54 +0400 | [diff] [blame] | 386 | |
Jonathan Tan | b3e8ca8 | 2017-09-20 16:55:02 -0700 | [diff] [blame] | 387 | if (!oidcmp(&ospec->oid, &spec->oid) && |
| 388 | ospec->mode == spec->mode) |
| 389 | break; |
| 390 | } |
Alexander Gavrilov | ae7c5dc | 2008-07-27 00:52:54 +0400 | [diff] [blame] | 391 | /* fallthrough */ |
| 392 | |
| 393 | case DIFF_STATUS_TYPE_CHANGED: |
| 394 | case DIFF_STATUS_MODIFIED: |
| 395 | case DIFF_STATUS_ADDED: |
Alexander Gavrilov | 03db452 | 2008-07-19 16:21:24 +0400 | [diff] [blame] | 396 | /* |
| 397 | * Links refer to objects in another repositories; |
| 398 | * output the SHA-1 verbatim. |
| 399 | */ |
Geoffrey Irving | 79559f2 | 2009-07-27 22:20:22 -0400 | [diff] [blame] | 400 | if (no_data || S_ISGITLINK(spec->mode)) |
Jeff King | 6280dfd | 2011-08-05 16:36:22 -0600 | [diff] [blame] | 401 | printf("M %06o %s ", spec->mode, |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 402 | sha1_to_hex(anonymize ? |
brian m. carlson | 273f8ee | 2017-02-21 23:47:23 +0000 | [diff] [blame] | 403 | anonymize_sha1(&spec->oid) : |
brian m. carlson | a0d12c4 | 2016-06-24 23:09:23 +0000 | [diff] [blame] | 404 | spec->oid.hash)); |
Alexander Gavrilov | 03db452 | 2008-07-19 16:21:24 +0400 | [diff] [blame] | 405 | else { |
Stefan Beller | 5abddd1 | 2018-06-28 18:21:52 -0700 | [diff] [blame] | 406 | struct object *object = lookup_object(the_repository, |
| 407 | spec->oid.hash); |
Jeff King | 6280dfd | 2011-08-05 16:36:22 -0600 | [diff] [blame] | 408 | printf("M %06o :%d ", spec->mode, |
| 409 | get_object_mark(object)); |
Alexander Gavrilov | 03db452 | 2008-07-19 16:21:24 +0400 | [diff] [blame] | 410 | } |
Jeff King | 6280dfd | 2011-08-05 16:36:22 -0600 | [diff] [blame] | 411 | print_path(spec->path); |
Jonathan Tan | b3e8ca8 | 2017-09-20 16:55:02 -0700 | [diff] [blame] | 412 | string_list_insert(changed, spec->path); |
Jeff King | 6280dfd | 2011-08-05 16:36:22 -0600 | [diff] [blame] | 413 | putchar('\n'); |
Alexander Gavrilov | ae7c5dc | 2008-07-27 00:52:54 +0400 | [diff] [blame] | 414 | break; |
| 415 | |
| 416 | default: |
| 417 | die("Unexpected comparison status '%c' for %s, %s", |
| 418 | q->queue[i]->status, |
| 419 | ospec->path ? ospec->path : "none", |
| 420 | spec->path ? spec->path : "none"); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | static const char *find_encoding(const char *begin, const char *end) |
| 426 | { |
| 427 | const char *needle = "\nencoding "; |
| 428 | char *bol, *eol; |
| 429 | |
| 430 | bol = memmem(begin, end ? end - begin : strlen(begin), |
| 431 | needle, strlen(needle)); |
| 432 | if (!bol) |
| 433 | return git_commit_encoding; |
| 434 | bol += strlen(needle); |
| 435 | eol = strchrnul(bol, '\n'); |
| 436 | *eol = '\0'; |
| 437 | return bol; |
| 438 | } |
| 439 | |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 440 | static void *anonymize_ref_component(const void *old, size_t *len) |
| 441 | { |
| 442 | static int counter; |
| 443 | struct strbuf out = STRBUF_INIT; |
| 444 | strbuf_addf(&out, "ref%d", counter++); |
| 445 | return strbuf_detach(&out, len); |
| 446 | } |
| 447 | |
| 448 | static const char *anonymize_refname(const char *refname) |
| 449 | { |
| 450 | /* |
| 451 | * If any of these prefixes is found, we will leave it intact |
| 452 | * so that tags remain tags and so forth. |
| 453 | */ |
| 454 | static const char *prefixes[] = { |
| 455 | "refs/heads/", |
| 456 | "refs/tags/", |
| 457 | "refs/remotes/", |
| 458 | "refs/" |
| 459 | }; |
| 460 | static struct hashmap refs; |
| 461 | static struct strbuf anon = STRBUF_INIT; |
| 462 | int i; |
| 463 | |
| 464 | /* |
| 465 | * We also leave "master" as a special case, since it does not reveal |
| 466 | * anything interesting. |
| 467 | */ |
| 468 | if (!strcmp(refname, "refs/heads/master")) |
| 469 | return refname; |
| 470 | |
| 471 | strbuf_reset(&anon); |
| 472 | for (i = 0; i < ARRAY_SIZE(prefixes); i++) { |
| 473 | if (skip_prefix(refname, prefixes[i], &refname)) { |
| 474 | strbuf_addstr(&anon, prefixes[i]); |
| 475 | break; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | anonymize_path(&anon, refname, &refs, anonymize_ref_component); |
| 480 | return anon.buf; |
| 481 | } |
| 482 | |
| 483 | /* |
| 484 | * We do not even bother to cache commit messages, as they are unlikely |
| 485 | * to be repeated verbatim, and it is not that interesting when they are. |
| 486 | */ |
| 487 | static char *anonymize_commit_message(const char *old) |
| 488 | { |
| 489 | static int counter; |
| 490 | return xstrfmt("subject %d\n\nbody\n", counter++); |
| 491 | } |
| 492 | |
| 493 | static struct hashmap idents; |
| 494 | static void *anonymize_ident(const void *old, size_t *len) |
| 495 | { |
| 496 | static int counter; |
| 497 | struct strbuf out = STRBUF_INIT; |
| 498 | strbuf_addf(&out, "User %d <user%d@example.com>", counter, counter); |
| 499 | counter++; |
| 500 | return strbuf_detach(&out, len); |
| 501 | } |
| 502 | |
| 503 | /* |
| 504 | * Our strategy here is to anonymize the names and email addresses, |
| 505 | * but keep timestamps intact, as they influence things like traversal |
| 506 | * order (and by themselves should not be too revealing). |
| 507 | */ |
| 508 | static void anonymize_ident_line(const char **beg, const char **end) |
| 509 | { |
| 510 | static struct strbuf buffers[] = { STRBUF_INIT, STRBUF_INIT }; |
| 511 | static unsigned which_buffer; |
| 512 | |
| 513 | struct strbuf *out; |
| 514 | struct ident_split split; |
| 515 | const char *end_of_header; |
| 516 | |
| 517 | out = &buffers[which_buffer++]; |
| 518 | which_buffer %= ARRAY_SIZE(buffers); |
| 519 | strbuf_reset(out); |
| 520 | |
| 521 | /* skip "committer", "author", "tagger", etc */ |
| 522 | end_of_header = strchr(*beg, ' '); |
| 523 | if (!end_of_header) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 524 | BUG("malformed line fed to anonymize_ident_line: %.*s", |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 525 | (int)(*end - *beg), *beg); |
| 526 | end_of_header++; |
| 527 | strbuf_add(out, *beg, end_of_header - *beg); |
| 528 | |
| 529 | if (!split_ident_line(&split, end_of_header, *end - end_of_header) && |
| 530 | split.date_begin) { |
| 531 | const char *ident; |
| 532 | size_t len; |
| 533 | |
| 534 | len = split.mail_end - split.name_begin; |
| 535 | ident = anonymize_mem(&idents, anonymize_ident, |
| 536 | split.name_begin, &len); |
| 537 | strbuf_add(out, ident, len); |
| 538 | strbuf_addch(out, ' '); |
| 539 | strbuf_add(out, split.date_begin, split.tz_end - split.date_begin); |
| 540 | } else { |
| 541 | strbuf_addstr(out, "Malformed Ident <malformed@example.com> 0 -0000"); |
| 542 | } |
| 543 | |
| 544 | *beg = out->buf; |
| 545 | *end = out->buf + out->len; |
| 546 | } |
| 547 | |
Jonathan Tan | b3e8ca8 | 2017-09-20 16:55:02 -0700 | [diff] [blame] | 548 | static void handle_commit(struct commit *commit, struct rev_info *rev, |
| 549 | struct string_list *paths_of_changed_objects) |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 550 | { |
| 551 | int saved_output_format = rev->diffopt.output_format; |
Jeff King | bc6b8fc | 2014-06-10 17:41:51 -0400 | [diff] [blame] | 552 | const char *commit_buffer; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 553 | const char *author, *author_end, *committer, *committer_end; |
| 554 | const char *encoding, *message; |
| 555 | char *reencoded = NULL; |
| 556 | struct commit_list *p; |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 557 | const char *refname; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 558 | int i; |
| 559 | |
| 560 | rev->diffopt.output_format = DIFF_FORMAT_CALLBACK; |
| 561 | |
Jeff King | 683ff88 | 2013-10-24 04:53:46 -0400 | [diff] [blame] | 562 | parse_commit_or_die(commit); |
Jeff King | 8597ea3 | 2014-06-10 17:44:13 -0400 | [diff] [blame] | 563 | commit_buffer = get_commit_buffer(commit, NULL); |
Jeff King | bc6b8fc | 2014-06-10 17:41:51 -0400 | [diff] [blame] | 564 | author = strstr(commit_buffer, "\nauthor "); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 565 | if (!author) |
| 566 | die ("Could not find author in commit %s", |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 567 | oid_to_hex(&commit->object.oid)); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 568 | author++; |
| 569 | author_end = strchrnul(author, '\n'); |
| 570 | committer = strstr(author_end, "\ncommitter "); |
| 571 | if (!committer) |
| 572 | die ("Could not find committer in commit %s", |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 573 | oid_to_hex(&commit->object.oid)); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 574 | committer++; |
| 575 | committer_end = strchrnul(committer, '\n'); |
| 576 | message = strstr(committer_end, "\n\n"); |
| 577 | encoding = find_encoding(committer_end, message); |
| 578 | if (message) |
| 579 | message += 2; |
| 580 | |
Elijah Newren | ebeec7d | 2009-03-25 17:53:23 -0600 | [diff] [blame] | 581 | if (commit->parents && |
Elijah Newren | 4087a02 | 2010-07-17 11:00:50 -0600 | [diff] [blame] | 582 | get_object_mark(&commit->parents->item->object) != 0 && |
| 583 | !full_tree) { |
Jeff King | 683ff88 | 2013-10-24 04:53:46 -0400 | [diff] [blame] | 584 | parse_commit_or_die(commit->parents->item); |
Derrick Stolee | 2e27bd7 | 2018-04-06 19:09:38 +0000 | [diff] [blame] | 585 | diff_tree_oid(get_commit_tree_oid(commit->parents->item), |
| 586 | get_commit_tree_oid(commit), "", &rev->diffopt); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 587 | } |
| 588 | else |
Derrick Stolee | 2e27bd7 | 2018-04-06 19:09:38 +0000 | [diff] [blame] | 589 | diff_root_tree_oid(get_commit_tree_oid(commit), |
Brandon Williams | 7b8dea0 | 2017-05-30 10:30:57 -0700 | [diff] [blame] | 590 | "", &rev->diffopt); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 591 | |
Alexander Gavrilov | 03db452 | 2008-07-19 16:21:24 +0400 | [diff] [blame] | 592 | /* Export the referenced blobs, and remember the marks. */ |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 593 | for (i = 0; i < diff_queued_diff.nr; i++) |
Alexander Gavrilov | 03db452 | 2008-07-19 16:21:24 +0400 | [diff] [blame] | 594 | if (!S_ISGITLINK(diff_queued_diff.queue[i]->two->mode)) |
brian m. carlson | 273f8ee | 2017-02-21 23:47:23 +0000 | [diff] [blame] | 595 | export_blob(&diff_queued_diff.queue[i]->two->oid); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 596 | |
Nguyễn Thái Ngọc Duy | 87be252 | 2018-05-19 07:28:24 +0200 | [diff] [blame] | 597 | refname = *revision_sources_at(&revision_sources, commit); |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 598 | if (anonymize) { |
| 599 | refname = anonymize_refname(refname); |
| 600 | anonymize_ident_line(&committer, &committer_end); |
| 601 | anonymize_ident_line(&author, &author_end); |
| 602 | } |
| 603 | |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 604 | mark_next_object(&commit->object); |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 605 | if (anonymize) |
| 606 | reencoded = anonymize_commit_message(message); |
| 607 | else if (!is_encoding_utf8(encoding)) |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 608 | reencoded = reencode_string(message, "UTF-8", encoding); |
Shawn O. Pearce | d8933f0 | 2008-06-13 00:38:55 -0400 | [diff] [blame] | 609 | if (!commit->parents) |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 610 | printf("reset %s\n", refname); |
Ramsay Jones | 6e1c234 | 2008-07-03 16:52:09 +0100 | [diff] [blame] | 611 | printf("commit %s\nmark :%"PRIu32"\n%.*s\n%.*s\ndata %u\n%s", |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 612 | refname, last_idnum, |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 613 | (int)(author_end - author), author, |
| 614 | (int)(committer_end - committer), committer, |
| 615 | (unsigned)(reencoded |
| 616 | ? strlen(reencoded) : message |
| 617 | ? strlen(message) : 0), |
| 618 | reencoded ? reencoded : message ? message : ""); |
Jim Meyering | 8e0f700 | 2008-01-31 18:26:32 +0100 | [diff] [blame] | 619 | free(reencoded); |
Jeff King | bc6b8fc | 2014-06-10 17:41:51 -0400 | [diff] [blame] | 620 | unuse_commit_buffer(commit, commit_buffer); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 621 | |
| 622 | for (i = 0, p = commit->parents; p; p = p->next) { |
| 623 | int mark = get_object_mark(&p->item->object); |
| 624 | if (!mark) |
| 625 | continue; |
| 626 | if (i == 0) |
| 627 | printf("from :%d\n", mark); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 628 | else |
Pieter de Bie | 5070b49 | 2008-05-25 01:21:53 +0200 | [diff] [blame] | 629 | printf("merge :%d\n", mark); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 630 | i++; |
| 631 | } |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 632 | |
Elijah Newren | 4087a02 | 2010-07-17 11:00:50 -0600 | [diff] [blame] | 633 | if (full_tree) |
| 634 | printf("deleteall\n"); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 635 | log_tree_diff_flush(rev); |
Jonathan Tan | b3e8ca8 | 2017-09-20 16:55:02 -0700 | [diff] [blame] | 636 | string_list_clear(paths_of_changed_objects, 0); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 637 | rev->diffopt.output_format = saved_output_format; |
| 638 | |
| 639 | printf("\n"); |
| 640 | |
| 641 | show_progress(); |
| 642 | } |
| 643 | |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 644 | static void *anonymize_tag(const void *old, size_t *len) |
| 645 | { |
| 646 | static int counter; |
| 647 | struct strbuf out = STRBUF_INIT; |
| 648 | strbuf_addf(&out, "tag message %d", counter++); |
| 649 | return strbuf_detach(&out, len); |
| 650 | } |
| 651 | |
Jonathan Tan | b3e8ca8 | 2017-09-20 16:55:02 -0700 | [diff] [blame] | 652 | static void handle_tail(struct object_array *commits, struct rev_info *revs, |
| 653 | struct string_list *paths_of_changed_objects) |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 654 | { |
| 655 | struct commit *commit; |
| 656 | while (commits->nr) { |
Martin Ågren | 7199203 | 2017-09-23 01:34:53 +0200 | [diff] [blame] | 657 | commit = (struct commit *)object_array_pop(commits); |
Martin Ågren | be011bb | 2018-04-21 00:12:31 +0200 | [diff] [blame] | 658 | if (has_unshown_parent(commit)) { |
| 659 | /* Queue again, to be handled later */ |
| 660 | add_object_array(&commit->object, NULL, commits); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 661 | return; |
Martin Ågren | be011bb | 2018-04-21 00:12:31 +0200 | [diff] [blame] | 662 | } |
Jonathan Tan | b3e8ca8 | 2017-09-20 16:55:02 -0700 | [diff] [blame] | 663 | handle_commit(commit, revs, paths_of_changed_objects); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
| 667 | static void handle_tag(const char *name, struct tag *tag) |
| 668 | { |
| 669 | unsigned long size; |
| 670 | enum object_type type; |
| 671 | char *buf; |
| 672 | const char *tagger, *tagger_end, *message; |
| 673 | size_t message_size = 0; |
Elijah Newren | 02c48cd | 2009-06-25 22:48:28 -0600 | [diff] [blame] | 674 | struct object *tagged; |
Elijah Newren | 2d8ad46 | 2009-06-25 22:48:31 -0600 | [diff] [blame] | 675 | int tagged_mark; |
| 676 | struct commit *p; |
Elijah Newren | 02c48cd | 2009-06-25 22:48:28 -0600 | [diff] [blame] | 677 | |
Ondřej Bílka | 98e023d | 2013-07-29 10:18:21 +0200 | [diff] [blame] | 678 | /* Trees have no identifier in fast-export output, thus we have no way |
Elijah Newren | 02c48cd | 2009-06-25 22:48:28 -0600 | [diff] [blame] | 679 | * to output tags of trees, tags of tags of trees, etc. Simply omit |
| 680 | * such tags. |
| 681 | */ |
| 682 | tagged = tag->tagged; |
| 683 | while (tagged->type == OBJ_TAG) { |
| 684 | tagged = ((struct tag *)tagged)->tagged; |
| 685 | } |
| 686 | if (tagged->type == OBJ_TREE) { |
| 687 | warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.", |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 688 | oid_to_hex(&tag->object.oid)); |
Elijah Newren | 02c48cd | 2009-06-25 22:48:28 -0600 | [diff] [blame] | 689 | return; |
| 690 | } |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 691 | |
brian m. carlson | b4f5aca | 2018-03-12 02:27:53 +0000 | [diff] [blame] | 692 | buf = read_object_file(&tag->object.oid, &type, &size); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 693 | if (!buf) |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 694 | die ("Could not read tag %s", oid_to_hex(&tag->object.oid)); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 695 | message = memmem(buf, size, "\n\n", 2); |
| 696 | if (message) { |
| 697 | message += 2; |
| 698 | message_size = strlen(message); |
| 699 | } |
| 700 | tagger = memmem(buf, message ? message - buf : size, "\ntagger ", 8); |
Johannes Schindelin | 4e46a8d | 2008-12-20 01:00:27 +0100 | [diff] [blame] | 701 | if (!tagger) { |
| 702 | if (fake_missing_tagger) |
| 703 | tagger = "tagger Unspecified Tagger " |
| 704 | "<unspecified-tagger> 0 +0000"; |
| 705 | else |
| 706 | tagger = ""; |
| 707 | tagger_end = tagger + strlen(tagger); |
| 708 | } else { |
| 709 | tagger++; |
| 710 | tagger_end = strchrnul(tagger, '\n'); |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 711 | if (anonymize) |
| 712 | anonymize_ident_line(&tagger, &tagger_end); |
| 713 | } |
| 714 | |
| 715 | if (anonymize) { |
| 716 | name = anonymize_refname(name); |
| 717 | if (message) { |
| 718 | static struct hashmap tags; |
| 719 | message = anonymize_mem(&tags, anonymize_tag, |
| 720 | message, &message_size); |
| 721 | } |
Johannes Schindelin | 4e46a8d | 2008-12-20 01:00:27 +0100 | [diff] [blame] | 722 | } |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 723 | |
| 724 | /* handle signed tags */ |
| 725 | if (message) { |
| 726 | const char *signature = strstr(message, |
| 727 | "\n-----BEGIN PGP SIGNATURE-----\n"); |
| 728 | if (signature) |
| 729 | switch(signed_tag_mode) { |
| 730 | case ABORT: |
| 731 | die ("Encountered signed tag %s; use " |
Paul Price | 04a74b6 | 2013-04-12 10:05:55 -0400 | [diff] [blame] | 732 | "--signed-tags=<mode> to handle it.", |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 733 | oid_to_hex(&tag->object.oid)); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 734 | case WARN: |
| 735 | warning ("Exporting signed tag %s", |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 736 | oid_to_hex(&tag->object.oid)); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 737 | /* fallthru */ |
Johannes Schindelin | ee4bc37 | 2007-12-03 22:44:39 +0000 | [diff] [blame] | 738 | case VERBATIM: |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 739 | break; |
John Keeping | cd16c59 | 2013-04-14 11:57:06 +0100 | [diff] [blame] | 740 | case WARN_STRIP: |
| 741 | warning ("Stripping signature from tag %s", |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 742 | oid_to_hex(&tag->object.oid)); |
John Keeping | cd16c59 | 2013-04-14 11:57:06 +0100 | [diff] [blame] | 743 | /* fallthru */ |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 744 | case STRIP: |
| 745 | message_size = signature + 1 - message; |
| 746 | break; |
| 747 | } |
| 748 | } |
| 749 | |
Elijah Newren | 2d8ad46 | 2009-06-25 22:48:31 -0600 | [diff] [blame] | 750 | /* handle tag->tagged having been filtered out due to paths specified */ |
| 751 | tagged = tag->tagged; |
| 752 | tagged_mark = get_object_mark(tagged); |
| 753 | if (!tagged_mark) { |
| 754 | switch(tag_of_filtered_mode) { |
| 755 | case ABORT: |
| 756 | die ("Tag %s tags unexported object; use " |
| 757 | "--tag-of-filtered-object=<mode> to handle it.", |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 758 | oid_to_hex(&tag->object.oid)); |
Elijah Newren | 2d8ad46 | 2009-06-25 22:48:31 -0600 | [diff] [blame] | 759 | case DROP: |
| 760 | /* Ignore this tag altogether */ |
Johannes Schindelin | 1efb1e9 | 2017-05-04 15:57:33 +0200 | [diff] [blame] | 761 | free(buf); |
Elijah Newren | 2d8ad46 | 2009-06-25 22:48:31 -0600 | [diff] [blame] | 762 | return; |
| 763 | case REWRITE: |
| 764 | if (tagged->type != OBJ_COMMIT) { |
| 765 | die ("Tag %s tags unexported %s!", |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 766 | oid_to_hex(&tag->object.oid), |
Brandon Williams | debca9d | 2018-02-14 10:59:24 -0800 | [diff] [blame] | 767 | type_name(tagged->type)); |
Elijah Newren | 2d8ad46 | 2009-06-25 22:48:31 -0600 | [diff] [blame] | 768 | } |
| 769 | p = (struct commit *)tagged; |
| 770 | for (;;) { |
| 771 | if (p->parents && p->parents->next) |
| 772 | break; |
| 773 | if (p->object.flags & UNINTERESTING) |
| 774 | break; |
| 775 | if (!(p->object.flags & TREESAME)) |
| 776 | break; |
| 777 | if (!p->parents) |
| 778 | die ("Can't find replacement commit for tag %s\n", |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 779 | oid_to_hex(&tag->object.oid)); |
Elijah Newren | 2d8ad46 | 2009-06-25 22:48:31 -0600 | [diff] [blame] | 780 | p = p->parents->item; |
| 781 | } |
| 782 | tagged_mark = get_object_mark(&p->object); |
| 783 | } |
| 784 | } |
| 785 | |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 786 | if (starts_with(name, "refs/tags/")) |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 787 | name += 10; |
Johannes Schindelin | 4e46a8d | 2008-12-20 01:00:27 +0100 | [diff] [blame] | 788 | printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n", |
Elijah Newren | 2d8ad46 | 2009-06-25 22:48:31 -0600 | [diff] [blame] | 789 | name, tagged_mark, |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 790 | (int)(tagger_end - tagger), tagger, |
Johannes Schindelin | 4e46a8d | 2008-12-20 01:00:27 +0100 | [diff] [blame] | 791 | tagger == tagger_end ? "" : "\n", |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 792 | (int)message_size, (int)message_size, message ? message : ""); |
Johannes Schindelin | 1efb1e9 | 2017-05-04 15:57:33 +0200 | [diff] [blame] | 793 | free(buf); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 794 | } |
| 795 | |
Felipe Contreras | 3e9b9cb | 2013-09-01 02:05:48 -0500 | [diff] [blame] | 796 | static struct commit *get_commit(struct rev_cmdline_entry *e, char *full_name) |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 797 | { |
Felipe Contreras | 3e9b9cb | 2013-09-01 02:05:48 -0500 | [diff] [blame] | 798 | switch (e->item->type) { |
| 799 | case OBJ_COMMIT: |
| 800 | return (struct commit *)e->item; |
| 801 | case OBJ_TAG: { |
| 802 | struct tag *tag = (struct tag *)e->item; |
| 803 | |
| 804 | /* handle nested tags */ |
| 805 | while (tag && tag->object.type == OBJ_TAG) { |
Stefan Beller | 109cd76 | 2018-06-28 18:21:51 -0700 | [diff] [blame] | 806 | parse_object(the_repository, &tag->object.oid); |
Felipe Contreras | 3e9b9cb | 2013-09-01 02:05:48 -0500 | [diff] [blame] | 807 | string_list_append(&extra_refs, full_name)->util = tag; |
| 808 | tag = (struct tag *)tag->tagged; |
| 809 | } |
| 810 | if (!tag) |
| 811 | die("Tag %s points nowhere?", e->name); |
| 812 | return (struct commit *)tag; |
| 813 | break; |
| 814 | } |
| 815 | default: |
| 816 | return NULL; |
| 817 | } |
| 818 | } |
| 819 | |
Felipe Contreras | 1d844ee | 2013-09-01 02:05:47 -0500 | [diff] [blame] | 820 | static void get_tags_and_duplicates(struct rev_cmdline_info *info) |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 821 | { |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 822 | int i; |
| 823 | |
Felipe Contreras | 49266e8 | 2012-11-28 23:23:59 +0100 | [diff] [blame] | 824 | for (i = 0; i < info->nr; i++) { |
| 825 | struct rev_cmdline_entry *e = info->rev + i; |
brian m. carlson | 273f8ee | 2017-02-21 23:47:23 +0000 | [diff] [blame] | 826 | struct object_id oid; |
Felipe Contreras | 2d242de | 2012-11-28 23:11:08 +0100 | [diff] [blame] | 827 | struct commit *commit; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 828 | char *full_name; |
| 829 | |
Felipe Contreras | 49266e8 | 2012-11-28 23:23:59 +0100 | [diff] [blame] | 830 | if (e->flags & UNINTERESTING) |
| 831 | continue; |
| 832 | |
brian m. carlson | cca5fa6 | 2017-10-15 22:06:57 +0000 | [diff] [blame] | 833 | if (dwim_ref(e->name, strlen(e->name), &oid, &full_name) != 1) |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 834 | continue; |
| 835 | |
Brandon Williams | 16eefc8 | 2018-05-16 15:57:59 -0700 | [diff] [blame] | 836 | if (refspecs.nr) { |
Felipe Contreras | 03e9010 | 2014-04-20 13:59:24 -0500 | [diff] [blame] | 837 | char *private; |
Brandon Williams | d000414 | 2018-05-16 15:58:11 -0700 | [diff] [blame] | 838 | private = apply_refspecs(&refspecs, full_name); |
Felipe Contreras | 03e9010 | 2014-04-20 13:59:24 -0500 | [diff] [blame] | 839 | if (private) { |
| 840 | free(full_name); |
| 841 | full_name = private; |
| 842 | } |
| 843 | } |
| 844 | |
Felipe Contreras | 3e9b9cb | 2013-09-01 02:05:48 -0500 | [diff] [blame] | 845 | commit = get_commit(e, full_name); |
| 846 | if (!commit) { |
Erik Faye-Lund | 2d07f6d | 2009-03-23 12:53:07 +0000 | [diff] [blame] | 847 | warning("%s: Unexpected object of type %s, skipping.", |
| 848 | e->name, |
Brandon Williams | debca9d | 2018-02-14 10:59:24 -0800 | [diff] [blame] | 849 | type_name(e->item->type)); |
Erik Faye-Lund | 2d07f6d | 2009-03-23 12:53:07 +0000 | [diff] [blame] | 850 | continue; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 851 | } |
Felipe Contreras | f28e7c9 | 2012-11-28 23:24:00 +0100 | [diff] [blame] | 852 | |
Felipe Contreras | 3e9b9cb | 2013-09-01 02:05:48 -0500 | [diff] [blame] | 853 | switch(commit->object.type) { |
| 854 | case OBJ_COMMIT: |
| 855 | break; |
| 856 | case OBJ_BLOB: |
brian m. carlson | 273f8ee | 2017-02-21 23:47:23 +0000 | [diff] [blame] | 857 | export_blob(&commit->object.oid); |
Felipe Contreras | 3e9b9cb | 2013-09-01 02:05:48 -0500 | [diff] [blame] | 858 | continue; |
| 859 | default: /* OBJ_TAG (nested tags) is already handled */ |
| 860 | warning("Tag points to object of unexpected type %s, skipping.", |
Brandon Williams | debca9d | 2018-02-14 10:59:24 -0800 | [diff] [blame] | 861 | type_name(commit->object.type)); |
Felipe Contreras | 3e9b9cb | 2013-09-01 02:05:48 -0500 | [diff] [blame] | 862 | continue; |
| 863 | } |
| 864 | |
Felipe Contreras | f28e7c9 | 2012-11-28 23:24:00 +0100 | [diff] [blame] | 865 | /* |
| 866 | * This ref will not be updated through a commit, lets make |
| 867 | * sure it gets properly updated eventually. |
| 868 | */ |
Nguyễn Thái Ngọc Duy | 87be252 | 2018-05-19 07:28:24 +0200 | [diff] [blame] | 869 | if (*revision_sources_at(&revision_sources, commit) || |
| 870 | commit->object.flags & SHOWN) |
Felipe Contreras | 1d844ee | 2013-09-01 02:05:47 -0500 | [diff] [blame] | 871 | string_list_append(&extra_refs, full_name)->util = commit; |
Nguyễn Thái Ngọc Duy | 87be252 | 2018-05-19 07:28:24 +0200 | [diff] [blame] | 872 | if (!*revision_sources_at(&revision_sources, commit)) |
| 873 | *revision_sources_at(&revision_sources, commit) = full_name; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 874 | } |
| 875 | } |
| 876 | |
Felipe Contreras | 1d844ee | 2013-09-01 02:05:47 -0500 | [diff] [blame] | 877 | static void handle_tags_and_duplicates(void) |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 878 | { |
| 879 | struct commit *commit; |
| 880 | int i; |
| 881 | |
Felipe Contreras | 1d844ee | 2013-09-01 02:05:47 -0500 | [diff] [blame] | 882 | for (i = extra_refs.nr - 1; i >= 0; i--) { |
| 883 | const char *name = extra_refs.items[i].string; |
| 884 | struct object *object = extra_refs.items[i].util; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 885 | switch (object->type) { |
| 886 | case OBJ_TAG: |
| 887 | handle_tag(name, (struct tag *)object); |
| 888 | break; |
| 889 | case OBJ_COMMIT: |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 890 | if (anonymize) |
| 891 | name = anonymize_refname(name); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 892 | /* create refs pointing to already seen commits */ |
| 893 | commit = (struct commit *)object; |
| 894 | printf("reset %s\nfrom :%d\n\n", name, |
| 895 | get_object_mark(&commit->object)); |
| 896 | show_progress(); |
| 897 | break; |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 902 | static void export_marks(char *file) |
| 903 | { |
| 904 | unsigned int i; |
| 905 | uint32_t mark; |
Jonathan Tan | ddd3e31 | 2017-12-07 16:14:24 -0800 | [diff] [blame] | 906 | struct decoration_entry *deco = idnums.entries; |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 907 | FILE *f; |
Matthias Andree | 96d69b5 | 2009-07-24 10:17:13 +0200 | [diff] [blame] | 908 | int e = 0; |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 909 | |
Johannes Schindelin | ea56518 | 2016-01-11 19:35:54 +0100 | [diff] [blame] | 910 | f = fopen_for_writing(file); |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 911 | if (!f) |
Sverre Rabbelier | bb6ad28 | 2010-03-28 00:42:48 -0500 | [diff] [blame] | 912 | die_errno("Unable to open marks file %s for writing.", file); |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 913 | |
Junio C Hamano | 6991357 | 2008-07-03 00:25:23 -0700 | [diff] [blame] | 914 | for (i = 0; i < idnums.size; i++) { |
| 915 | if (deco->base && deco->base->type == 1) { |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 916 | mark = ptr_to_mark(deco->decoration); |
Matthias Andree | 96d69b5 | 2009-07-24 10:17:13 +0200 | [diff] [blame] | 917 | if (fprintf(f, ":%"PRIu32" %s\n", mark, |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 918 | oid_to_hex(&deco->base->oid)) < 0) { |
Matthias Andree | 96d69b5 | 2009-07-24 10:17:13 +0200 | [diff] [blame] | 919 | e = 1; |
| 920 | break; |
| 921 | } |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 922 | } |
Junio C Hamano | 6991357 | 2008-07-03 00:25:23 -0700 | [diff] [blame] | 923 | deco++; |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 924 | } |
| 925 | |
Matthias Andree | 96d69b5 | 2009-07-24 10:17:13 +0200 | [diff] [blame] | 926 | e |= ferror(f); |
| 927 | e |= fclose(f); |
| 928 | if (e) |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 929 | error("Unable to write marks file %s.", file); |
| 930 | } |
| 931 | |
Junio C Hamano | 6991357 | 2008-07-03 00:25:23 -0700 | [diff] [blame] | 932 | static void import_marks(char *input_file) |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 933 | { |
| 934 | char line[512]; |
Nguyễn Thái Ngọc Duy | 23a9e07 | 2017-05-03 17:16:46 +0700 | [diff] [blame] | 935 | FILE *f = xfopen(input_file, "r"); |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 936 | |
| 937 | while (fgets(line, sizeof(line), f)) { |
| 938 | uint32_t mark; |
| 939 | char *line_end, *mark_end; |
brian m. carlson | 273f8ee | 2017-02-21 23:47:23 +0000 | [diff] [blame] | 940 | struct object_id oid; |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 941 | struct object *object; |
Felipe Contreras | 47bd9bf | 2013-05-05 17:38:54 -0500 | [diff] [blame] | 942 | struct commit *commit; |
Felipe Contreras | e6812cf | 2013-05-05 17:38:53 -0500 | [diff] [blame] | 943 | enum object_type type; |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 944 | |
| 945 | line_end = strchr(line, '\n'); |
| 946 | if (line[0] != ':' || !line_end) |
| 947 | die("corrupt mark line: %s", line); |
Junio C Hamano | 6991357 | 2008-07-03 00:25:23 -0700 | [diff] [blame] | 948 | *line_end = '\0'; |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 949 | |
| 950 | mark = strtoumax(line + 1, &mark_end, 10); |
| 951 | if (!mark || mark_end == line + 1 |
brian m. carlson | 273f8ee | 2017-02-21 23:47:23 +0000 | [diff] [blame] | 952 | || *mark_end != ' ' || get_oid_hex(mark_end + 1, &oid)) |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 953 | die("corrupt mark line: %s", line); |
| 954 | |
Antoine Pelisse | c4458ec | 2013-04-06 19:04:31 +0200 | [diff] [blame] | 955 | if (last_idnum < mark) |
| 956 | last_idnum = mark; |
| 957 | |
Stefan Beller | 0df8e96 | 2018-04-25 11:20:59 -0700 | [diff] [blame] | 958 | type = oid_object_info(the_repository, &oid, NULL); |
Felipe Contreras | e6812cf | 2013-05-05 17:38:53 -0500 | [diff] [blame] | 959 | if (type < 0) |
brian m. carlson | 273f8ee | 2017-02-21 23:47:23 +0000 | [diff] [blame] | 960 | die("object not found: %s", oid_to_hex(&oid)); |
Felipe Contreras | e6812cf | 2013-05-05 17:38:53 -0500 | [diff] [blame] | 961 | |
| 962 | if (type != OBJ_COMMIT) |
| 963 | /* only commits */ |
Antoine Pelisse | c4458ec | 2013-04-06 19:04:31 +0200 | [diff] [blame] | 964 | continue; |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 965 | |
Stefan Beller | c1f5eb4 | 2018-06-28 18:21:59 -0700 | [diff] [blame] | 966 | commit = lookup_commit(the_repository, &oid); |
Felipe Contreras | 47bd9bf | 2013-05-05 17:38:54 -0500 | [diff] [blame] | 967 | if (!commit) |
brian m. carlson | 273f8ee | 2017-02-21 23:47:23 +0000 | [diff] [blame] | 968 | die("not a commit? can't happen: %s", oid_to_hex(&oid)); |
Felipe Contreras | 47bd9bf | 2013-05-05 17:38:54 -0500 | [diff] [blame] | 969 | |
| 970 | object = &commit->object; |
Felipe Contreras | e6812cf | 2013-05-05 17:38:53 -0500 | [diff] [blame] | 971 | |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 972 | if (object->flags & SHOWN) |
brian m. carlson | 273f8ee | 2017-02-21 23:47:23 +0000 | [diff] [blame] | 973 | error("Object %s already has a mark", oid_to_hex(&oid)); |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 974 | |
| 975 | mark_object(object, mark); |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 976 | |
| 977 | object->flags |= SHOWN; |
| 978 | } |
| 979 | fclose(f); |
| 980 | } |
| 981 | |
Felipe Contreras | 60ed264 | 2014-04-20 13:59:28 -0500 | [diff] [blame] | 982 | static void handle_deletes(void) |
| 983 | { |
| 984 | int i; |
Brandon Williams | 16eefc8 | 2018-05-16 15:57:59 -0700 | [diff] [blame] | 985 | for (i = 0; i < refspecs.nr; i++) { |
| 986 | struct refspec_item *refspec = &refspecs.items[i]; |
Felipe Contreras | 60ed264 | 2014-04-20 13:59:28 -0500 | [diff] [blame] | 987 | if (*refspec->src) |
| 988 | continue; |
| 989 | |
| 990 | printf("reset %s\nfrom %s\n\n", |
| 991 | refspec->dst, sha1_to_hex(null_sha1)); |
| 992 | } |
| 993 | } |
| 994 | |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 995 | int cmd_fast_export(int argc, const char **argv, const char *prefix) |
| 996 | { |
| 997 | struct rev_info revs; |
Thiago Farina | 3cd4745 | 2010-08-28 23:04:17 -0300 | [diff] [blame] | 998 | struct object_array commits = OBJECT_ARRAY_INIT; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 999 | struct commit *commit; |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 1000 | char *export_filename = NULL, *import_filename = NULL; |
Antoine Pelisse | c4458ec | 2013-04-06 19:04:31 +0200 | [diff] [blame] | 1001 | uint32_t lastimportid; |
Felipe Contreras | 03e9010 | 2014-04-20 13:59:24 -0500 | [diff] [blame] | 1002 | struct string_list refspecs_list = STRING_LIST_INIT_NODUP; |
Jonathan Tan | b3e8ca8 | 2017-09-20 16:55:02 -0700 | [diff] [blame] | 1003 | struct string_list paths_of_changed_objects = STRING_LIST_INIT_DUP; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 1004 | struct option options[] = { |
| 1005 | OPT_INTEGER(0, "progress", &progress, |
Nguyễn Thái Ngọc Duy | 3b787b9 | 2012-08-20 19:32:08 +0700 | [diff] [blame] | 1006 | N_("show progress after <n> objects")), |
| 1007 | OPT_CALLBACK(0, "signed-tags", &signed_tag_mode, N_("mode"), |
| 1008 | N_("select handling of signed tags"), |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 1009 | parse_opt_signed_tag_mode), |
Nguyễn Thái Ngọc Duy | 3b787b9 | 2012-08-20 19:32:08 +0700 | [diff] [blame] | 1010 | OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode, N_("mode"), |
| 1011 | N_("select handling of tags that tag filtered objects"), |
Elijah Newren | 2d8ad46 | 2009-06-25 22:48:31 -0600 | [diff] [blame] | 1012 | parse_opt_tag_of_filtered_mode), |
Nguyễn Thái Ngọc Duy | 3b787b9 | 2012-08-20 19:32:08 +0700 | [diff] [blame] | 1013 | OPT_STRING(0, "export-marks", &export_filename, N_("file"), |
| 1014 | N_("Dump marks to this file")), |
| 1015 | OPT_STRING(0, "import-marks", &import_filename, N_("file"), |
| 1016 | N_("Import marks from this file")), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 1017 | OPT_BOOL(0, "fake-missing-tagger", &fake_missing_tagger, |
| 1018 | N_("Fake a tagger when tags lack one")), |
| 1019 | OPT_BOOL(0, "full-tree", &full_tree, |
| 1020 | N_("Output full tree for each commit")), |
| 1021 | OPT_BOOL(0, "use-done-feature", &use_done_feature, |
Nguyễn Thái Ngọc Duy | 3b787b9 | 2012-08-20 19:32:08 +0700 | [diff] [blame] | 1022 | N_("Use the done feature to terminate the stream")), |
| 1023 | OPT_BOOL(0, "no-data", &no_data, N_("Skip output of blob data")), |
Felipe Contreras | 03e9010 | 2014-04-20 13:59:24 -0500 | [diff] [blame] | 1024 | OPT_STRING_LIST(0, "refspec", &refspecs_list, N_("refspec"), |
| 1025 | N_("Apply refspec to exported refs")), |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 1026 | OPT_BOOL(0, "anonymize", &anonymize, N_("anonymize output")), |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 1027 | OPT_END() |
| 1028 | }; |
| 1029 | |
Miklos Vajna | dcfdbdf | 2009-01-03 04:59:12 +0100 | [diff] [blame] | 1030 | if (argc == 1) |
| 1031 | usage_with_options (fast_export_usage, options); |
| 1032 | |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 1033 | /* we handle encodings */ |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 1034 | git_config(git_default_config, NULL); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 1035 | |
| 1036 | init_revisions(&revs, prefix); |
Nguyễn Thái Ngọc Duy | 87be252 | 2018-05-19 07:28:24 +0200 | [diff] [blame] | 1037 | init_revision_sources(&revision_sources); |
Elijah Newren | 668f3aa | 2009-06-25 22:48:27 -0600 | [diff] [blame] | 1038 | revs.topo_order = 1; |
Nguyễn Thái Ngọc Duy | 87be252 | 2018-05-19 07:28:24 +0200 | [diff] [blame] | 1039 | revs.sources = &revision_sources; |
Elijah Newren | 3216413 | 2009-06-25 22:48:30 -0600 | [diff] [blame] | 1040 | revs.rewrite_parents = 1; |
Felipe Contreras | 8b2f86a | 2014-04-20 13:59:23 -0500 | [diff] [blame] | 1041 | argc = parse_options(argc, argv, prefix, options, fast_export_usage, |
| 1042 | PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 1043 | argc = setup_revisions(argc, argv, &revs, NULL); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 1044 | if (argc > 1) |
| 1045 | usage_with_options (fast_export_usage, options); |
| 1046 | |
Felipe Contreras | 03e9010 | 2014-04-20 13:59:24 -0500 | [diff] [blame] | 1047 | if (refspecs_list.nr) { |
Felipe Contreras | 03e9010 | 2014-04-20 13:59:24 -0500 | [diff] [blame] | 1048 | int i; |
| 1049 | |
Felipe Contreras | 03e9010 | 2014-04-20 13:59:24 -0500 | [diff] [blame] | 1050 | for (i = 0; i < refspecs_list.nr; i++) |
Brandon Williams | 16eefc8 | 2018-05-16 15:57:59 -0700 | [diff] [blame] | 1051 | refspec_append(&refspecs, refspecs_list.items[i].string); |
Felipe Contreras | 03e9010 | 2014-04-20 13:59:24 -0500 | [diff] [blame] | 1052 | |
| 1053 | string_list_clear(&refspecs_list, 1); |
Felipe Contreras | 03e9010 | 2014-04-20 13:59:24 -0500 | [diff] [blame] | 1054 | } |
| 1055 | |
Sverre Rabbelier | 82670a5 | 2011-07-16 15:03:33 +0200 | [diff] [blame] | 1056 | if (use_done_feature) |
| 1057 | printf("feature done\n"); |
| 1058 | |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 1059 | if (import_filename) |
| 1060 | import_marks(import_filename); |
Antoine Pelisse | c4458ec | 2013-04-06 19:04:31 +0200 | [diff] [blame] | 1061 | lastimportid = last_idnum; |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 1062 | |
Nguyễn Thái Ngọc Duy | afe069d | 2010-12-17 19:43:06 +0700 | [diff] [blame] | 1063 | if (import_filename && revs.prune_data.nr) |
Elijah Newren | 4087a02 | 2010-07-17 11:00:50 -0600 | [diff] [blame] | 1064 | full_tree = 1; |
| 1065 | |
Felipe Contreras | 1d844ee | 2013-09-01 02:05:47 -0500 | [diff] [blame] | 1066 | get_tags_and_duplicates(&revs.cmdline); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 1067 | |
Martin Koegler | 3d51e1b | 2008-02-18 08:31:56 +0100 | [diff] [blame] | 1068 | if (prepare_revision_walk(&revs)) |
| 1069 | die("revision walk setup failed"); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 1070 | revs.diffopt.format_callback = show_filemodify; |
Jonathan Tan | b3e8ca8 | 2017-09-20 16:55:02 -0700 | [diff] [blame] | 1071 | revs.diffopt.format_callback_data = &paths_of_changed_objects; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 1072 | revs.diffopt.flags.recursive = 1; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 1073 | while ((commit = get_revision(&revs))) { |
| 1074 | if (has_unshown_parent(commit)) { |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 1075 | add_object_array(&commit->object, NULL, &commits); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 1076 | } |
| 1077 | else { |
Jonathan Tan | b3e8ca8 | 2017-09-20 16:55:02 -0700 | [diff] [blame] | 1078 | handle_commit(commit, &revs, &paths_of_changed_objects); |
| 1079 | handle_tail(&commits, &revs, &paths_of_changed_objects); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 1080 | } |
| 1081 | } |
| 1082 | |
Felipe Contreras | 1d844ee | 2013-09-01 02:05:47 -0500 | [diff] [blame] | 1083 | handle_tags_and_duplicates(); |
Felipe Contreras | 60ed264 | 2014-04-20 13:59:28 -0500 | [diff] [blame] | 1084 | handle_deletes(); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 1085 | |
Antoine Pelisse | c4458ec | 2013-04-06 19:04:31 +0200 | [diff] [blame] | 1086 | if (export_filename && lastimportid != last_idnum) |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 1087 | export_marks(export_filename); |
| 1088 | |
Sverre Rabbelier | 82670a5 | 2011-07-16 15:03:33 +0200 | [diff] [blame] | 1089 | if (use_done_feature) |
| 1090 | printf("done\n"); |
| 1091 | |
Brandon Williams | 16eefc8 | 2018-05-16 15:57:59 -0700 | [diff] [blame] | 1092 | refspec_clear(&refspecs); |
Felipe Contreras | 03e9010 | 2014-04-20 13:59:24 -0500 | [diff] [blame] | 1093 | |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 1094 | return 0; |
| 1095 | } |