Skip to content

Commit e0f4dc3

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 fbc9bf3 commit e0f4dc3

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
@@ -2268,6 +2268,28 @@ pid_t waitpid(pid_t pid, int *status, int options)
22682268
return -1;
22692269
}
22702270

2271+
int mingw_is_mount_point(struct strbuf *path)
2272+
{
2273+
WIN32_FIND_DATAW findbuf = { 0 };
2274+
HANDLE handle;
2275+
wchar_t wfilename[MAX_PATH];
2276+
int wlen = xutftowcs_path(wfilename, path->buf);
2277+
if (wlen < 0)
2278+
die(_("could not get long path for '%s'"), path->buf);
2279+
2280+
/* remove trailing slash, if any */
2281+
if (wlen > 0 && wfilename[wlen - 1] == L'/')
2282+
wfilename[--wlen] = L'\0';
2283+
2284+
handle = FindFirstFileW(wfilename, &findbuf);
2285+
if (handle == INVALID_HANDLE_VALUE)
2286+
return 0;
2287+
FindClose(handle);
2288+
2289+
return (findbuf.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
2290+
(findbuf.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT);
2291+
}
2292+
22712293
int xutftowcsn(wchar_t *wcs, const char *utfs, size_t wcslen, int utflen)
22722294
{
22732295
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
@@ -409,6 +409,10 @@ static inline char *git_find_last_dir_sep(const char *path)
409409
#define find_last_dir_sep git_find_last_dir_sep
410410
#endif
411411

412+
#ifndef is_mount_point
413+
#define is_mount_point is_mount_point_via_stat
414+
#endif
415+
412416
#ifndef query_user_email
413417
#define query_user_email() NULL
414418
#endif

path.c

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

1272+
int is_mount_point_via_stat(struct strbuf *path)
1273+
{
1274+
size_t len = path->len;
1275+
unsigned int current_dev;
1276+
struct stat st;
1277+
1278+
if (!strcmp("/", path->buf))
1279+
return 1;
1280+
1281+
strbuf_addstr(path, "/.");
1282+
if (lstat(path->buf, &st)) {
1283+
/*
1284+
* If we cannot access the current directory, we cannot say
1285+
* that it is a bind mount.
1286+
*/
1287+
strbuf_setlen(path, len);
1288+
return 0;
1289+
}
1290+
current_dev = st.st_dev;
1291+
1292+
/* Now look at the parent directory */
1293+
strbuf_addch(path, '.');
1294+
if (lstat(path->buf, &st)) {
1295+
/*
1296+
* If we cannot access the parent directory, we cannot say
1297+
* that it is a bind mount.
1298+
*/
1299+
strbuf_setlen(path, len);
1300+
return 0;
1301+
}
1302+
strbuf_setlen(path, len);
1303+
1304+
/*
1305+
* If the device ID differs between current and parent directory,
1306+
* then it is a bind mount.
1307+
*/
1308+
return current_dev != st.st_dev;
1309+
}
1310+
12721311
int daemon_avoid_alias(const char *p)
12731312
{
12741313
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)