From 6b3ee18dc5c620d7cb4324e009339b5ca9ac488c Mon Sep 17 00:00:00 2001 From: Paul Tan Date: Sun, 14 Jun 2015 16:41:48 +0800 Subject: parse-options-cb: implement parse_opt_passthru() Certain git commands, such as git-pull, are simply wrappers around other git commands like git-fetch, git-merge and git-rebase. As such, these wrapper commands will typically need to "pass through" command-line options of the commands they wrap. Implement the parse_opt_passthru() parse-options callback, which will reconstruct the command-line option into an char* string, such that it can be passed to another git command. Helped-by: Johannes Schindelin Helped-by: Junio C Hamano Helped-by: Stefan Beller Signed-off-by: Paul Tan Signed-off-by: Junio C Hamano diff --git a/Documentation/technical/api-parse-options.txt b/Documentation/technical/api-parse-options.txt index 1f2db31..85d10ab 100644 --- a/Documentation/technical/api-parse-options.txt +++ b/Documentation/technical/api-parse-options.txt @@ -212,6 +212,13 @@ There are some macros to easily define options: Use it to hide deprecated options that are still to be recognized and ignored silently. +`OPT_PASSTHRU(short, long, &char_var, arg_str, description, flags)`:: + Introduce an option that will be reconstructed into a char* string, + which must be initialized to NULL. This is useful when you need to + pass the command-line option to another command. Any previous value + will be overwritten, so this should only be used for options where + the last one specified on the command line wins. + The last element of the array must be `OPT_END()`. diff --git a/parse-options-cb.c b/parse-options-cb.c index be8c413..68bc593 100644 --- a/parse-options-cb.c +++ b/parse-options-cb.c @@ -134,3 +134,52 @@ int parse_opt_noop_cb(const struct option *opt, const char *arg, int unset) { return 0; } + +/** + * Recreates the command-line option in the strbuf. + */ +static int recreate_opt(struct strbuf *sb, const struct option *opt, + const char *arg, int unset) +{ + strbuf_reset(sb); + + if (opt->long_name) { + strbuf_addstr(sb, unset ? "--no-" : "--"); + strbuf_addstr(sb, opt->long_name); + if (arg) { + strbuf_addch(sb, '='); + strbuf_addstr(sb, arg); + } + } else if (opt->short_name && !unset) { + strbuf_addch(sb, '-'); + strbuf_addch(sb, opt->short_name); + if (arg) + strbuf_addstr(sb, arg); + } else + return -1; + + return 0; +} + +/** + * For an option opt, recreates the command-line option in opt->value which + * must be an char* initialized to NULL. This is useful when we need to pass + * the command-line option to another command. Since any previous value will be + * overwritten, this callback should only be used for options where the last + * one wins. + */ +int parse_opt_passthru(const struct option *opt, const char *arg, int unset) +{ + static struct strbuf sb = STRBUF_INIT; + char **opt_value = opt->value; + + if (recreate_opt(&sb, opt, arg, unset) < 0) + return -1; + + if (*opt_value) + free(*opt_value); + + *opt_value = strbuf_detach(&sb, NULL); + + return 0; +} diff --git a/parse-options.h b/parse-options.h index c71e9da..5b0f886 100644 --- a/parse-options.h +++ b/parse-options.h @@ -224,6 +224,7 @@ extern int parse_opt_with_commit(const struct option *, const char *, int); extern int parse_opt_tertiary(const struct option *, const char *, int); extern int parse_opt_string_list(const struct option *, const char *, int); extern int parse_opt_noop_cb(const struct option *, const char *, int); +extern int parse_opt_passthru(const struct option *, const char *, int); #define OPT__VERBOSE(var, h) OPT_COUNTUP('v', "verbose", (var), (h)) #define OPT__QUIET(var, h) OPT_COUNTUP('q', "quiet", (var), (h)) @@ -242,5 +243,7 @@ extern int parse_opt_noop_cb(const struct option *, const char *, int); OPT_COLOR_FLAG(0, "color", (var), (h)) #define OPT_COLUMN(s, l, v, h) \ { OPTION_CALLBACK, (s), (l), (v), N_("style"), (h), PARSE_OPT_OPTARG, parseopt_column_callback } +#define OPT_PASSTHRU(s, l, v, a, h, f) \ + { OPTION_CALLBACK, (s), (l), (v), (a), (h), (f), parse_opt_passthru } #endif -- cgit v0.10.2-6-g49f6 From ffad85c599307441323de565c3fafde227e04a8f Mon Sep 17 00:00:00 2001 From: Paul Tan Date: Sun, 14 Jun 2015 16:41:49 +0800 Subject: parse-options-cb: implement parse_opt_passthru_argv() Certain git commands, such as git-pull, are simply wrappers around other git commands like git-fetch, git-merge and git-rebase. As such, these wrapper commands will typically need to "pass through" command-line options of the commands they wrap. Implement the parse_opt_passthru_argv() parse-options callback, which will reconstruct all the provided command-line options into an argv_array, such that it can be passed to another git command. This is useful for passing command-line options that can be specified multiple times. Helped-by: Stefan Beller Signed-off-by: Paul Tan Signed-off-by: Junio C Hamano diff --git a/Documentation/technical/api-parse-options.txt b/Documentation/technical/api-parse-options.txt index 85d10ab..0b0ab01 100644 --- a/Documentation/technical/api-parse-options.txt +++ b/Documentation/technical/api-parse-options.txt @@ -219,6 +219,12 @@ There are some macros to easily define options: will be overwritten, so this should only be used for options where the last one specified on the command line wins. +`OPT_PASSTHRU_ARGV(short, long, &argv_array_var, arg_str, description, flags)`:: + Introduce an option where all instances of it on the command-line will + be reconstructed into an argv_array. This is useful when you need to + pass the command-line option, which can be specified multiple times, + to another command. + The last element of the array must be `OPT_END()`. diff --git a/parse-options-cb.c b/parse-options-cb.c index 68bc593..5ab6ed6 100644 --- a/parse-options-cb.c +++ b/parse-options-cb.c @@ -4,6 +4,7 @@ #include "commit.h" #include "color.h" #include "string-list.h" +#include "argv-array.h" /*----- some often used options -----*/ @@ -183,3 +184,22 @@ int parse_opt_passthru(const struct option *opt, const char *arg, int unset) return 0; } + +/** + * For an option opt, recreate the command-line option, appending it to + * opt->value which must be a argv_array. This is useful when we need to pass + * the command-line option, which can be specified multiple times, to another + * command. + */ +int parse_opt_passthru_argv(const struct option *opt, const char *arg, int unset) +{ + static struct strbuf sb = STRBUF_INIT; + struct argv_array *opt_value = opt->value; + + if (recreate_opt(&sb, opt, arg, unset) < 0) + return -1; + + argv_array_push(opt_value, sb.buf); + + return 0; +} diff --git a/parse-options.h b/parse-options.h index 5b0f886..aba06688 100644 --- a/parse-options.h +++ b/parse-options.h @@ -225,6 +225,7 @@ extern int parse_opt_tertiary(const struct option *, const char *, int); extern int parse_opt_string_list(const struct option *, const char *, int); extern int parse_opt_noop_cb(const struct option *, const char *, int); extern int parse_opt_passthru(const struct option *, const char *, int); +extern int parse_opt_passthru_argv(const struct option *, const char *, int); #define OPT__VERBOSE(var, h) OPT_COUNTUP('v', "verbose", (var), (h)) #define OPT__QUIET(var, h) OPT_COUNTUP('q', "quiet", (var), (h)) @@ -245,5 +246,7 @@ extern int parse_opt_passthru(const struct option *, const char *, int); { OPTION_CALLBACK, (s), (l), (v), N_("style"), (h), PARSE_OPT_OPTARG, parseopt_column_callback } #define OPT_PASSTHRU(s, l, v, a, h, f) \ { OPTION_CALLBACK, (s), (l), (v), (a), (h), (f), parse_opt_passthru } +#define OPT_PASSTHRU_ARGV(s, l, v, a, h, f) \ + { OPTION_CALLBACK, (s), (l), (v), (a), (h), (f), parse_opt_passthru_argv } #endif -- cgit v0.10.2-6-g49f6 From 85b343245b495a47f937007e1c0650f2070b9b4f Mon Sep 17 00:00:00 2001 From: Paul Tan Date: Sun, 14 Jun 2015 16:41:50 +0800 Subject: argv-array: implement argv_array_pushv() When we have a null-terminated array, it would be useful to convert it or append it to an argv_array for further manipulation. Implement argv_array_pushv() which will push a null-terminated array of strings on to an argv_array. Signed-off-by: Paul Tan Signed-off-by: Junio C Hamano diff --git a/Documentation/technical/api-argv-array.txt b/Documentation/technical/api-argv-array.txt index 1a79781..8076172 100644 --- a/Documentation/technical/api-argv-array.txt +++ b/Documentation/technical/api-argv-array.txt @@ -46,6 +46,9 @@ Functions Format a string and push it onto the end of the array. This is a convenience wrapper combining `strbuf_addf` and `argv_array_push`. +`argv_array_pushv`:: + Push a null-terminated array of strings onto the end of the array. + `argv_array_pop`:: Remove the final element from the array. If there are no elements in the array, do nothing. diff --git a/argv-array.c b/argv-array.c index 256741d..eaed477 100644 --- a/argv-array.c +++ b/argv-array.c @@ -49,6 +49,12 @@ void argv_array_pushl(struct argv_array *array, ...) va_end(ap); } +void argv_array_pushv(struct argv_array *array, const char **argv) +{ + for (; *argv; argv++) + argv_array_push(array, *argv); +} + void argv_array_pop(struct argv_array *array) { if (!array->argc) diff --git a/argv-array.h b/argv-array.h index c65e6e8..a2fa0aa 100644 --- a/argv-array.h +++ b/argv-array.h @@ -17,6 +17,7 @@ __attribute__((format (printf,2,3))) void argv_array_pushf(struct argv_array *, const char *fmt, ...); LAST_ARG_MUST_BE_NULL void argv_array_pushl(struct argv_array *, ...); +void argv_array_pushv(struct argv_array *, const char **); void argv_array_pop(struct argv_array *); void argv_array_clear(struct argv_array *); -- cgit v0.10.2-6-g49f6 From 1e1ea69fa4e5fb20baefe9e5422527e971d56a86 Mon Sep 17 00:00:00 2001 From: Paul Tan Date: Sun, 14 Jun 2015 16:41:51 +0800 Subject: pull: implement skeletal builtin pull For the purpose of rewriting git-pull.sh into a C builtin, implement a skeletal builtin/pull.c that redirects to $GIT_EXEC_PATH/git-pull.sh if the environment variable _GIT_USE_BUILTIN_PULL is not defined. This allows us to fall back on the functional git-pull.sh when running the test suite for tests that depend on a working git-pull implementation. This redirection should be removed when all the features of git-pull.sh have been re-implemented in builtin/pull.c. Helped-by: Junio C Hamano Signed-off-by: Paul Tan Signed-off-by: Junio C Hamano diff --git a/Makefile b/Makefile index 54ec511..2057a9d 100644 --- a/Makefile +++ b/Makefile @@ -877,6 +877,7 @@ BUILTIN_OBJS += builtin/pack-refs.o BUILTIN_OBJS += builtin/patch-id.o BUILTIN_OBJS += builtin/prune-packed.o BUILTIN_OBJS += builtin/prune.o +BUILTIN_OBJS += builtin/pull.o BUILTIN_OBJS += builtin/push.o BUILTIN_OBJS += builtin/read-tree.o BUILTIN_OBJS += builtin/receive-pack.o diff --git a/builtin.h b/builtin.h index b87df70..ea3c834 100644 --- a/builtin.h +++ b/builtin.h @@ -98,6 +98,7 @@ extern int cmd_pack_redundant(int argc, const char **argv, const char *prefix); extern int cmd_patch_id(int argc, const char **argv, const char *prefix); extern int cmd_prune(int argc, const char **argv, const char *prefix); extern int cmd_prune_packed(int argc, const char **argv, const char *prefix); +extern int cmd_pull(int argc, const char **argv, const char *prefix); extern int cmd_push(int argc, const char **argv, const char *prefix); extern int cmd_read_tree(int argc, const char **argv, const char *prefix); extern int cmd_receive_pack(int argc, const char **argv, const char *prefix); diff --git a/builtin/pull.c b/builtin/pull.c new file mode 100644 index 0000000..cabeed4 --- /dev/null +++ b/builtin/pull.c @@ -0,0 +1,33 @@ +/* + * Builtin "git pull" + * + * Based on git-pull.sh by Junio C Hamano + * + * Fetch one or more remote refs and merge it/them into the current HEAD. + */ +#include "cache.h" +#include "builtin.h" +#include "parse-options.h" +#include "exec_cmd.h" + +static const char * const pull_usage[] = { + NULL +}; + +static struct option pull_options[] = { + OPT_END() +}; + +int cmd_pull(int argc, const char **argv, const char *prefix) +{ + if (!getenv("_GIT_USE_BUILTIN_PULL")) { + const char *path = mkpath("%s/git-pull", git_exec_path()); + + if (sane_execvp(path, (char **)argv) < 0) + die_errno("could not exec %s", path); + } + + argc = parse_options(argc, argv, prefix, pull_options, pull_usage, 0); + + return 0; +} diff --git a/git.c b/git.c index 44374b1..e7a7713 100644 --- a/git.c +++ b/git.c @@ -445,6 +445,7 @@ static struct cmd_struct commands[] = { { "pickaxe", cmd_blame, RUN_SETUP }, { "prune", cmd_prune, RUN_SETUP }, { "prune-packed", cmd_prune_packed, RUN_SETUP }, + { "pull", cmd_pull, RUN_SETUP | NEED_WORK_TREE }, { "push", cmd_push, RUN_SETUP }, { "read-tree", cmd_read_tree, RUN_SETUP }, { "receive-pack", cmd_receive_pack }, -- cgit v0.10.2-6-g49f6 From f2c5baa14e01e74527cc5807e5d47fdf67a05dd3 Mon Sep 17 00:00:00 2001 From: Paul Tan Date: Sun, 14 Jun 2015 16:41:52 +0800 Subject: pull: implement fetch + merge Implement the fetch + merge functionality of git-pull, by first running git-fetch with the repo and refspecs provided on the command line, then running git-merge on FETCH_HEAD to merge the fetched refs into the current branch. Helped-by: Junio C Hamano Signed-off-by: Paul Tan Signed-off-by: Junio C Hamano diff --git a/builtin/pull.c b/builtin/pull.c index cabeed4..9157536 100644 --- a/builtin/pull.c +++ b/builtin/pull.c @@ -9,8 +9,10 @@ #include "builtin.h" #include "parse-options.h" #include "exec_cmd.h" +#include "run-command.h" static const char * const pull_usage[] = { + N_("git pull [options] [ [...]]"), NULL }; @@ -18,8 +20,61 @@ static struct option pull_options[] = { OPT_END() }; +/** + * Parses argv into [ [...]], returning their values in `repo` + * as a string and `refspecs` as a null-terminated array of strings. If `repo` + * is not provided in argv, it is set to NULL. + */ +static void parse_repo_refspecs(int argc, const char **argv, const char **repo, + const char ***refspecs) +{ + if (argc > 0) { + *repo = *argv++; + argc--; + } else + *repo = NULL; + *refspecs = argv; +} + +/** + * Runs git-fetch, returning its exit status. `repo` and `refspecs` are the + * repository and refspecs to fetch, or NULL if they are not provided. + */ +static int run_fetch(const char *repo, const char **refspecs) +{ + struct argv_array args = ARGV_ARRAY_INIT; + int ret; + + argv_array_pushl(&args, "fetch", "--update-head-ok", NULL); + if (repo) { + argv_array_push(&args, repo); + argv_array_pushv(&args, refspecs); + } else if (*refspecs) + die("BUG: refspecs without repo?"); + ret = run_command_v_opt(args.argv, RUN_GIT_CMD); + argv_array_clear(&args); + return ret; +} + +/** + * Runs git-merge, returning its exit status. + */ +static int run_merge(void) +{ + int ret; + struct argv_array args = ARGV_ARRAY_INIT; + + argv_array_pushl(&args, "merge", NULL); + argv_array_push(&args, "FETCH_HEAD"); + ret = run_command_v_opt(args.argv, RUN_GIT_CMD); + argv_array_clear(&args); + return ret; +} + int cmd_pull(int argc, const char **argv, const char *prefix) { + const char *repo, **refspecs; + if (!getenv("_GIT_USE_BUILTIN_PULL")) { const char *path = mkpath("%s/git-pull", git_exec_path()); @@ -29,5 +84,10 @@ int cmd_pull(int argc, const char **argv, const char *prefix) argc = parse_options(argc, argv, prefix, pull_options, pull_usage, 0); - return 0; + parse_repo_refspecs(argc, argv, &repo, &refspecs); + + if (run_fetch(repo, refspecs)) + return 1; + + return run_merge(); } -- cgit v0.10.2-6-g49f6 From 2a747902c3290efba0ba44c77cf2e5692d998af2 Mon Sep 17 00:00:00 2001 From: Paul Tan Date: Sun, 14 Jun 2015 16:41:53 +0800 Subject: pull: pass verbosity, --progress flags to fetch and merge 7f87aff (Teach/Fix pull/fetch -q/-v options, 2008-11-15) taught git-pull to accept the verbosity -v and -q options and pass them to git-fetch and git-merge. Re-implement support for the verbosity flags by adding it to the options list and introducing argv_push_verbosity() to push the flags into the argv array used to execute git-fetch and git-merge. 9839018 (fetch and pull: learn --progress, 2010-02-24) and bebd2fd (pull: propagate --progress to merge, 2011-02-20) taught git-pull to accept the --progress option and pass it to git-fetch and git-merge. Use OPT_PASSTHRU() implemented earlier to pass the "--[no-]progress" command line options to git-fetch and git-merge. Helped-by: Junio C Hamano Signed-off-by: Paul Tan Signed-off-by: Junio C Hamano diff --git a/builtin/pull.c b/builtin/pull.c index 9157536..5d9f2b5 100644 --- a/builtin/pull.c +++ b/builtin/pull.c @@ -16,11 +16,35 @@ static const char * const pull_usage[] = { NULL }; +/* Shared options */ +static int opt_verbosity; +static char *opt_progress; + static struct option pull_options[] = { + /* Shared options */ + OPT__VERBOSITY(&opt_verbosity), + OPT_PASSTHRU(0, "progress", &opt_progress, NULL, + N_("force progress reporting"), + PARSE_OPT_NOARG), + OPT_END() }; /** + * Pushes "-q" or "-v" switches into arr to match the opt_verbosity level. + */ +static void argv_push_verbosity(struct argv_array *arr) +{ + int verbosity; + + for (verbosity = opt_verbosity; verbosity > 0; verbosity--) + argv_array_push(arr, "-v"); + + for (verbosity = opt_verbosity; verbosity < 0; verbosity++) + argv_array_push(arr, "-q"); +} + +/** * Parses argv into [ [...]], returning their values in `repo` * as a string and `refspecs` as a null-terminated array of strings. If `repo` * is not provided in argv, it is set to NULL. @@ -46,6 +70,12 @@ static int run_fetch(const char *repo, const char **refspecs) int ret; argv_array_pushl(&args, "fetch", "--update-head-ok", NULL); + + /* Shared options */ + argv_push_verbosity(&args); + if (opt_progress) + argv_array_push(&args, opt_progress); + if (repo) { argv_array_push(&args, repo); argv_array_pushv(&args, refspecs); @@ -65,6 +95,12 @@ static int run_merge(void) struct argv_array args = ARGV_ARRAY_INIT; argv_array_pushl(&args, "merge", NULL); + + /* Shared options */ + argv_push_verbosity(&args); + if (opt_progress) + argv_array_push(&args, opt_progress); + argv_array_push(&args, "FETCH_HEAD"); ret = run_command_v_opt(args.argv, RUN_GIT_CMD); argv_array_clear(&args); -- cgit v0.10.2-6-g49f6 From 11b6d1780173727b5ecb82428cf49e5788b2985a Mon Sep 17 00:00:00 2001 From: Paul Tan Date: Sun, 14 Jun 2015 16:41:54 +0800 Subject: pull: pass git-merge's options to git-merge Specify git-merge's options in the option list, and pass any specified options to git-merge. These options are: * -n, --stat, --summary: since d8abe14 (merge, pull: introduce '--(no-)stat' option, 2008-04-06) * --log: since efb779f (merge, pull: add '--(no-)log' command line option, 2008-04-06) * --squash: since 7d0c688 (git-merge --squash, 2006-06-23) * --commit: since 5072a32 (Teach git-pull about --[no-]ff, --no-squash and --commit, 2007-10-29) * --edit: since 8580830 ("git pull" doesn't know "--edit", 2012-02-11) * --ff, --ff-only: since 5072a32 (Teach git-pull about --[no-]ff, --no-squash and --commit, 2007-10-29) * --verify-signatures: since efed002 (merge/pull: verify GPG signatures of commits being merged, 2013-03-31) * -s, --strategy: since 60fb5b2 (Use git-merge in git-pull (second try)., 2005-09-25) * -X, --strategy-option: since ee2c795 (Teach git-pull to pass -X