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" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 7 | #include "config.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 8 | #include "gettext.h" |
Elijah Newren | 41771fa | 2023-02-24 00:09:27 +0000 | [diff] [blame] | 9 | #include "hex.h" |
Michael Haggerty | fb58c8d | 2015-06-22 16:03:05 +0200 | [diff] [blame] | 10 | #include "refs.h" |
Brandon Williams | ec0cb49 | 2018-05-16 15:57:48 -0700 | [diff] [blame] | 11 | #include "refspec.h" |
Elijah Newren | 87bed17 | 2023-04-11 00:41:53 -0700 | [diff] [blame] | 12 | #include "object-file.h" |
Elijah Newren | a034e91 | 2023-05-16 06:34:06 +0000 | [diff] [blame] | 13 | #include "object-store-ll.h" |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 14 | #include "commit.h" |
| 15 | #include "object.h" |
| 16 | #include "tag.h" |
| 17 | #include "diff.h" |
| 18 | #include "diffcore.h" |
| 19 | #include "log-tree.h" |
| 20 | #include "revision.h" |
| 21 | #include "decorate.h" |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 22 | #include "string-list.h" |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 23 | #include "utf8.h" |
| 24 | #include "parse-options.h" |
Jeff King | 6280dfd | 2011-08-05 16:36:22 -0600 | [diff] [blame] | 25 | #include "quote.h" |
Felipe Contreras | 03e9010 | 2014-04-20 13:59:24 -0500 | [diff] [blame] | 26 | #include "remote.h" |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 27 | #include "blob.h" |
Nguyễn Thái Ngọc Duy | 87be252 | 2018-05-19 07:28:24 +0200 | [diff] [blame] | 28 | #include "commit-slab.h" |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 29 | |
| 30 | static const char *fast_export_usage[] = { |
Jean-Noël Avila | 9164d97 | 2022-01-31 22:07:49 +0000 | [diff] [blame] | 31 | N_("git fast-export [<rev-list-opts>]"), |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 32 | NULL |
| 33 | }; |
| 34 | |
| 35 | static int progress; |
Jeff King | 66e3309 | 2023-08-31 17:21:07 -0400 | [diff] [blame] | 36 | static enum signed_tag_mode { SIGNED_TAG_ABORT, VERBATIM, WARN, WARN_STRIP, STRIP } signed_tag_mode = SIGNED_TAG_ABORT; |
| 37 | static enum tag_of_filtered_mode { TAG_FILTERING_ABORT, DROP, REWRITE } tag_of_filtered_mode = TAG_FILTERING_ABORT; |
| 38 | static enum reencode_mode { REENCODE_ABORT, REENCODE_YES, REENCODE_NO } reencode_mode = REENCODE_ABORT; |
Johannes Schindelin | 4e46a8d | 2008-12-20 01:00:27 +0100 | [diff] [blame] | 39 | static int fake_missing_tagger; |
Sverre Rabbelier | 82670a5 | 2011-07-16 15:03:33 +0200 | [diff] [blame] | 40 | static int use_done_feature; |
Geoffrey Irving | 79559f2 | 2009-07-27 22:20:22 -0400 | [diff] [blame] | 41 | static int no_data; |
Elijah Newren | 7f40ab0 | 2010-07-17 11:00:51 -0600 | [diff] [blame] | 42 | static int full_tree; |
Elijah Newren | 530ca19 | 2018-11-15 23:59:54 -0800 | [diff] [blame] | 43 | static int reference_excluded_commits; |
Elijah Newren | a965bb3 | 2018-11-15 23:59:56 -0800 | [diff] [blame] | 44 | static int show_original_ids; |
Elijah Newren | a1638cf | 2019-10-03 13:27:07 -0700 | [diff] [blame] | 45 | static int mark_tags; |
Felipe Contreras | 1d844ee | 2013-09-01 02:05:47 -0500 | [diff] [blame] | 46 | static struct string_list extra_refs = STRING_LIST_INIT_NODUP; |
Elijah Newren | fdf31b6 | 2018-11-15 23:59:53 -0800 | [diff] [blame] | 47 | static struct string_list tag_refs = STRING_LIST_INIT_NODUP; |
Brandon Williams | 16eefc8 | 2018-05-16 15:57:59 -0700 | [diff] [blame] | 48 | static struct refspec refspecs = REFSPEC_INIT_FETCH; |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 49 | static int anonymize; |
Jeff King | 65b5d9f | 2020-06-25 15:48:32 -0400 | [diff] [blame] | 50 | static struct hashmap anonymized_seeds; |
Nguyễn Thái Ngọc Duy | 87be252 | 2018-05-19 07:28:24 +0200 | [diff] [blame] | 51 | static struct revision_sources revision_sources; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 52 | |
| 53 | static int parse_opt_signed_tag_mode(const struct option *opt, |
| 54 | const char *arg, int unset) |
| 55 | { |
Jeff King | 66e3309 | 2023-08-31 17:21:07 -0400 | [diff] [blame] | 56 | enum signed_tag_mode *val = opt->value; |
| 57 | |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 58 | if (unset || !strcmp(arg, "abort")) |
Jeff King | 66e3309 | 2023-08-31 17:21:07 -0400 | [diff] [blame] | 59 | *val = SIGNED_TAG_ABORT; |
Johannes Schindelin | ee4bc37 | 2007-12-03 22:44:39 +0000 | [diff] [blame] | 60 | else if (!strcmp(arg, "verbatim") || !strcmp(arg, "ignore")) |
Jeff King | 66e3309 | 2023-08-31 17:21:07 -0400 | [diff] [blame] | 61 | *val = VERBATIM; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 62 | else if (!strcmp(arg, "warn")) |
Jeff King | 66e3309 | 2023-08-31 17:21:07 -0400 | [diff] [blame] | 63 | *val = WARN; |
John Keeping | cd16c59 | 2013-04-14 11:57:06 +0100 | [diff] [blame] | 64 | else if (!strcmp(arg, "warn-strip")) |
Jeff King | 66e3309 | 2023-08-31 17:21:07 -0400 | [diff] [blame] | 65 | *val = WARN_STRIP; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 66 | else if (!strcmp(arg, "strip")) |
Jeff King | 66e3309 | 2023-08-31 17:21:07 -0400 | [diff] [blame] | 67 | *val = STRIP; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 68 | else |
Paul Price | 04a74b6 | 2013-04-12 10:05:55 -0400 | [diff] [blame] | 69 | return error("Unknown signed-tags mode: %s", arg); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 70 | return 0; |
| 71 | } |
| 72 | |
Elijah Newren | 2d8ad46 | 2009-06-25 22:48:31 -0600 | [diff] [blame] | 73 | static int parse_opt_tag_of_filtered_mode(const struct option *opt, |
| 74 | const char *arg, int unset) |
| 75 | { |
Jeff King | 66e3309 | 2023-08-31 17:21:07 -0400 | [diff] [blame] | 76 | enum tag_of_filtered_mode *val = opt->value; |
| 77 | |
Elijah Newren | 2d8ad46 | 2009-06-25 22:48:31 -0600 | [diff] [blame] | 78 | if (unset || !strcmp(arg, "abort")) |
Jeff King | 66e3309 | 2023-08-31 17:21:07 -0400 | [diff] [blame] | 79 | *val = TAG_FILTERING_ABORT; |
Elijah Newren | 2d8ad46 | 2009-06-25 22:48:31 -0600 | [diff] [blame] | 80 | else if (!strcmp(arg, "drop")) |
Jeff King | 66e3309 | 2023-08-31 17:21:07 -0400 | [diff] [blame] | 81 | *val = DROP; |
Elijah Newren | 2d8ad46 | 2009-06-25 22:48:31 -0600 | [diff] [blame] | 82 | else if (!strcmp(arg, "rewrite")) |
Jeff King | 66e3309 | 2023-08-31 17:21:07 -0400 | [diff] [blame] | 83 | *val = REWRITE; |
Elijah Newren | 2d8ad46 | 2009-06-25 22:48:31 -0600 | [diff] [blame] | 84 | else |
| 85 | return error("Unknown tag-of-filtered mode: %s", arg); |
| 86 | return 0; |
| 87 | } |
| 88 | |
Elijah Newren | e80001f | 2019-05-13 21:31:02 -0700 | [diff] [blame] | 89 | static int parse_opt_reencode_mode(const struct option *opt, |
| 90 | const char *arg, int unset) |
| 91 | { |
Jeff King | 66e3309 | 2023-08-31 17:21:07 -0400 | [diff] [blame] | 92 | enum reencode_mode *val = opt->value; |
| 93 | |
Elijah Newren | e80001f | 2019-05-13 21:31:02 -0700 | [diff] [blame] | 94 | if (unset) { |
Jeff King | 66e3309 | 2023-08-31 17:21:07 -0400 | [diff] [blame] | 95 | *val = REENCODE_ABORT; |
Elijah Newren | e80001f | 2019-05-13 21:31:02 -0700 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | switch (git_parse_maybe_bool(arg)) { |
| 100 | case 0: |
Jeff King | 66e3309 | 2023-08-31 17:21:07 -0400 | [diff] [blame] | 101 | *val = REENCODE_NO; |
Elijah Newren | e80001f | 2019-05-13 21:31:02 -0700 | [diff] [blame] | 102 | break; |
| 103 | case 1: |
Jeff King | 66e3309 | 2023-08-31 17:21:07 -0400 | [diff] [blame] | 104 | *val = REENCODE_YES; |
Elijah Newren | e80001f | 2019-05-13 21:31:02 -0700 | [diff] [blame] | 105 | break; |
| 106 | default: |
| 107 | if (!strcasecmp(arg, "abort")) |
Jeff King | 66e3309 | 2023-08-31 17:21:07 -0400 | [diff] [blame] | 108 | *val = REENCODE_ABORT; |
Elijah Newren | e80001f | 2019-05-13 21:31:02 -0700 | [diff] [blame] | 109 | else |
| 110 | return error("Unknown reencoding mode: %s", arg); |
| 111 | } |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 116 | static struct decoration idnums; |
| 117 | static uint32_t last_idnum; |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 118 | struct anonymized_entry { |
| 119 | struct hashmap_entry hash; |
Jeff King | 76e50f7 | 2023-03-22 13:37:17 -0400 | [diff] [blame] | 120 | char *anon; |
Jeff King | 55b0145 | 2020-06-23 11:24:58 -0400 | [diff] [blame] | 121 | const char orig[FLEX_ARRAY]; |
Jeff King | a0f6564 | 2020-06-23 11:24:56 -0400 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | struct anonymized_entry_key { |
| 125 | struct hashmap_entry hash; |
| 126 | const char *orig; |
| 127 | size_t orig_len; |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 128 | }; |
| 129 | |
Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 19:09:48 +0200 | [diff] [blame] | 130 | static int anonymized_entry_cmp(const void *cmp_data UNUSED, |
Eric Wong | 939af16 | 2019-10-06 23:30:37 +0000 | [diff] [blame] | 131 | const struct hashmap_entry *eptr, |
| 132 | const struct hashmap_entry *entry_or_key, |
Jeff King | a0f6564 | 2020-06-23 11:24:56 -0400 | [diff] [blame] | 133 | const void *keydata) |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 134 | { |
Eric Wong | 939af16 | 2019-10-06 23:30:37 +0000 | [diff] [blame] | 135 | const struct anonymized_entry *a, *b; |
| 136 | |
| 137 | a = container_of(eptr, const struct anonymized_entry, hash); |
Jeff King | a0f6564 | 2020-06-23 11:24:56 -0400 | [diff] [blame] | 138 | if (keydata) { |
| 139 | const struct anonymized_entry_key *key = keydata; |
| 140 | int equal = !strncmp(a->orig, key->orig, key->orig_len) && |
| 141 | !a->orig[key->orig_len]; |
| 142 | return !equal; |
| 143 | } |
Eric Wong | 939af16 | 2019-10-06 23:30:37 +0000 | [diff] [blame] | 144 | |
Jeff King | a0f6564 | 2020-06-23 11:24:56 -0400 | [diff] [blame] | 145 | b = container_of(entry_or_key, const struct anonymized_entry, hash); |
| 146 | return strcmp(a->orig, b->orig); |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 147 | } |
| 148 | |
Jeff King | dcc4e13 | 2023-03-22 13:40:51 -0400 | [diff] [blame] | 149 | static struct anonymized_entry *add_anonymized_entry(struct hashmap *map, |
| 150 | unsigned hash, |
| 151 | const char *orig, size_t len, |
| 152 | char *anon) |
| 153 | { |
| 154 | struct anonymized_entry *ret, *old; |
| 155 | |
| 156 | if (!map->cmpfn) |
| 157 | hashmap_init(map, anonymized_entry_cmp, NULL, 0); |
| 158 | |
| 159 | FLEX_ALLOC_MEM(ret, orig, orig, len); |
| 160 | hashmap_entry_init(&ret->hash, hash); |
| 161 | ret->anon = anon; |
| 162 | old = hashmap_put_entry(map, ret, hash); |
| 163 | |
| 164 | if (old) { |
| 165 | free(old->anon); |
| 166 | free(old); |
| 167 | } |
| 168 | |
| 169 | return ret; |
| 170 | } |
| 171 | |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 172 | /* |
| 173 | * Basically keep a cache of X->Y so that we can repeatedly replace |
| 174 | * the same anonymized string with another. The actual generation |
| 175 | * is farmed out to the generate function. |
| 176 | */ |
Jeff King | 7f40759 | 2020-06-23 11:24:54 -0400 | [diff] [blame] | 177 | static const char *anonymize_str(struct hashmap *map, |
Jeff King | 65c756f | 2023-03-22 13:42:51 -0400 | [diff] [blame] | 178 | char *(*generate)(void), |
| 179 | const char *orig, size_t len) |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 180 | { |
Jeff King | a0f6564 | 2020-06-23 11:24:56 -0400 | [diff] [blame] | 181 | struct anonymized_entry_key key; |
| 182 | struct anonymized_entry *ret; |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 183 | |
Jeff King | 7f40759 | 2020-06-23 11:24:54 -0400 | [diff] [blame] | 184 | hashmap_entry_init(&key.hash, memhash(orig, len)); |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 185 | key.orig = orig; |
Jeff King | 7f40759 | 2020-06-23 11:24:54 -0400 | [diff] [blame] | 186 | key.orig_len = len; |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 187 | |
Jeff King | 65b5d9f | 2020-06-25 15:48:32 -0400 | [diff] [blame] | 188 | /* First check if it's a token the user configured manually... */ |
Jeff King | d6484e9 | 2023-03-22 13:38:04 -0400 | [diff] [blame] | 189 | ret = hashmap_get_entry(&anonymized_seeds, &key, hash, &key); |
Jeff King | 65b5d9f | 2020-06-25 15:48:32 -0400 | [diff] [blame] | 190 | |
| 191 | /* ...otherwise check if we've already seen it in this context... */ |
| 192 | if (!ret) |
| 193 | ret = hashmap_get_entry(map, &key, hash, &key); |
| 194 | |
| 195 | /* ...and finally generate a new mapping if necessary */ |
Jeff King | dcc4e13 | 2023-03-22 13:40:51 -0400 | [diff] [blame] | 196 | if (!ret) |
| 197 | ret = add_anonymized_entry(map, key.hash.hash, |
Jeff King | 65c756f | 2023-03-22 13:42:51 -0400 | [diff] [blame] | 198 | orig, len, generate()); |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 199 | |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 200 | return ret->anon; |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * We anonymize each component of a path individually, |
| 205 | * so that paths a/b and a/c will share a common root. |
| 206 | * The paths are cached via anonymize_mem so that repeated |
| 207 | * lookups for "a" will yield the same value. |
| 208 | */ |
| 209 | static void anonymize_path(struct strbuf *out, const char *path, |
| 210 | struct hashmap *map, |
Jeff King | 65c756f | 2023-03-22 13:42:51 -0400 | [diff] [blame] | 211 | char *(*generate)(void)) |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 212 | { |
| 213 | while (*path) { |
| 214 | const char *end_of_component = strchrnul(path, '/'); |
| 215 | size_t len = end_of_component - path; |
Jeff King | 65c756f | 2023-03-22 13:42:51 -0400 | [diff] [blame] | 216 | const char *c = anonymize_str(map, generate, path, len); |
Jeff King | 7f40759 | 2020-06-23 11:24:54 -0400 | [diff] [blame] | 217 | strbuf_addstr(out, c); |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 218 | path = end_of_component; |
| 219 | if (*path) |
| 220 | strbuf_addch(out, *path++); |
| 221 | } |
| 222 | } |
| 223 | |
René Scharfe | c112084 | 2018-05-09 23:06:46 +0200 | [diff] [blame] | 224 | static inline void *mark_to_ptr(uint32_t mark) |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 225 | { |
René Scharfe | c112084 | 2018-05-09 23:06:46 +0200 | [diff] [blame] | 226 | return (void *)(uintptr_t)mark; |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | static inline uint32_t ptr_to_mark(void * mark) |
| 230 | { |
René Scharfe | c112084 | 2018-05-09 23:06:46 +0200 | [diff] [blame] | 231 | return (uint32_t)(uintptr_t)mark; |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | static inline void mark_object(struct object *object, uint32_t mark) |
| 235 | { |
| 236 | add_decoration(&idnums, object, mark_to_ptr(mark)); |
| 237 | } |
| 238 | |
| 239 | static inline void mark_next_object(struct object *object) |
| 240 | { |
| 241 | mark_object(object, ++last_idnum); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | static int get_object_mark(struct object *object) |
| 245 | { |
| 246 | void *decoration = lookup_decoration(&idnums, object); |
| 247 | if (!decoration) |
| 248 | return 0; |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 249 | return ptr_to_mark(decoration); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Elijah Newren | f129c42 | 2018-11-15 23:59:51 -0800 | [diff] [blame] | 252 | static struct commit *rewrite_commit(struct commit *p) |
| 253 | { |
| 254 | for (;;) { |
| 255 | if (p->parents && p->parents->next) |
| 256 | break; |
| 257 | if (p->object.flags & UNINTERESTING) |
| 258 | break; |
| 259 | if (!(p->object.flags & TREESAME)) |
| 260 | break; |
| 261 | if (!p->parents) |
| 262 | return NULL; |
| 263 | p = p->parents->item; |
| 264 | } |
| 265 | return p; |
| 266 | } |
| 267 | |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 268 | static void show_progress(void) |
| 269 | { |
| 270 | static int counter = 0; |
| 271 | if (!progress) |
| 272 | return; |
| 273 | if ((++counter % progress) == 0) |
| 274 | printf("progress %d objects\n", counter); |
| 275 | } |
| 276 | |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 277 | /* |
| 278 | * Ideally we would want some transformation of the blob data here |
| 279 | * that is unreversible, but would still be the same size and have |
| 280 | * the same data relationship to other blobs (so that we get the same |
| 281 | * delta and packing behavior as the original). But the first and last |
| 282 | * requirements there are probably mutually exclusive, so let's take |
| 283 | * the easy way out for now, and just generate arbitrary content. |
| 284 | * |
| 285 | * There's no need to cache this result with anonymize_mem, since |
| 286 | * we already handle blob content caching with marks. |
| 287 | */ |
| 288 | static char *anonymize_blob(unsigned long *size) |
| 289 | { |
| 290 | static int counter; |
| 291 | struct strbuf out = STRBUF_INIT; |
| 292 | strbuf_addf(&out, "anonymous blob %d", counter++); |
| 293 | *size = out.len; |
| 294 | return strbuf_detach(&out, NULL); |
| 295 | } |
| 296 | |
brian m. carlson | 273f8ee | 2017-02-21 23:47:23 +0000 | [diff] [blame] | 297 | static void export_blob(const struct object_id *oid) |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 298 | { |
| 299 | unsigned long size; |
| 300 | enum object_type type; |
| 301 | char *buf; |
| 302 | struct object *object; |
Jeff King | 30b939c | 2013-03-17 04:38:57 -0400 | [diff] [blame] | 303 | int eaten; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 304 | |
Geoffrey Irving | 79559f2 | 2009-07-27 22:20:22 -0400 | [diff] [blame] | 305 | if (no_data) |
| 306 | return; |
| 307 | |
brian m. carlson | 273f8ee | 2017-02-21 23:47:23 +0000 | [diff] [blame] | 308 | if (is_null_oid(oid)) |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 309 | return; |
| 310 | |
Jeff King | d0229ab | 2019-06-20 03:41:14 -0400 | [diff] [blame] | 311 | object = lookup_object(the_repository, oid); |
Jeff King | 30b939c | 2013-03-17 04:38:57 -0400 | [diff] [blame] | 312 | if (object && object->flags & SHOWN) |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 313 | return; |
| 314 | |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 315 | if (anonymize) { |
| 316 | buf = anonymize_blob(&size); |
Stefan Beller | da14a7f | 2018-06-28 18:21:55 -0700 | [diff] [blame] | 317 | object = (struct object *)lookup_blob(the_repository, oid); |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 318 | eaten = 0; |
| 319 | } else { |
Ævar Arnfjörð Bjarmason | bc726bd | 2023-03-28 15:58:50 +0200 | [diff] [blame] | 320 | buf = repo_read_object_file(the_repository, oid, &type, &size); |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 321 | if (!buf) |
Nguyễn Thái Ngọc Duy | 1a07e59 | 2018-07-21 09:49:19 +0200 | [diff] [blame] | 322 | die("could not read blob %s", oid_to_hex(oid)); |
Matheus Tavares | b98d188 | 2020-01-30 17:32:23 -0300 | [diff] [blame] | 323 | if (check_object_signature(the_repository, oid, buf, size, |
Ævar Arnfjörð Bjarmason | 44439c1 | 2022-02-05 00:48:32 +0100 | [diff] [blame] | 324 | type) < 0) |
Elijah Newren | 843b9e6 | 2018-11-15 23:59:46 -0800 | [diff] [blame] | 325 | die("oid mismatch in blob %s", oid_to_hex(oid)); |
Stefan Beller | 1ec5bfd | 2018-06-28 18:21:53 -0700 | [diff] [blame] | 326 | object = parse_object_buffer(the_repository, oid, type, |
| 327 | size, buf, &eaten); |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 328 | } |
| 329 | |
Jeff King | 30b939c | 2013-03-17 04:38:57 -0400 | [diff] [blame] | 330 | if (!object) |
brian m. carlson | 273f8ee | 2017-02-21 23:47:23 +0000 | [diff] [blame] | 331 | die("Could not read blob %s", oid_to_hex(oid)); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 332 | |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 333 | mark_next_object(object); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 334 | |
Elijah Newren | a965bb3 | 2018-11-15 23:59:56 -0800 | [diff] [blame] | 335 | printf("blob\nmark :%"PRIu32"\n", last_idnum); |
| 336 | if (show_original_ids) |
| 337 | printf("original-oid %s\n", oid_to_hex(oid)); |
Junio C Hamano | 4d59753 | 2019-01-04 13:33:33 -0800 | [diff] [blame] | 338 | printf("data %"PRIuMAX"\n", (uintmax_t)size); |
Alex Riesen | b0fe0d7 | 2007-12-11 23:01:28 +0100 | [diff] [blame] | 339 | if (size && fwrite(buf, size, 1, stdout) != 1) |
Nguyễn Thái Ngọc Duy | 1a07e59 | 2018-07-21 09:49:19 +0200 | [diff] [blame] | 340 | die_errno("could not write blob '%s'", oid_to_hex(oid)); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 341 | printf("\n"); |
| 342 | |
| 343 | show_progress(); |
| 344 | |
| 345 | object->flags |= SHOWN; |
Jeff King | 30b939c | 2013-03-17 04:38:57 -0400 | [diff] [blame] | 346 | if (!eaten) |
| 347 | free(buf); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Elijah Newren | 060df62 | 2010-07-09 07:10:55 -0600 | [diff] [blame] | 350 | static int depth_first(const void *a_, const void *b_) |
| 351 | { |
| 352 | const struct diff_filepair *a = *((const struct diff_filepair **)a_); |
| 353 | const struct diff_filepair *b = *((const struct diff_filepair **)b_); |
| 354 | const char *name_a, *name_b; |
| 355 | int len_a, len_b, len; |
| 356 | int cmp; |
| 357 | |
| 358 | name_a = a->one ? a->one->path : a->two->path; |
| 359 | name_b = b->one ? b->one->path : b->two->path; |
| 360 | |
| 361 | len_a = strlen(name_a); |
| 362 | len_b = strlen(name_b); |
| 363 | len = (len_a < len_b) ? len_a : len_b; |
| 364 | |
| 365 | /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */ |
| 366 | cmp = memcmp(name_a, name_b, len); |
| 367 | if (cmp) |
| 368 | return cmp; |
Johannes Sixt | 4ce6fb8 | 2010-09-07 21:33:02 +0200 | [diff] [blame] | 369 | cmp = len_b - len_a; |
| 370 | if (cmp) |
| 371 | return cmp; |
| 372 | /* |
| 373 | * Move 'R'ename entries last so that all references of the file |
| 374 | * appear in the output before it is renamed (e.g., when a file |
| 375 | * was copied and renamed in the same commit). |
| 376 | */ |
| 377 | return (a->status == 'R') - (b->status == 'R'); |
Elijah Newren | 060df62 | 2010-07-09 07:10:55 -0600 | [diff] [blame] | 378 | } |
| 379 | |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 380 | static void print_path_1(const char *path) |
Jeff King | 6280dfd | 2011-08-05 16:36:22 -0600 | [diff] [blame] | 381 | { |
| 382 | int need_quote = quote_c_style(path, NULL, NULL, 0); |
| 383 | if (need_quote) |
| 384 | quote_c_style(path, NULL, stdout, 0); |
Jay Soffian | ff59f6d | 2012-06-27 17:58:01 -0400 | [diff] [blame] | 385 | else if (strchr(path, ' ')) |
| 386 | printf("\"%s\"", path); |
Jeff King | 6280dfd | 2011-08-05 16:36:22 -0600 | [diff] [blame] | 387 | else |
| 388 | printf("%s", path); |
| 389 | } |
| 390 | |
Jeff King | 65c756f | 2023-03-22 13:42:51 -0400 | [diff] [blame] | 391 | static char *anonymize_path_component(void) |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 392 | { |
| 393 | static int counter; |
| 394 | struct strbuf out = STRBUF_INIT; |
| 395 | strbuf_addf(&out, "path%d", counter++); |
Jeff King | 7f40759 | 2020-06-23 11:24:54 -0400 | [diff] [blame] | 396 | return strbuf_detach(&out, NULL); |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | static void print_path(const char *path) |
| 400 | { |
| 401 | if (!anonymize) |
| 402 | print_path_1(path); |
| 403 | else { |
| 404 | static struct hashmap paths; |
| 405 | static struct strbuf anon = STRBUF_INIT; |
| 406 | |
| 407 | anonymize_path(&anon, path, &paths, anonymize_path_component); |
| 408 | print_path_1(anon.buf); |
| 409 | strbuf_reset(&anon); |
| 410 | } |
| 411 | } |
| 412 | |
Jeff King | 65c756f | 2023-03-22 13:42:51 -0400 | [diff] [blame] | 413 | static char *generate_fake_oid(void) |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 414 | { |
Elijah Newren | 843b9e6 | 2018-11-15 23:59:46 -0800 | [diff] [blame] | 415 | static uint32_t counter = 1; /* avoid null oid */ |
| 416 | const unsigned hashsz = the_hash_algo->rawsz; |
Jeff King | 176380f | 2020-09-24 15:22:29 -0400 | [diff] [blame] | 417 | struct object_id oid; |
Jeff King | 750bb32 | 2020-06-23 11:24:51 -0400 | [diff] [blame] | 418 | char *hex = xmallocz(GIT_MAX_HEXSZ); |
| 419 | |
Jeff King | 176380f | 2020-09-24 15:22:29 -0400 | [diff] [blame] | 420 | oidclr(&oid); |
| 421 | put_be32(oid.hash + hashsz - 4, counter++); |
| 422 | return oid_to_hex_r(hex, &oid); |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 423 | } |
| 424 | |
Jeff King | 750bb32 | 2020-06-23 11:24:51 -0400 | [diff] [blame] | 425 | static const char *anonymize_oid(const char *oid_hex) |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 426 | { |
Elijah Newren | 843b9e6 | 2018-11-15 23:59:46 -0800 | [diff] [blame] | 427 | static struct hashmap objs; |
Jeff King | 750bb32 | 2020-06-23 11:24:51 -0400 | [diff] [blame] | 428 | size_t len = strlen(oid_hex); |
Jeff King | 65c756f | 2023-03-22 13:42:51 -0400 | [diff] [blame] | 429 | return anonymize_str(&objs, generate_fake_oid, oid_hex, len); |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 430 | } |
| 431 | |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 432 | static void show_filemodify(struct diff_queue_struct *q, |
Jeff King | 61bdc7c | 2022-12-13 06:13:48 -0500 | [diff] [blame] | 433 | struct diff_options *options UNUSED, void *data) |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 434 | { |
| 435 | int i; |
Jonathan Tan | b3e8ca8 | 2017-09-20 16:55:02 -0700 | [diff] [blame] | 436 | struct string_list *changed = data; |
Elijah Newren | 060df62 | 2010-07-09 07:10:55 -0600 | [diff] [blame] | 437 | |
| 438 | /* |
| 439 | * Handle files below a directory first, in case they are all deleted |
| 440 | * and the directory changes to a file or symlink. |
| 441 | */ |
René Scharfe | 9ed0d8d | 2016-09-29 17:27:31 +0200 | [diff] [blame] | 442 | QSORT(q->queue, q->nr, depth_first); |
Elijah Newren | 060df62 | 2010-07-09 07:10:55 -0600 | [diff] [blame] | 443 | |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 444 | for (i = 0; i < q->nr; i++) { |
Alexander Gavrilov | ae7c5dc | 2008-07-27 00:52:54 +0400 | [diff] [blame] | 445 | struct diff_filespec *ospec = q->queue[i]->one; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 446 | struct diff_filespec *spec = q->queue[i]->two; |
Alexander Gavrilov | ae7c5dc | 2008-07-27 00:52:54 +0400 | [diff] [blame] | 447 | |
| 448 | switch (q->queue[i]->status) { |
| 449 | case DIFF_STATUS_DELETED: |
Jeff King | 6280dfd | 2011-08-05 16:36:22 -0600 | [diff] [blame] | 450 | printf("D "); |
| 451 | print_path(spec->path); |
Jonathan Tan | b3e8ca8 | 2017-09-20 16:55:02 -0700 | [diff] [blame] | 452 | string_list_insert(changed, spec->path); |
Jeff King | 6280dfd | 2011-08-05 16:36:22 -0600 | [diff] [blame] | 453 | putchar('\n'); |
Alexander Gavrilov | ae7c5dc | 2008-07-27 00:52:54 +0400 | [diff] [blame] | 454 | break; |
| 455 | |
| 456 | case DIFF_STATUS_COPIED: |
| 457 | case DIFF_STATUS_RENAMED: |
Jonathan Tan | b3e8ca8 | 2017-09-20 16:55:02 -0700 | [diff] [blame] | 458 | /* |
| 459 | * If a change in the file corresponding to ospec->path |
| 460 | * has been observed, we cannot trust its contents |
| 461 | * because the diff is calculated based on the prior |
| 462 | * contents, not the current contents. So, declare a |
| 463 | * copy or rename only if there was no change observed. |
| 464 | */ |
| 465 | if (!string_list_has_string(changed, ospec->path)) { |
| 466 | printf("%c ", q->queue[i]->status); |
| 467 | print_path(ospec->path); |
| 468 | putchar(' '); |
| 469 | print_path(spec->path); |
| 470 | string_list_insert(changed, spec->path); |
| 471 | putchar('\n'); |
Alexander Gavrilov | ae7c5dc | 2008-07-27 00:52:54 +0400 | [diff] [blame] | 472 | |
Jeff King | 4a7e27e | 2018-08-28 17:22:40 -0400 | [diff] [blame] | 473 | if (oideq(&ospec->oid, &spec->oid) && |
Jonathan Tan | b3e8ca8 | 2017-09-20 16:55:02 -0700 | [diff] [blame] | 474 | ospec->mode == spec->mode) |
| 475 | break; |
| 476 | } |
Alexander Gavrilov | ae7c5dc | 2008-07-27 00:52:54 +0400 | [diff] [blame] | 477 | /* fallthrough */ |
| 478 | |
| 479 | case DIFF_STATUS_TYPE_CHANGED: |
| 480 | case DIFF_STATUS_MODIFIED: |
| 481 | case DIFF_STATUS_ADDED: |
Alexander Gavrilov | 03db452 | 2008-07-19 16:21:24 +0400 | [diff] [blame] | 482 | /* |
| 483 | * Links refer to objects in another repositories; |
| 484 | * output the SHA-1 verbatim. |
| 485 | */ |
Geoffrey Irving | 79559f2 | 2009-07-27 22:20:22 -0400 | [diff] [blame] | 486 | if (no_data || S_ISGITLINK(spec->mode)) |
Jeff King | 6280dfd | 2011-08-05 16:36:22 -0600 | [diff] [blame] | 487 | printf("M %06o %s ", spec->mode, |
Jeff King | 750bb32 | 2020-06-23 11:24:51 -0400 | [diff] [blame] | 488 | anonymize ? |
| 489 | anonymize_oid(oid_to_hex(&spec->oid)) : |
| 490 | oid_to_hex(&spec->oid)); |
Alexander Gavrilov | 03db452 | 2008-07-19 16:21:24 +0400 | [diff] [blame] | 491 | else { |
Stefan Beller | 5abddd1 | 2018-06-28 18:21:52 -0700 | [diff] [blame] | 492 | struct object *object = lookup_object(the_repository, |
Jeff King | d0229ab | 2019-06-20 03:41:14 -0400 | [diff] [blame] | 493 | &spec->oid); |
Jeff King | 6280dfd | 2011-08-05 16:36:22 -0600 | [diff] [blame] | 494 | printf("M %06o :%d ", spec->mode, |
| 495 | get_object_mark(object)); |
Alexander Gavrilov | 03db452 | 2008-07-19 16:21:24 +0400 | [diff] [blame] | 496 | } |
Jeff King | 6280dfd | 2011-08-05 16:36:22 -0600 | [diff] [blame] | 497 | print_path(spec->path); |
Jonathan Tan | b3e8ca8 | 2017-09-20 16:55:02 -0700 | [diff] [blame] | 498 | string_list_insert(changed, spec->path); |
Jeff King | 6280dfd | 2011-08-05 16:36:22 -0600 | [diff] [blame] | 499 | putchar('\n'); |
Alexander Gavrilov | ae7c5dc | 2008-07-27 00:52:54 +0400 | [diff] [blame] | 500 | break; |
| 501 | |
| 502 | default: |
| 503 | die("Unexpected comparison status '%c' for %s, %s", |
| 504 | q->queue[i]->status, |
| 505 | ospec->path ? ospec->path : "none", |
| 506 | spec->path ? spec->path : "none"); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | static const char *find_encoding(const char *begin, const char *end) |
| 512 | { |
| 513 | const char *needle = "\nencoding "; |
| 514 | char *bol, *eol; |
| 515 | |
| 516 | bol = memmem(begin, end ? end - begin : strlen(begin), |
| 517 | needle, strlen(needle)); |
| 518 | if (!bol) |
Elijah Newren | 57a8be2 | 2019-05-13 21:31:01 -0700 | [diff] [blame] | 519 | return NULL; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 520 | bol += strlen(needle); |
| 521 | eol = strchrnul(bol, '\n'); |
| 522 | *eol = '\0'; |
| 523 | return bol; |
| 524 | } |
| 525 | |
Jeff King | 65c756f | 2023-03-22 13:42:51 -0400 | [diff] [blame] | 526 | static char *anonymize_ref_component(void) |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 527 | { |
| 528 | static int counter; |
| 529 | struct strbuf out = STRBUF_INIT; |
| 530 | strbuf_addf(&out, "ref%d", counter++); |
Jeff King | 7f40759 | 2020-06-23 11:24:54 -0400 | [diff] [blame] | 531 | return strbuf_detach(&out, NULL); |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | static const char *anonymize_refname(const char *refname) |
| 535 | { |
| 536 | /* |
| 537 | * If any of these prefixes is found, we will leave it intact |
| 538 | * so that tags remain tags and so forth. |
| 539 | */ |
| 540 | static const char *prefixes[] = { |
| 541 | "refs/heads/", |
| 542 | "refs/tags/", |
| 543 | "refs/remotes/", |
| 544 | "refs/" |
| 545 | }; |
| 546 | static struct hashmap refs; |
| 547 | static struct strbuf anon = STRBUF_INIT; |
| 548 | int i; |
| 549 | |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 550 | strbuf_reset(&anon); |
| 551 | for (i = 0; i < ARRAY_SIZE(prefixes); i++) { |
| 552 | if (skip_prefix(refname, prefixes[i], &refname)) { |
| 553 | strbuf_addstr(&anon, prefixes[i]); |
| 554 | break; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | anonymize_path(&anon, refname, &refs, anonymize_ref_component); |
| 559 | return anon.buf; |
| 560 | } |
| 561 | |
| 562 | /* |
| 563 | * We do not even bother to cache commit messages, as they are unlikely |
| 564 | * to be repeated verbatim, and it is not that interesting when they are. |
| 565 | */ |
Jeff King | d051f17 | 2023-03-22 13:43:22 -0400 | [diff] [blame] | 566 | static char *anonymize_commit_message(void) |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 567 | { |
| 568 | static int counter; |
| 569 | return xstrfmt("subject %d\n\nbody\n", counter++); |
| 570 | } |
| 571 | |
Jeff King | 65c756f | 2023-03-22 13:42:51 -0400 | [diff] [blame] | 572 | static char *anonymize_ident(void) |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 573 | { |
| 574 | static int counter; |
| 575 | struct strbuf out = STRBUF_INIT; |
| 576 | strbuf_addf(&out, "User %d <user%d@example.com>", counter, counter); |
| 577 | counter++; |
Jeff King | 7f40759 | 2020-06-23 11:24:54 -0400 | [diff] [blame] | 578 | return strbuf_detach(&out, NULL); |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | /* |
| 582 | * Our strategy here is to anonymize the names and email addresses, |
| 583 | * but keep timestamps intact, as they influence things like traversal |
| 584 | * order (and by themselves should not be too revealing). |
| 585 | */ |
| 586 | static void anonymize_ident_line(const char **beg, const char **end) |
| 587 | { |
Jeff King | 6416a86 | 2020-06-23 11:25:01 -0400 | [diff] [blame] | 588 | static struct hashmap idents; |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 589 | static struct strbuf buffers[] = { STRBUF_INIT, STRBUF_INIT }; |
| 590 | static unsigned which_buffer; |
| 591 | |
| 592 | struct strbuf *out; |
| 593 | struct ident_split split; |
| 594 | const char *end_of_header; |
| 595 | |
| 596 | out = &buffers[which_buffer++]; |
| 597 | which_buffer %= ARRAY_SIZE(buffers); |
| 598 | strbuf_reset(out); |
| 599 | |
| 600 | /* skip "committer", "author", "tagger", etc */ |
| 601 | end_of_header = strchr(*beg, ' '); |
| 602 | if (!end_of_header) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 603 | BUG("malformed line fed to anonymize_ident_line: %.*s", |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 604 | (int)(*end - *beg), *beg); |
| 605 | end_of_header++; |
| 606 | strbuf_add(out, *beg, end_of_header - *beg); |
| 607 | |
| 608 | if (!split_ident_line(&split, end_of_header, *end - end_of_header) && |
| 609 | split.date_begin) { |
| 610 | const char *ident; |
| 611 | size_t len; |
| 612 | |
| 613 | len = split.mail_end - split.name_begin; |
Jeff King | 7f40759 | 2020-06-23 11:24:54 -0400 | [diff] [blame] | 614 | ident = anonymize_str(&idents, anonymize_ident, |
Jeff King | 65c756f | 2023-03-22 13:42:51 -0400 | [diff] [blame] | 615 | split.name_begin, len); |
Jeff King | 7f40759 | 2020-06-23 11:24:54 -0400 | [diff] [blame] | 616 | strbuf_addstr(out, ident); |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 617 | strbuf_addch(out, ' '); |
| 618 | strbuf_add(out, split.date_begin, split.tz_end - split.date_begin); |
| 619 | } else { |
| 620 | strbuf_addstr(out, "Malformed Ident <malformed@example.com> 0 -0000"); |
| 621 | } |
| 622 | |
| 623 | *beg = out->buf; |
| 624 | *end = out->buf + out->len; |
| 625 | } |
| 626 | |
Jonathan Tan | b3e8ca8 | 2017-09-20 16:55:02 -0700 | [diff] [blame] | 627 | static void handle_commit(struct commit *commit, struct rev_info *rev, |
| 628 | struct string_list *paths_of_changed_objects) |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 629 | { |
| 630 | int saved_output_format = rev->diffopt.output_format; |
Jeff King | bc6b8fc | 2014-06-10 17:41:51 -0400 | [diff] [blame] | 631 | const char *commit_buffer; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 632 | const char *author, *author_end, *committer, *committer_end; |
| 633 | const char *encoding, *message; |
| 634 | char *reencoded = NULL; |
| 635 | struct commit_list *p; |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 636 | const char *refname; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 637 | int i; |
| 638 | |
| 639 | rev->diffopt.output_format = DIFF_FORMAT_CALLBACK; |
| 640 | |
Jeff King | 683ff88 | 2013-10-24 04:53:46 -0400 | [diff] [blame] | 641 | parse_commit_or_die(commit); |
Ævar Arnfjörð Bjarmason | ecb5091 | 2023-03-28 15:58:48 +0200 | [diff] [blame] | 642 | commit_buffer = repo_get_commit_buffer(the_repository, commit, NULL); |
Jeff King | bc6b8fc | 2014-06-10 17:41:51 -0400 | [diff] [blame] | 643 | author = strstr(commit_buffer, "\nauthor "); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 644 | if (!author) |
Nguyễn Thái Ngọc Duy | 1a07e59 | 2018-07-21 09:49:19 +0200 | [diff] [blame] | 645 | die("could not find author in commit %s", |
| 646 | oid_to_hex(&commit->object.oid)); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 647 | author++; |
| 648 | author_end = strchrnul(author, '\n'); |
| 649 | committer = strstr(author_end, "\ncommitter "); |
| 650 | if (!committer) |
Nguyễn Thái Ngọc Duy | 1a07e59 | 2018-07-21 09:49:19 +0200 | [diff] [blame] | 651 | die("could not find committer in commit %s", |
| 652 | oid_to_hex(&commit->object.oid)); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 653 | committer++; |
| 654 | committer_end = strchrnul(committer, '\n'); |
| 655 | message = strstr(committer_end, "\n\n"); |
| 656 | encoding = find_encoding(committer_end, message); |
| 657 | if (message) |
| 658 | message += 2; |
| 659 | |
Elijah Newren | ebeec7d | 2009-03-25 17:53:23 -0600 | [diff] [blame] | 660 | if (commit->parents && |
Elijah Newren | 530ca19 | 2018-11-15 23:59:54 -0800 | [diff] [blame] | 661 | (get_object_mark(&commit->parents->item->object) != 0 || |
| 662 | reference_excluded_commits) && |
Elijah Newren | 4087a02 | 2010-07-17 11:00:50 -0600 | [diff] [blame] | 663 | !full_tree) { |
Jeff King | 683ff88 | 2013-10-24 04:53:46 -0400 | [diff] [blame] | 664 | parse_commit_or_die(commit->parents->item); |
Derrick Stolee | 2e27bd7 | 2018-04-06 19:09:38 +0000 | [diff] [blame] | 665 | diff_tree_oid(get_commit_tree_oid(commit->parents->item), |
| 666 | get_commit_tree_oid(commit), "", &rev->diffopt); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 667 | } |
| 668 | else |
Derrick Stolee | 2e27bd7 | 2018-04-06 19:09:38 +0000 | [diff] [blame] | 669 | diff_root_tree_oid(get_commit_tree_oid(commit), |
Brandon Williams | 7b8dea0 | 2017-05-30 10:30:57 -0700 | [diff] [blame] | 670 | "", &rev->diffopt); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 671 | |
Alexander Gavrilov | 03db452 | 2008-07-19 16:21:24 +0400 | [diff] [blame] | 672 | /* Export the referenced blobs, and remember the marks. */ |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 673 | for (i = 0; i < diff_queued_diff.nr; i++) |
Alexander Gavrilov | 03db452 | 2008-07-19 16:21:24 +0400 | [diff] [blame] | 674 | if (!S_ISGITLINK(diff_queued_diff.queue[i]->two->mode)) |
brian m. carlson | 273f8ee | 2017-02-21 23:47:23 +0000 | [diff] [blame] | 675 | export_blob(&diff_queued_diff.queue[i]->two->oid); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 676 | |
Nguyễn Thái Ngọc Duy | 87be252 | 2018-05-19 07:28:24 +0200 | [diff] [blame] | 677 | refname = *revision_sources_at(&revision_sources, commit); |
Elijah Newren | fdf31b6 | 2018-11-15 23:59:53 -0800 | [diff] [blame] | 678 | /* |
| 679 | * FIXME: string_list_remove() below for each ref is overall |
| 680 | * O(N^2). Compared to a history walk and diffing trees, this is |
| 681 | * just lost in the noise in practice. However, theoretically a |
| 682 | * repo may have enough refs for this to become slow. |
| 683 | */ |
| 684 | string_list_remove(&extra_refs, refname, 0); |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 685 | if (anonymize) { |
| 686 | refname = anonymize_refname(refname); |
| 687 | anonymize_ident_line(&committer, &committer_end); |
| 688 | anonymize_ident_line(&author, &author_end); |
| 689 | } |
| 690 | |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 691 | mark_next_object(&commit->object); |
Elijah Newren | e80001f | 2019-05-13 21:31:02 -0700 | [diff] [blame] | 692 | if (anonymize) { |
Jeff King | d051f17 | 2023-03-22 13:43:22 -0400 | [diff] [blame] | 693 | reencoded = anonymize_commit_message(); |
Elijah Newren | e80001f | 2019-05-13 21:31:02 -0700 | [diff] [blame] | 694 | } else if (encoding) { |
| 695 | switch(reencode_mode) { |
| 696 | case REENCODE_YES: |
| 697 | reencoded = reencode_string(message, "UTF-8", encoding); |
| 698 | break; |
| 699 | case REENCODE_NO: |
| 700 | break; |
| 701 | case REENCODE_ABORT: |
| 702 | die("Encountered commit-specific encoding %s in commit " |
| 703 | "%s; use --reencode=[yes|no] to handle it", |
| 704 | encoding, oid_to_hex(&commit->object.oid)); |
| 705 | } |
| 706 | } |
Shawn O. Pearce | d8933f0 | 2008-06-13 00:38:55 -0400 | [diff] [blame] | 707 | if (!commit->parents) |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 708 | printf("reset %s\n", refname); |
Elijah Newren | a965bb3 | 2018-11-15 23:59:56 -0800 | [diff] [blame] | 709 | printf("commit %s\nmark :%"PRIu32"\n", refname, last_idnum); |
| 710 | if (show_original_ids) |
| 711 | printf("original-oid %s\n", oid_to_hex(&commit->object.oid)); |
Elijah Newren | ccbfc96 | 2019-05-13 21:31:00 -0700 | [diff] [blame] | 712 | printf("%.*s\n%.*s\n", |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 713 | (int)(author_end - author), author, |
Elijah Newren | ccbfc96 | 2019-05-13 21:31:00 -0700 | [diff] [blame] | 714 | (int)(committer_end - committer), committer); |
| 715 | if (!reencoded && encoding) |
| 716 | printf("encoding %s\n", encoding); |
| 717 | printf("data %u\n%s", |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 718 | (unsigned)(reencoded |
| 719 | ? strlen(reencoded) : message |
| 720 | ? strlen(message) : 0), |
| 721 | reencoded ? reencoded : message ? message : ""); |
Jim Meyering | 8e0f700 | 2008-01-31 18:26:32 +0100 | [diff] [blame] | 722 | free(reencoded); |
Ævar Arnfjörð Bjarmason | ecb5091 | 2023-03-28 15:58:48 +0200 | [diff] [blame] | 723 | repo_unuse_commit_buffer(the_repository, commit, commit_buffer); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 724 | |
| 725 | for (i = 0, p = commit->parents; p; p = p->next) { |
Elijah Newren | 530ca19 | 2018-11-15 23:59:54 -0800 | [diff] [blame] | 726 | struct object *obj = &p->item->object; |
| 727 | int mark = get_object_mark(obj); |
| 728 | |
| 729 | if (!mark && !reference_excluded_commits) |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 730 | continue; |
| 731 | if (i == 0) |
Elijah Newren | 530ca19 | 2018-11-15 23:59:54 -0800 | [diff] [blame] | 732 | printf("from "); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 733 | else |
Elijah Newren | 530ca19 | 2018-11-15 23:59:54 -0800 | [diff] [blame] | 734 | printf("merge "); |
| 735 | if (mark) |
| 736 | printf(":%d\n", mark); |
| 737 | else |
Jeff King | 750bb32 | 2020-06-23 11:24:51 -0400 | [diff] [blame] | 738 | printf("%s\n", |
| 739 | anonymize ? |
| 740 | anonymize_oid(oid_to_hex(&obj->oid)) : |
| 741 | oid_to_hex(&obj->oid)); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 742 | i++; |
| 743 | } |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 744 | |
Elijah Newren | 4087a02 | 2010-07-17 11:00:50 -0600 | [diff] [blame] | 745 | if (full_tree) |
| 746 | printf("deleteall\n"); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 747 | log_tree_diff_flush(rev); |
Jonathan Tan | b3e8ca8 | 2017-09-20 16:55:02 -0700 | [diff] [blame] | 748 | string_list_clear(paths_of_changed_objects, 0); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 749 | rev->diffopt.output_format = saved_output_format; |
| 750 | |
| 751 | printf("\n"); |
| 752 | |
| 753 | show_progress(); |
| 754 | } |
| 755 | |
Jeff King | 65c756f | 2023-03-22 13:42:51 -0400 | [diff] [blame] | 756 | static char *anonymize_tag(void) |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 757 | { |
| 758 | static int counter; |
| 759 | struct strbuf out = STRBUF_INIT; |
| 760 | strbuf_addf(&out, "tag message %d", counter++); |
Jeff King | 7f40759 | 2020-06-23 11:24:54 -0400 | [diff] [blame] | 761 | return strbuf_detach(&out, NULL); |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 762 | } |
| 763 | |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 764 | |
| 765 | static void handle_tag(const char *name, struct tag *tag) |
| 766 | { |
| 767 | unsigned long size; |
| 768 | enum object_type type; |
| 769 | char *buf; |
| 770 | const char *tagger, *tagger_end, *message; |
| 771 | size_t message_size = 0; |
Elijah Newren | 02c48cd | 2009-06-25 22:48:28 -0600 | [diff] [blame] | 772 | struct object *tagged; |
Elijah Newren | 2d8ad46 | 2009-06-25 22:48:31 -0600 | [diff] [blame] | 773 | int tagged_mark; |
| 774 | struct commit *p; |
Elijah Newren | 02c48cd | 2009-06-25 22:48:28 -0600 | [diff] [blame] | 775 | |
Ondřej Bílka | 98e023d | 2013-07-29 10:18:21 +0200 | [diff] [blame] | 776 | /* 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] | 777 | * to output tags of trees, tags of tags of trees, etc. Simply omit |
| 778 | * such tags. |
| 779 | */ |
| 780 | tagged = tag->tagged; |
| 781 | while (tagged->type == OBJ_TAG) { |
| 782 | tagged = ((struct tag *)tagged)->tagged; |
| 783 | } |
| 784 | if (tagged->type == OBJ_TREE) { |
| 785 | 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] | 786 | oid_to_hex(&tag->object.oid)); |
Elijah Newren | 02c48cd | 2009-06-25 22:48:28 -0600 | [diff] [blame] | 787 | return; |
| 788 | } |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 789 | |
Ævar Arnfjörð Bjarmason | bc726bd | 2023-03-28 15:58:50 +0200 | [diff] [blame] | 790 | buf = repo_read_object_file(the_repository, &tag->object.oid, &type, |
| 791 | &size); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 792 | if (!buf) |
Nguyễn Thái Ngọc Duy | 1a07e59 | 2018-07-21 09:49:19 +0200 | [diff] [blame] | 793 | die("could not read tag %s", oid_to_hex(&tag->object.oid)); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 794 | message = memmem(buf, size, "\n\n", 2); |
| 795 | if (message) { |
| 796 | message += 2; |
| 797 | message_size = strlen(message); |
| 798 | } |
| 799 | tagger = memmem(buf, message ? message - buf : size, "\ntagger ", 8); |
Johannes Schindelin | 4e46a8d | 2008-12-20 01:00:27 +0100 | [diff] [blame] | 800 | if (!tagger) { |
| 801 | if (fake_missing_tagger) |
| 802 | tagger = "tagger Unspecified Tagger " |
| 803 | "<unspecified-tagger> 0 +0000"; |
| 804 | else |
| 805 | tagger = ""; |
| 806 | tagger_end = tagger + strlen(tagger); |
| 807 | } else { |
| 808 | tagger++; |
| 809 | tagger_end = strchrnul(tagger, '\n'); |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 810 | if (anonymize) |
| 811 | anonymize_ident_line(&tagger, &tagger_end); |
| 812 | } |
| 813 | |
| 814 | if (anonymize) { |
| 815 | name = anonymize_refname(name); |
| 816 | if (message) { |
| 817 | static struct hashmap tags; |
Jeff King | 7f40759 | 2020-06-23 11:24:54 -0400 | [diff] [blame] | 818 | message = anonymize_str(&tags, anonymize_tag, |
Jeff King | 65c756f | 2023-03-22 13:42:51 -0400 | [diff] [blame] | 819 | message, message_size); |
Tal Kelrich | 2f040a9 | 2021-08-31 15:55:54 +0000 | [diff] [blame] | 820 | message_size = strlen(message); |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 821 | } |
Johannes Schindelin | 4e46a8d | 2008-12-20 01:00:27 +0100 | [diff] [blame] | 822 | } |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 823 | |
| 824 | /* handle signed tags */ |
| 825 | if (message) { |
| 826 | const char *signature = strstr(message, |
| 827 | "\n-----BEGIN PGP SIGNATURE-----\n"); |
| 828 | if (signature) |
| 829 | switch(signed_tag_mode) { |
Elijah Newren | b93b81e | 2018-11-15 23:59:49 -0800 | [diff] [blame] | 830 | case SIGNED_TAG_ABORT: |
Nguyễn Thái Ngọc Duy | 1a07e59 | 2018-07-21 09:49:19 +0200 | [diff] [blame] | 831 | die("encountered signed tag %s; use " |
| 832 | "--signed-tags=<mode> to handle it", |
| 833 | oid_to_hex(&tag->object.oid)); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 834 | case WARN: |
Nguyễn Thái Ngọc Duy | 1a07e59 | 2018-07-21 09:49:19 +0200 | [diff] [blame] | 835 | warning("exporting signed tag %s", |
| 836 | oid_to_hex(&tag->object.oid)); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 837 | /* fallthru */ |
Johannes Schindelin | ee4bc37 | 2007-12-03 22:44:39 +0000 | [diff] [blame] | 838 | case VERBATIM: |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 839 | break; |
John Keeping | cd16c59 | 2013-04-14 11:57:06 +0100 | [diff] [blame] | 840 | case WARN_STRIP: |
Nguyễn Thái Ngọc Duy | 1a07e59 | 2018-07-21 09:49:19 +0200 | [diff] [blame] | 841 | warning("stripping signature from tag %s", |
| 842 | oid_to_hex(&tag->object.oid)); |
John Keeping | cd16c59 | 2013-04-14 11:57:06 +0100 | [diff] [blame] | 843 | /* fallthru */ |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 844 | case STRIP: |
| 845 | message_size = signature + 1 - message; |
| 846 | break; |
| 847 | } |
| 848 | } |
| 849 | |
Elijah Newren | 2d8ad46 | 2009-06-25 22:48:31 -0600 | [diff] [blame] | 850 | /* handle tag->tagged having been filtered out due to paths specified */ |
| 851 | tagged = tag->tagged; |
| 852 | tagged_mark = get_object_mark(tagged); |
| 853 | if (!tagged_mark) { |
| 854 | switch(tag_of_filtered_mode) { |
Elijah Newren | b93b81e | 2018-11-15 23:59:49 -0800 | [diff] [blame] | 855 | case TAG_FILTERING_ABORT: |
Nguyễn Thái Ngọc Duy | 1a07e59 | 2018-07-21 09:49:19 +0200 | [diff] [blame] | 856 | die("tag %s tags unexported object; use " |
| 857 | "--tag-of-filtered-object=<mode> to handle it", |
| 858 | oid_to_hex(&tag->object.oid)); |
Elijah Newren | 2d8ad46 | 2009-06-25 22:48:31 -0600 | [diff] [blame] | 859 | case DROP: |
| 860 | /* Ignore this tag altogether */ |
Johannes Schindelin | 1efb1e9 | 2017-05-04 15:57:33 +0200 | [diff] [blame] | 861 | free(buf); |
Elijah Newren | 2d8ad46 | 2009-06-25 22:48:31 -0600 | [diff] [blame] | 862 | return; |
| 863 | case REWRITE: |
Elijah Newren | 941790d | 2019-10-03 13:27:09 -0700 | [diff] [blame] | 864 | if (tagged->type == OBJ_TAG && !mark_tags) { |
| 865 | die(_("Error: Cannot export nested tags unless --mark-tags is specified.")); |
| 866 | } else if (tagged->type == OBJ_COMMIT) { |
| 867 | p = rewrite_commit((struct commit *)tagged); |
| 868 | if (!p) { |
| 869 | printf("reset %s\nfrom %s\n\n", |
brian m. carlson | 1422844 | 2021-04-26 01:02:56 +0000 | [diff] [blame] | 870 | name, oid_to_hex(null_oid())); |
Elijah Newren | 941790d | 2019-10-03 13:27:09 -0700 | [diff] [blame] | 871 | free(buf); |
| 872 | return; |
| 873 | } |
| 874 | tagged_mark = get_object_mark(&p->object); |
| 875 | } else { |
| 876 | /* tagged->type is either OBJ_BLOB or OBJ_TAG */ |
| 877 | tagged_mark = get_object_mark(tagged); |
Elijah Newren | 2d8ad46 | 2009-06-25 22:48:31 -0600 | [diff] [blame] | 878 | } |
Elijah Newren | 2d8ad46 | 2009-06-25 22:48:31 -0600 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | |
Elijah Newren | 941790d | 2019-10-03 13:27:09 -0700 | [diff] [blame] | 882 | if (tagged->type == OBJ_TAG) { |
| 883 | printf("reset %s\nfrom %s\n\n", |
brian m. carlson | 1422844 | 2021-04-26 01:02:56 +0000 | [diff] [blame] | 884 | name, oid_to_hex(null_oid())); |
Elijah Newren | 941790d | 2019-10-03 13:27:09 -0700 | [diff] [blame] | 885 | } |
Junio C Hamano | 145136a | 2020-01-30 11:35:46 -0800 | [diff] [blame] | 886 | skip_prefix(name, "refs/tags/", &name); |
Elijah Newren | af2abd8 | 2019-09-24 18:39:58 -0700 | [diff] [blame] | 887 | printf("tag %s\n", name); |
Elijah Newren | a1638cf | 2019-10-03 13:27:07 -0700 | [diff] [blame] | 888 | if (mark_tags) { |
| 889 | mark_next_object(&tag->object); |
| 890 | printf("mark :%"PRIu32"\n", last_idnum); |
| 891 | } |
Elijah Newren | af2abd8 | 2019-09-24 18:39:58 -0700 | [diff] [blame] | 892 | if (tagged_mark) |
| 893 | printf("from :%d\n", tagged_mark); |
| 894 | else |
| 895 | printf("from %s\n", oid_to_hex(&tagged->oid)); |
| 896 | |
Elijah Newren | a965bb3 | 2018-11-15 23:59:56 -0800 | [diff] [blame] | 897 | if (show_original_ids) |
| 898 | printf("original-oid %s\n", oid_to_hex(&tag->object.oid)); |
| 899 | printf("%.*s%sdata %d\n%.*s\n", |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 900 | (int)(tagger_end - tagger), tagger, |
Johannes Schindelin | 4e46a8d | 2008-12-20 01:00:27 +0100 | [diff] [blame] | 901 | tagger == tagger_end ? "" : "\n", |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 902 | (int)message_size, (int)message_size, message ? message : ""); |
Johannes Schindelin | 1efb1e9 | 2017-05-04 15:57:33 +0200 | [diff] [blame] | 903 | free(buf); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 904 | } |
| 905 | |
Felipe Contreras | 3e9b9cb | 2013-09-01 02:05:48 -0500 | [diff] [blame] | 906 | 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] | 907 | { |
Felipe Contreras | 3e9b9cb | 2013-09-01 02:05:48 -0500 | [diff] [blame] | 908 | switch (e->item->type) { |
| 909 | case OBJ_COMMIT: |
| 910 | return (struct commit *)e->item; |
| 911 | case OBJ_TAG: { |
| 912 | struct tag *tag = (struct tag *)e->item; |
| 913 | |
| 914 | /* handle nested tags */ |
| 915 | while (tag && tag->object.type == OBJ_TAG) { |
Stefan Beller | 109cd76 | 2018-06-28 18:21:51 -0700 | [diff] [blame] | 916 | parse_object(the_repository, &tag->object.oid); |
Elijah Newren | fdf31b6 | 2018-11-15 23:59:53 -0800 | [diff] [blame] | 917 | string_list_append(&tag_refs, full_name)->util = tag; |
Felipe Contreras | 3e9b9cb | 2013-09-01 02:05:48 -0500 | [diff] [blame] | 918 | tag = (struct tag *)tag->tagged; |
| 919 | } |
| 920 | if (!tag) |
| 921 | die("Tag %s points nowhere?", e->name); |
| 922 | return (struct commit *)tag; |
Felipe Contreras | 3e9b9cb | 2013-09-01 02:05:48 -0500 | [diff] [blame] | 923 | } |
| 924 | default: |
| 925 | return NULL; |
| 926 | } |
| 927 | } |
| 928 | |
Felipe Contreras | 1d844ee | 2013-09-01 02:05:47 -0500 | [diff] [blame] | 929 | static void get_tags_and_duplicates(struct rev_cmdline_info *info) |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 930 | { |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 931 | int i; |
| 932 | |
Felipe Contreras | 49266e8 | 2012-11-28 23:23:59 +0100 | [diff] [blame] | 933 | for (i = 0; i < info->nr; i++) { |
| 934 | struct rev_cmdline_entry *e = info->rev + i; |
brian m. carlson | 273f8ee | 2017-02-21 23:47:23 +0000 | [diff] [blame] | 935 | struct object_id oid; |
Felipe Contreras | 2d242de | 2012-11-28 23:11:08 +0100 | [diff] [blame] | 936 | struct commit *commit; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 937 | char *full_name; |
| 938 | |
Felipe Contreras | 49266e8 | 2012-11-28 23:23:59 +0100 | [diff] [blame] | 939 | if (e->flags & UNINTERESTING) |
| 940 | continue; |
| 941 | |
Ævar Arnfjörð Bjarmason | 12cb1c1 | 2023-03-28 15:58:54 +0200 | [diff] [blame] | 942 | if (repo_dwim_ref(the_repository, e->name, strlen(e->name), |
| 943 | &oid, &full_name, 0) != 1) |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 944 | continue; |
| 945 | |
Brandon Williams | 16eefc8 | 2018-05-16 15:57:59 -0700 | [diff] [blame] | 946 | if (refspecs.nr) { |
Felipe Contreras | 03e9010 | 2014-04-20 13:59:24 -0500 | [diff] [blame] | 947 | char *private; |
Brandon Williams | d000414 | 2018-05-16 15:58:11 -0700 | [diff] [blame] | 948 | private = apply_refspecs(&refspecs, full_name); |
Felipe Contreras | 03e9010 | 2014-04-20 13:59:24 -0500 | [diff] [blame] | 949 | if (private) { |
| 950 | free(full_name); |
| 951 | full_name = private; |
| 952 | } |
| 953 | } |
| 954 | |
Felipe Contreras | 3e9b9cb | 2013-09-01 02:05:48 -0500 | [diff] [blame] | 955 | commit = get_commit(e, full_name); |
| 956 | if (!commit) { |
Erik Faye-Lund | 2d07f6d | 2009-03-23 12:53:07 +0000 | [diff] [blame] | 957 | warning("%s: Unexpected object of type %s, skipping.", |
| 958 | e->name, |
Brandon Williams | debca9d | 2018-02-14 10:59:24 -0800 | [diff] [blame] | 959 | type_name(e->item->type)); |
Erik Faye-Lund | 2d07f6d | 2009-03-23 12:53:07 +0000 | [diff] [blame] | 960 | continue; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 961 | } |
Felipe Contreras | f28e7c9 | 2012-11-28 23:24:00 +0100 | [diff] [blame] | 962 | |
Felipe Contreras | 3e9b9cb | 2013-09-01 02:05:48 -0500 | [diff] [blame] | 963 | switch(commit->object.type) { |
| 964 | case OBJ_COMMIT: |
| 965 | break; |
| 966 | case OBJ_BLOB: |
brian m. carlson | 273f8ee | 2017-02-21 23:47:23 +0000 | [diff] [blame] | 967 | export_blob(&commit->object.oid); |
Felipe Contreras | 3e9b9cb | 2013-09-01 02:05:48 -0500 | [diff] [blame] | 968 | continue; |
| 969 | default: /* OBJ_TAG (nested tags) is already handled */ |
| 970 | warning("Tag points to object of unexpected type %s, skipping.", |
Brandon Williams | debca9d | 2018-02-14 10:59:24 -0800 | [diff] [blame] | 971 | type_name(commit->object.type)); |
Felipe Contreras | 3e9b9cb | 2013-09-01 02:05:48 -0500 | [diff] [blame] | 972 | continue; |
| 973 | } |
| 974 | |
Felipe Contreras | f28e7c9 | 2012-11-28 23:24:00 +0100 | [diff] [blame] | 975 | /* |
Elijah Newren | fdf31b6 | 2018-11-15 23:59:53 -0800 | [diff] [blame] | 976 | * Make sure this ref gets properly updated eventually, whether |
| 977 | * through a commit or manually at the end. |
Felipe Contreras | f28e7c9 | 2012-11-28 23:24:00 +0100 | [diff] [blame] | 978 | */ |
Elijah Newren | fdf31b6 | 2018-11-15 23:59:53 -0800 | [diff] [blame] | 979 | if (e->item->type != OBJ_TAG) |
Felipe Contreras | 1d844ee | 2013-09-01 02:05:47 -0500 | [diff] [blame] | 980 | string_list_append(&extra_refs, full_name)->util = commit; |
Elijah Newren | fdf31b6 | 2018-11-15 23:59:53 -0800 | [diff] [blame] | 981 | |
Nguyễn Thái Ngọc Duy | 87be252 | 2018-05-19 07:28:24 +0200 | [diff] [blame] | 982 | if (!*revision_sources_at(&revision_sources, commit)) |
| 983 | *revision_sources_at(&revision_sources, commit) = full_name; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 984 | } |
Elijah Newren | fdf31b6 | 2018-11-15 23:59:53 -0800 | [diff] [blame] | 985 | |
| 986 | string_list_sort(&extra_refs); |
| 987 | string_list_remove_duplicates(&extra_refs, 0); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 988 | } |
| 989 | |
Elijah Newren | fdf31b6 | 2018-11-15 23:59:53 -0800 | [diff] [blame] | 990 | static void handle_tags_and_duplicates(struct string_list *extras) |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 991 | { |
| 992 | struct commit *commit; |
| 993 | int i; |
| 994 | |
Elijah Newren | fdf31b6 | 2018-11-15 23:59:53 -0800 | [diff] [blame] | 995 | for (i = extras->nr - 1; i >= 0; i--) { |
| 996 | const char *name = extras->items[i].string; |
| 997 | struct object *object = extras->items[i].util; |
| 998 | int mark; |
| 999 | |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 1000 | switch (object->type) { |
| 1001 | case OBJ_TAG: |
| 1002 | handle_tag(name, (struct tag *)object); |
| 1003 | break; |
| 1004 | case OBJ_COMMIT: |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 1005 | if (anonymize) |
| 1006 | name = anonymize_refname(name); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 1007 | /* create refs pointing to already seen commits */ |
Elijah Newren | cd13762 | 2018-11-15 23:59:52 -0800 | [diff] [blame] | 1008 | commit = rewrite_commit((struct commit *)object); |
| 1009 | if (!commit) { |
| 1010 | /* |
| 1011 | * Neither this object nor any of its |
| 1012 | * ancestors touch any relevant paths, so |
| 1013 | * it has been filtered to nothing. Delete |
| 1014 | * it. |
| 1015 | */ |
| 1016 | printf("reset %s\nfrom %s\n\n", |
brian m. carlson | 1422844 | 2021-04-26 01:02:56 +0000 | [diff] [blame] | 1017 | name, oid_to_hex(null_oid())); |
Elijah Newren | cd13762 | 2018-11-15 23:59:52 -0800 | [diff] [blame] | 1018 | continue; |
| 1019 | } |
Elijah Newren | fdf31b6 | 2018-11-15 23:59:53 -0800 | [diff] [blame] | 1020 | |
| 1021 | mark = get_object_mark(&commit->object); |
| 1022 | if (!mark) { |
| 1023 | /* |
| 1024 | * Getting here means we have a commit which |
| 1025 | * was excluded by a negative refspec (e.g. |
Johannes Schindelin | 5a0c32b | 2020-09-21 22:01:22 +0000 | [diff] [blame] | 1026 | * fast-export ^HEAD HEAD). If we are |
Elijah Newren | 530ca19 | 2018-11-15 23:59:54 -0800 | [diff] [blame] | 1027 | * referencing excluded commits, set the ref |
| 1028 | * to the exact commit. Otherwise, the user |
Elijah Newren | fdf31b6 | 2018-11-15 23:59:53 -0800 | [diff] [blame] | 1029 | * wants the branch exported but every commit |
Elijah Newren | 530ca19 | 2018-11-15 23:59:54 -0800 | [diff] [blame] | 1030 | * in its history to be deleted, which basically |
| 1031 | * just means deletion of the ref. |
Elijah Newren | fdf31b6 | 2018-11-15 23:59:53 -0800 | [diff] [blame] | 1032 | */ |
Elijah Newren | 530ca19 | 2018-11-15 23:59:54 -0800 | [diff] [blame] | 1033 | if (!reference_excluded_commits) { |
| 1034 | /* delete the ref */ |
| 1035 | printf("reset %s\nfrom %s\n\n", |
brian m. carlson | 1422844 | 2021-04-26 01:02:56 +0000 | [diff] [blame] | 1036 | name, oid_to_hex(null_oid())); |
Elijah Newren | 530ca19 | 2018-11-15 23:59:54 -0800 | [diff] [blame] | 1037 | continue; |
| 1038 | } |
| 1039 | /* set ref to commit using oid, not mark */ |
| 1040 | printf("reset %s\nfrom %s\n\n", name, |
| 1041 | oid_to_hex(&commit->object.oid)); |
Elijah Newren | fdf31b6 | 2018-11-15 23:59:53 -0800 | [diff] [blame] | 1042 | continue; |
| 1043 | } |
| 1044 | |
| 1045 | printf("reset %s\nfrom :%d\n\n", name, mark |
| 1046 | ); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 1047 | show_progress(); |
| 1048 | break; |
| 1049 | } |
| 1050 | } |
| 1051 | } |
| 1052 | |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 1053 | static void export_marks(char *file) |
| 1054 | { |
| 1055 | unsigned int i; |
| 1056 | uint32_t mark; |
Jonathan Tan | ddd3e31 | 2017-12-07 16:14:24 -0800 | [diff] [blame] | 1057 | struct decoration_entry *deco = idnums.entries; |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 1058 | FILE *f; |
Matthias Andree | 96d69b5 | 2009-07-24 10:17:13 +0200 | [diff] [blame] | 1059 | int e = 0; |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 1060 | |
Johannes Schindelin | ea56518 | 2016-01-11 19:35:54 +0100 | [diff] [blame] | 1061 | f = fopen_for_writing(file); |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 1062 | if (!f) |
Sverre Rabbelier | bb6ad28 | 2010-03-28 00:42:48 -0500 | [diff] [blame] | 1063 | die_errno("Unable to open marks file %s for writing.", file); |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 1064 | |
Junio C Hamano | 6991357 | 2008-07-03 00:25:23 -0700 | [diff] [blame] | 1065 | for (i = 0; i < idnums.size; i++) { |
| 1066 | if (deco->base && deco->base->type == 1) { |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 1067 | mark = ptr_to_mark(deco->decoration); |
Matthias Andree | 96d69b5 | 2009-07-24 10:17:13 +0200 | [diff] [blame] | 1068 | if (fprintf(f, ":%"PRIu32" %s\n", mark, |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 1069 | oid_to_hex(&deco->base->oid)) < 0) { |
Matthias Andree | 96d69b5 | 2009-07-24 10:17:13 +0200 | [diff] [blame] | 1070 | e = 1; |
| 1071 | break; |
| 1072 | } |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 1073 | } |
Junio C Hamano | 6991357 | 2008-07-03 00:25:23 -0700 | [diff] [blame] | 1074 | deco++; |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 1075 | } |
| 1076 | |
Matthias Andree | 96d69b5 | 2009-07-24 10:17:13 +0200 | [diff] [blame] | 1077 | e |= ferror(f); |
| 1078 | e |= fclose(f); |
| 1079 | if (e) |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 1080 | error("Unable to write marks file %s.", file); |
| 1081 | } |
| 1082 | |
Elijah Newren | 208d692 | 2019-10-03 13:27:06 -0700 | [diff] [blame] | 1083 | static void import_marks(char *input_file, int check_exists) |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 1084 | { |
| 1085 | char line[512]; |
Elijah Newren | 208d692 | 2019-10-03 13:27:06 -0700 | [diff] [blame] | 1086 | FILE *f; |
| 1087 | struct stat sb; |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 1088 | |
Elijah Newren | 208d692 | 2019-10-03 13:27:06 -0700 | [diff] [blame] | 1089 | if (check_exists && stat(input_file, &sb)) |
| 1090 | return; |
| 1091 | |
| 1092 | f = xfopen(input_file, "r"); |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 1093 | while (fgets(line, sizeof(line), f)) { |
| 1094 | uint32_t mark; |
| 1095 | char *line_end, *mark_end; |
brian m. carlson | 273f8ee | 2017-02-21 23:47:23 +0000 | [diff] [blame] | 1096 | struct object_id oid; |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 1097 | struct object *object; |
Felipe Contreras | 47bd9bf | 2013-05-05 17:38:54 -0500 | [diff] [blame] | 1098 | struct commit *commit; |
Felipe Contreras | e6812cf | 2013-05-05 17:38:53 -0500 | [diff] [blame] | 1099 | enum object_type type; |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 1100 | |
| 1101 | line_end = strchr(line, '\n'); |
| 1102 | if (line[0] != ':' || !line_end) |
| 1103 | die("corrupt mark line: %s", line); |
Junio C Hamano | 6991357 | 2008-07-03 00:25:23 -0700 | [diff] [blame] | 1104 | *line_end = '\0'; |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 1105 | |
| 1106 | mark = strtoumax(line + 1, &mark_end, 10); |
| 1107 | if (!mark || mark_end == line + 1 |
brian m. carlson | 273f8ee | 2017-02-21 23:47:23 +0000 | [diff] [blame] | 1108 | || *mark_end != ' ' || get_oid_hex(mark_end + 1, &oid)) |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 1109 | die("corrupt mark line: %s", line); |
| 1110 | |
Antoine Pelisse | c4458ec | 2013-04-06 19:04:31 +0200 | [diff] [blame] | 1111 | if (last_idnum < mark) |
| 1112 | last_idnum = mark; |
| 1113 | |
Stefan Beller | 0df8e96 | 2018-04-25 11:20:59 -0700 | [diff] [blame] | 1114 | type = oid_object_info(the_repository, &oid, NULL); |
Felipe Contreras | e6812cf | 2013-05-05 17:38:53 -0500 | [diff] [blame] | 1115 | if (type < 0) |
brian m. carlson | 273f8ee | 2017-02-21 23:47:23 +0000 | [diff] [blame] | 1116 | die("object not found: %s", oid_to_hex(&oid)); |
Felipe Contreras | e6812cf | 2013-05-05 17:38:53 -0500 | [diff] [blame] | 1117 | |
| 1118 | if (type != OBJ_COMMIT) |
| 1119 | /* only commits */ |
Antoine Pelisse | c4458ec | 2013-04-06 19:04:31 +0200 | [diff] [blame] | 1120 | continue; |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 1121 | |
Stefan Beller | c1f5eb4 | 2018-06-28 18:21:59 -0700 | [diff] [blame] | 1122 | commit = lookup_commit(the_repository, &oid); |
Felipe Contreras | 47bd9bf | 2013-05-05 17:38:54 -0500 | [diff] [blame] | 1123 | if (!commit) |
brian m. carlson | 273f8ee | 2017-02-21 23:47:23 +0000 | [diff] [blame] | 1124 | die("not a commit? can't happen: %s", oid_to_hex(&oid)); |
Felipe Contreras | 47bd9bf | 2013-05-05 17:38:54 -0500 | [diff] [blame] | 1125 | |
| 1126 | object = &commit->object; |
Felipe Contreras | e6812cf | 2013-05-05 17:38:53 -0500 | [diff] [blame] | 1127 | |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 1128 | if (object->flags & SHOWN) |
brian m. carlson | 273f8ee | 2017-02-21 23:47:23 +0000 | [diff] [blame] | 1129 | error("Object %s already has a mark", oid_to_hex(&oid)); |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 1130 | |
| 1131 | mark_object(object, mark); |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 1132 | |
| 1133 | object->flags |= SHOWN; |
| 1134 | } |
| 1135 | fclose(f); |
| 1136 | } |
| 1137 | |
Felipe Contreras | 60ed264 | 2014-04-20 13:59:28 -0500 | [diff] [blame] | 1138 | static void handle_deletes(void) |
| 1139 | { |
| 1140 | int i; |
Brandon Williams | 16eefc8 | 2018-05-16 15:57:59 -0700 | [diff] [blame] | 1141 | for (i = 0; i < refspecs.nr; i++) { |
| 1142 | struct refspec_item *refspec = &refspecs.items[i]; |
Felipe Contreras | 60ed264 | 2014-04-20 13:59:28 -0500 | [diff] [blame] | 1143 | if (*refspec->src) |
| 1144 | continue; |
| 1145 | |
| 1146 | printf("reset %s\nfrom %s\n\n", |
brian m. carlson | 1422844 | 2021-04-26 01:02:56 +0000 | [diff] [blame] | 1147 | refspec->dst, oid_to_hex(null_oid())); |
Felipe Contreras | 60ed264 | 2014-04-20 13:59:28 -0500 | [diff] [blame] | 1148 | } |
| 1149 | } |
| 1150 | |
Jeff King | 65b5d9f | 2020-06-25 15:48:32 -0400 | [diff] [blame] | 1151 | static int parse_opt_anonymize_map(const struct option *opt, |
| 1152 | const char *arg, int unset) |
| 1153 | { |
| 1154 | struct hashmap *map = opt->value; |
| 1155 | const char *delim, *value; |
| 1156 | size_t keylen; |
| 1157 | |
| 1158 | BUG_ON_OPT_NEG(unset); |
| 1159 | |
| 1160 | delim = strchr(arg, ':'); |
| 1161 | if (delim) { |
| 1162 | keylen = delim - arg; |
| 1163 | value = delim + 1; |
| 1164 | } else { |
| 1165 | keylen = strlen(arg); |
| 1166 | value = arg; |
| 1167 | } |
| 1168 | |
| 1169 | if (!keylen || !*value) |
| 1170 | return error(_("--anonymize-map token cannot be empty")); |
| 1171 | |
Jeff King | aa54845 | 2023-03-22 13:42:13 -0400 | [diff] [blame] | 1172 | add_anonymized_entry(map, memhash(arg, keylen), arg, keylen, |
| 1173 | xstrdup(value)); |
Jeff King | 65b5d9f | 2020-06-25 15:48:32 -0400 | [diff] [blame] | 1174 | |
| 1175 | return 0; |
| 1176 | } |
| 1177 | |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 1178 | int cmd_fast_export(int argc, const char **argv, const char *prefix) |
| 1179 | { |
| 1180 | struct rev_info revs; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 1181 | struct commit *commit; |
Elijah Newren | 208d692 | 2019-10-03 13:27:06 -0700 | [diff] [blame] | 1182 | char *export_filename = NULL, |
| 1183 | *import_filename = NULL, |
| 1184 | *import_filename_if_exists = NULL; |
Antoine Pelisse | c4458ec | 2013-04-06 19:04:31 +0200 | [diff] [blame] | 1185 | uint32_t lastimportid; |
Felipe Contreras | 03e9010 | 2014-04-20 13:59:24 -0500 | [diff] [blame] | 1186 | struct string_list refspecs_list = STRING_LIST_INIT_NODUP; |
Jonathan Tan | b3e8ca8 | 2017-09-20 16:55:02 -0700 | [diff] [blame] | 1187 | struct string_list paths_of_changed_objects = STRING_LIST_INIT_DUP; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 1188 | struct option options[] = { |
| 1189 | OPT_INTEGER(0, "progress", &progress, |
Nguyễn Thái Ngọc Duy | 3b787b9 | 2012-08-20 19:32:08 +0700 | [diff] [blame] | 1190 | N_("show progress after <n> objects")), |
| 1191 | OPT_CALLBACK(0, "signed-tags", &signed_tag_mode, N_("mode"), |
| 1192 | N_("select handling of signed tags"), |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 1193 | parse_opt_signed_tag_mode), |
Nguyễn Thái Ngọc Duy | 3b787b9 | 2012-08-20 19:32:08 +0700 | [diff] [blame] | 1194 | OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode, N_("mode"), |
| 1195 | N_("select handling of tags that tag filtered objects"), |
Elijah Newren | 2d8ad46 | 2009-06-25 22:48:31 -0600 | [diff] [blame] | 1196 | parse_opt_tag_of_filtered_mode), |
Elijah Newren | e80001f | 2019-05-13 21:31:02 -0700 | [diff] [blame] | 1197 | OPT_CALLBACK(0, "reencode", &reencode_mode, N_("mode"), |
| 1198 | N_("select handling of commit messages in an alternate encoding"), |
| 1199 | parse_opt_reencode_mode), |
Nguyễn Thái Ngọc Duy | 3b787b9 | 2012-08-20 19:32:08 +0700 | [diff] [blame] | 1200 | OPT_STRING(0, "export-marks", &export_filename, N_("file"), |
ZheNing Hu | e73fe3d | 2021-01-06 14:44:03 +0000 | [diff] [blame] | 1201 | N_("dump marks to this file")), |
Nguyễn Thái Ngọc Duy | 3b787b9 | 2012-08-20 19:32:08 +0700 | [diff] [blame] | 1202 | OPT_STRING(0, "import-marks", &import_filename, N_("file"), |
ZheNing Hu | e73fe3d | 2021-01-06 14:44:03 +0000 | [diff] [blame] | 1203 | N_("import marks from this file")), |
Elijah Newren | 208d692 | 2019-10-03 13:27:06 -0700 | [diff] [blame] | 1204 | OPT_STRING(0, "import-marks-if-exists", |
| 1205 | &import_filename_if_exists, |
| 1206 | N_("file"), |
ZheNing Hu | e73fe3d | 2021-01-06 14:44:03 +0000 | [diff] [blame] | 1207 | N_("import marks from this file if it exists")), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 1208 | OPT_BOOL(0, "fake-missing-tagger", &fake_missing_tagger, |
ZheNing Hu | e73fe3d | 2021-01-06 14:44:03 +0000 | [diff] [blame] | 1209 | N_("fake a tagger when tags lack one")), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 1210 | OPT_BOOL(0, "full-tree", &full_tree, |
ZheNing Hu | e73fe3d | 2021-01-06 14:44:03 +0000 | [diff] [blame] | 1211 | N_("output full tree for each commit")), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 1212 | OPT_BOOL(0, "use-done-feature", &use_done_feature, |
ZheNing Hu | e73fe3d | 2021-01-06 14:44:03 +0000 | [diff] [blame] | 1213 | N_("use the done feature to terminate the stream")), |
| 1214 | 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] | 1215 | OPT_STRING_LIST(0, "refspec", &refspecs_list, N_("refspec"), |
ZheNing Hu | e73fe3d | 2021-01-06 14:44:03 +0000 | [diff] [blame] | 1216 | N_("apply refspec to exported refs")), |
Jeff King | a872275 | 2014-08-27 13:01:28 -0400 | [diff] [blame] | 1217 | OPT_BOOL(0, "anonymize", &anonymize, N_("anonymize output")), |
Jeff King | 65b5d9f | 2020-06-25 15:48:32 -0400 | [diff] [blame] | 1218 | OPT_CALLBACK_F(0, "anonymize-map", &anonymized_seeds, N_("from:to"), |
| 1219 | N_("convert <from> to <to> in anonymized output"), |
| 1220 | PARSE_OPT_NONEG, parse_opt_anonymize_map), |
Elijah Newren | 530ca19 | 2018-11-15 23:59:54 -0800 | [diff] [blame] | 1221 | OPT_BOOL(0, "reference-excluded-parents", |
ZheNing Hu | e73fe3d | 2021-01-06 14:44:03 +0000 | [diff] [blame] | 1222 | &reference_excluded_commits, N_("reference parents which are not in fast-export stream by object id")), |
Elijah Newren | a965bb3 | 2018-11-15 23:59:56 -0800 | [diff] [blame] | 1223 | OPT_BOOL(0, "show-original-ids", &show_original_ids, |
ZheNing Hu | e73fe3d | 2021-01-06 14:44:03 +0000 | [diff] [blame] | 1224 | N_("show original object ids of blobs/commits")), |
Elijah Newren | a1638cf | 2019-10-03 13:27:07 -0700 | [diff] [blame] | 1225 | OPT_BOOL(0, "mark-tags", &mark_tags, |
ZheNing Hu | e73fe3d | 2021-01-06 14:44:03 +0000 | [diff] [blame] | 1226 | N_("label tags with mark ids")), |
Elijah Newren | 530ca19 | 2018-11-15 23:59:54 -0800 | [diff] [blame] | 1227 | |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 1228 | OPT_END() |
| 1229 | }; |
| 1230 | |
Miklos Vajna | dcfdbdf | 2009-01-03 04:59:12 +0100 | [diff] [blame] | 1231 | if (argc == 1) |
| 1232 | usage_with_options (fast_export_usage, options); |
| 1233 | |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 1234 | /* we handle encodings */ |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 1235 | git_config(git_default_config, NULL); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 1236 | |
Nguyễn Thái Ngọc Duy | 2abf350 | 2018-09-21 17:57:38 +0200 | [diff] [blame] | 1237 | repo_init_revisions(the_repository, &revs, prefix); |
Nguyễn Thái Ngọc Duy | 87be252 | 2018-05-19 07:28:24 +0200 | [diff] [blame] | 1238 | init_revision_sources(&revision_sources); |
Elijah Newren | 668f3aa | 2009-06-25 22:48:27 -0600 | [diff] [blame] | 1239 | revs.topo_order = 1; |
Nguyễn Thái Ngọc Duy | 87be252 | 2018-05-19 07:28:24 +0200 | [diff] [blame] | 1240 | revs.sources = &revision_sources; |
Elijah Newren | 3216413 | 2009-06-25 22:48:30 -0600 | [diff] [blame] | 1241 | revs.rewrite_parents = 1; |
Felipe Contreras | 8b2f86a | 2014-04-20 13:59:23 -0500 | [diff] [blame] | 1242 | argc = parse_options(argc, argv, prefix, options, fast_export_usage, |
SZEDER Gábor | 99d86d6 | 2022-08-19 18:03:57 +0200 | [diff] [blame] | 1243 | PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN_OPT); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 1244 | argc = setup_revisions(argc, argv, &revs, NULL); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 1245 | if (argc > 1) |
| 1246 | usage_with_options (fast_export_usage, options); |
| 1247 | |
Jeff King | 65b5d9f | 2020-06-25 15:48:32 -0400 | [diff] [blame] | 1248 | if (anonymized_seeds.cmpfn && !anonymize) |
Jean-Noël Avila | 6fa00ee | 2022-01-05 20:02:19 +0000 | [diff] [blame] | 1249 | die(_("the option '%s' requires '%s'"), "--anonymize-map", "--anonymize"); |
Jeff King | 65b5d9f | 2020-06-25 15:48:32 -0400 | [diff] [blame] | 1250 | |
Felipe Contreras | 03e9010 | 2014-04-20 13:59:24 -0500 | [diff] [blame] | 1251 | if (refspecs_list.nr) { |
Felipe Contreras | 03e9010 | 2014-04-20 13:59:24 -0500 | [diff] [blame] | 1252 | int i; |
| 1253 | |
Felipe Contreras | 03e9010 | 2014-04-20 13:59:24 -0500 | [diff] [blame] | 1254 | for (i = 0; i < refspecs_list.nr; i++) |
Brandon Williams | 16eefc8 | 2018-05-16 15:57:59 -0700 | [diff] [blame] | 1255 | refspec_append(&refspecs, refspecs_list.items[i].string); |
Felipe Contreras | 03e9010 | 2014-04-20 13:59:24 -0500 | [diff] [blame] | 1256 | |
| 1257 | string_list_clear(&refspecs_list, 1); |
Felipe Contreras | 03e9010 | 2014-04-20 13:59:24 -0500 | [diff] [blame] | 1258 | } |
| 1259 | |
Sverre Rabbelier | 82670a5 | 2011-07-16 15:03:33 +0200 | [diff] [blame] | 1260 | if (use_done_feature) |
| 1261 | printf("feature done\n"); |
| 1262 | |
Elijah Newren | 208d692 | 2019-10-03 13:27:06 -0700 | [diff] [blame] | 1263 | if (import_filename && import_filename_if_exists) |
Jean-Noël Avila | 12909b6 | 2022-01-05 20:02:16 +0000 | [diff] [blame] | 1264 | die(_("options '%s' and '%s' cannot be used together"), "--import-marks", "--import-marks-if-exists"); |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 1265 | if (import_filename) |
Elijah Newren | 208d692 | 2019-10-03 13:27:06 -0700 | [diff] [blame] | 1266 | import_marks(import_filename, 0); |
| 1267 | else if (import_filename_if_exists) |
| 1268 | import_marks(import_filename_if_exists, 1); |
Antoine Pelisse | c4458ec | 2013-04-06 19:04:31 +0200 | [diff] [blame] | 1269 | lastimportid = last_idnum; |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 1270 | |
Nguyễn Thái Ngọc Duy | afe069d | 2010-12-17 19:43:06 +0700 | [diff] [blame] | 1271 | if (import_filename && revs.prune_data.nr) |
Elijah Newren | 4087a02 | 2010-07-17 11:00:50 -0600 | [diff] [blame] | 1272 | full_tree = 1; |
| 1273 | |
Felipe Contreras | 1d844ee | 2013-09-01 02:05:47 -0500 | [diff] [blame] | 1274 | get_tags_and_duplicates(&revs.cmdline); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 1275 | |
Martin Koegler | 3d51e1b | 2008-02-18 08:31:56 +0100 | [diff] [blame] | 1276 | if (prepare_revision_walk(&revs)) |
| 1277 | die("revision walk setup failed"); |
William Sprent | 726a228 | 2021-12-16 16:23:09 +0000 | [diff] [blame] | 1278 | |
| 1279 | revs.reverse = 1; |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 1280 | revs.diffopt.format_callback = show_filemodify; |
Jonathan Tan | b3e8ca8 | 2017-09-20 16:55:02 -0700 | [diff] [blame] | 1281 | revs.diffopt.format_callback_data = &paths_of_changed_objects; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 1282 | revs.diffopt.flags.recursive = 1; |
René Scharfe | d1c2527 | 2022-04-30 16:31:43 +0200 | [diff] [blame] | 1283 | revs.diffopt.no_free = 1; |
William Sprent | 726a228 | 2021-12-16 16:23:09 +0000 | [diff] [blame] | 1284 | while ((commit = get_revision(&revs))) |
| 1285 | handle_commit(commit, &revs, &paths_of_changed_objects); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 1286 | |
Elijah Newren | fdf31b6 | 2018-11-15 23:59:53 -0800 | [diff] [blame] | 1287 | handle_tags_and_duplicates(&extra_refs); |
| 1288 | handle_tags_and_duplicates(&tag_refs); |
Felipe Contreras | 60ed264 | 2014-04-20 13:59:28 -0500 | [diff] [blame] | 1289 | handle_deletes(); |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 1290 | |
Antoine Pelisse | c4458ec | 2013-04-06 19:04:31 +0200 | [diff] [blame] | 1291 | if (export_filename && lastimportid != last_idnum) |
Pieter de Bie | df6a7ff | 2008-06-11 13:17:04 +0200 | [diff] [blame] | 1292 | export_marks(export_filename); |
| 1293 | |
Sverre Rabbelier | 82670a5 | 2011-07-16 15:03:33 +0200 | [diff] [blame] | 1294 | if (use_done_feature) |
| 1295 | printf("done\n"); |
| 1296 | |
Brandon Williams | 16eefc8 | 2018-05-16 15:57:59 -0700 | [diff] [blame] | 1297 | refspec_clear(&refspecs); |
Ævar Arnfjörð Bjarmason | 2108fe4 | 2022-04-13 22:01:36 +0200 | [diff] [blame] | 1298 | release_revisions(&revs); |
Felipe Contreras | 03e9010 | 2014-04-20 13:59:24 -0500 | [diff] [blame] | 1299 | |
Johannes Schindelin | f2dc849 | 2007-12-02 14:14:13 +0000 | [diff] [blame] | 1300 | return 0; |
| 1301 | } |