Skip to content

Commit 901b4b4

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 <johannes.schindelin@gmx.de>
2 parents e2f19a1 + 85f1f4c commit 901b4b4

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

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

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

598+
#ifndef is_mount_point
599+
#define is_mount_point is_mount_point_via_stat
600+
#endif
601+
598602
#ifndef query_user_email
599603
#define query_user_email() NULL
600604
#endif

path.c

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

1270+
int is_mount_point_via_stat(struct strbuf *path)
1271+
{
1272+
size_t len = path->len;
1273+
unsigned int current_dev;
1274+
struct stat st;
1275+
1276+
if (!strcmp("/", path->buf))
1277+
return 1;
1278+
1279+
strbuf_addstr(path, "/.");
1280+
if (lstat(path->buf, &st)) {
1281+
/*
1282+
* If we cannot access the current directory, we cannot say
1283+
* that it is a bind mount.
1284+
*/
1285+
strbuf_setlen(path, len);
1286+
return 0;
1287+
}
1288+
current_dev = st.st_dev;
1289+
1290+
/* Now look at the parent directory */
1291+
strbuf_addch(path, '.');
1292+
if (lstat(path->buf, &st)) {
1293+
/*
1294+
* If we cannot access the parent directory, we cannot say
1295+
* that it is a bind mount.
1296+
*/
1297+
strbuf_setlen(path, len);
1298+
return 0;
1299+
}
1300+
strbuf_setlen(path, len);
1301+
1302+
/*
1303+
* If the device ID differs between current and parent directory,
1304+
* then it is a bind mount.
1305+
*/
1306+
return current_dev != st.st_dev;
1307+
}
1308+
12701309
int daemon_avoid_alias(const char *p)
12711310
{
12721311
int sl, ndot;

path.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ int normalize_path_copy(char *dst, const char *src);
199199
int strbuf_normalize_path(struct strbuf *src);
200200
int longest_ancestor_length(const char *path, struct string_list *prefixes);
201201
char *strip_path_suffix(const char *path, const char *suffix);
202+
int is_mount_point_via_stat(struct strbuf *path);
202203
int daemon_avoid_alias(const char *path);
203204

204205
/*

t/t7300-clean.sh

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

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

0 commit comments

Comments
 (0)