Skip to content

Commit 023731f

Browse files
committed
Merge branch 'en/fast-imexport-nested-tags' into pu
Updates to fast-import/export. * en/fast-imexport-nested-tags: fast-export: handle nested tags t9350: add tests for tags of things other than a commit fast-export: allow user to request tags be marked with --mark-tags fast-export: add support for --import-marks-if-exists fast-import: add support for new 'alias' command fast-import: allow tags to be identified by mark labels fast-import: fix handling of deleted tags fast-export: fix exporting a tag and nothing else
2 parents 88d7a0f + 0e2eeb4 commit 023731f

File tree

6 files changed

+268
-38
lines changed

6 files changed

+268
-38
lines changed

Documentation/git-fast-export.txt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,20 @@ produced incorrect results if you gave these options.
7575
Before processing any input, load the marks specified in
7676
<file>. The input file must exist, must be readable, and
7777
must use the same format as produced by --export-marks.
78+
79+
--mark-tags::
80+
In addition to labelling blobs and commits with mark ids, also
81+
label tags. This is useful in conjunction with
82+
`--export-marks` and `--import-marks`, and is also useful (and
83+
necessary) for exporting of nested tags. It does not hurt
84+
other cases and would be the default, but many fast-import
85+
frontends are not prepared to accept tags with mark
86+
identifiers.
7887
+
79-
Any commits that have already been marked will not be exported again.
80-
If the backend uses a similar --import-marks file, this allows for
81-
incremental bidirectional exporting of the repository by keeping the
82-
marks the same across runs.
88+
Any commits (or tags) that have already been marked will not be
89+
exported again. If the backend uses a similar --import-marks file,
90+
this allows for incremental bidirectional exporting of the repository
91+
by keeping the marks the same across runs.
8392

8493
--fake-missing-tagger::
8594
Some old repositories have tags without a tagger. The

Documentation/git-fast-import.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,13 @@ and control the current import process. More detailed discussion
337337
`commit` command. This command is optional and is not
338338
needed to perform an import.
339339

340+
`alias`::
341+
Record that a mark refers to a given object without first
342+
creating any new object. Using --import-marks and referring
343+
to missing marks will cause fast-import to fail, so aliases
344+
can provide a way to set otherwise pruned commits to a valid
345+
value (e.g. the nearest non-pruned ancestor).
346+
340347
`checkpoint`::
341348
Forces fast-import to close the current packfile, generate its
342349
unique SHA-1 checksum and index, and start a new packfile.
@@ -774,6 +781,7 @@ lightweight (non-annotated) tags see the `reset` command below.
774781

775782
....
776783
'tag' SP <name> LF
784+
mark?
777785
'from' SP <commit-ish> LF
778786
original-oid?
779787
'tagger' (SP <name>)? SP LT <email> GT SP <when> LF
@@ -913,6 +921,21 @@ a data chunk which does not have an LF as its last byte.
913921
+
914922
The `LF` after `<delim> LF` is optional (it used to be required).
915923

924+
`alias`
925+
~~~~~~~
926+
Record that a mark refers to a given object without first creating any
927+
new object.
928+
929+
....
930+
'alias' LF
931+
mark
932+
'to' SP <commit-ish> LF
933+
LF?
934+
....
935+
936+
For a detailed description of `<commit-ish>` see above under `from`.
937+
938+
916939
`checkpoint`
917940
~~~~~~~~~~~~
918941
Forces fast-import to close the current packfile, start a new one, and to

