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 /* Options passed to git-merge */
24 static char *opt_diffstat
;
26 static char *opt_squash
;
27 static char *opt_commit
;
28 static char *opt_edit
;
30 static char *opt_verify_signatures
;
31 static struct argv_array opt_strategies
= ARGV_ARRAY_INIT
;
32 static struct argv_array opt_strategy_opts
= ARGV_ARRAY_INIT
;
33 static char *opt_gpg_sign
;
35 /* Options passed to git-fetch */
37 static char *opt_append
;
38 static char *opt_upload_pack
;
40 static char *opt_tags
;
41 static char *opt_prune
;
42 static char *opt_recurse_submodules
;
43 static int opt_dry_run
;
44 static char *opt_keep
;
45 static char *opt_depth
;
46 static char *opt_unshallow
;
47 static char *opt_update_shallow
;
48 static char *opt_refmap
;
50 static struct option pull_options
[] = {
52 OPT__VERBOSITY(&opt_verbosity
),
53 OPT_PASSTHRU(0, "progress", &opt_progress
, NULL
,
54 N_("force progress reporting"),
57 /* Options passed to git-merge */
58 OPT_GROUP(N_("Options related to merging")),
59 OPT_PASSTHRU('n', NULL
, &opt_diffstat
, NULL
,
60 N_("do not show a diffstat at the end of the merge"),
61 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
),
62 OPT_PASSTHRU(0, "stat", &opt_diffstat
, NULL
,
63 N_("show a diffstat at the end of the merge"),
65 OPT_PASSTHRU(0, "summary", &opt_diffstat
, NULL
,
66 N_("(synonym to --stat)"),
67 PARSE_OPT_NOARG
| PARSE_OPT_HIDDEN
),
68 OPT_PASSTHRU(0, "log", &opt_log
, N_("n"),
69 N_("add (at most <n>) entries from shortlog to merge commit message"),
71 OPT_PASSTHRU(0, "squash", &opt_squash
, NULL
,
72 N_("create a single commit instead of doing a merge"),
74 OPT_PASSTHRU(0, "commit", &opt_commit
, NULL
,
75 N_("perform a commit if the merge succeeds (default)"),
77 OPT_PASSTHRU(0, "edit", &opt_edit
, NULL
,
78 N_("edit message before committing"),
80 OPT_PASSTHRU(0, "ff", &opt_ff
, NULL
,
81 N_("allow fast-forward"),
83 OPT_PASSTHRU(0, "ff-only", &opt_ff
, NULL
,
84 N_("abort if fast-forward is not possible"),
85 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
),
86 OPT_PASSTHRU(0, "verify-signatures", &opt_verify_signatures
, NULL
,
87 N_("verify that the named commit has a valid GPG signature"),
89 OPT_PASSTHRU_ARGV('s', "strategy", &opt_strategies
, N_("strategy"),
90 N_("merge strategy to use"),
92 OPT_PASSTHRU_ARGV('X', "strategy-option", &opt_strategy_opts
,
94 N_("option for selected merge strategy"),
96 OPT_PASSTHRU('S', "gpg-sign", &opt_gpg_sign
, N_("key-id"),
97 N_("GPG sign commit"),
100 /* Options passed to git-fetch */
101 OPT_GROUP(N_("Options related to fetching")),
102 OPT_PASSTHRU(0, "all", &opt_all
, NULL
,
103 N_("fetch from all remotes"),
105 OPT_PASSTHRU('a', "append", &opt_append
, NULL
,
106 N_("append to .git/FETCH_HEAD instead of overwriting"),
108 OPT_PASSTHRU(0, "upload-pack", &opt_upload_pack
, N_("path"),
109 N_("path to upload pack on remote end"),
111 OPT__FORCE(&opt_force
, N_("force overwrite of local branch")),
112 OPT_PASSTHRU('t', "tags", &opt_tags
, NULL
,
113 N_("fetch all tags and associated objects"),
115 OPT_PASSTHRU('p', "prune", &opt_prune
, NULL
,
116 N_("prune remote-tracking branches no longer on remote"),
118 OPT_PASSTHRU(0, "recurse-submodules", &opt_recurse_submodules
,
120 N_("control recursive fetching of submodules"),
122 OPT_BOOL(0, "dry-run", &opt_dry_run
,
124 OPT_PASSTHRU('k', "keep", &opt_keep
, NULL
,
125 N_("keep downloaded pack"),
127 OPT_PASSTHRU(0, "depth", &opt_depth
, N_("depth"),
128 N_("deepen history of shallow clone"),
130 OPT_PASSTHRU(0, "unshallow", &opt_unshallow
, NULL
,
131 N_("convert to a complete repository"),
132 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
),
133 OPT_PASSTHRU(0, "update-shallow", &opt_update_shallow
, NULL
,
134 N_("accept refs that update .git/shallow"),
136 OPT_PASSTHRU(0, "refmap", &opt_refmap
, N_("refmap"),
137 N_("specify fetch refmap"),
144 * Pushes "-q" or "-v" switches into arr to match the opt_verbosity level.
146 static void argv_push_verbosity(struct argv_array
*arr
)
150 for (verbosity
= opt_verbosity
; verbosity
> 0; verbosity
--)
151 argv_array_push(arr
, "-v");
153 for (verbosity
= opt_verbosity
; verbosity
< 0; verbosity
++)
154 argv_array_push(arr
, "-q");
158 * Pushes "-f" switches into arr to match the opt_force level.
160 static void argv_push_force(struct argv_array
*arr
)
162 int force
= opt_force
;
164 argv_array_push(arr
, "-f");
168 * Parses argv into [<repo> [<refspecs>...]], returning their values in `repo`
169 * as a string and `refspecs` as a null-terminated array of strings. If `repo`
170 * is not provided in argv, it is set to NULL.
172 static void parse_repo_refspecs(int argc
, const char **argv
, const char **repo
,
173 const char ***refspecs
)
184 * Runs git-fetch, returning its exit status. `repo` and `refspecs` are the
185 * repository and refspecs to fetch, or NULL if they are not provided.
187 static int run_fetch(const char *repo
, const char **refspecs
)
189 struct argv_array args
= ARGV_ARRAY_INIT
;
192 argv_array_pushl(&args
, "fetch", "--update-head-ok", NULL
);
195 argv_push_verbosity(&args
);
197 argv_array_push(&args
, opt_progress
);
199 /* Options passed to git-fetch */
201 argv_array_push(&args
, opt_all
);
203 argv_array_push(&args
, opt_append
);
205 argv_array_push(&args
, opt_upload_pack
);
206 argv_push_force(&args
);
208 argv_array_push(&args
, opt_tags
);
210 argv_array_push(&args
, opt_prune
);
211 if (opt_recurse_submodules
)
212 argv_array_push(&args
, opt_recurse_submodules
);
214 argv_array_push(&args
, "--dry-run");
216 argv_array_push(&args
, opt_keep
);
218 argv_array_push(&args
, opt_depth
);
220 argv_array_push(&args
, opt_unshallow
);
221 if (opt_update_shallow
)
222 argv_array_push(&args
, opt_update_shallow
);
224 argv_array_push(&args
, opt_refmap
);
227 argv_array_push(&args
, repo
);
228 argv_array_pushv(&args
, refspecs
);
229 } else if (*refspecs
)
230 die("BUG: refspecs without repo?");
231 ret
= run_command_v_opt(args
.argv
, RUN_GIT_CMD
);
232 argv_array_clear(&args
);
237 * Runs git-merge, returning its exit status.
239 static int run_merge(void)
242 struct argv_array args
= ARGV_ARRAY_INIT
;
244 argv_array_pushl(&args
, "merge", NULL
);
247 argv_push_verbosity(&args
);
249 argv_array_push(&args
, opt_progress
);
251 /* Options passed to git-merge */
253 argv_array_push(&args
, opt_diffstat
);
255 argv_array_push(&args
, opt_log
);
257 argv_array_push(&args
, opt_squash
);
259 argv_array_push(&args
, opt_commit
);
261 argv_array_push(&args
, opt_edit
);
263 argv_array_push(&args
, opt_ff
);
264 if (opt_verify_signatures
)
265 argv_array_push(&args
, opt_verify_signatures
);
266 argv_array_pushv(&args
, opt_strategies
.argv
);
267 argv_array_pushv(&args
, opt_strategy_opts
.argv
);
269 argv_array_push(&args
, opt_gpg_sign
);
271 argv_array_push(&args
, "FETCH_HEAD");
272 ret
= run_command_v_opt(args
.argv
, RUN_GIT_CMD
);
273 argv_array_clear(&args
);
277 int cmd_pull(int argc
, const char **argv
, const char *prefix
)
279 const char *repo
, **refspecs
;
281 if (!getenv("_GIT_USE_BUILTIN_PULL")) {
282 const char *path
= mkpath("%s/git-pull", git_exec_path());
284 if (sane_execvp(path
, (char **)argv
) < 0)
285 die_errno("could not exec %s", path
);
288 argc
= parse_options(argc
, argv
, prefix
, pull_options
, pull_usage
, 0);
290 parse_repo_refspecs(argc
, argv
, &repo
, &refspecs
);
292 if (run_fetch(repo
, refspecs
))