Skip to content

Commit 8059005

Browse files
committed
stash: optionally use the scripted version again
We recently converted the `git stash` command from Unix shell scripts to builtins. Let's end users a way out when they discover a bug in the builtin command: `stash.useBuiltin`. 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]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6111407 commit 8059005

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
@@ -83,6 +83,7 @@
8383
/git-interpret-trailers
8484
/git-instaweb
8585
/git-legacy-rebase
86+
/git-legacy-stash
8687
/git-log
8788
/git-ls-files
8889
/git-ls-remote

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@ SCRIPT_SH += git-merge-resolve.sh
633633
SCRIPT_SH += git-mergetool.sh
634634
SCRIPT_SH += git-quiltimport.sh
635635
SCRIPT_SH += git-legacy-rebase.sh
636+
SCRIPT_SH += git-legacy-stash.sh
636637
SCRIPT_SH += git-remote-testgit.sh
637638
SCRIPT_SH += git-request-pull.sh
638639
SCRIPT_SH += git-submodule.sh

builtin/stash.c

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

1819
#define INCLUDE_ALL_FILES 2
1920

@@ -1515,6 +1516,26 @@ static int save_stash(int argc, const char **argv, const char *prefix)
15151516
return ret;
15161517
}
15171518

1519+
static int use_builtin_stash(void)
1520+
{
1521+
struct child_process cp = CHILD_PROCESS_INIT;
1522+
struct strbuf out = STRBUF_INIT;
1523+
int ret;
1524+
1525+
argv_array_pushl(&cp.args,
1526+
"config", "--bool", "stash.usebuiltin", NULL);
1527+
cp.git_cmd = 1;
1528+
if (capture_command(&cp, &out, 6)) {
1529+
strbuf_release(&out);
1530+
return 1;
1531+
}
1532+
1533+
strbuf_trim(&out);
1534+
ret = !strcmp("true", out.buf);
1535+
strbuf_release(&out);
1536+
return ret;
1537+
}
1538+
15181539
int cmd_stash(int argc, const char **argv, const char *prefix)
15191540
{
15201541
int i = -1;
@@ -1526,6 +1547,20 @@ int cmd_stash(int argc, const char **argv, const char *prefix)
15261547
OPT_END()
15271548
};
15281549

1550+
if (!use_builtin_stash()) {
1551+
const char *path = mkpath("%s/git-legacy-stash",
1552+
git_exec_path());
1553+
1554+
if (sane_execvp(path, (char **)argv) < 0)
1555+
die_errno(_("could not exec %s"), path);
1556+
else
1557+
BUG("sane_execvp() returned???");
1558+
}
1559+
1560+
prefix = setup_git_directory();
1561+
trace_repo_setup(prefix);
1562+
setup_work_tree();
1563+
15291564
git_config(git_diff_basic_config, NULL);
15301565

15311566
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
@@ -80,6 +80,28 @@ clear_stash () {
8080
fi
8181
}
8282

83+
maybe_quiet () {
84+
case "$1" in
85+
--keep-stdout)
86+
shift
87+
if test -n "$GIT_QUIET"
88+
then
89+
eval "$@" 2>/dev/null
90+
else
91+
eval "$@"
92+
fi
93+
;;
94+
*)
95+
if test -n "$GIT_QUIET"
96+
then
97+
eval "$@" >/dev/null 2>&1
98+
else
99+
eval "$@"
100+
fi
101+
;;
102+
esac
103+
}
104+
83105
create_stash () {
84106

85107
prepare_fallback_ident
@@ -112,15 +134,18 @@ create_stash () {
112134
done
113135

114136
git update-index -q --refresh
115-
if no_changes "$@"
137+
if maybe_quiet no_changes "$@"
116138
then
117139
exit 0
118140
fi
119141

120142
# state of the base commit
121-
if b_commit=$(git rev-parse --verify HEAD)
143+
if b_commit=$(maybe_quiet --keep-stdout git rev-parse --verify HEAD)
122144
then
123145
head=$(git rev-list --oneline -n 1 HEAD --)
146+
elif test -n "$GIT_QUIET"
147+
then
148+
exit 1
124149
else
125150
die "$(gettext "You do not have the initial commit yet")"
126151
fi
@@ -315,7 +340,7 @@ push_stash () {
315340
test -n "$untracked" || git ls-files --error-unmatch -- "$@" >/dev/null || exit 1
316341

317342
git update-index -q --refresh
318-
if no_changes "$@"
343+
if maybe_quiet no_changes "$@"
319344
then
320345
say "$(gettext "No local changes to save")"
321346
exit 0
@@ -370,6 +395,9 @@ save_stash () {
370395
while test $# != 0
371396
do
372397
case "$1" in
398+
-q|--quiet)
399+
GIT_QUIET=t
400+
;;
373401
--)
374402
shift
375403
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
@@ -555,7 +555,12 @@ static struct cmd_struct commands[] = {
555555
{ "show-index", cmd_show_index },
556556
{ "show-ref", cmd_show_ref, RUN_SETUP },
557557
{ "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
558-
{ "stash", cmd_stash, RUN_SETUP | NEED_WORK_TREE },
558+
/*
559+
* NEEDSWORK: Until the builtin stash is thoroughly robust and no
560+
* longer needs redirection to the stash shell script this is kept as
561+
* is, then should be changed to RUN_SETUP | NEED_WORK_TREE
562+
*/
563+
{ "stash", cmd_stash },
559564
{ "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
560565
{ "stripspace", cmd_stripspace },
561566
{ "submodule--helper", cmd_submodule__helper, RUN_SETUP | SUPPORT_SUPER_PREFIX | NO_PARSEOPT },

0 commit comments

Comments
 (0)