Skip to content

Commit b2b355f

Browse files
committed
clean: do not traverse mount points
It seems to be not exactly rare on Windows to install NTFS junction points (the equivalent of "bind mounts" on Linux/Unix) in worktrees, e.g. to map some development tools into a subdirectory. In such a scenario, it is pretty horrible if `git clean -dfx` traverses into the mapped directory and starts to "clean up". Let's just not do that. Let's make sure before we traverse into a directory that it is not a mount point (or junction). This addresses #607 Signed-off-by: Johannes Schindelin <[email protected]>
1 parent fd2ade2 commit b2b355f

File tree

7 files changed

+92
-0
lines changed

7 files changed

+92
-0
lines changed

builtin/clean.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ static const char *msg_remove = N_("Removing %s\n");
3939
static const char *msg_would_remove = N_("Would remove %s\n");
4040
static const char *msg_skip_git_dir = N_("Skipping repository %s\n");
4141
static const char *msg_would_skip_git_dir = N_("Would skip repository %s\n");
42+
static const char *msg_skip_mount_point = N_("Skipping mount point %s\n");
43+
static const char *msg_would_skip_mount_point = N_("Would skip mount point %s\n");
4244
static const char *msg_warn_remove_failed = N_("failed to remove %s");
4345
static const char *msg_warn_lstat_failed = N_("could not lstat %s\n");
4446
static const char *msg_skip_cwd = N_("Refusing to remove current working directory\n");
@@ -183,6 +185,18 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
183185
goto out;
184186
}
185187

188+
if (is_mount_point(path)) {
189+
if (!quiet) {
190+
quote_path(path->buf, prefix, &quoted, 0);
191+
printf(dry_run ?
192+
_(msg_would_skip_mount_point) :
193+
_(msg_skip_mount_point), quoted.buf);
194+
}
195+
*dir_gone = 0;
196+
197+
goto out;
198+
}
199+
186200
dir = opendir(path->buf);
187201
if (!dir) {
188202
/* an empty dir could be removed even if it is unreadble */

compat/mingw.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2517,6 +2517,28 @@ pid_t waitpid(pid_t pid, int *status, int options)
25172517
return -1;
25182518
}
25192519

2520+
int mingw_is_mount_point(struct strbuf *path)
2521+
{
2522+
WIN32_FIND_DATAW findbuf = { 0 };
2523+
HANDLE handle;
2524+
wchar_t wfilename[MAX_PATH];
2525+
int wlen = xutftowcs_path(wfilename, path->buf);
2526+
if (wlen < 0)
2527+
die(_("could not get long path for '%s'"), path->buf);
2528+
2529+
/* remove trailing slash, if any */
2530+
if (wlen > 0 && wfilename[wlen - 1] == L'/')
2531+
wfilename[--wlen] = L'\0';
2532+
2533+
handle = FindFirstFileW(wfilename, &findbuf);
2534+
if (handle == INVALID_HANDLE_VALUE)
2535+
return 0;
2536+
FindClose(handle);
2537+
2538+
return (findbuf.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
2539+
(findbuf.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT);
2540+
}
2541+
25202542
int xutftowcsn(wchar_t *wcs, const char *utfs, size_t wcslen, int utflen)
25212543
{
25222544
int upos = 0, wpos = 0;

compat/mingw.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,9 @@ static inline void convert_slashes(char *path)
454454
if (*path == '\\')
455455
*path = '/';
456456
}
457+
struct strbuf;
458+
int mingw_is_mount_point(struct strbuf *path);
459+
#define is_mount_point mingw_is_mount_point
457460
#define PATH_SEP ';'
458461
char *mingw_query_user_email(void);
459462
#define query_user_email mingw_query_user_email

git-compat-util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,10 @@ static inline int git_has_dir_sep(const char *path)
577577
#define has_dir_sep(path) git_has_dir_sep(path)
578578
#endif
579579

580+
#ifndef is_mount_point
581+
#define is_mount_point is_mount_point_via_stat
582+
#endif
583+
580584
#ifndef query_user_email
581585
#define query_user_email() NULL
582586
#endif

path.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,45 @@ char *strip_path_suffix(const char *path, const char *suffix)
13321332
return offset == -1 ? NULL : xstrndup(path, offset);
13331333
}
13341334

1335+
int is_mount_point_via_stat(struct strbuf *path)
1336+
{
1337+
size_t len = path->len;
1338+
unsigned int current_dev;
1339+
struct stat st;
1340+
1341+
if (!strcmp("/", path->buf))
1342+
return 1;
1343+
1344+
strbuf_addstr(path, "/.");
1345+
if (lstat(path->buf, &st)) {
1346+
/*
1347+
* If we cannot access the current directory, we cannot say
1348+
* that it is a bind mount.
1349+
*/
1350+
strbuf_setlen(path, len);
1351+
return 0;
1352+
}
1353+
current_dev = st.st_dev;
1354+
1355+
/* Now look at the parent directory */
1356+
strbuf_addch(path, '.');
1357+
if (lstat(path->buf, &st)) {
1358+
/*
1359+
* If we cannot access the parent directory, we cannot say
1360+
* that it is a bind mount.
1361+
*/
1362+
strbuf_setlen(path, len);
1363+
return 0;
1364+
}
1365+
strbuf_setlen(path, len);
1366+
1367+
/*
1368+
* If the device ID differs between current and parent directory,
1369+
* then it is a bind mount.
1370+
*/
1371+
return current_dev != st.st_dev;
1372+
}
1373+
13351374
int daemon_avoid_alias(const char *p)
13361375
{
13371376
int sl, ndot;

path.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ int normalize_path_copy(char *dst, const char *src);
198198
int strbuf_normalize_path(struct strbuf *src);
199199
int longest_ancestor_length(const char *path, struct string_list *prefixes);
200200
char *strip_path_suffix(const char *path, const char *suffix);
201+
int is_mount_point_via_stat(struct strbuf *path);
201202
int daemon_avoid_alias(const char *path);
202203

203204
/*

t/t7300-clean.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,4 +789,13 @@ test_expect_success 'traverse into directories that may have ignored entries' '
789789
)
790790
'
791791

792+
test_expect_success MINGW 'clean does not traverse mount points' '
793+
mkdir target &&
794+
>target/dont-clean-me &&
795+
git init with-mountpoint &&
796+
cmd //c "mklink /j with-mountpoint\\mountpoint target" &&
797+
git -C with-mountpoint clean -dfx &&
798+
test_path_is_file target/dont-clean-me
799+
'
800+
792801
test_done

0 commit comments

Comments
 (0)