Skip to content

Commit efb2f3f

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 1932d59 + 89459a7 commit efb2f3f

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
@@ -2525,6 +2525,28 @@ pid_t waitpid(pid_t pid, int *status, int options)
25252525
return -1;
25262526
}
25272527

2528+
int mingw_is_mount_point(struct strbuf *path)
2529+
{
2530+
WIN32_FIND_DATAW findbuf = { 0 };
2531+
HANDLE handle;
2532+
wchar_t wfilename[MAX_PATH];
2533+
int wlen = xutftowcs_path(wfilename, path->buf);
2534+
if (wlen < 0)
2535+
die(_("could not get long path for '%s'"), path->buf);
2536+
2537+
/* remove trailing slash, if any */
2538+
if (wlen > 0 && wfilename[wlen - 1] == L'/')
2539+
wfilename[--wlen] = L'\0';
2540+
2541+
handle = FindFirstFileW(wfilename, &findbuf);
2542+
if (handle == INVALID_HANDLE_VALUE)
2543+
return 0;
2544+
FindClose(handle);
2545+
2546+
return (findbuf.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
2547+
(findbuf.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT);
2548+
}
2549+
25282550
int xutftowcsn(wchar_t *wcs, const char *utfs, size_t wcslen, int utflen)
25292551
{
25302552
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
@@ -581,6 +581,10 @@ static inline int git_has_dir_sep(const char *path)
581581
#define has_dir_sep(path) git_has_dir_sep(path)
582582
#endif
583583

584+
#ifndef is_mount_point
585+
#define is_mount_point is_mount_point_via_stat
586+
#endif
587+
584588
#ifndef query_user_email
585589
#define query_user_email() NULL
586590
#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
@@ -198,6 +198,7 @@ int normalize_path_copy(char *dst, const char *src);
198198
int strbuf_normalize_path(struct strbuf *src);
199199
int longest_ancestor_length(const char *path, struct string_list *prefixes);
200200
char *strip_path_suffix(const char *path, const char *suffix);
201+
int is_mount_point_via_stat(struct strbuf *path);
201202
int daemon_avoid_alias(const char *path);
202203

203204
/*

t/t7300-clean.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,4 +789,14 @@ test_expect_success 'traverse into directories that may have ignored entries' '
789789
)
790790
'
791791

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

0 commit comments

Comments
 (0)