Skip to content

Commit 3ba2e86

Browse files
j6tgitster
authored andcommitted
stash: copy the index using --index-output instead of cp -p
'git stash create' must operate with a temporary index. For this purpose, it used 'cp -p' to create a copy. -p is needed to preserve the timestamp of the index file. Now Jakob Pfender reported a certain combination of a Linux NFS client, OpenBSD NFS server, and cp implementation where this operation failed. Luckily, the first operation in git-stash after copying the index is to call 'git read-tree'. Therefore, use --index-output instead of 'cp -p' to write the copy of the index. --index-output requires that the specified file is on the same volume as the source index, so that the lock file can be rename()d. For this reason, the name of the temporary index is constructed in a way different from the other temporary files. The code path of 'stash -p' also needs a temporary index, but we do not use the new name because it does not depend on the same precondition as --index-output. Signed-off-by: Johannes Sixt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 23a32ff commit 3ba2e86

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

git-stash.sh

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ require_work_tree
1717
cd_to_toplevel
1818

1919
TMP="$GIT_DIR/.git-stash.$$"
20-
trap 'rm -f "$TMP-"*' 0
20+
TMPindex=${GIT_INDEX_FILE-"$GIT_DIR/index"}.stash.$$
21+
trap 'rm -f "$TMP-"* "$TMPindex"' 0
2122

2223
ref_stash=refs/stash
2324

@@ -81,14 +82,12 @@ create_stash () {
8182

8283
# state of the working tree
8384
w_tree=$( (
84-
rm -f "$TMP-index" &&
85-
cp -p ${GIT_INDEX_FILE-"$GIT_DIR/index"} "$TMP-index" &&
86-
GIT_INDEX_FILE="$TMP-index" &&
85+
git read-tree --index-output="$TMPindex" -m $i_tree &&
86+
GIT_INDEX_FILE="$TMPindex" &&
8787
export GIT_INDEX_FILE &&
88-
git read-tree -m $i_tree &&
8988
git diff --name-only -z HEAD | git update-index -z --add --remove --stdin &&
9089
git write-tree &&
91-
rm -f "$TMP-index"
90+
rm -f "$TMPindex"
9291
) ) ||
9392
die "Cannot save the current worktree state"
9493

0 commit comments

Comments
 (0)