Skip to content

Commit 497babc

Browse files
dschoGit for Windows Build Agent
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 6c6cbbf commit 497babc

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

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

176+
if (is_mount_point(path)) {
177+
if (!quiet) {
178+
quote_path_relative(path->buf, prefix, &quoted);
179+
printf(dry_run ?
180+
_(msg_would_skip_mount_point) :
181+
_(msg_skip_mount_point), quoted.buf);
182+
}
183+
*dir_gone = 0;
184+
185+
goto out;
186+
}
187+
174188
dir = opendir(path->buf);
175189
if (!dir) {
176190
/* 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
@@ -1266,6 +1266,7 @@ int normalize_path_copy_len(char *dst, const char *src, int *prefix_len);
12661266
int normalize_path_copy(char *dst, const char *src);
12671267
int longest_ancestor_length(const char *path, struct string_list *prefixes);
12681268
char *strip_path_suffix(const char *path, const char *suffix);
1269+
int is_mount_point_via_stat(struct strbuf *path);
12691270
int daemon_avoid_alias(const char *path);
12701271

12711272
/*

compat/mingw.c

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

2466+
int mingw_is_mount_point(struct strbuf *path)
2467+
{
2468+
WIN32_FIND_DATAW findbuf = { 0 };
2469+
HANDLE handle;
2470+
wchar_t wfilename[MAX_PATH];
2471+
int wlen = xutftowcs_path(wfilename, path->buf);
2472+
if (wlen < 0)
2473+
die(_("could not get long path for '%s'"), path->buf);
2474+
2475+
/* remove trailing slash, if any */
2476+
if (wlen > 0 && wfilename[wlen - 1] == L'/')
2477+
wfilename[--wlen] = L'\0';
2478+
2479+
handle = FindFirstFileW(wfilename, &findbuf);
2480+
if (handle == INVALID_HANDLE_VALUE)
2481+
return 0;
2482+
FindClose(handle);
2483+
2484+
return (findbuf.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
2485+
(findbuf.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT);
2486+
}
2487+
24662488
int xutftowcsn(wchar_t *wcs, const char *utfs, size_t wcslen, int utflen)
24672489
{
24682490
int upos = 0, wpos = 0;

compat/mingw.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,9 @@ static inline void convert_slashes(char *path)
443443
if (*path == '\\')
444444
*path = '/';
445445
}
446+
struct strbuf;
447+
int mingw_is_mount_point(struct strbuf *path);
448+
#define is_mount_point mingw_is_mount_point
446449
#define PATH_SEP ';'
447450
char *mingw_query_user_email(void);
448451
#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
@@ -411,6 +411,10 @@ static inline int git_has_dir_sep(const char *path)
411411
#define has_dir_sep(path) git_has_dir_sep(path)
412412
#endif
413413

414+
#ifndef is_mount_point
415+
#define is_mount_point is_mount_point_via_stat
416+
#endif
417+
414418
#ifndef query_user_email
415419
#define query_user_email() NULL
416420
#endif

path.c

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

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

t/t7300-clean.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,4 +788,13 @@ test_expect_success 'traverse into directories that may have ignored entries' '
788788
)
789789
'
790790

791+
test_expect_success MINGW 'clean does not traverse mount points' '
792+
mkdir target &&
793+
>target/dont-clean-me &&
794+
git init with-mountpoint &&
795+
cmd //c "mklink /j with-mountpoint\\mountpoint target" &&
796+
git -C with-mountpoint clean -dfx &&
797+
test_path_is_file target/dont-clean-me
798+
'
799+
791800
test_done

0 commit comments

Comments
 (0)