Skip to content

Commit a3573bf

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 cc02a0d commit a3573bf

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
@@ -2846,6 +2846,28 @@ pid_t waitpid(pid_t pid, int *status, int options)
28462846
return -1;
28472847
}
28482848

2849+
int mingw_is_mount_point(struct strbuf *path)
2850+
{
2851+
WIN32_FIND_DATAW findbuf = { 0 };
2852+
HANDLE handle;
2853+
wchar_t wfilename[MAX_LONG_PATH];
2854+
int wlen = xutftowcs_long_path(wfilename, path->buf);
2855+
if (wlen < 0)
2856+
die(_("could not get long path for '%s'"), path->buf);
2857+
2858+
/* remove trailing slash, if any */
2859+
if (wlen > 0 && wfilename[wlen - 1] == L'/')
2860+
wfilename[--wlen] = L'\0';
2861+
2862+
handle = FindFirstFileW(wfilename, &findbuf);
2863+
if (handle == INVALID_HANDLE_VALUE)
2864+
return 0;
2865+
FindClose(handle);
2866+
2867+
return (findbuf.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
2868+
(findbuf.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT);
2869+
}
2870+
28492871
int xutftowcsn(wchar_t *wcs, const char *utfs, size_t wcslen, int utflen)
28502872
{
28512873
int upos = 0, wpos = 0;

compat/mingw.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,9 @@ static inline void convert_slashes(char *path)
463463
if (*path == '\\')
464464
*path = '/';
465465
}
466+
struct strbuf;
467+
int mingw_is_mount_point(struct strbuf *path);
468+
#define is_mount_point mingw_is_mount_point
466469
#define PATH_SEP ';'
467470
extern char *mingw_query_user_email(void);
468471
#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
@@ -415,6 +415,10 @@ static inline int git_create_symlink(struct index_state *index, const char *targ
415415
#define create_symlink git_create_symlink
416416
#endif
417417

418+
#ifndef is_mount_point
419+
#define is_mount_point is_mount_point_via_stat
420+
#endif
421+
418422
#ifndef query_user_email
419423
#define query_user_email() NULL
420424
#endif

path.c

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

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