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 <gitster@pobox.com> Signed-off-by: Slavica Djukic <slawica92@hotmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Slavica Djukic committed Nov 18, 2018 at 14:44 UTC 3bc2111fc2e9e8ff33b48bb2ccd17b77ca7dbced
2 files changed +45
git-stash.sh
+17
@@ -55,6 +55,20 @@ untracked_files () {
55 git ls-files -o $z $excl_opt -- "$@"
56 }
57
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 +
72 clear_stash () {
73 if test $# != 0
74 then
@@ -67,6 +81,9 @@ clear_stash () {
81 }
82
83 create_stash () {
84 +
85 + prepare_fallback_ident
86 +
87 stash_msg=
88 untracked=
89 while test $# != 0
t/t3903-stash.sh
+28
@@ -1096,4 +1096,32 @@ test_expect_success 'stash -- <subdir> works with binary files' '
1096 test_path_is_file subdir/untracked
1097 '
1098
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 +
1127 test_done