Skip to content

Commit f45fa2a

Browse files
author
Junio C Hamano
committed
Merge branch 'master' of git://repo.or.cz/git/fastimport
* 'master' of git://repo.or.cz/git/fastimport: Allow fast-import frontends to reload the marks table Use atomic updates to the fast-import mark file Preallocate memory earlier in fast-import
2 parents 05ef58e + e843842 commit f45fa2a

File tree

3 files changed

+88
-18
lines changed

3 files changed

+88
-18
lines changed

Documentation/git-fast-import.txt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,18 @@ OPTIONS
6262
Dumps the internal marks table to <file> when complete.
6363
Marks are written one per line as `:markid SHA-1`.
6464
Frontends can use this file to validate imports after they
65-
have been completed.
65+
have been completed, or to save the marks table across
66+
incremental runs. As <file> is only opened and truncated
67+
at checkpoint (or completion) the same path can also be
68+
safely given to \--import-marks.
69+
70+
--import-marks=<file>::
71+
Before processing any input, load the marks specified in
72+
<file>. The input file must exist, must be readable, and
73+
must use the same format as produced by \--export-marks.
74+
Multiple options may be supplied to import more than one
75+
set of marks. If a mark is defined to different values,
76+
the last file wins.
6677

6778
--export-pack-edges=<file>::
6879
After creating a packfile, print a line of data to

fast-import.c

Lines changed: 68 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,16 +1371,33 @@ static void dump_marks_helper(FILE *f,
13711371

13721372
static void dump_marks(void)
13731373
{
1374-
if (mark_file)
1375-
{
1376-
FILE *f = fopen(mark_file, "w");
1377-
if (f) {
1378-
dump_marks_helper(f, 0, marks);
1379-
fclose(f);
1380-
} else
1381-
failure |= error("Unable to write marks file %s: %s",
1382-
mark_file, strerror(errno));
1374+
static struct lock_file mark_lock;
1375+
int mark_fd;
1376+
FILE *f;
1377+
1378+
if (!mark_file)
1379+
return;
1380+
1381+
mark_fd = hold_lock_file_for_update(&mark_lock, mark_file, 0);
1382+
if (mark_fd < 0) {
1383+
failure |= error("Unable to write marks file %s: %s",
1384+
mark_file, strerror(errno));
1385+
return;
1386+
}
1387+
1388+
f = fdopen(mark_fd, "w");
1389+
if (!f) {
1390+
rollback_lock_file(&mark_lock);
1391+
failure |= error("Unable to write marks file %s: %s",
1392+
mark_file, strerror(errno));
1393+
return;
13831394
}
1395+
1396+
dump_marks_helper(f, 0, marks);
1397+
fclose(f);
1398+
if (commit_lock_file(&mark_lock))
1399+
failure |= error("Unable to write marks file %s: %s",
1400+
mark_file, strerror(errno));
13841401
}
13851402

13861403
static void read_next_command(void)
@@ -1976,6 +1993,40 @@ static void cmd_checkpoint(void)
19761993
read_next_command();
19771994
}
19781995

1996+
static void import_marks(const char *input_file)
1997+
{
1998+
char line[512];
1999+
FILE *f = fopen(input_file, "r");
2000+
if (!f)
2001+
die("cannot read %s: %s", input_file, strerror(errno));
2002+
while (fgets(line, sizeof(line), f)) {
2003+
uintmax_t mark;
2004+
char *end;
2005+
unsigned char sha1[20];
2006+
struct object_entry *e;
2007+
2008+
end = strchr(line, '\n');
2009+
if (line[0] != ':' || !end)
2010+
die("corrupt mark line: %s", line);
2011+
*end = 0;
2012+
mark = strtoumax(line + 1, &end, 10);
2013+
if (!mark || end == line + 1
2014+
|| *end != ' ' || get_sha1(end + 1, sha1))
2015+
die("corrupt mark line: %s", line);
2016+
e = find_object(sha1);
2017+
if (!e) {
2018+
enum object_type type = sha1_object_info(sha1, NULL);
2019+
if (type < 0)
2020+
die("object not found: %s", sha1_to_hex(sha1));
2021+
e = insert_object(sha1);
2022+
e->type = type;
2023+
e->pack_id = MAX_PACK_ID;
2024+
}
2025+
insert_mark(mark, e);
2026+
}
2027+
fclose(f);
2028+
}
2029+
19792030
static const char fast_import_usage[] =
19802031
"git-fast-import [--date-format=f] [--max-pack-size=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file]";
19812032

@@ -1984,6 +2035,12 @@ int main(int argc, const char **argv)
19842035
int i, show_stats = 1;
19852036

19862037
git_config(git_default_config);
2038+
alloc_objects(object_entry_alloc);
2039+
strbuf_init(&command_buf);
2040+
atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
2041+
branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
2042+
avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
2043+
marks = pool_calloc(1, sizeof(struct mark_set));
19872044

19882045
for (i = 1; i < argc; i++) {
19892046
const char *a = argv[i];
@@ -2007,6 +2064,8 @@ int main(int argc, const char **argv)
20072064
max_depth = strtoul(a + 8, NULL, 0);
20082065
else if (!prefixcmp(a, "--active-branches="))
20092066
max_active_branches = strtoul(a + 18, NULL, 0);
2067+
else if (!prefixcmp(a, "--import-marks="))
2068+
import_marks(a + 15);
20102069
else if (!prefixcmp(a, "--export-marks="))
20112070
mark_file = a + 15;
20122071
else if (!prefixcmp(a, "--export-pack-edges=")) {
@@ -2027,14 +2086,6 @@ int main(int argc, const char **argv)
20272086
if (i != argc)
20282087
usage(fast_import_usage);
20292088

2030-
alloc_objects(object_entry_alloc);
2031-
strbuf_init(&command_buf);
2032-
2033-
atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
2034-
branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
2035-
avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
2036-
marks = pool_calloc(1, sizeof(struct mark_set));
2037-
20382089
start_packfile();
20392090
for (;;) {
20402091
read_next_command();

t/t9300-fast-import.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ test_expect_success \
111111
'A: verify marks output' \
112112
'diff -u expect marks.out'
113113

114+
test_expect_success \
115+
'A: verify marks import' \
116+
'git-fast-import \
117+
--import-marks=marks.out \
118+
--export-marks=marks.new \
119+
</dev/null &&
120+
diff -u expect marks.new'
121+
114122
###
115123
### series B
116124
###

0 commit comments

Comments
 (0)