Skip to content

Commit 90a4627

Browse files
dschogitster
authored andcommittedMar 7, 2019
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: Thomas Gummerer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8d8e9c2 commit 90a4627

File tree

6 files changed

+75
-4
lines changed

6 files changed

+75
-4
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
/git-interpret-trailers
8383
/git-instaweb
8484
/git-legacy-rebase
85+
/git-legacy-stash
8586
/git-log
8687
/git-ls-files
8788
/git-ls-remote

‎Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,7 @@ SCRIPT_SH += git-merge-resolve.sh
617617
SCRIPT_SH += git-mergetool.sh
618618
SCRIPT_SH += git-quiltimport.sh
619619
SCRIPT_SH += git-legacy-rebase.sh
620+
SCRIPT_SH += git-legacy-stash.sh
620621
SCRIPT_SH += git-remote-testgit.sh
621622
SCRIPT_SH += git-request-pull.sh
622623
SCRIPT_SH += git-submodule.sh

‎builtin/stash.c

+35
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
#define INCLUDE_ALL_FILES 2
1819

@@ -1510,6 +1511,26 @@ static int save_stash(int argc, const char **argv, const char *prefix)
15101511
return ret;
15111512
}
15121513

1514+
static int use_builtin_stash(void)
1515+
{
1516+
struct child_process cp = CHILD_PROCESS_INIT;
1517+
struct strbuf out = STRBUF_INIT;
1518+
int ret;
1519+
1520+
argv_array_pushl(&cp.args,
1521+
"config", "--bool", "stash.usebuiltin", NULL);
1522+
cp.git_cmd = 1;
1523+
if (capture_command(&cp, &out, 6)) {
1524+
strbuf_release(&out);
1525+
return 1;
1526+
}
1527+
1528+
strbuf_trim(&out);
1529+
ret = !strcmp("true", out.buf);
1530+
strbuf_release(&out);
1531+
return ret;
1532+
}
1533+
15131534
int cmd_stash(int argc, const char **argv, const char *prefix)
15141535
{
15151536
int i = -1;
@@ -1521,6 +1542,20 @@ int cmd_stash(int argc, const char **argv, const char *prefix)
15211542
OPT_END()
15221543
};
15231544

1545+
if (!use_builtin_stash()) {
1546+
const char *path = mkpath("%s/git-legacy-stash",
1547+
git_exec_path());
1548+
1549+
if (sane_execvp(path, (char **)argv) < 0)
1550+
die_errno(_("could not exec %s"), path);
1551+
else
1552+
BUG("sane_execvp() returned???");
1553+
}
1554+
1555+
prefix = setup_git_directory();
1556+
trace_repo_setup(prefix);
1557+
setup_work_tree();
1558+
15241559
git_config(git_diff_basic_config, NULL);
15251560

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

‎git-stash.sh ‎git-legacy-stash.sh

+31-3
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

+1
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

+6-1
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,12 @@ static struct cmd_struct commands[] = {
554554
{ "show-index", cmd_show_index },
555555
{ "show-ref", cmd_show_ref, RUN_SETUP },
556556
{ "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
557-
{ "stash", cmd_stash, RUN_SETUP | NEED_WORK_TREE },
557+
/*
558+
* NEEDSWORK: Until the builtin stash is thoroughly robust and no
559+
* longer needs redirection to the stash shell script this is kept as
560+
* is, then should be changed to RUN_SETUP | NEED_WORK_TREE
561+
*/
562+
{ "stash", cmd_stash },
558563
{ "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
559564
{ "stripspace", cmd_stripspace },
560565
{ "submodule--helper", cmd_submodule__helper, RUN_SETUP | SUPPORT_SUPER_PREFIX | NO_PARSEOPT },

0 commit comments

Comments
 (0)
Please sign in to comment.