pull: pass verbosity, --progress flags to fetch and merge
[alt-git.git] / builtin / pull.c
blob5d9f2b529e018b517bc001e20057fb1af6aa9e2a
1 /*
2 * Builtin "git pull"
4 * Based on git-pull.sh by Junio C Hamano
6 * Fetch one or more remote refs and merge it/them into the current HEAD.
7 */
8 #include "cache.h"
9 #include "builtin.h"
10 #include "parse-options.h"
11 #include "exec_cmd.h"
12 #include "run-command.h"
14 static const char * const pull_usage[] = {
15 N_("git pull [options] [<repository> [<refspec>...]]"),
16 NULL
19 /* Shared options */
20 static int opt_verbosity;
21 static char *opt_progress;
23 static struct option pull_options[] = {
24 /* Shared options */
25 OPT__VERBOSITY(&opt_verbosity),
26 OPT_PASSTHRU(0, "progress", &opt_progress, NULL,
27 N_("force progress reporting"),
28 PARSE_OPT_NOARG),
30 OPT_END()
33 /**
34 * Pushes "-q" or "-v" switches into arr to match the opt_verbosity level.
36 static void argv_push_verbosity(struct argv_array *arr)
38 int verbosity;
40 for (verbosity = opt_verbosity; verbosity > 0; verbosity--)
41 argv_array_push(arr, "-v");
43 for (verbosity = opt_verbosity; verbosity < 0; verbosity++)
44 argv_array_push(arr, "-q");
47 /**
48 * Parses argv into [<repo> [<refspecs>...]], returning their values in `repo`
49 * as a string and `refspecs` as a null-terminated array of strings. If `repo`
50 * is not provided in argv, it is set to NULL.
52 static void parse_repo_refspecs(int argc, const char **argv, const char **repo,
53 const char ***refspecs)
55 if (argc > 0) {
56 *repo = *argv++;
57 argc--;
58 } else
59 *repo = NULL;
60 *refspecs = argv;
63 /**
64 * Runs git-fetch, returning its exit status. `repo` and `refspecs` are the
65 * repository and refspecs to fetch, or NULL if they are not provided.
67 static int run_fetch(const char *repo, const char **refspecs)
69 struct argv_array args = ARGV_ARRAY_INIT;
70 int ret;
72 argv_array_pushl(&args, "fetch", "--update-head-ok", NULL);
74 /* Shared options */
75 argv_push_verbosity(&args);
76 if (opt_progress)
77 argv_array_push(&args, opt_progress);
79 if (repo) {
80 argv_array_push(&args, repo);
81 argv_array_pushv(&args, refspecs);
82 } else if (*refspecs)
83 die("BUG: refspecs without repo?");
84 ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
85 argv_array_clear(&args);
86 return ret;
89 /**
90 * Runs git-merge, returning its exit status.
92 static int run_merge(void)
94 int ret;
95 struct argv_array args = ARGV_ARRAY_INIT;
97 argv_array_pushl(&args, "merge", NULL);
99 /* Shared options */
100 argv_push_verbosity(&args);
101 if (opt_progress)
102 argv_array_push(&args, opt_progress);
104 argv_array_push(&args, "FETCH_HEAD");
105 ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
106 argv_array_clear(&args);
107 return ret;
110 int cmd_pull(int argc, const char **argv, const char *prefix)
112 const char *repo, **refspecs;
114 if (!getenv("_GIT_USE_BUILTIN_PULL")) {
115 const char *path = mkpath("%s/git-pull", git_exec_path());
117 if (sane_execvp(path, (char **)argv) < 0)
118 die_errno("could not exec %s", path);
121 argc = parse_options(argc, argv, prefix, pull_options, pull_usage, 0);
123 parse_repo_refspecs(argc, argv, &repo, &refspecs);
125 if (run_fetch(repo, refspecs))
126 return 1;
128 return run_merge();