Skip to content

Commit 08f9d84

Browse files
committed
Merge branch 'dont-clean-junctions'
This topic branch teaches `git clean` to respect NTFS junctions and Unix bind mounts: it will now stop at those boundaries. Signed-off-by: Johannes Schindelin <[email protected]>
2 parents 5fee4d5 + 9f47325 commit 08f9d84

File tree

7 files changed

+107
-0
lines changed

7 files changed

+107
-0
lines changed

builtin/clean.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ 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+
#ifndef CAN_UNLINK_MOUNT_POINTS
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");
39+
#endif
3640
static const char *msg_warn_remove_failed = N_("failed to remove %s");
3741
static const char *msg_warn_lstat_failed = N_("could not lstat %s\n");
3842

@@ -170,6 +174,29 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
170174
goto out;
171175
}
172176

177+
if (is_mount_point(path)) {
178+
#ifndef CAN_UNLINK_MOUNT_POINTS
179+
if (!quiet) {
180+
quote_path_relative(path->buf, prefix, &quoted);
181+
printf(dry_run ?
182+
_(msg_would_skip_mount_point) :
183+
_(msg_skip_mount_point), quoted.buf);
184+
}
185+
*dir_gone = 0;
186+
#else
187+
if (!dry_run && unlink(path->buf)) {
188+
int saved_errno = errno;
189+
quote_path_relative(path->buf, prefix, &quoted);
190+
errno = saved_errno;
191+
warning_errno(_(msg_warn_remove_failed), quoted.buf);
192+
*dir_gone = 0;
193+
ret = -1;
194+
}
195+
#endif
196+
197+
goto out;
198+
}
199+
173200
dir = opendir(path->buf);
174201
if (!dir) {
175202
/* 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
@@ -1325,6 +1325,7 @@ int normalize_path_copy_len(char *dst, const char *src, int *prefix_len);
13251325
int normalize_path_copy(char *dst, const char *src);
13261326
int longest_ancestor_length(const char *path, struct string_list *prefixes);
13271327
char *strip_path_suffix(const char *path, const char *suffix);
1328+
int is_mount_point_via_stat(struct strbuf *path);
13281329
int daemon_avoid_alias(const char *path);
13291330

13301331
/*

compat/mingw.c

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

2279+
int mingw_is_mount_point(struct strbuf *path)
2280+
{
2281+
WIN32_FIND_DATAW findbuf = { 0 };
2282+
HANDLE handle;
2283+
wchar_t wfilename[MAX_PATH];
2284+
int wlen = xutftowcs_path(wfilename, path->buf);
2285+
if (wlen < 0)
2286+
die(_("could not get long path for '%s'"), path->buf);
2287+
2288+
/* remove trailing slash, if any */
2289+
if (wlen > 0 && wfilename[wlen - 1] == L'/')
2290+
wfilename[--wlen] = L'\0';
2291+
2292+
handle = FindFirstFileW(wfilename, &findbuf);
2293+
if (handle == INVALID_HANDLE_VALUE)
2294+
return 0;
2295+
FindClose(handle);
2296+
2297+
return (findbuf.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
2298+
(findbuf.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT);
2299+
}
2300+
22792301
int xutftowcsn(wchar_t *wcs, const char *utfs, size_t wcslen, int utflen)
22802302
{
22812303
int upos = 0, wpos = 0;

compat/mingw.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,10 @@ 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
448+
#define CAN_UNLINK_MOUNT_POINTS 1
445449
#define PATH_SEP ';'
446450
char *mingw_query_user_email(void);
447451
#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
@@ -405,6 +405,10 @@ static inline char *git_find_last_dir_sep(const char *path)
405405
#define find_last_dir_sep git_find_last_dir_sep
406406
#endif
407407

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

path.c

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

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

t/t7300-clean.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,4 +737,14 @@ 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_missing with-mountpoint/mountpoint &&
747+
test_path_is_file target/dont-clean-me
748+
'
749+
740750
test_done

0 commit comments

Comments
 (0)