Skip to content

Commit 9863110

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 2c35e3f + d6ae4d0 commit 9863110

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
@@ -40,6 +40,10 @@ static const char *msg_remove = N_("Removing %s\n");
4040
static const char *msg_would_remove = N_("Would remove %s\n");
4141
static const char *msg_skip_git_dir = N_("Skipping repository %s\n");
4242
static const char *msg_would_skip_git_dir = N_("Would skip repository %s\n");
43+
#ifndef CAN_UNLINK_MOUNT_POINTS
44+
static const char *msg_skip_mount_point = N_("Skipping mount point %s\n");
45+
static const char *msg_would_skip_mount_point = N_("Would skip mount point %s\n");
46+
#endif
4347
static const char *msg_warn_remove_failed = N_("failed to remove %s");
4448
static const char *msg_warn_lstat_failed = N_("could not lstat %s\n");
4549
static const char *msg_skip_cwd = N_("Refusing to remove current working directory\n");
@@ -184,6 +188,29 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
184188
goto out;
185189
}
186190

191+
if (is_mount_point(path)) {
192+
#ifndef CAN_UNLINK_MOUNT_POINTS
193+
if (!quiet) {
194+
quote_path(path->buf, prefix, &quoted, 0);
195+
printf(dry_run ?
196+
_(msg_would_skip_mount_point) :
197+
_(msg_skip_mount_point), quoted.buf);
198+
}
199+
*dir_gone = 0;
200+
#else
201+
if (!dry_run && unlink(path->buf)) {
202+
int saved_errno = errno;
203+
quote_path(path->buf, prefix, &quoted, 0);
204+
errno = saved_errno;
205+
warning_errno(_(msg_warn_remove_failed), quoted.buf);
206+
*dir_gone = 0;
207+
ret = -1;
208+
}
209+
#endif
210+
211+
goto out;
212+
}
213+
187214
dir = opendir(path->buf);
188215
if (!dir) {
189216
/* an empty dir could be removed even if it is unreadble */

compat/mingw.c

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

2699+
int mingw_is_mount_point(struct strbuf *path)
2700+
{
2701+
WIN32_FIND_DATAW findbuf = { 0 };
2702+
HANDLE handle;
2703+
wchar_t wfilename[MAX_PATH];
2704+
int wlen = xutftowcs_path(wfilename, path->buf);
2705+
if (wlen < 0)
2706+
die(_("could not get long path for '%s'"), path->buf);
2707+
2708+
/* remove trailing slash, if any */
2709+
if (wlen > 0 && wfilename[wlen - 1] == L'/')
2710+
wfilename[--wlen] = L'\0';
2711+
2712+
handle = FindFirstFileW(wfilename, &findbuf);
2713+
if (handle == INVALID_HANDLE_VALUE)
2714+
return 0;
2715+
FindClose(handle);
2716+
2717+
return (findbuf.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
2718+
(findbuf.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT);
2719+
}
2720+
26992721
int xutftowcsn(wchar_t *wcs, const char *utfs, size_t wcslen, int utflen)
27002722
{
27012723
int upos = 0, wpos = 0;

compat/mingw.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,10 @@ static inline void convert_slashes(char *path)
454454
if (*path == '\\')
455455
*path = '/';
456456
}
457+
struct strbuf;
458+
int mingw_is_mount_point(struct strbuf *path);
459+
#define is_mount_point mingw_is_mount_point
460+
#define CAN_UNLINK_MOUNT_POINTS 1
457461
#define PATH_SEP ';'
458462
char *mingw_query_user_email(void);
459463
#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
@@ -626,6 +626,10 @@ static inline int git_has_dir_sep(const char *path)
626626
#define has_dir_sep(path) git_has_dir_sep(path)
627627
#endif
628628

629+
#ifndef is_mount_point
630+
#define is_mount_point is_mount_point_via_stat
631+
#endif
632+
629633
#ifndef query_user_email
630634
#define query_user_email() NULL
631635
#endif

path.c

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

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

path.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ int normalize_path_copy(char *dst, const char *src);
172172
int strbuf_normalize_path(struct strbuf *src);
173173
int longest_ancestor_length(const char *path, struct string_list *prefixes);
174174
char *strip_path_suffix(const char *path, const char *suffix);
175+
int is_mount_point_via_stat(struct strbuf *path);
175176
int daemon_avoid_alias(const char *path);
176177

177178
/*

t/t7300-clean.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,4 +800,14 @@ test_expect_success 'traverse into directories that may have ignored entries' '
800800
)
801801
'
802802

803+
test_expect_success MINGW 'clean does not traverse mount points' '
804+
mkdir target &&
805+
>target/dont-clean-me &&
806+
git init with-mountpoint &&
807+
cmd //c "mklink /j with-mountpoint\\mountpoint target" &&
808+
git -C with-mountpoint clean -dfx &&
809+
test_path_is_missing with-mountpoint/mountpoint &&
810+
test_path_is_file target/dont-clean-me
811+
'
812+
803813
test_done

0 commit comments

Comments
 (0)