Skip to content

Commit dc91502

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 f71e59d commit dc91502

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

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

174+
if (is_mount_point(path)) {
175+
if (!quiet) {
176+
quote_path_relative(path->buf, prefix, &quoted);
177+
printf(dry_run ?
178+
_(msg_would_skip_mount_point) :
179+
_(msg_skip_mount_point), quoted.buf);
180+
}
181+
*dir_gone = 0;
182+
183+
goto out;
184+
}
185+
172186
dir = opendir(path->buf);
173187
if (!dir) {
174188
/* 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
@@ -1243,6 +1243,7 @@ int normalize_path_copy_len(char *dst, const char *src, int *prefix_len);
12431243
int normalize_path_copy(char *dst, const char *src);
12441244
int longest_ancestor_length(const char *path, struct string_list *prefixes);
12451245
char *strip_path_suffix(const char *path, const char *suffix);
1246+
int is_mount_point_via_stat(struct strbuf *path);
12461247
int daemon_avoid_alias(const char *path);
12471248

12481249
/*

compat/mingw.c

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

2226+
int mingw_is_mount_point(struct strbuf *path)
2227+
{
2228+
WIN32_FIND_DATAW findbuf = { 0 };
2229+
HANDLE handle;
2230+
wchar_t wfilename[MAX_LONG_PATH];
2231+
int wlen = xutftowcs_long_path(wfilename, path->buf);
2232+
if (wlen < 0)
2233+
die(_("could not get long path for '%s'"), path->buf);
2234+
2235+
/* remove trailing slash, if any */
2236+
if (wlen > 0 && wfilename[wlen - 1] == L'/')
2237+
wfilename[--wlen] = L'\0';
2238+
2239+
handle = FindFirstFileW(wfilename, &findbuf);
2240+
if (handle == INVALID_HANDLE_VALUE)
2241+
return 0;
2242+
FindClose(handle);
2243+
2244+
return (findbuf.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
2245+
(findbuf.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT);
2246+
}
2247+
22262248
int xutftowcsn(wchar_t *wcs, const char *utfs, size_t wcslen, int utflen)
22272249
{
22282250
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
extern 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
@@ -404,6 +404,10 @@ static inline char *git_find_last_dir_sep(const char *path)
404404
#define find_last_dir_sep git_find_last_dir_sep
405405
#endif
406406

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

path.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,45 @@ char *strip_path_suffix(const char *path, const char *suffix)
12481248
return xstrndup(path, chomp_trailing_dir_sep(path, path_len));
12491249
}
12501250

1251+
int is_mount_point_via_stat(struct strbuf *path)
1252+
{
1253+
size_t len = path->len;
1254+
unsigned int current_dev;
1255+
struct stat st;
1256+
1257+
if (!strcmp("/", path->buf))
1258+
return 1;
1259+
1260+
strbuf_addstr(path, "/.");
1261+
if (lstat(path->buf, &st)) {
1262+
/*
1263+
* If we cannot access the current directory, we cannot say
1264+
* that it is a bind mount.
1265+
*/
1266+
strbuf_setlen(path, len);
1267+
return 0;
1268+
}
1269+
current_dev = st.st_dev;
1270+
1271+
/* Now look at the parent directory */
1272+
strbuf_addch(path, '.');
1273+
if (lstat(path->buf, &st)) {
1274+
/*
1275+
* If we cannot access the parent directory, we cannot say
1276+
* that it is a bind mount.
1277+
*/
1278+
strbuf_setlen(path, len);
1279+
return 0;
1280+
}
1281+
strbuf_setlen(path, len);
1282+
1283+
/*
1284+
* If the device ID differs between current and parent directory,
1285+
* then it is a bind mount.
1286+
*/
1287+
return current_dev != st.st_dev;
1288+
}
1289+
12511290
int daemon_avoid_alias(const char *p)
12521291
{
12531292
int sl, ndot;

t/t7300-clean.sh

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

684+
test_expect_success MINGW 'clean does not traverse mount points' '
685+
mkdir target &&
686+
>target/dont-clean-me &&
687+
git init with-mountpoint &&
688+
cmd //c "mklink /j with-mountpoint\\mountpoint target" &&
689+
git -C with-mountpoint clean -dfx &&
690+
test_path_is_file target/dont-clean-me
691+
'
692+
684693
test_done

0 commit comments

Comments
 (0)