{"thread":{"id":"39374","subject":"[PATCH] use file_exists() to check if a file exists in the worktree","startedAt":"2015-05-19T21:44:23Z","lastAt":"2015-05-19T21:44:23Z","messageCount":1,"participants":["René Scharfe"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"261602","messageId":"555BAEB7.7000807@web.de","threadId":"39374","inReplyTo":null,"subject":"[PATCH] use file_exists() to check if a file exists in the worktree","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2015-05-19T21:44:23Z","receivedAt":"2015-05-19T21:44:23Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Call file_exists() instead of open-coding it.  That's shorter, simpler\nand the intent becomes clearer.\n\nSigned-off-by: Rene Scharfe <l.s.r@web.de>\n---\n builtin/blame.c   | 15 +++------------\n builtin/rm.c      |  3 +--\n merge-recursive.c |  3 +--\n sha1_name.c       |  7 +++----\n submodule.c       |  3 +--\n 5 files changed, 9 insertions(+), 22 deletions(-)\n\ndiff --git a/builtin/blame.c b/builtin/blame.c\nindex 8d70623..ff97825 100644\n--- a/builtin/blame.c\n+++ b/builtin/blame.c\n@@ -26,6 +26,7 @@\n #include \"userdiff.h\"\n #include \"line-range.h\"\n #include \"line-log.h\"\n+#include \"dir.h\"\n \n static char blame_usage[] = N_(\"git blame [<options>] [<rev-opts>] [<rev>] [--] file\");\n \n@@ -2151,16 +2152,6 @@ static void sanity_check_refcnt(struct scoreboard *sb)\n \t}\n }\n \n-/*\n- * Used for the command line parsing; check if the path exists\n- * in the working tree.\n- */\n-static int has_string_in_work_tree(const char *path)\n-{\n-\tstruct stat st;\n-\treturn !lstat(path, &st);\n-}\n-\n static unsigned parse_score(const char *arg)\n {\n \tchar *end;\n@@ -2656,14 +2647,14 @@ parse_done:\n \t\tif (argc < 2)\n \t\t\tusage_with_options(blame_opt_usage, options);\n \t\tpath = add_prefix(prefix, argv[argc - 1]);\n-\t\tif (argc == 3 && !has_string_in_work_tree(path)) { /* (2b) */\n+\t\tif (argc == 3 && !file_exists(path)) { /* (2b) */\n \t\t\tpath = add_prefix(prefix, argv[1]);\n \t\t\targv[1] = argv[2];\n \t\t}\n \t\targv[argc - 1] = \"--\";\n \n \t\tsetup_work_tree();\n-\t\tif (!has_string_in_work_tree(path))\n+\t\tif (!file_exists(path))\n \t\t\tdie_errno(\"cannot stat path '%s'\", path);\n \t}\n \ndiff --git a/builtin/rm.c b/builtin/rm.c\nindex 3304bff..80b972f 100644\n--- a/builtin/rm.c\n+++ b/builtin/rm.c\n@@ -84,7 +84,6 @@ static int check_submodules_use_gitfiles(void)\n \t\tconst char *name = list.entry[i].name;\n \t\tint pos;\n \t\tconst struct cache_entry *ce;\n-\t\tstruct stat st;\n \n \t\tpos = cache_name_pos(name, strlen(name));\n \t\tif (pos < 0) {\n@@ -95,7 +94,7 @@ static int check_submodules_use_gitfiles(void)\n \t\tce = active_cache[pos];\n \n \t\tif (!S_ISGITLINK(ce->ce_mode) ||\n-\t\t    (lstat(ce->name, &st) < 0) ||\n+\t\t    !file_exists(ce->name) ||\n \t\t    is_empty_dir(name))\n \t\t\tcontinue;\n \ndiff --git a/merge-recursive.c b/merge-recursive.c\nindex 1c9c30d..44d85be 100644\n--- a/merge-recursive.c\n+++ b/merge-recursive.c\n@@ -611,7 +611,6 @@ static char *unique_path(struct merge_options *o, const char *path, const char *\n {\n \tstruct strbuf newpath = STRBUF_INIT;\n \tint suffix = 0;\n-\tstruct stat st;\n \tsize_t base_len;\n \n \tstrbuf_addf(&newpath, \"%s~\", path);\n@@ -620,7 +619,7 @@ static char *unique_path(struct merge_options *o, const char *path, const char *\n \tbase_len = newpath.len;\n \twhile (string_list_has_string(&o->current_file_set, newpath.buf) ||\n \t       string_list_has_string(&o->current_directory_set, newpath.buf) ||\n-\t       lstat(newpath.buf, &st) == 0) {\n+\t       file_exists(newpath.buf)) {\n \t\tstrbuf_setlen(&newpath, base_len);\n \t\tstrbuf_addf(&newpath, \"_%d\", suffix++);\n \t}\ndiff --git a/sha1_name.c b/sha1_name.c\nindex 6d10f05..6de8c87 100644\n--- a/sha1_name.c\n+++ b/sha1_name.c\n@@ -6,6 +6,7 @@\n #include \"tree-walk.h\"\n #include \"refs.h\"\n #include \"remote.h\"\n+#include \"dir.h\"\n \n static int get_sha1_oneline(const char *, unsigned char *, struct commit_list *);\n \n@@ -1237,14 +1238,13 @@ static void diagnose_invalid_sha1_path(const char *prefix,\n \t\t\t\t       const char *object_name,\n \t\t\t\t       int object_name_len)\n {\n-\tstruct stat st;\n \tunsigned char sha1[20];\n \tunsigned mode;\n \n \tif (!prefix)\n \t\tprefix = \"\";\n \n-\tif (!lstat(filename, &st))\n+\tif (file_exists(filename))\n \t\tdie(\"Path '%s' exists on disk, but not in '%.*s'.\",\n \t\t    filename, object_name_len, object_name);\n \tif (errno == ENOENT || errno == ENOTDIR) {\n@@ -1271,7 +1271,6 @@ static void diagnose_invalid_index_path(int stage,\n \t\t\t\t\tconst char *prefix,\n \t\t\t\t\tconst char *filename)\n {\n-\tstruct stat st;\n \tconst struct cache_entry *ce;\n \tint pos;\n \tunsigned namelen = strlen(filename);\n@@ -1314,7 +1313,7 @@ static void diagnose_invalid_index_path(int stage,\n \t\t\t    ce_stage(ce), filename);\n \t}\n \n-\tif (!lstat(filename, &st))\n+\tif (file_exists(filename))\n \t\tdie(\"Path '%s' exists on disk, but not in the index.\", filename);\n \tif (errno == ENOENT || errno == ENOTDIR)\n \t\tdie(\"Path '%s' does not exist (neither on disk nor in the index).\",\ndiff --git a/submodule.c b/submodule.c\nindex d491e6a..b8747f5 100644\n--- a/submodule.c\n+++ b/submodule.c\n@@ -891,7 +891,6 @@ int submodule_uses_gitfile(const char *path)\n \n int ok_to_remove_submodule(const char *path)\n {\n-\tstruct stat st;\n \tssize_t len;\n \tstruct child_process cp = CHILD_PROCESS_INIT;\n \tconst char *argv[] = {\n@@ -904,7 +903,7 @@ int ok_to_remove_submodule(const char *path)\n \tstruct strbuf buf = STRBUF_INIT;\n \tint ok_to_remove = 1;\n \n-\tif ((lstat(path, &st) < 0) || is_empty_dir(path))\n+\tif (!file_exists(path) || is_empty_dir(path))\n \t\treturn 1;\n \n \tif (!submodule_uses_gitfile(path))\n-- \n2.4.1\n"}]}