Skip to content

Commit b8ff1fa

Browse files
dschodrizzd
authored andcommitted
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 271c090 commit b8ff1fa

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

3840
enum color_clean {
@@ -168,6 +170,18 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
168170
goto out;
169171
}
170172

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

12721273
/*

compat/mingw.c

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

2848+
int mingw_is_mount_point(struct strbuf *path)
2849+
{
2850+
WIN32_FIND_DATAW findbuf = { 0 };
2851+
HANDLE handle;
2852+
wchar_t wfilename[MAX_LONG_PATH];
2853+
int wlen = xutftowcs_long_path(wfilename, path->buf);
2854+
if (wlen < 0)
2855+
die(_("could not get long path for '%s'"), path->buf);
2856+
2857+
/* remove trailing slash, if any */
2858+
if (wlen > 0 && wfilename[wlen - 1] == L'/')
2859+
wfilename[--wlen] = L'\0';
2860+
2861+
handle = FindFirstFileW(wfilename, &findbuf);
2862+
if (handle == INVALID_HANDLE_VALUE)
2863+
return 0;
2864+
FindClose(handle);
2865+
2866+
return (findbuf.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
2867+
(findbuf.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT);
2868+
}
2869+
28482870
int xutftowcsn(wchar_t *wcs, const char *utfs, size_t wcslen, int utflen)
28492871
{
28502872
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
@@ -680,4 +680,13 @@ test_expect_success MINGW 'handle clean & core.longpaths = false nicely' '
680680
grep "too long" .git/err
681681
'
682682

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

0 commit comments

Comments
 (0)