Skip to content

Commit ee98317

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 59a35b5 + 83647ea commit ee98317

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
@@ -1288,6 +1288,7 @@ int normalize_path_copy_len(char *dst, const char *src, int *prefix_len);
12881288
int normalize_path_copy(char *dst, const char *src);
12891289
int longest_ancestor_length(const char *path, struct string_list *prefixes);
12901290
char *strip_path_suffix(const char *path, const char *suffix);
1291+
int is_mount_point_via_stat(struct strbuf *path);
12911292
int daemon_avoid_alias(const char *path);
12921293

12931294
/*

compat/mingw.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2315,6 +2315,28 @@ pid_t waitpid(pid_t pid, int *status, int options)
23152315
return -1;
23162316
}
23172317

2318+
int mingw_is_mount_point(struct strbuf *path)
2319+
{
2320+
WIN32_FIND_DATAW findbuf = { 0 };
2321+
HANDLE handle;
2322+
wchar_t wfilename[MAX_PATH];
2323+
int wlen = xutftowcs_path(wfilename, path->buf);
2324+
if (wlen < 0)
2325+
die(_("could not get long path for '%s'"), path->buf);
2326+
2327+
/* remove trailing slash, if any */
2328+
if (wlen > 0 && wfilename[wlen - 1] == L'/')
2329+
wfilename[--wlen] = L'\0';
2330+
2331+
handle = FindFirstFileW(wfilename, &findbuf);
2332+
if (handle == INVALID_HANDLE_VALUE)
2333+
return 0;
2334+
FindClose(handle);
2335+
2336+
return (findbuf.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
2337+
(findbuf.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT);
2338+
}
2339+
23182340
int xutftowcsn(wchar_t *wcs, const char *utfs, size_t wcslen, int utflen)
23192341
{
23202342
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
@@ -409,6 +409,10 @@ static inline char *git_find_last_dir_sep(const char *path)
409409
#define find_last_dir_sep git_find_last_dir_sep
410410
#endif
411411

412+
#ifndef is_mount_point
413+
#define is_mount_point is_mount_point_via_stat
414+
#endif
415+
412416
#ifndef query_user_email
413417
#define query_user_email() NULL
414418
#endif

path.c

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

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

t/t7300-clean.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,4 +737,14 @@ test_expect_success MINGW 'handle clean & core.longpaths = false nicely' '
737737
test_i18ngrep "too long" .git/err
738738
'
739739

740+
test_expect_success MINGW 'clean does not traverse mount points' '
741+
mkdir target &&
742+
>target/dont-clean-me &&
743+
git init with-mountpoint &&
744+
cmd //c "mklink /j with-mountpoint\\mountpoint target" &&
745+
git -C with-mountpoint clean -dfx &&
746+
test_path_is_missing with-mountpoint/mountpoint &&
747+
test_path_is_file target/dont-clean-me
748+
'
749+
740750
test_done

0 commit comments

Comments
 (0)