Skip to content

Commit 3d40e37

Browse files
newrengitster
authored andcommitted
stash: avoid feeding directories to update-index
When a file is removed from the cache, but there is a file of the same name present in the working directory, we would normally treat that file in the working directory as untracked. However, in the case of stash, doing that would prevent a simple 'git stash push', because the untracked file would be in the way of restoring the deleted file. git stash, however, blindly assumes that whatever is in the working directory for a deleted file is wanted and passes that path along to update-index. That causes problems when the working directory contains a directory with the same name as the deleted file. Add some code for this special case that will avoid passing directory names to update-index. Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4dbf7f3 commit 3d40e37

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

builtin/stash.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,27 @@ static int reset_head(void)
313313
return run_command(&cp);
314314
}
315315

316+
static int is_path_a_directory(const char *path)
317+
{
318+
/*
319+
* This function differs from abspath.c:is_directory() in that
320+
* here we use lstat() instead of stat(); we do not want to
321+
* follow symbolic links here.
322+
*/
323+
struct stat st;
324+
return (!lstat(path, &st) && S_ISDIR(st.st_mode));
325+
}
326+
316327
static void add_diff_to_buf(struct diff_queue_struct *q,
317328
struct diff_options *options,
318329
void *data)
319330
{
320331
int i;
321332

322333
for (i = 0; i < q->nr; i++) {
334+
if (is_path_a_directory(q->queue[i]->one->path))
335+
continue;
336+
323337
strbuf_addstr(data, q->queue[i]->one->path);
324338

325339
/* NUL-terminate: will be fed to update-index -z */

t/t3903-stash.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1307,7 +1307,7 @@ test_expect_success 'stash -c stash.useBuiltin=false warning ' '
13071307
test_must_be_empty err
13081308
'
13091309

1310-
test_expect_failure 'git stash succeeds despite directory/file change' '
1310+
test_expect_success 'git stash succeeds despite directory/file change' '
13111311
test_create_repo directory_file_switch_v1 &&
13121312
(
13131313
cd directory_file_switch_v1 &&

0 commit comments

Comments
 (0)