Skip to content

Commit 3bc2111

Browse files
Slavica Djukicgitster
authored andcommitted
stash: tolerate missing user identity
The "git stash" command insists on having a usable user identity to the same degree as the "git commit-tree" and "git commit" commands do, because it uses the same codepath that creates commit objects as these commands. It is not strictly necesary to do so. Check if we will barf before creating commit objects and then supply fake identity to please the machinery that creates commits. Add test to document that stash executes correctly both with and without valid ident. This is not that much of usability improvement, as the users who run "git stash" would eventually want to record their changes that are temporarily stored in the stashes in a more permanent history by committing, and they must do "git config user.{name,email}" at that point anyway, so arguably this change is only delaying a step that is necessary to work in the repository. Helped-by: Junio C Hamano <[email protected]> Signed-off-by: Slavica Djukic <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d166e6a commit 3bc2111

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

git-stash.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ untracked_files () {
5555
git ls-files -o $z $excl_opt -- "$@"
5656
}
5757

58+
prepare_fallback_ident () {
59+
if ! git -c user.useconfigonly=yes var GIT_COMMITTER_IDENT >/dev/null 2>&1
60+
then
61+
GIT_AUTHOR_NAME="git stash"
62+
GIT_AUTHOR_EMAIL=git@stash
63+
GIT_COMMITTER_NAME="git stash"
64+
GIT_COMMITTER_EMAIL=git@stash
65+
export GIT_AUTHOR_NAME
66+
export GIT_AUTHOR_EMAIL
67+
export GIT_COMMITTER_NAME
68+
export GIT_COMMITTER_EMAIL
69+
fi
70+
}
71+
5872
clear_stash () {
5973
if test $# != 0
6074
then
@@ -67,6 +81,9 @@ clear_stash () {
6781
}
6882

6983
create_stash () {
84+
85+
prepare_fallback_ident
86+
7087
stash_msg=
7188
untracked=
7289
while test $# != 0

t/t3903-stash.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,4 +1096,32 @@ test_expect_success 'stash -- <subdir> works with binary files' '
10961096
test_path_is_file subdir/untracked
10971097
'
10981098

1099+
test_expect_success 'stash works when user.name and user.email are not set' '
1100+
git reset &&
1101+
>1 &&
1102+
git add 1 &&
1103+
echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>" >expect &&
1104+
git stash &&
1105+
git show -s --format="%an <%ae>" refs/stash >actual &&
1106+
test_cmp expect actual &&
1107+
>2 &&
1108+
git add 2 &&
1109+
test_config user.useconfigonly true &&
1110+
test_config stash.usebuiltin true &&
1111+
(
1112+
sane_unset GIT_AUTHOR_NAME &&
1113+
sane_unset GIT_AUTHOR_EMAIL &&
1114+
sane_unset GIT_COMMITTER_NAME &&
1115+
sane_unset GIT_COMMITTER_EMAIL &&
1116+
test_unconfig user.email &&
1117+
test_unconfig user.name &&
1118+
test_must_fail git commit -m "should fail" &&
1119+
echo "git stash <git@stash>" >expect &&
1120+
>2 &&
1121+
git stash &&
1122+
git show -s --format="%an <%ae>" refs/stash >actual &&
1123+
test_cmp expect actual
1124+
)
1125+
'
1126+
10991127
test_done

0 commit comments

Comments
 (0)