builtin/fast-export.c

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ static int no_data;
4040
static int full_tree;
4141
static int reference_excluded_commits;
4242
static int show_original_ids;
43+
static int mark_tags;
4344
static struct string_list extra_refs = STRING_LIST_INIT_NODUP;
4445
static struct string_list tag_refs = STRING_LIST_INIT_NODUP;
4546
static struct refspec refspecs = REFSPEC_INIT_FETCH;
@@ -847,25 +848,40 @@ static void handle_tag(const char *name, struct tag *tag)
847848
free(buf);
848849
return;
849850
case REWRITE:
850-
if (tagged->type != OBJ_COMMIT) {
851-
die("tag %s tags unexported %s!",
852-
oid_to_hex(&tag->object.oid),
853-
type_name(tagged->type));
854-
}
855-
p = rewrite_commit((struct commit *)tagged);
856-
if (!p) {
857-
printf("reset %s\nfrom %s\n\n",
858-
name, oid_to_hex(&null_oid));
859-
free(buf);
860-
return;
851+
if (tagged->type == OBJ_TAG && !mark_tags) {
852+
die(_("Error: Cannot export nested tags unless --mark-tags is specified."));
853+
} else if (tagged->type == OBJ_COMMIT) {
854+
p = rewrite_commit((struct commit *)tagged);
855+
if (!p) {
856+
printf("reset %s\nfrom %s\n\n",
857+
name, oid_to_hex(&null_oid));
858+
free(buf);
859+
return;
860+
}
861+
tagged_mark = get_object_mark(&p->object);
862+
} else {
863+
/* tagged->type is either OBJ_BLOB or OBJ_TAG */
864+
tagged_mark = get_object_mark(tagged);
861865
}
862-
tagged_mark = get_object_mark(&p->object);
863866
}
864867
}
865868

869+
if (tagged->type == OBJ_TAG) {
870+
printf("reset %s\nfrom %s\n\n",
871+
name, oid_to_hex(&null_oid));
872+
}
866873
if (starts_with(name, "refs/tags/"))
867874
name += 10;
868-
printf("tag %s\nfrom :%d\n", name, tagged_mark);
875+
printf("tag %s\n", name);
876+
if (mark_tags) {
877+
mark_next_object(&tag->object);
878+
printf("mark :%"PRIu32"\n", last_idnum);
879+
}
880+
if (tagged_mark)
881+
printf("from :%d\n", tagged_mark);
882+
else
883+
printf("from %s\n", oid_to_hex(&tagged->oid));
884+
869885
if (show_original_ids)
870886
printf("original-oid %s\n", oid_to_hex(&tag->object.oid));
871887
printf("%.*s%sdata %d\n%.*s\n",
@@ -1052,11 +1068,16 @@ static void export_marks(char *file)
10521068
error("Unable to write marks file %s.", file);
10531069
}
10541070

1055-
static void import_marks(char *input_file)
1071+
static void import_marks(char *input_file, int check_exists)
10561072
{
10571073
char line[512];
1058-
FILE *f = xfopen(input_file, "r");
1074+
FILE *f;
1075+
struct stat sb;
1076+
1077+
if (check_exists && stat(input_file, &sb))
1078+
return;
10591079

1080+
f = xfopen(input_file, "r");
10601081
while (fgets(line, sizeof(line), f)) {
10611082
uint32_t mark;
10621083
char *line_end, *mark_end;
@@ -1120,7 +1141,9 @@ int cmd_fast_export(int argc, const char **argv, const char *prefix)
11201141
struct rev_info revs;
11211142
struct object_array commits = OBJECT_ARRAY_INIT;
11221143
struct commit *commit;
1123-
char *export_filename = NULL, *import_filename = NULL;
1144+
char *export_filename = NULL,
1145+
*import_filename = NULL,
1146+
*import_filename_if_exists = NULL;
11241147
uint32_t lastimportid;
11251148
struct string_list refspecs_list = STRING_LIST_INIT_NODUP;
11261149
struct string_list paths_of_changed_objects = STRING_LIST_INIT_DUP;
@@ -1140,6 +1163,10 @@ int cmd_fast_export(int argc, const char **argv, const char *prefix)
11401163
N_("Dump marks to this file")),
11411164
OPT_STRING(0, "import-marks", &import_filename, N_("file"),
11421165
N_("Import marks from this file")),
1166+
OPT_STRING(0, "import-marks-if-exists",
1167+
&import_filename_if_exists,
1168+
N_("file"),
1169+
N_("Import marks from this file if it exists")),
11431170
OPT_BOOL(0, "fake-missing-tagger", &fake_missing_tagger,
11441171
N_("Fake a tagger when tags lack one")),
11451172
OPT_BOOL(0, "full-tree", &full_tree,
@@ -1154,6 +1181,8 @@ int cmd_fast_export(int argc, const char **argv, const char *prefix)
11541181
&reference_excluded_commits, N_("Reference parents which are not in fast-export stream by object id")),
11551182
OPT_BOOL(0, "show-original-ids", &show_original_ids,
11561183
N_("Show original object ids of blobs/commits")),
1184+
OPT_BOOL(0, "mark-tags", &mark_tags,
1185+
N_("Label tags with mark ids")),
11571186

11581187
OPT_END()
11591188
};
@@ -1187,8 +1216,12 @@ int cmd_fast_export(int argc, const char **argv, const char *prefix)
11871216
if (use_done_feature)
11881217
printf("feature done\n");
11891218

