Skip to content

Commit 3ecaa3b

Browse files
committed
Merge branch 'pc/remove-warn'
* pc/remove-warn: Remove a redundant errno test in a usage of remove_path Introduce remove_or_warn function Implement the rmdir_or_warn function Generalise the unlink_or_warn function
2 parents 43acff3 + 25755e8 commit 3ecaa3b

File tree

6 files changed

+68
-21
lines changed

6 files changed

+68
-21
lines changed

builtin/apply.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3141,11 +3141,7 @@ static void remove_file(struct patch *patch, int rmdir_empty)
31413141
die("unable to remove %s from index", patch->old_name);
31423142
}
31433143
if (!cached) {
3144-
if (S_ISGITLINK(patch->old_mode)) {
3145-
if (rmdir(patch->old_name))
3146-
warning("unable to remove submodule %s",
3147-
patch->old_name);
3148-
} else if (!unlink_or_warn(patch->old_name) && rmdir_empty) {
3144+
if (!remove_or_warn(patch->old_mode, patch->old_name) && rmdir_empty) {
31493145
remove_path(patch->old_name);
31503146
}
31513147
}

git-compat-util.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,5 +479,14 @@ void git_qsort(void *base, size_t nmemb, size_t size,
479479
* Always returns the return value of unlink(2).
480480
*/
481481
int unlink_or_warn(const char *path);
482+
/*
483+
* Likewise for rmdir(2).
484+
*/
485+
int rmdir_or_warn(const char *path);
486+
/*
487+
* Calls the correct function out of {unlink,rmdir}_or_warn based on
488+
* the supplied file mode.
489+
*/
490+
int remove_or_warn(unsigned int mode, const char *path);
482491

483492
#endif

merge-recursive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ static int remove_file(struct merge_options *o, int clean,
409409
return -1;
410410
}
411411
if (update_working_directory) {
412-
if (remove_path(path) && errno != ENOENT)
412+
if (remove_path(path))
413413
return -1;
414414
}
415415
return 0;

t/t4134-apply-submodule.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/sh
2+
#
3+
# Copyright (c) 2010 Peter Collingbourne
4+
#
5+
6+
test_description='git apply submodule tests'
7+
8+
. ./test-lib.sh
9+
10+
test_expect_success setup '
11+
cat > create-sm.patch <<EOF
12+
diff --git a/dir/sm b/dir/sm
13+
new file mode 160000
14+
index 0000000..0123456
15+
--- /dev/null
16+
+++ b/dir/sm
17+
@@ -0,0 +1 @@
18+
+Subproject commit 0123456789abcdef0123456789abcdef01234567
19+
EOF
20+
cat > remove-sm.patch <<EOF
21+
diff --git a/dir/sm b/dir/sm
22+
deleted file mode 160000
23+
index 0123456..0000000
24+
--- a/dir/sm
25+
+++ /dev/null
26+
@@ -1 +0,0 @@
27+
-Subproject commit 0123456789abcdef0123456789abcdef01234567
28+
EOF
29+
'
30+
31+
test_expect_success 'removing a submodule also removes all leading subdirectories' '
32+
git apply --index create-sm.patch &&
33+
test -d dir/sm &&
34+
git apply --index remove-sm.patch &&
35+
test \! -d dir
36+
'
37+
38+
test_done

unpack-trees.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,8 @@ static void unlink_entry(struct cache_entry *ce)
6767
{
6868
if (has_symlink_or_noent_leading_path(ce->name, ce_namelen(ce)))
6969
return;
70-
if (S_ISGITLINK(ce->ce_mode)) {
71-
if (rmdir(ce->name)) {
72-
warning("unable to rmdir %s: %s",
73-
ce->name, strerror(errno));
74-
return;
75-
}
76-
}
77-
else
78-
if (unlink_or_warn(ce->name))
79-
return;
70+
if (remove_or_warn(ce->ce_mode, ce->name))
71+
return;
8072
schedule_dir_for_removal(ce->name, ce_namelen(ce));
8173
}
8274

wrapper.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -311,18 +311,30 @@ int odb_pack_keep(char *name, size_t namesz, unsigned char *sha1)
311311
return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
312312
}
313313

314-
int unlink_or_warn(const char *file)
314+
static int warn_if_unremovable(const char *op, const char *file, int rc)
315315
{
316-
int rc = unlink(file);
317-
318316
if (rc < 0) {
319317
int err = errno;
320318
if (ENOENT != err) {
321-
warning("unable to unlink %s: %s",
322-
file, strerror(errno));
319+
warning("unable to %s %s: %s",
320+
op, file, strerror(errno));
323321
errno = err;
324322
}
325323
}
326324
return rc;
327325
}
328326

327+
int unlink_or_warn(const char *file)
328+
{
329+
return warn_if_unremovable("unlink", file, unlink(file));
330+
}
331+
332+
int rmdir_or_warn(const char *file)
333+
{
334+
return warn_if_unremovable("rmdir", file, rmdir(file));
335+
}
336+
337+
int remove_or_warn(unsigned int mode, const char *file)
338+
{
339+
return S_ISGITLINK(mode) ? rmdir_or_warn(file) : unlink_or_warn(file);
340+
}

0 commit comments

Comments
 (0)