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.
10 #include "parse-options.h"
12 #include "run-command.h"
14 static const char * const pull_usage
[] = {
15 N_("git pull [options] [<repository> [<refspec>...]]"),
20 static int opt_verbosity
;
21 static char *opt_progress
;
23 static struct option pull_options
[] = {
25 OPT__VERBOSITY(&opt_verbosity
),
26 OPT_PASSTHRU(0, "progress", &opt_progress
, NULL
,
27 N_("force progress reporting"),
34 * Pushes "-q" or "-v" switches into arr to match the opt_verbosity level.
36 static void argv_push_verbosity(struct argv_array
*arr
)
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");
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
)
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
;
72 argv_array_pushl(&args
, "fetch", "--update-head-ok", NULL
);
75 argv_push_verbosity(&args
);
77 argv_array_push(&args
, opt_progress
);
80 argv_array_push(&args
, repo
);
81 argv_array_pushv(&args
, refspecs
);
83 die("BUG: refspecs without repo?");
84 ret
= run_command_v_opt(args
.argv
, RUN_GIT_CMD
);
85 argv_array_clear(&args
);
90 * Runs git-merge, returning its exit status.
92 static int run_merge(void)
95 struct argv_array args
= ARGV_ARRAY_INIT
;
97 argv_array_pushl(&args
, "merge", NULL
);
100 argv_push_verbosity(&args
);
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
);
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
))