From dbe44faadb87f88e092f3dac387f96070268137a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Tue, 19 May 2015 23:44:23 +0200 Subject: use file_exists() to check if a file exists in the worktree Call file_exists() instead of open-coding it. That's shorter, simpler and the intent becomes clearer. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano diff --git a/builtin/blame.c b/builtin/blame.c index 06484c2..91d4221 100644 --- a/builtin/blame.c +++ b/builtin/blame.c @@ -26,6 +26,7 @@ #include "userdiff.h" #include "line-range.h" #include "line-log.h" +#include "dir.h" static char blame_usage[] = N_("git blame [] [] [] [--] file"); @@ -2151,16 +2152,6 @@ static void sanity_check_refcnt(struct scoreboard *sb) } } -/* - * Used for the command line parsing; check if the path exists - * in the working tree. - */ -static int has_string_in_work_tree(const char *path) -{ - struct stat st; - return !lstat(path, &st); -} - static unsigned parse_score(const char *arg) { char *end; @@ -2655,14 +2646,14 @@ parse_done: if (argc < 2) usage_with_options(blame_opt_usage, options); path = add_prefix(prefix, argv[argc - 1]); - if (argc == 3 && !has_string_in_work_tree(path)) { /* (2b) */ + if (argc == 3 && !file_exists(path)) { /* (2b) */ path = add_prefix(prefix, argv[1]); argv[1] = argv[2]; } argv[argc - 1] = "--"; setup_work_tree(); - if (!has_string_in_work_tree(path)) + if (!file_exists(path)) die_errno("cannot stat path '%s'", path); } diff --git a/builtin/rm.c b/builtin/rm.c index 3304bff..80b972f 100644 --- a/builtin/rm.c +++ b/builtin/rm.c @@ -84,7 +84,6 @@ static int check_submodules_use_gitfiles(void) const char *name = list.entry[i].name; int pos; const struct cache_entry *ce; - struct stat st; pos = cache_name_pos(name, strlen(name)); if (pos < 0) { @@ -95,7 +94,7 @@ static int check_submodules_use_gitfiles(void) ce = active_cache[pos]; if (!S_ISGITLINK(ce->ce_mode) || - (lstat(ce->name, &st) < 0) || + !file_exists(ce->name) || is_empty_dir(name)) continue; diff --git a/merge-recursive.c b/merge-recursive.c index 1c9c30d..44d85be 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -611,7 +611,6 @@ static char *unique_path(struct merge_options *o, const char *path, const char * { struct strbuf newpath = STRBUF_INIT; int suffix = 0; - struct stat st; size_t base_len; strbuf_addf(&newpath, "%s~", path); @@ -620,7 +619,7 @@ static char *unique_path(struct merge_options *o, const char *path, const char * base_len = newpath.len; while (string_list_has_string(&o->current_file_set, newpath.buf) || string_list_has_string(&o->current_directory_set, newpath.buf) || - lstat(newpath.buf, &st) == 0) { + file_exists(newpath.buf)) { strbuf_setlen(&newpath, base_len); strbuf_addf(&newpath, "_%d", suffix++); } diff --git a/sha1_name.c b/sha1_name.c index 6d10f05..6de8c87 100644 --- a/sha1_name.c +++ b/sha1_name.c @@ -6,6 +6,7 @@ #include "tree-walk.h" #include "refs.h" #include "remote.h" +#include "dir.h" static int get_sha1_oneline(const char *, unsigned char *, struct commit_list *); @@ -1237,14 +1238,13 @@ static void diagnose_invalid_sha1_path(const char *prefix, const char *object_name, int object_name_len) { - struct stat st; unsigned char sha1[20]; unsigned mode; if (!prefix) prefix = ""; - if (!lstat(filename, &st)) + if (file_exists(filename)) die("Path '%s' exists on disk, but not in '%.*s'.", filename, object_name_len, object_name); if (errno == ENOENT || errno == ENOTDIR) { @@ -1271,7 +1271,6 @@ static void diagnose_invalid_index_path(int stage, const char *prefix, const char *filename) { - struct stat st; const struct cache_entry *ce; int pos; unsigned namelen = strlen(filename); @@ -1314,7 +1313,7 @@ static void diagnose_invalid_index_path(int stage, ce_stage(ce), filename); } - if (!lstat(filename, &st)) + if (file_exists(filename)) die("Path '%s' exists on disk, but not in the index.", filename); if (errno == ENOENT || errno == ENOTDIR) die("Path '%s' does not exist (neither on disk nor in the index).", diff --git a/submodule.c b/submodule.c index c0e6c81..5a563ad 100644 --- a/submodule.c +++ b/submodule.c @@ -891,7 +891,6 @@ int submodule_uses_gitfile(const char *path) int ok_to_remove_submodule(const char *path) { - struct stat st; ssize_t len; struct child_process cp = CHILD_PROCESS_INIT; const char *argv[] = { @@ -904,7 +903,7 @@ int ok_to_remove_submodule(const char *path) struct strbuf buf = STRBUF_INIT; int ok_to_remove = 1; - if ((lstat(path, &st) < 0) || is_empty_dir(path)) + if (!file_exists(path) || is_empty_dir(path)) return 1; if (!submodule_uses_gitfile(path)) -- cgit v0.10.2-6-g49f6 From 5cd83e18851445cdc20d5842b53373fe15554ae9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Wed, 20 May 2015 00:13:26 +0200 Subject: clean: remove unused variable buf It had never been used. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano diff --git a/builtin/clean.c b/builtin/clean.c index 98c103f..a9eb772 100644 --- a/builtin/clean.c +++ b/builtin/clean.c @@ -314,7 +314,6 @@ static void print_highlight_menu_stuff(struct menu_stuff *stuff, int **chosen) { struct string_list menu_list = STRING_LIST_INIT_DUP; struct strbuf menu = STRBUF_INIT; - struct strbuf buf = STRBUF_INIT; struct menu_item *menu_item; struct string_list_item *string_list_item; int i; @@ -363,7 +362,6 @@ static void print_highlight_menu_stuff(struct menu_stuff *stuff, int **chosen) pretty_print_menus(&menu_list); strbuf_release(&menu); - strbuf_release(&buf); string_list_clear(&menu_list, 0); } -- cgit v0.10.2-6-g49f6 From 22570b68e3cb9380403d903680be3b3112a26490 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Wed, 20 May 2015 00:13:33 +0200 Subject: dir: remove unused variable sb It had never been used. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano diff --git a/dir.c b/dir.c index a3e7073..480ad05 100644 --- a/dir.c +++ b/dir.c @@ -385,7 +385,6 @@ int report_path_error(const char *ps_matched, /* * Make sure all pathspec matched; otherwise it is an error. */ - struct strbuf sb = STRBUF_INIT; int num, errors = 0; for (num = 0; num < pathspec->nr; num++) { int other, found_dup; @@ -417,7 +416,6 @@ int report_path_error(const char *ps_matched, pathspec->items[num].original); errors++; } - strbuf_release(&sb); return errors; } -- cgit v0.10.2-6-g49f6