[PATCH 1/2] Add option to not delete a .git directory in remove_dir_recursively()
From: Jason Holden <hidden>
Date: 2016-06-15 22:47:00
Subsystem:
the rest · Maintainer:
Linus Torvalds
Because all existing calls to remove_dir_recursively() do not currently have this protection, default all existing calls to 0 (to not keep .git directories) Signed-off-by: Jason Holden <redacted> --- builtin-clean.c | 2 +- builtin-clone.c | 4 ++-- dir.c | 17 +++++++++++++++-- dir.h | 2 +- refs.c | 2 +- transport.c | 4 ++-- 6 files changed, 22 insertions(+), 9 deletions(-)
diff --git a/builtin-clean.c b/builtin-clean.c
index 1c1b6d2..cd82407 100644
--- a/builtin-clean.c
+++ b/builtin-clean.c@@ -141,7 +141,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix) (matches == MATCHED_EXACTLY)) { if (!quiet) printf("Removing %s\n", qname); - if (remove_dir_recursively(&directory, 0) != 0) { + if (remove_dir_recursively(&directory, 0, 0) != 0) { warning("failed to remove '%s'", qname); errors++; }
diff --git a/builtin-clone.c b/builtin-clone.c
index 2ceacb7..0c00c87 100644
--- a/builtin-clone.c
+++ b/builtin-clone.c@@ -304,12 +304,12 @@ static void remove_junk(void) return; if (junk_git_dir) { strbuf_addstr(&sb, junk_git_dir); - remove_dir_recursively(&sb, 0); + remove_dir_recursively(&sb, 0, 0); strbuf_reset(&sb); } if (junk_work_tree) { strbuf_addstr(&sb, junk_work_tree); - remove_dir_recursively(&sb, 0); + remove_dir_recursively(&sb, 0, 0); strbuf_reset(&sb); } }
diff --git a/dir.c b/dir.c
index bbfcb56..eadcddd 100644
--- a/dir.c
+++ b/dir.c@@ -800,7 +800,7 @@ int is_empty_dir(const char *path) return ret; } -int remove_dir_recursively(struct strbuf *path, int only_empty) +int remove_dir_recursively(struct strbuf *path, int only_empty, int keep_dot_git) { DIR *dir = opendir(path->buf); struct dirent *e;
@@ -812,6 +812,19 @@ int remove_dir_recursively(struct strbuf *path, int only_empty) strbuf_addch(path, '/'); len = path->len; + + if (keep_dot_git) { + char end_of_path[6]; /* enough space for ".git/"*/ + memset(end_of_path, '\0', 6); + if (len >= 5) { + strncpy(end_of_path, path->buf + len - 5, 5); + if (strcmp(end_of_path, ".git/") == 0) { + printf("********Found .git!!!! Skipping delete\n"); + return 0; + } + } + } + while ((e = readdir(dir)) != NULL) { struct stat st; if (is_dot_or_dotdot(e->d_name))
@@ -822,7 +835,7 @@ int remove_dir_recursively(struct strbuf *path, int only_empty) if (lstat(path->buf, &st)) ; /* fall thru */ else if (S_ISDIR(st.st_mode)) { - if (!remove_dir_recursively(path, only_empty)) + if (!remove_dir_recursively(path, only_empty, keep_dot_git)) continue; /* happy */ } else if (!only_empty && !unlink(path->buf)) continue; /* happy, too */
diff --git a/dir.h b/dir.h
index 541286a..8273bb9 100644
--- a/dir.h
+++ b/dir.h@@ -89,7 +89,7 @@ static inline int is_dot_or_dotdot(const char *name) extern int is_empty_dir(const char *dir); extern void setup_standard_excludes(struct dir_struct *dir); -extern int remove_dir_recursively(struct strbuf *path, int only_empty); +extern int remove_dir_recursively(struct strbuf *path, int only_empty, int keep_dot_git); /* tries to remove the path with empty directories along it, ignores ENOENT */ extern int remove_path(const char *path);
diff --git a/refs.c b/refs.c
index 24438c6..4ddb361 100644
--- a/refs.c
+++ b/refs.c@@ -820,7 +820,7 @@ static int remove_empty_directories(const char *file) strbuf_init(&path, 20); strbuf_addstr(&path, file); - result = remove_dir_recursively(&path, 1); + result = remove_dir_recursively(&path, 1, 0); strbuf_release(&path);
diff --git a/transport.c b/transport.c
index 501a77b..067d6b1 100644
--- a/transport.c
+++ b/transport.c@@ -196,7 +196,7 @@ static struct ref *get_refs_via_rsync(struct transport *transport, int for_push) insert_packed_refs(temp_dir.buf, &tail); strbuf_setlen(&temp_dir, temp_dir_len); - if (remove_dir_recursively(&temp_dir, 0)) + if (remove_dir_recursively(&temp_dir, 0, 0)) warning ("Error removing temporary directory %s.", temp_dir.buf);
@@ -342,7 +342,7 @@ static int rsync_transport_push(struct transport *transport, result = error("Could not push to %s", rsync_url(transport->url)); - if (remove_dir_recursively(&temp_dir, 0)) + if (remove_dir_recursively(&temp_dir, 0, 0)) warning ("Could not remove temporary directory %s.", temp_dir.buf);
--
1.6.3.2.207.ga8208