Skip to content

Commit eeb3587

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 11bcf27 + e1c57f4 commit eeb3587

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

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

2541+
int mingw_is_mount_point(struct strbuf *path)
2542+
{
2543+
WIN32_FIND_DATAW findbuf = { 0 };
2544+
HANDLE handle;
2545+
wchar_t wfilename[MAX_PATH];
2546+
int wlen = xutftowcs_path(wfilename, path->buf);
2547+
if (wlen < 0)
2548+
die(_("could not get long path for '%s'"), path->buf);
2549+
2550+
/* remove trailing slash, if any */
2551+
if (wlen > 0 && wfilename[wlen - 1] == L'/')
2552+
wfilename[--wlen] = L'\0';
2553+
2554+
handle = FindFirstFileW(wfilename, &findbuf);
2555+
if (handle == INVALID_HANDLE_VALUE)
2556+
return 0;
2557+
FindClose(handle);
2558+
2559+
return (findbuf.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
2560+
(findbuf.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT);
2561+
}
2562+
25412563
int xutftowcsn(wchar_t *wcs, const char *utfs, size_t wcslen, int utflen)
25422564
{
25432565
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
@@ -582,6 +582,10 @@ static inline int git_has_dir_sep(const char *path)
582582
#define has_dir_sep(path) git_has_dir_sep(path)
583583
#endif
584584

585+
#ifndef is_mount_point
586+
#define is_mount_point is_mount_point_via_stat
587+
#endif
588+
585589
#ifndef query_user_email
586590
#define query_user_email() NULL
587591
#endif

path.c

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

1335+
int is_mount_point_via_stat(struct strbuf *path)
1336+
{
1337+
size_t len = path->len;
1338+
unsigned int current_dev;
1339+
struct stat st;
1340+
1341+
if (!strcmp("/", path->buf))
1342+
return 1;
1343+
1344+
strbuf_addstr(path, "/.");
1345+
if (lstat(path->buf, &st)) {
1346+
/*
1347+
* If we cannot access the current directory, we cannot say
1348+
* that it is a bind mount.
1349+
*/
1350+
strbuf_setlen(path, len);
1351+
return 0;
1352+
}
1353+
current_dev = st.st_dev;
1354+
1355+
/* Now look at the parent directory */
1356+
strbuf_addch(path, '.');
1357+
if (lstat(path->buf, &st)) {
1358+
/*
1359+
* If we cannot access the parent directory, we cannot say
1360+
* that it is a bind mount.
1361+
*/
1362+
strbuf_setlen(path, len);
1363+
return 0;
1364+
}
1365+
strbuf_setlen(path, len);
1366+
1367+
/*
1368+
* If the device ID differs between current and parent directory,
1369+
* then it is a bind mount.
1370+
*/
1371+
return current_dev != st.st_dev;
1372+
}
1373+
13351374
int daemon_avoid_alias(const char *p)
13361375
{
13371376
int sl, ndot;

path.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ int normalize_path_copy(char *dst, const char *src);
196196
int strbuf_normalize_path(struct strbuf *src);
197197
int longest_ancestor_length(const char *path, struct string_list *prefixes);
198198
char *strip_path_suffix(const char *path, const char *suffix);
199+
int is_mount_point_via_stat(struct strbuf *path);
199200
int daemon_avoid_alias(const char *path);
200201

201202
/*

t/t7300-clean.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,4 +794,14 @@ test_expect_success 'traverse into directories that may have ignored entries' '
794794
)
795795
'
796796

797+
test_expect_success MINGW 'clean does not traverse mount points' '
798+
mkdir target &&
799+
>target/dont-clean-me &&
800+
git init with-mountpoint &&
801+
cmd //c "mklink /j with-mountpoint\\mountpoint target" &&
802+
git -C with-mountpoint clean -dfx &&
803+
test_path_is_missing with-mountpoint/mountpoint &&
804+
test_path_is_file target/dont-clean-me
805+
'
806+
797807
test_done

0 commit comments

Comments
 (0)