Skip to content

Commit 154cd45

Browse files
dschoGit for Windows Build Agent
authored andcommitted
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 7360e2d + aaf8e16 commit 154cd45

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
@@ -2442,6 +2442,28 @@ pid_t waitpid(pid_t pid, int *status, int options)
24422442
return -1;
24432443
}
24442444

2445+
int mingw_is_mount_point(struct strbuf *path)
2446+
{
2447+
WIN32_FIND_DATAW findbuf = { 0 };
2448+
HANDLE handle;
2449+
wchar_t wfilename[MAX_PATH];
2450+
int wlen = xutftowcs_path(wfilename, path->buf);
2451+
if (wlen < 0)
2452+
die(_("could not get long path for '%s'"), path->buf);
2453+
2454+
/* remove trailing slash, if any */
2455+
if (wlen > 0 && wfilename[wlen - 1] == L'/')
2456+
wfilename[--wlen] = L'\0';
2457+
2458+
handle = FindFirstFileW(wfilename, &findbuf);
2459+
if (handle == INVALID_HANDLE_VALUE)
2460+
return 0;
2461+
FindClose(handle);
2462+
2463+
return (findbuf.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
2464+
(findbuf.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT);
2465+
}
2466+
24452467
int xutftowcsn(wchar_t *wcs, const char *utfs, size_t wcslen, int utflen)
24462468
{
24472469
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
@@ -389,6 +389,10 @@ static inline char *git_find_last_dir_sep(const char *path)
389389
#define find_last_dir_sep git_find_last_dir_sep
390390
#endif
391391

392+
#ifndef is_mount_point
393+
#define is_mount_point is_mount_point_via_stat
394+
#endif
395+
392396
#ifndef query_user_email
393397
#define query_user_email() NULL
394398
#endif

path.c

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

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

t/t7300-clean.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,4 +746,14 @@ test_expect_success 'clean untracked paths by pathspec' '
746746
test_must_be_empty actual
747747
'
748748

749+
test_expect_success MINGW 'clean does not traverse mount points' '
750+
mkdir target &&
751+
>target/dont-clean-me &&
752+
git init with-mountpoint &&
753+
cmd //c "mklink /j with-mountpoint\\mountpoint target" &&
754+
git -C with-mountpoint clean -dfx &&
755+
test_path_is_missing with-mountpoint/mountpoint &&
756+
test_path_is_file target/dont-clean-me
757+
'
758+
749759
test_done

0 commit comments

Comments
 (0)