Skip to content

Commit 4f62bd9

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 ea8effe commit 4f62bd9

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
@@ -33,6 +33,8 @@ static const char *msg_remove = N_("Removing %s\n");
3333
static const char *msg_would_remove = N_("Would remove %s\n");
3434
static const char *msg_skip_git_dir = N_("Skipping repository %s\n");
3535
static const char *msg_would_skip_git_dir = N_("Would skip repository %s\n");
36+
static const char *msg_skip_mount_point = N_("Skipping mount point %s\n");
37+
static const char *msg_would_skip_mount_point = N_("Would skip mount point %s\n");
3638
static const char *msg_warn_remove_failed = N_("failed to remove %s");
3739
static const char *msg_warn_lstat_failed = N_("could not lstat %s\n");
3840

@@ -170,6 +172,18 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
170172
goto out;
171173
}
172174

175+
if (is_mount_point(path)) {
176+
if (!quiet) {
177+
quote_path_relative(path->buf, prefix, &quoted);
178+
printf(dry_run ?
179+
_(msg_would_skip_mount_point) :
180+
_(msg_skip_mount_point), quoted.buf);
181+
}
182+
*dir_gone = 0;
183+
184+
goto out;
185+
}
186+
173187
dir = opendir(path->buf);
174188
if (!dir) {
175189
/* an empty dir could be removed even if it is unreadble */

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,7 @@ int normalize_path_copy_len(char *dst, const char *src, int *prefix_len);
12881288
int normalize_path_copy(char *dst, const char *src);
12891289
int longest_ancestor_length(const char *path, struct string_list *prefixes);
12901290
char *strip_path_suffix(const char *path, const char *suffix);
1291+
int is_mount_point_via_stat(struct strbuf *path);
12911292
int daemon_avoid_alias(const char *path);
12921293

12931294
/*

compat/mingw.c

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

2232+
int mingw_is_mount_point(struct strbuf *path)
2233+
{
2234+
WIN32_FIND_DATAW findbuf = { 0 };
2235+
HANDLE handle;
2236+
wchar_t wfilename[MAX_PATH];
2237+
int wlen = xutftowcs_path(wfilename, path->buf);
2238+
if (wlen < 0)
2239+
die(_("could not get long path for '%s'"), path->buf);
2240+
2241+
/* remove trailing slash, if any */
2242+
if (wlen > 0 && wfilename[wlen - 1] == L'/')
2243+
wfilename[--wlen] = L'\0';
2244+
2245+
handle = FindFirstFileW(wfilename, &findbuf);
2246+
if (handle == INVALID_HANDLE_VALUE)
2247+
return 0;
2248+
FindClose(handle);
2249+
2250+
return (findbuf.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
2251+
(findbuf.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT);
2252+
}
2253+
22322254
int xutftowcsn(wchar_t *wcs, const char *utfs, size_t wcslen, int utflen)
22332255
{
22342256
int upos = 0, wpos = 0;

compat/mingw.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,9 @@ static inline void convert_slashes(char *path)
442442
if (*path == '\\')
443443
*path = '/';
444444
}
445+
struct strbuf;
446+
int mingw_is_mount_point(struct strbuf *path);
447+
#define is_mount_point mingw_is_mount_point
445448
#define PATH_SEP ';'
446449
char *mingw_query_user_email(void);
447450
#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
@@ -405,6 +405,10 @@ static inline char *git_find_last_dir_sep(const char *path)
405405
#define find_last_dir_sep git_find_last_dir_sep
406406
#endif
407407

408+
#ifndef is_mount_point
409+
#define is_mount_point is_mount_point_via_stat
410+
#endif
411+
408412
#ifndef query_user_email
409413
#define query_user_email() NULL
410414
#endif

path.c

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

1284+
int is_mount_point_via_stat(struct strbuf *path)
1285+
{
1286+
size_t len = path->len;
1287+
unsigned int current_dev;
1288+
struct stat st;
1289+
1290+
if (!strcmp("/", path->buf))
1291+
return 1;
1292+
1293+
strbuf_addstr(path, "/.");
1294+
if (lstat(path->buf, &st)) {
1295+
/*
1296+
* If we cannot access the current directory, we cannot say
1297+
* that it is a bind mount.
1298+
*/
1299+
strbuf_setlen(path, len);
1300+
return 0;
1301+
}
1302+
current_dev = st.st_dev;
1303+
1304+
/* Now look at the parent directory */
1305+
strbuf_addch(path, '.');
1306+
if (lstat(path->buf, &st)) {
1307+
/*
1308+
* If we cannot access the parent directory, we cannot say
1309+
* that it is a bind mount.
1310+
*/
1311+
strbuf_setlen(path, len);
1312+
return 0;
1313+
}
1314+
strbuf_setlen(path, len);
1315+
1316+
/*
1317+
* If the device ID differs between current and parent directory,
1318+
* then it is a bind mount.
1319+
*/
1320+
return current_dev != st.st_dev;
1321+
}
1322+
12841323
int daemon_avoid_alias(const char *p)
12851324
{
12861325
int sl, ndot;

t/t7300-clean.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,4 +737,13 @@ test_expect_success MINGW 'handle clean & core.longpaths = false nicely' '
737737
test_i18ngrep "too long" .git/err
738738
'
739739

740+
test_expect_success MINGW 'clean does not traverse mount points' '
741+
mkdir target &&
742+
>target/dont-clean-me &&
743+
git init with-mountpoint &&
744+
cmd //c "mklink /j with-mountpoint\\mountpoint target" &&
745+
git -C with-mountpoint clean -dfx &&
746+
test_path_is_file target/dont-clean-me
747+
'
748+
740749
test_done

0 commit comments

Comments
 (0)