1219+
if (import_filename && import_filename_if_exists)
1220+
die(_("Cannot pass both --import-marks and --import-marks-if-exists"));
11901221
if (import_filename)
1191-
import_marks(import_filename);
1222+
import_marks(import_filename, 0);
1223+
else if (import_filename_if_exists)
1224+
import_marks(import_filename_if_exists, 1);
11921225
lastimportid = last_idnum;
11931226

11941227
if (import_filename && revs.prune_data.nr)

fast-import.c

Lines changed: 83 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2489,27 +2489,23 @@ static void parse_from_existing(struct branch *b)
24892489
}
24902490
}
24912491

2492-
static int parse_from(struct branch *b)
2492+
static int parse_objectish(struct branch *b, const char *objectish)
24932493
{
2494-
const char *from;
24952494
struct branch *s;
24962495
struct object_id oid;
24972496

2498-
if (!skip_prefix(command_buf.buf, "from ", &from))
2499-
return 0;
2500-
25012497
oidcpy(&oid, &b->branch_tree.versions[1].oid);
25022498

2503-
s = lookup_branch(from);
2499+
s = lookup_branch(objectish);
25042500
if (b == s)
25052501
die("Can't create a branch from itself: %s", b->name);
25062502
else if (s) {
25072503
struct object_id *t = &s->branch_tree.versions[1].oid;
25082504
oidcpy(&b->oid, &s->oid);
25092505
oidcpy(&b->branch_tree.versions[0].oid, t);
25102506
oidcpy(&b->branch_tree.versions[1].oid, t);
2511-
} else if (*from == ':') {
2512-
uintmax_t idnum = parse_mark_ref_eol(from);
2507+
} else if (*objectish == ':') {
2508+
uintmax_t idnum = parse_mark_ref_eol(objectish);
25132509
struct object_entry *oe = find_mark(idnum);
25142510
if (oe->type != OBJ_COMMIT)
25152511
die("Mark :%" PRIuMAX " not a commit", idnum);
@@ -2523,13 +2519,13 @@ static int parse_from(struct branch *b)
25232519
} else
25242520
parse_from_existing(b);
25252521
}
2526-
} else if (!get_oid(from, &b->oid)) {
2522+
} else if (!get_oid(objectish, &b->oid)) {
25272523
parse_from_existing(b);
25282524
if (is_null_oid(&b->oid))
25292525
b->delete = 1;
25302526
}
25312527
else
2532-
die("Invalid ref name or SHA1 expression: %s", from);
2528+
die("Invalid ref name or SHA1 expression: %s", objectish);
25332529

25342530
if (b->branch_tree.tree && !oideq(&oid, &b->branch_tree.versions[1].oid)) {
25352531
release_tree_content_recursive(b->branch_tree.tree);
@@ -2540,6 +2536,26 @@ static int parse_from(struct branch *b)
25402536
return 1;
25412537
}
25422538

