Skip to content

Commit a2ca598

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 faf58a9 + 7b7b1fc commit a2ca598

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
@@ -37,6 +37,10 @@ static const char *msg_remove = N_("Removing %s\n");
3737
static const char *msg_would_remove = N_("Would remove %s\n");
3838
static const char *msg_skip_git_dir = N_("Skipping repository %s\n");
3939
static const char *msg_would_skip_git_dir = N_("Would skip repository %s\n");
40+
#ifndef CAN_UNLINK_MOUNT_POINTS
41+
static const char *msg_skip_mount_point = N_("Skipping mount point %s\n");
42+
static const char *msg_would_skip_mount_point = N_("Would skip mount point %s\n");
43+
#endif
4044
static const char *msg_warn_remove_failed = N_("failed to remove %s");
4145
static const char *msg_warn_lstat_failed = N_("could not lstat %s\n");
4246
static const char *msg_skip_cwd = N_("Refusing to remove current working directory\n");
@@ -181,6 +185,29 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
181185
goto out;
182186
}
183187

188+
if (is_mount_point(path)) {
189+
#ifndef CAN_UNLINK_MOUNT_POINTS
190+
if (!quiet) {
191+
quote_path(path->buf, prefix, &quoted, 0);
192+
printf(dry_run ?
193+
_(msg_would_skip_mount_point) :
194+
_(msg_skip_mount_point), quoted.buf);
195+
}
196+
*dir_gone = 0;
197+
#else
198+
if (!dry_run && unlink(path->buf)) {
199+
int saved_errno = errno;
200+
quote_path(path->buf, prefix, &quoted, 0);
201+
errno = saved_errno;
202+
warning_errno(_(msg_warn_remove_failed), quoted.buf);
203+
*dir_gone = 0;
204+
ret = -1;
205+
}
206+
#endif
207+
208+
goto out;
209+
}
210+
184211
dir = opendir(path->buf);
185212
if (!dir) {
186213
/* 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
@@ -2693,6 +2693,28 @@ pid_t waitpid(pid_t pid, int *status, int options)
26932693
return -1;
26942694
}
26952695

2696+
int mingw_is_mount_point(struct strbuf *path)
2697+
{
2698+
WIN32_FIND_DATAW findbuf = { 0 };
2699+
HANDLE handle;
2700+
wchar_t wfilename[MAX_PATH];
2701+
int wlen = xutftowcs_path(wfilename, path->buf);
2702+
if (wlen < 0)
2703+
die(_("could not get long path for '%s'"), path->buf);
2704+
2705+
/* remove trailing slash, if any */
2706+
if (wlen > 0 && wfilename[wlen - 1] == L'/')
2707+
wfilename[--wlen] = L'\0';
2708+
2709+
handle = FindFirstFileW(wfilename, &findbuf);
2710+
if (handle == INVALID_HANDLE_VALUE)
2711+
return 0;
2712+
FindClose(handle);
2713+
2714+
return (findbuf.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
2715+
(findbuf.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT);
2716+
}
2717+
26962718
int xutftowcsn(wchar_t *wcs, const char *utfs, size_t wcslen, int utflen)
26972719
{
26982720
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
@@ -616,6 +616,10 @@ static inline int git_has_dir_sep(const char *path)
616616
#define has_dir_sep(path) git_has_dir_sep(path)
617617
#endif
618618

619+
#ifndef is_mount_point
620+
#define is_mount_point is_mount_point_via_stat
621+
#endif
622+
619623
#ifndef query_user_email
620624
#define query_user_email() NULL
621625
#endif

path.c

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

1229+
int is_mount_point_via_stat(struct strbuf *path)
1230+
{
1231+
size_t len = path->len;
1232+
dev_t current_dev;
1233+
struct stat st;
1234+
1235+
if (!strcmp("/", path->buf))
1236+
return 1;
1237+
1238+
strbuf_addstr(path, "/.");
1239+
if (lstat(path->buf, &st)) {
1240+
/*
1241+
* If we cannot access the current directory, we cannot say
1242+
* that it is a bind mount.
1243+
*/
1244+
strbuf_setlen(path, len);
1245+
return 0;
1246+
}
1247+
current_dev = st.st_dev;
1248+
1249+
/* Now look at the parent directory */
1250+
strbuf_addch(path, '.');
1251+
if (lstat(path->buf, &st)) {
1252+
/*
1253+
* If we cannot access the parent directory, we cannot say
1254+
* that it is a bind mount.
1255+
*/
1256+
strbuf_setlen(path, len);
1257+
return 0;
1258+
}
1259+
strbuf_setlen(path, len);
1260+
1261+
/*
1262+
* If the device ID differs between current and parent directory,
1263+
* then it is a bind mount.
1264+
*/
1265+
return current_dev != st.st_dev;
1266+
}
1267+
12291268
int daemon_avoid_alias(const char *p)
12301269
{
12311270
int sl, ndot;

path.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ int normalize_path_copy(char *dst, const char *src);
183183
int strbuf_normalize_path(struct strbuf *src);
184184
int longest_ancestor_length(const char *path, struct string_list *prefixes);
185185
char *strip_path_suffix(const char *path, const char *suffix);
186+
int is_mount_point_via_stat(struct strbuf *path);
186187
int daemon_avoid_alias(const char *path);
187188

188189
/*

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)