Skip to content

Commit f66398e

Browse files
mehul2029gitster
authored andcommitted
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 <[email protected]> Helped-by: Junio C Hamano <[email protected]> Helped-by: Paul Tan <[email protected]> Helped-by: Eric Sunshine <[email protected]> Signed-off-by: Mehul Jain <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c48d73b commit f66398e

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

Documentation/git-pull.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,15 @@ unless you have read linkgit:git-rebase[1] carefully.
128128
--no-rebase::
129129
Override earlier --rebase.
130130

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+
131140
Options related to fetching
132141
~~~~~~~~~~~~~~~~~~~~~~~~~~~
133142

builtin/pull.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ static char *opt_commit;
8686
static char *opt_edit;
8787
static char *opt_ff;
8888
static char *opt_verify_signatures;
89+
static int opt_autostash = -1;
8990
static int config_autostash;
9091
static struct argv_array opt_strategies = ARGV_ARRAY_INIT;
9192
static struct argv_array opt_strategy_opts = ARGV_ARRAY_INIT;
@@ -150,6 +151,8 @@ static struct option pull_options[] = {
150151
OPT_PASSTHRU(0, "verify-signatures", &opt_verify_signatures, NULL,
151152
N_("verify that the named commit has a valid GPG signature"),
152153
PARSE_OPT_NOARG),
154+
OPT_BOOL(0, "autostash", &opt_autostash,
155+
N_("automatically stash/stash pop before and after rebase")),
153156
OPT_PASSTHRU_ARGV('s', "strategy", &opt_strategies, N_("strategy"),
154157
N_("merge strategy to use"),
155158
0),
@@ -802,6 +805,10 @@ static int run_rebase(const unsigned char *curr_head,
802805
argv_array_pushv(&args, opt_strategy_opts.argv);
803806
if (opt_gpg_sign)
804807
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");
805812

806813
argv_array_push(&args, "--onto");
807814
argv_array_push(&args, sha1_to_hex(merge_head));
@@ -847,8 +854,13 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
847854
if (get_sha1("HEAD", orig_head))
848855
hashclr(orig_head);
849856

857+
if (!opt_rebase && opt_autostash != -1)
858+
die(_("--[no-]autostash option is only valid with --rebase."));
859+
850860
if (opt_rebase) {
851861
int autostash = config_autostash;
862+
if (opt_autostash != -1)
863+
autostash = opt_autostash;
852864

853865
if (is_null_sha1(orig_head) && !is_cache_unborn())
854866
die(_("Updating an unborn branch with changes added to the index."));

t/t5520-pull.sh

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,76 @@ test_expect_success 'pull --rebase succeeds with dirty working directory and reb
256256
test "$(cat file)" = "modified again"
257257
'
258258

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+
259329
test_expect_success 'pull.rebase' '
260330
git reset --hard before-rebase &&
261331
test_config pull.rebase true &&

0 commit comments

Comments
 (0)