2539+
static int parse_from(struct branch *b)
2540+
{
2541+
const char *from;
2542+
2543+
if (!skip_prefix(command_buf.buf, "from ", &from))
2544+
return 0;
2545+
2546+
return parse_objectish(b, from);
2547+
}
2548+
2549+
static int parse_objectish_with_prefix(struct branch *b, const char *prefix)
2550+
{
2551+
const char *base;
2552+
2553+
if (!skip_prefix(command_buf.buf, prefix, &base))
2554+
return 0;
2555+
2556+
return parse_objectish(b, base);
2557+
}
2558+
25432559
static struct hash_list *parse_merge(unsigned int *count)
25442560
{
25452561
struct hash_list *list = NULL, **tail = &list, *n;
@@ -2714,6 +2730,7 @@ static void parse_new_tag(const char *arg)
27142730
first_tag = t;
27152731
last_tag = t;
27162732
read_next_command();
2733+
parse_mark();
27172734

27182735
/* from ... */
27192736
if (!skip_prefix(command_buf.buf, "from ", &from))
@@ -2770,7 +2787,7 @@ static void parse_new_tag(const char *arg)
27702787
strbuf_addbuf(&new_data, &msg);
27712788
free(tagger);
27722789

2773-
if (store_object(OBJ_TAG, &new_data, NULL, &t->oid, 0))
2790+
if (store_object(OBJ_TAG, &new_data, NULL, &t->oid, next_mark))
27742791
t->pack_id = MAX_PACK_ID;
27752792
else
27762793
t->pack_id = pack_id;
@@ -2794,6 +2811,35 @@ static void parse_reset_branch(const char *arg)
27942811
b = new_branch(arg);
27952812
read_next_command();
27962813
parse_from(b);
2814+
if (b->delete && !strncmp(b->name, "refs/tags/", 10)) {
2815+
/*
2816+
* Elsewhere, we call dump_branches() before dump_tags(),
2817+
* and dump_branches() will handle ref deletions first, so
2818+
* in order to make sure the deletion actually takes effect,
2819+
* we need to remove the tag from our list of tags to update.
2820+
*
2821+
* NEEDSWORK: replace list of tags with hashmap for faster
2822+
* deletion?
2823+
*/
2824+
struct strbuf tag_name = STRBUF_INIT;
2825+
struct tag *t, *prev = NULL;
2826+
for (t = first_tag; t; t = t->next_tag) {
2827+
strbuf_reset(&tag_name);
2828+
strbuf_addf(&tag_name, "refs/tags/%s", t->name);
2829+
if (!strcmp(b->name, tag_name.buf))
2830+
break;
2831+
prev = t;
2832+
}
2833+
if (t) {
2834+
if (prev)
2835+
prev->next_tag = t->next_tag;
2836+
else
2837+
first_tag = t->next_tag;
2838+
if (!t->next_tag)
2839+
last_tag = prev;
2840+
/* There is no mem_pool_free(t) function to call. */
2841+
}
2842+
}
27972843
if (command_buf.len > 0)
27982844
unread_command_buf = 1;
27992845
}
@@ -3060,6 +3106,28 @@ static void parse_progress(void)
30603106
skip_optional_lf();
30613107
}
30623108

3109+
static void parse_alias(void)
3110+
{
3111+
struct object_entry *e;
3112+
struct branch b;
3113+
3114+
skip_optional_lf();
3115+
read_next_command();
3116+
3117+
/* mark ... */
3118+
parse_mark();
3119+
if (!next_mark)
3120+
die(_("Expected 'mark' command, got %s"), command_buf.buf);
3121+
3122+
/* to ... */
3123+
memset(&b, 0, sizeof(b));
3124+
if (!parse_objectish_with_prefix(&b, "to "))
3125+
die(_("Expected 'to' command, got %s"), command_buf.buf);
3126+
e = find_object(&b.oid);
3127+
assert(e);
3128+
insert_mark(next_mark, e);
3129+
}
3130+
30633131
static char* make_fast_import_path(const char *path)
30643132
{
30653133
if (!relative_marks_paths || is_absolute_path(path))
@@ -3187,6 +3255,8 @@ static int parse_one_feature(const char *feature, int from_stream)
31873255
option_import_marks(arg, from_stream, 1);
31883256
} else if (skip_prefix(feature, "export-marks=", &arg)) {
31893257
option_export_marks(arg);
3258+
} else if (!strcmp(feature, "alias")) {
3259+
; /* Don't die - this feature is supported */
31903260
} else if (!strcmp(feature, "get-mark")) {
31913261
; /* Don't die - this feature is supported */
31923262
} else if (!strcmp(feature, "cat-blob")) {
@@ -3343,6 +3413,8 @@ int cmd_main(int argc, const char **argv)
33433413
parse_checkpoint();
33443414
else if (!strcmp("done", command_buf.buf))
33453415
break;
3416+
else if (!strcmp("alias", command_buf.buf))
3417+
parse_alias();
33463418
else if (starts_with(command_buf.buf, "progress "))
33473419
parse_progress();
33483420
else if (skip_prefix(command_buf.buf, "feature ", &v))

0 commit comments

Comments
 (0)