{"thread":{"id":"6491","subject":"[PATCH 1/4] Move remove_path() into libgit.a","startedAt":"2007-01-24T01:13:31Z","lastAt":"2007-01-25T00:07:16Z","messageCount":2,"participants":["Johannes Schindelin","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":4},"messages":[{"id":"32466","messageId":"Pine.LNX.4.63.0701240213020.22628@wbgn013.biozentrum.uni-wuerzburg.de","threadId":"6491","inReplyTo":null,"subject":"[PATCH 1/4] Move remove_path() into libgit.a","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2007-01-24T01:13:31Z","receivedAt":"2007-01-24T01:13:31Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"\nRename the function remove_path()  to remove_path_recursively(),\nand move it to path.c.\n\nThis also fixes a bug in merge-recursive, where a now-empty\ndirectory was not removed.\n\nSigned-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>\n---\n cache.h           |    1 +\n merge-recursive.c |   24 +-----------------------\n path.c            |   25 +++++++++++++++++++++++++\n 3 files changed, 27 insertions(+), 23 deletions(-)\n\ndiff --git a/cache.h b/cache.h\nindex a435781..64ea1dc 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -240,6 +240,7 @@ static inline void hashclr(unsigned char *hash)\n }\n \n int git_mkstemp(char *path, size_t n, const char *template);\n+int remove_path_recursively(const char *name);\n \n enum sharedrepo {\n \tPERM_UMASK = 0,\ndiff --git a/merge-recursive.c b/merge-recursive.c\nindex fa320eb..091177d 100644\n--- a/merge-recursive.c\n+++ b/merge-recursive.c\n@@ -463,28 +463,6 @@ static int update_stages(const char *path, struct diff_filespec *o,\n \treturn 0;\n }\n \n-static int remove_path(const char *name)\n-{\n-\tint ret, len;\n-\tchar *slash, *dirs;\n-\n-\tret = unlink(name);\n-\tif (ret)\n-\t\treturn ret;\n-\tlen = strlen(name);\n-\tdirs = xmalloc(len+1);\n-\tmemcpy(dirs, name, len);\n-\tdirs[len] = '\\0';\n-\twhile ((slash = strrchr(name, '/'))) {\n-\t\t*slash = '\\0';\n-\t\tlen = slash - name;\n-\t\tif (rmdir(name) != 0)\n-\t\t\tbreak;\n-\t}\n-\tfree(dirs);\n-\treturn ret;\n-}\n-\n static int remove_file(int clean, const char *path, int no_wd)\n {\n \tint update_cache = index_only || clean;\n@@ -498,7 +476,7 @@ static int remove_file(int clean, const char *path, int no_wd)\n \t\tunlink(path);\n \t\tif (errno != ENOENT || errno != EISDIR)\n \t\t\treturn -1;\n-\t\tremove_path(path);\n+\t\tremove_path_recursively(path);\n \t}\n \treturn 0;\n }\ndiff --git a/path.c b/path.c\nindex c5d25a4..b1f64e6 100644\n--- a/path.c\n+++ b/path.c\n@@ -292,3 +292,28 @@ int adjust_shared_perm(const char *path)\n \t\treturn -2;\n \treturn 0;\n }\n+\n+/* this also removes the directories which became empty */\n+int remove_path_recursively(const char *name)\n+{\n+\tint ret, len;\n+\tchar *slash, *dirs;\n+\n+\tret = unlink(name);\n+\tif (ret == ENOENT || ret == EISDIR)\n+\t\tret = 0;\n+\tif (ret)\n+\t\treturn ret;\n+\tlen = strlen(name);\n+\tdirs = xmalloc(len+1);\n+\tmemcpy(dirs, name, len);\n+\tdirs[len] = '\\0';\n+\twhile ((slash = strrchr(name, '/'))) {\n+\t\t*slash = '\\0';\n+\t\tlen = slash - name;\n+\t\tif (rmdir(name) != 0)\n+\t\t\tbreak;\n+\t}\n+\tfree(dirs);\n+\treturn ret;\n+}\n-- \n1.5.0.rc2.gc5868-dirty\n"},{"id":"32574","messageId":"7v7ivcdl57.fsf@assigned-by-dhcp.cox.net","threadId":"6491","inReplyTo":"Pine.LNX.4.63.0701240213020.22628@wbgn013.biozentrum.uni-wuerzburg.de","subject":"Re: [PATCH 1/4] Move remove_path() into libgit.a","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2007-01-25T00:07:16Z","receivedAt":"2007-01-25T00:07:16Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"We seem to have variants of the seme theme in many places.\n\n * builtin-apply.c::remove_file()\n * builtin-rm.c::remove_file() -- this one leaks the strdup result\n * refs.c::remove_empty_dir_recursive()\n * unpack-trees.c::unlink_entry() \n * mrege-recursive.c::remove_path()\n\nIt would be nicer to consolidate their \"remove the now\npotentially empty directories recursively\" part in a single\nfunction and have them call it.\n\nSome of them try to be more efficient by not allocating an extra\nstring (because they know the string supplied by the caller is a\nfair game), so the clean-up might be slightly involved, but I\nthink it is worth it.\n\n \n\t\n"}]}