From 0f6927c2296d6f92f3f233fe9e073c30ca66e0ce Mon Sep 17 00:00:00 2001 From: Sverre Rabbelier Date: Fri, 4 Dec 2009 18:06:54 +0100 Subject: fast-import: put option parsing code in separate functions Putting the options in their own functions increases readability of the option parsing block and makes it easier to reuse the option parsing code later on. Signed-off-by: Sverre Rabbelier Signed-off-by: Junio C Hamano --- fast-import.c | 115 ++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 75 insertions(+), 40 deletions(-) diff --git a/fast-import.c b/fast-import.c index dd3c99d60d..fcd9e1e1e3 100644 --- a/fast-import.c +++ b/fast-import.c @@ -295,6 +295,7 @@ static unsigned long branch_count; static unsigned long branch_load_count; static int failure; static FILE *pack_edges; +static unsigned int show_stats = 1; /* Memory pools */ static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool); @@ -2420,7 +2421,7 @@ static void parse_progress(void) skip_optional_lf(); } -static void import_marks(const char *input_file) +static void option_import_marks(const char *input_file) { char line[512]; FILE *f = fopen(input_file, "r"); @@ -2455,6 +2456,76 @@ static void import_marks(const char *input_file) fclose(f); } +static void option_date_format(const char *fmt) +{ + if (!strcmp(fmt, "raw")) + whenspec = WHENSPEC_RAW; + else if (!strcmp(fmt, "rfc2822")) + whenspec = WHENSPEC_RFC2822; + else if (!strcmp(fmt, "now")) + whenspec = WHENSPEC_NOW; + else + die("unknown --date-format argument %s", fmt); +} + +static void option_max_pack_size(const char *packsize) +{ + max_packsize = strtoumax(packsize, NULL, 0) * 1024 * 1024; +} + +static void option_depth(const char *depth) +{ + max_depth = strtoul(depth, NULL, 0); + if (max_depth > MAX_DEPTH) + die("--depth cannot exceed %u", MAX_DEPTH); +} + +static void option_active_branches(const char *branches) +{ + max_active_branches = strtoul(branches, NULL, 0); +} + +static void option_export_marks(const char *marks) +{ + mark_file = xstrdup(marks); +} + +static void option_export_pack_edges(const char *edges) +{ + if (pack_edges) + fclose(pack_edges); + pack_edges = fopen(edges, "a"); + if (!pack_edges) + die_errno("Cannot open '%s'", edges); +} + +static void parse_one_option(const char *option) +{ + if (!prefixcmp(option, "date-format=")) { + option_date_format(option + 12); + } else if (!prefixcmp(option, "max-pack-size=")) { + option_max_pack_size(option + 14); + } else if (!prefixcmp(option, "depth=")) { + option_depth(option + 6); + } else if (!prefixcmp(option, "active-branches=")) { + option_active_branches(option + 16); + } else if (!prefixcmp(option, "import-marks=")) { + option_import_marks(option + 13); + } else if (!prefixcmp(option, "export-marks=")) { + option_export_marks(option + 13); + } else if (!prefixcmp(option, "export-pack-edges=")) { + option_export_pack_edges(option + 18); + } else if (!prefixcmp(option, "force")) { + force_update = 1; + } else if (!prefixcmp(option, "quiet")) { + show_stats = 0; + } else if (!prefixcmp(option, "stats")) { + show_stats = 1; + } else { + die("Unsupported option: %s", option); + } +} + static int git_pack_config(const char *k, const char *v, void *cb) { if (!strcmp(k, "pack.depth")) { @@ -2481,7 +2552,7 @@ static const char fast_import_usage[] = int main(int argc, const char **argv) { - unsigned int i, show_stats = 1; + unsigned int i; git_extract_argv0_path(argv[0]); @@ -2505,44 +2576,8 @@ int main(int argc, const char **argv) if (*a != '-' || !strcmp(a, "--")) break; - else if (!prefixcmp(a, "--date-format=")) { - const char *fmt = a + 14; - if (!strcmp(fmt, "raw")) - whenspec = WHENSPEC_RAW; - else if (!strcmp(fmt, "rfc2822")) - whenspec = WHENSPEC_RFC2822; - else if (!strcmp(fmt, "now")) - whenspec = WHENSPEC_NOW; - else - die("unknown --date-format argument %s", fmt); - } - else if (!prefixcmp(a, "--max-pack-size=")) - max_packsize = strtoumax(a + 16, NULL, 0) * 1024 * 1024; - else if (!prefixcmp(a, "--depth=")) { - max_depth = strtoul(a + 8, NULL, 0); - if (max_depth > MAX_DEPTH) - die("--depth cannot exceed %u", MAX_DEPTH); - } - else if (!prefixcmp(a, "--active-branches=")) - max_active_branches = strtoul(a + 18, NULL, 0); - else if (!prefixcmp(a, "--import-marks=")) - import_marks(a + 15); - else if (!prefixcmp(a, "--export-marks=")) - mark_file = a + 15; - else if (!prefixcmp(a, "--export-pack-edges=")) { - if (pack_edges) - fclose(pack_edges); - pack_edges = fopen(a + 20, "a"); - if (!pack_edges) - die_errno("Cannot open '%s'", a + 20); - } else if (!strcmp(a, "--force")) - force_update = 1; - else if (!strcmp(a, "--quiet")) - show_stats = 0; - else if (!strcmp(a, "--stats")) - show_stats = 1; - else - die("unknown option %s", a); + + parse_one_option(a + 2); } if (i != argc) usage(fast_import_usage); -- cgit v1.2.1 From 07cd9328b6256d3fdc05546e10c589144b5c63c5 Mon Sep 17 00:00:00 2001 From: Sverre Rabbelier Date: Fri, 4 Dec 2009 18:06:55 +0100 Subject: fast-import: put marks reading in its own function All options do nothing but set settings, with the exception of the --input-marks option. Delay the reading of the marks file till after all options have been parsed. Also, rename mark_file to export_marks_file as it is now ambiguous. Signed-off-by: Sverre Rabbelier Signed-off-by: Junio C Hamano --- fast-import.c | 93 ++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 51 insertions(+), 42 deletions(-) diff --git a/fast-import.c b/fast-import.c index fcd9e1e1e3..0458b03a2a 100644 --- a/fast-import.c +++ b/fast-import.c @@ -318,7 +318,8 @@ static unsigned int object_entry_alloc = 5000; static struct object_entry_pool *blocks; static struct object_entry *object_table[1 << 16]; static struct mark_set *marks; -static const char *mark_file; +static const char *export_marks_file; +static const char *import_marks_file; /* Our last blob */ static struct last_object last_blob = { STRBUF_INIT, 0, 0, 0 }; @@ -455,8 +456,8 @@ static void write_crash_report(const char *err) fputc('\n', rpt); fputs("Marks\n", rpt); fputs("-----\n", rpt); - if (mark_file) - fprintf(rpt, " exported to %s\n", mark_file); + if (export_marks_file) + fprintf(rpt, " exported to %s\n", export_marks_file); else dump_marks_helper(rpt, 0, marks); @@ -1603,13 +1604,13 @@ static void dump_marks(void) int mark_fd; FILE *f; - if (!mark_file) + if (!export_marks_file) return; - mark_fd = hold_lock_file_for_update(&mark_lock, mark_file, 0); + mark_fd = hold_lock_file_for_update(&mark_lock, export_marks_file, 0); if (mark_fd < 0) { failure |= error("Unable to write marks file %s: %s", - mark_file, strerror(errno)); + export_marks_file, strerror(errno)); return; } @@ -1618,7 +1619,7 @@ static void dump_marks(void) int saved_errno = errno; rollback_lock_file(&mark_lock); failure |= error("Unable to write marks file %s: %s", - mark_file, strerror(saved_errno)); + export_marks_file, strerror(saved_errno)); return; } @@ -1634,7 +1635,7 @@ static void dump_marks(void) int saved_errno = errno; rollback_lock_file(&mark_lock); failure |= error("Unable to write marks file %s: %s", - mark_file, strerror(saved_errno)); + export_marks_file, strerror(saved_errno)); return; } @@ -1642,11 +1643,47 @@ static void dump_marks(void) int saved_errno = errno; rollback_lock_file(&mark_lock); failure |= error("Unable to commit marks file %s: %s", - mark_file, strerror(saved_errno)); + export_marks_file, strerror(saved_errno)); return; } } +static void read_marks(void) +{ + char line[512]; + FILE *f = fopen(import_marks_file, "r"); + if (!f) + die_errno("cannot read '%s'", import_marks_file); + while (fgets(line, sizeof(line), f)) { + uintmax_t mark; + char *end; + unsigned char sha1[20]; + struct object_entry *e; + + end = strchr(line, '\n'); + if (line[0] != ':' || !end) + die("corrupt mark line: %s", line); + *end = 0; + mark = strtoumax(line + 1, &end, 10); + if (!mark || end == line + 1 + || *end != ' ' || get_sha1(end + 1, sha1)) + die("corrupt mark line: %s", line); + e = find_object(sha1); + if (!e) { + enum object_type type = sha1_object_info(sha1, NULL); + if (type < 0) + die("object not found: %s", sha1_to_hex(sha1)); + e = insert_object(sha1); + e->type = type; + e->pack_id = MAX_PACK_ID; + e->offset = 1; /* just not zero! */ + } + insert_mark(mark, e); + } + fclose(f); +} + + static int read_next_command(void) { static int stdin_eof = 0; @@ -2421,39 +2458,9 @@ static void parse_progress(void) skip_optional_lf(); } -static void option_import_marks(const char *input_file) +static void option_import_marks(const char *marks) { - char line[512]; - FILE *f = fopen(input_file, "r"); - if (!f) - die_errno("cannot read '%s'", input_file); - while (fgets(line, sizeof(line), f)) { - uintmax_t mark; - char *end; - unsigned char sha1[20]; - struct object_entry *e; - - end = strchr(line, '\n'); - if (line[0] != ':' || !end) - die("corrupt mark line: %s", line); - *end = 0; - mark = strtoumax(line + 1, &end, 10); - if (!mark || end == line + 1 - || *end != ' ' || get_sha1(end + 1, sha1)) - die("corrupt mark line: %s", line); - e = find_object(sha1); - if (!e) { - enum object_type type = sha1_object_info(sha1, NULL); - if (type < 0) - die("object not found: %s", sha1_to_hex(sha1)); - e = insert_object(sha1); - e->type = type; - e->pack_id = MAX_PACK_ID; - e->offset = 1; /* just not zero! */ - } - insert_mark(mark, e); - } - fclose(f); + import_marks_file = xstrdup(marks); } static void option_date_format(const char *fmt) @@ -2487,7 +2494,7 @@ static void option_active_branches(const char *branches) static void option_export_marks(const char *marks) { - mark_file = xstrdup(marks); + export_marks_file = xstrdup(marks); } static void option_export_pack_edges(const char *edges) @@ -2581,6 +2588,8 @@ int main(int argc, const char **argv) } if (i != argc) usage(fast_import_usage); + if (import_marks_file) + read_marks(); rc_free = pool_alloc(cmd_save * sizeof(*rc_free)); for (i = 0; i < (cmd_save - 1); i++) -- cgit v1.2.1 From f963bd5d71fd0db01f2c7f6f05df5a5f1af11b82 Mon Sep 17 00:00:00 2001 From: Sverre Rabbelier Date: Fri, 4 Dec 2009 18:06:56 +0100 Subject: fast-import: add feature command This allows the fronted to require a specific feature to be supported by the backend, or abort. Also add support for four initial feature, date-format=, force=, import-marks=, export-marks=. Signed-off-by: Sverre Rabbelier Signed-off-by: Junio C Hamano --- Documentation/git-fast-import.txt | 25 ++++++++++++++ fast-import.c | 38 +++++++++++++++++++++ t/t9300-fast-import.sh | 70 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+) diff --git a/Documentation/git-fast-import.txt b/Documentation/git-fast-import.txt index 288032c7b8..4357c213ed 100644 --- a/Documentation/git-fast-import.txt +++ b/Documentation/git-fast-import.txt @@ -303,6 +303,10 @@ and control the current import process. More detailed discussion standard output. This command is optional and is not needed to perform an import. +`feature`:: + Require that fast-import supports the specified feature, or + abort if it does not. + `commit` ~~~~~~~~ Create or update a branch with a new commit, recording one logical @@ -846,6 +850,27 @@ Placing a `progress` command immediately after a `checkpoint` will inform the reader when the `checkpoint` has been completed and it can safely access the refs that fast-import updated. +`feature` +~~~~~~~~~ +Require that fast-import supports the specified feature, or abort if +it does not. + +.... + 'feature' SP LF +.... + +The part of the command may be any string matching +^[a-zA-Z][a-zA-Z-]*$ and should be understood by fast-import. + +Feature work identical as their option counterparts. + +The following features are currently supported: + +* date-format +* import-marks +* export-marks +* force + Crash Reports ------------- If fast-import is supplied invalid input it will terminate with a diff --git a/fast-import.c b/fast-import.c index 0458b03a2a..ce0cd4e68a 100644 --- a/fast-import.c +++ b/fast-import.c @@ -353,6 +353,7 @@ static struct recent_command *rc_free; static unsigned int cmd_save = 100; static uintmax_t next_mark; static struct strbuf new_data = STRBUF_INIT; +static int seen_data_command; static void write_branch_report(FILE *rpt, struct branch *b) { @@ -1704,6 +1705,11 @@ static int read_next_command(void) if (stdin_eof) return EOF; + if (!seen_data_command + && prefixcmp(command_buf.buf, "feature ")) { + seen_data_command = 1; + } + rc = rc_free; if (rc) rc_free = rc->next; @@ -2533,6 +2539,36 @@ static void parse_one_option(const char *option) } } +static int parse_one_feature(const char *feature) +{ + if (!prefixcmp(feature, "date-format=")) { + option_date_format(feature + 12); + } else if (!prefixcmp(feature, "import-marks=")) { + option_import_marks(feature + 13); + } else if (!prefixcmp(feature, "export-marks=")) { + option_export_marks(feature + 13); + } else if (!prefixcmp(feature, "force")) { + force_update = 1; + } else { + return 0; + } + + return 1; +} + +static void parse_feature(void) +{ + char *feature = command_buf.buf + 8; + + if (seen_data_command) + die("Got feature command '%s' after data command", feature); + + if (parse_one_feature(feature)) + return; + + die("This version of fast-import does not support feature %s.", feature); +} + static int git_pack_config(const char *k, const char *v, void *cb) { if (!strcmp(k, "pack.depth")) { @@ -2612,6 +2648,8 @@ int main(int argc, const char **argv) parse_checkpoint(); else if (!prefixcmp(command_buf.buf, "progress ")) parse_progress(); + else if (!prefixcmp(command_buf.buf, "feature ")) + parse_feature(); else die("Unsupported command: %s", command_buf.buf); } diff --git a/t/t9300-fast-import.sh b/t/t9300-fast-import.sh index b49815d108..b2c521f557 100755 --- a/t/t9300-fast-import.sh +++ b/t/t9300-fast-import.sh @@ -1254,4 +1254,74 @@ test_expect_success \ 'Q: verify note for third commit' \ 'git cat-file blob refs/notes/foobar:$commit3 >actual && test_cmp expect actual' +### +### series R (feature) +### + +cat >input <input <input << EOF +blob +data 3 +hi +feature date-format=now +EOF + +test_expect_success 'R: abort on receiving feature after data command' ' + test_must_fail git fast-import input << EOF +feature export-marks=git.marks +blob +mark :1 +data 3 +hi + +EOF + +test_expect_success \ + 'R: export-marks feature results in a marks file being created' \ + 'cat input | git fast-import && + grep :1 git.marks' + +test_expect_success \ + 'R: export-marks options can be overriden by commandline options' \ + 'cat input | git fast-import --export-marks=other.marks && + grep :1 other.marks' + +cat >input << EOF +feature import-marks=marks.out +feature export-marks=marks.new +EOF + +test_expect_success \ + 'R: import to output marks works without any content' \ + 'cat input | git fast-import && + test_cmp marks.out marks.new' + +cat >input < Date: Fri, 4 Dec 2009 18:06:57 +0100 Subject: fast-import: add option command This allows the frontend to specify any of the supported options as long as no non-option command has been given. This way the user does not have to include any frontend-specific options, but instead she can rely on the frontend to tell fast-import what it needs. Also factor out parsing of argv and have it execute when we reach the first non-option command, or after all commands have been read and no non-option command has been encountered. Non-git options are ignored, unrecognised options result in an error. Signed-off-by: Sverre Rabbelier Signed-off-by: Junio C Hamano --- Documentation/git-fast-import.txt | 32 ++++++++++++++ fast-import.c | 87 ++++++++++++++++++++++++++++----------- 2 files changed, 94 insertions(+), 25 deletions(-) diff --git a/Documentation/git-fast-import.txt b/Documentation/git-fast-import.txt index 4357c213ed..2d5f533f63 100644 --- a/Documentation/git-fast-import.txt +++ b/Documentation/git-fast-import.txt @@ -307,6 +307,11 @@ and control the current import process. More detailed discussion Require that fast-import supports the specified feature, or abort if it does not. +`option`:: + Specify any of the options listed under OPTIONS that do not + change stream semantic to suit the frontend's needs. This + command is optional and is not needed to perform an import. + `commit` ~~~~~~~~ Create or update a branch with a new commit, recording one logical @@ -871,6 +876,33 @@ The following features are currently supported: * export-marks * force +`option` +~~~~~~~~ +Processes the specified option so that git fast-import behaves in a +way that suits the frontend's needs. +Note that options specified by the frontend are overridden by any +options the user may specify to git fast-import itself. + +.... + 'option' SP