pull --rebase: add --[no-]autostash flag
If rebase.autoStash configuration variable is set, there is no way to override it for "git pull --rebase" from the command line. Teach "git pull --rebase" the --[no-]autostash command line flag which overrides the current value of rebase.autoStash, if set. As "git rebase" understands the --[no-]autostash option, it's just a matter of passing the option to underlying "git rebase" when "git pull --rebase" is called. Helped-by: Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> Helped-by: Junio C Hamano <gitster@pobox.com> Helped-by: Paul Tan <pyokagan@gmail.com> Helped-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Mehul Jain <mehul.jain2029@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Mehul Jain committed
Mar 21, 2016 at 23:48 UTC
f66398eb57c627169429f47bbe4d943d2c975959
3 files changed
+91
Documentation/git-pull.txt
+9
@@ -128,6 +128,15 @@ unless you have read linkgit:git-rebase[1] carefully.
128
--no-rebase::
129
Override earlier --rebase.
130
131
+--autostash::
132
+--no-autostash::
133
+ Before starting rebase, stash local modifications away (see
134
+ linkgit:git-stash[1]) if needed, and apply the stash when
135
+ done. `--no-autostash` is useful to override the `rebase.autoStash`
136
+ configuration variable (see linkgit:git-config[1]).
137
++
138
+This option is only valid when "--rebase" is used.
139
+
140
Options related to fetching
141
~~~~~~~~~~~~~~~~~~~~~~~~~~~
142
builtin/pull.c
+12
@@ -86,6 +86,7 @@ static char *opt_commit;
86
static char *opt_edit;
87
static char *opt_ff;
88
static char *opt_verify_signatures;
89
+static int opt_autostash = -1;
90
static int config_autostash;
91
static struct argv_array opt_strategies = ARGV_ARRAY_INIT;
92
static struct argv_array opt_strategy_opts = ARGV_ARRAY_INIT;
@@ -150,6 +151,8 @@ static struct option pull_options[] = {
151
OPT_PASSTHRU(0, "verify-signatures", &opt_verify_signatures, NULL,
152
N_("verify that the named commit has a valid GPG signature"),
153
PARSE_OPT_NOARG),
154
+ OPT_BOOL(0, "autostash", &opt_autostash,
155
+ N_("automatically stash/stash pop before and after rebase")),
156
OPT_PASSTHRU_ARGV('s', "strategy", &opt_strategies, N_("strategy"),
157
N_("merge strategy to use"),
158
0),
@@ -802,6 +805,10 @@ static int run_rebase(const unsigned char *curr_head,
805
argv_array_pushv(&args, opt_strategy_opts.argv);
806
if (opt_gpg_sign)
807
argv_array_push(&args, opt_gpg_sign);
808
+ if (opt_autostash == 0)
809
+ argv_array_push(&args, "--no-autostash");
810
+ else if (opt_autostash == 1)
811
+ argv_array_push(&args, "--autostash");
812
813
argv_array_push(&args, "--onto");
814
argv_array_push(&args, sha1_to_hex(merge_head));
@@ -847,8 +854,13 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
854
if (get_sha1("HEAD", orig_head))
855
hashclr(orig_head);
856
857
+ if (!opt_rebase && opt_autostash != -1)
858
+ die(_("--[no-]autostash option is only valid with --rebase."));
859
+
860
if (opt_rebase) {
861
int autostash = config_autostash;
862
+ if (opt_autostash != -1)
863
+ autostash = opt_autostash;
864
865
if (is_null_sha1(orig_head) && !is_cache_unborn())
866
die(_("Updating an unborn branch with changes added to the index."));
t/t5520-pull.sh
+70
@@ -256,6 +256,76 @@ test_expect_success 'pull --rebase succeeds with dirty working directory and reb
256
test "$(cat file)" = "modified again"
257
'
258
259
+test_expect_success 'pull --rebase --autostash & rebase.autostash=true' '
260
+ test_config rebase.autostash true &&
261
+ git reset --hard before-rebase &&
262
+ echo dirty >new_file &&
263
+ git add new_file &&
264
+ git pull --rebase --autostash . copy &&
265
+ test_cmp_rev HEAD^ copy &&
266
+ test "$(cat new_file)" = dirty &&
267
+ test "$(cat file)" = "modified again"
268
+'
269
+
270
+test_expect_success 'pull --rebase --autostash & rebase.autoStash=false' '
271
+ test_config rebase.autostash false &&
272
+ git reset --hard before-rebase &&
273
+ echo dirty >new_file &&
274
+ git add new_file &&
275
+ git pull --rebase --autostash . copy &&
276
+ test_cmp_rev HEAD^ copy &&
277
+ test "$(cat new_file)" = dirty &&
278
+ test "$(cat file)" = "modified again"
279
+'
280
+
281
+test_expect_success 'pull --rebase: --autostash & rebase.autoStash unset' '
282
+ git reset --hard before-rebase &&
283
+ echo dirty >new_file &&
284
+ git add new_file &&
285
+ git pull --rebase --autostash . copy &&
286
+ test_cmp_rev HEAD^ copy &&
287
+ test "$(cat new_file)" = dirty &&
288
+ test "$(cat file)" = "modified again"
289
+'
290
+
291
+test_expect_success 'pull --rebase --no-autostash & rebase.autostash=true' '
292
+ test_config rebase.autostash true &&
293
+ git reset --hard before-rebase &&
294
+ echo dirty >new_file &&
295
+ git add new_file &&
296
+ test_must_fail git pull --rebase --no-autostash . copy 2>err &&
297
+ test_i18ngrep "Cannot pull with rebase: Your index contains uncommitted changes." err
298
+'
299
+
300
+test_expect_success 'pull --rebase --no-autostash & rebase.autostash=false' '
301
+ test_config rebase.autostash false &&
302
+ git reset --hard before-rebase &&
303
+ echo dirty >new_file &&
304
+ git add new_file &&
305
+ test_must_fail git pull --rebase --no-autostash . copy 2>err &&
306
+ test_i18ngrep "Cannot pull with rebase: Your index contains uncommitted changes." err
307
+'
308
+
309
+test_expect_success 'pull --rebase --no-autostash & rebase.autostash unset' '
310
+ git reset --hard before-rebase &&
311
+ echo dirty >new_file &&
312
+ git add new_file &&
313
+ test_must_fail git pull --rebase --no-autostash . copy 2>err &&
314
+ test_i18ngrep "Cannot pull with rebase: Your index contains uncommitted changes." err
315
+'
316
+
317
+test_expect_success 'pull --autostash (without --rebase) should error out' '
318
+ test_must_fail git pull --autostash . copy 2>actual &&
319
+ echo "fatal: --[no-]autostash option is only valid with --rebase." >expect &&
320
+ test_i18ncmp actual expect
321
+'
322
+
323
+test_expect_success 'pull --no-autostash (without --rebase) should error out' '
324
+ test_must_fail git pull --no-autostash . copy 2>actual &&
325
+ echo "fatal: --[no-]autostash option is only valid with --rebase." >expect &&
326
+ test_i18ncmp actual expect
327
+'
328
+
329
test_expect_success 'pull.rebase' '
330
git reset --hard before-rebase &&
331
test_config pull.rebase true &&