Skip to content

Commit f237751

Browse files
dschojamill
authored andcommitted
stash: optionally use the scripted version again
We recently converted the `git stash` command from Unix shell scripts to builtins. Just like we have `rebase.useBuiltin` to fall back to the scripted rebase, to give end users a way out when they discover a bug in the builtin command, this commit adds support for `stash.useBuiltin`. This is necessary because Git for Windows wants to ship the builtin stash earlier than core Git: Git for Windows v2.19.0 will come with the option of a drastically faster (if a lot less battle-tested) `git stash`. As the file name `git-stash` is already in use, let's rename the scripted backend to `git-legacy-stash`. To make the test suite pass with `stash.useBuiltin=false`, this commit also backports rudimentary support for `-q` (but only *just* enough to appease the test suite), and adds a super-ugly hack to force exit code 129 for `git stash -h`. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 4f49e25 commit f237751

File tree

6 files changed

+75
-4
lines changed

6 files changed

+75
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
/git-instaweb
8181
/git-legacy-rebase
8282
/git-legacy-rebase--interactive
83+
/git-legacy-stash
8384
/git-log
8485
/git-ls-files
8586
/git-ls-remote

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,7 @@ SCRIPT_SH += git-merge-resolve.sh
615615
SCRIPT_SH += git-mergetool.sh
616616
SCRIPT_SH += git-quiltimport.sh
617617
SCRIPT_SH += git-legacy-rebase.sh
618+
SCRIPT_SH += git-legacy-stash.sh
618619
SCRIPT_SH += git-remote-testgit.sh
619620
SCRIPT_SH += git-request-pull.sh
620621
SCRIPT_SH += git-submodule.sh

builtin/stash.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "revision.h"
1414
#include "log-tree.h"
1515
#include "diffcore.h"
16+
#include "exec-cmd.h"
1617

1718
static const char * const git_stash_usage[] = {
1819
N_("git stash list [<options>]"),
@@ -1476,6 +1477,26 @@ static int save_stash(int argc, const char **argv, const char *prefix)
14761477
return ret;
14771478
}
14781479

1480+
static int use_builtin_stash(void)
1481+
{
1482+
struct child_process cp = CHILD_PROCESS_INIT;
1483+
struct strbuf out = STRBUF_INIT;
1484+
int ret;
1485+
1486+
argv_array_pushl(&cp.args,
1487+
"config", "--bool", "stash.usebuiltin", NULL);
1488+
cp.git_cmd = 1;
1489+
if (capture_command(&cp, &out, 6)) {
1490+
strbuf_release(&out);
1491+
return 1;
1492+
}
1493+
1494+
strbuf_trim(&out);
1495+
ret = !strcmp("true", out.buf);
1496+
strbuf_release(&out);
1497+
return ret;
1498+
}
1499+
14791500
int cmd_stash(int argc, const char **argv, const char *prefix)
14801501
{
14811502
int i = -1;
@@ -1487,6 +1508,20 @@ int cmd_stash(int argc, const char **argv, const char *prefix)
14871508
OPT_END()
14881509
};
14891510

1511+
if (!use_builtin_stash()) {
1512+
const char *path = mkpath("%s/git-legacy-stash",
1513+
git_exec_path());
1514+
1515+
if (sane_execvp(path, (char **)argv) < 0)
1516+
die_errno(_("could not exec %s"), path);
1517+
else
1518+
BUG("sane_execvp() returned???");
1519+
}
1520+
1521+
prefix = setup_git_directory();
1522+
trace_repo_setup(prefix);
1523+
setup_work_tree();
1524+
14901525
git_config(git_default_config, NULL);
14911526

14921527
argc = parse_options(argc, argv, prefix, options, git_stash_usage,

git-stash.sh renamed to git-legacy-stash.sh

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,28 @@ clear_stash () {
6666
fi
6767
}
6868

69+
maybe_quiet () {
70+
case "$1" in
71+
--keep-stdout)
72+
shift
73+
if test -n "$GIT_QUIET"
74+
then
75+
eval "$@" 2>/dev/null
76+
else
77+
eval "$@"
78+
fi
79+
;;
80+
*)
81+
if test -n "$GIT_QUIET"
82+
then
83+
eval "$@" >/dev/null 2>&1
84+
else
85+
eval "$@"
86+
fi
87+
;;
88+
esac
89+
}
90+
6991
create_stash () {
7092
stash_msg=
7193
untracked=
@@ -95,15 +117,18 @@ create_stash () {
95117
done
96118

97119
git update-index -q --refresh
98-
if no_changes "$@"
120+
if maybe_quiet no_changes "$@"
99121
then
100122
exit 0
101123
fi
102124

103125
# state of the base commit
104-
if b_commit=$(git rev-parse --verify HEAD)
126+
if b_commit=$(maybe_quiet --keep-stdout git rev-parse --verify HEAD)
105127
then
106128
head=$(git rev-list --oneline -n 1 HEAD --)
129+
elif test -n "$GIT_QUIET"
130+
then
131+
exit 1
107132
else
108133
die "$(gettext "You do not have the initial commit yet")"
109134
fi
@@ -298,7 +323,7 @@ push_stash () {
298323
test -n "$untracked" || git ls-files --error-unmatch -- "$@" >/dev/null || exit 1
299324

300325
git update-index -q --refresh
301-
if no_changes "$@"
326+
if maybe_quiet no_changes "$@"
302327
then
303328
say "$(gettext "No local changes to save")"
304329
exit 0
@@ -353,6 +378,9 @@ save_stash () {
353378
while test $# != 0
354379
do
355380
case "$1" in
381+
-q|--quiet)
382+
GIT_QUIET=t
383+
;;
356384
--)
357385
shift
358386
break

git-sh-setup.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ $LONG_USAGE")"
101101
case "$1" in
102102
-h)
103103
echo "$LONG_USAGE"
104+
case "$0" in *git-legacy-stash) exit 129;; esac
104105
exit
105106
esac
106107
fi

git.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,12 @@ static struct cmd_struct commands[] = {
550550
{ "show-index", cmd_show_index },
551551
{ "show-ref", cmd_show_ref, RUN_SETUP },
552552
{ "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
553-
{ "stash", cmd_stash, RUN_SETUP | NEED_WORK_TREE },
553+
/*
554+
* NEEDSWORK: Until the builtin stash is thoroughly robust and no
555+
* longer needs redirection to the stash shell script this is kept as
556+
* is, then should be changed to RUN_SETUP | NEED_WORK_TREE
557+
*/
558+
{ "stash", cmd_stash },
554559
{ "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
555560
{ "stripspace", cmd_stripspace },
556561
{ "submodule--helper", cmd_submodule__helper, RUN_SETUP | SUPPORT_SUPER_PREFIX | NO_PARSEOPT },

0 commit comments

Comments
 (0)