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 <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
May 19, 2015 at 23:44 UTC
dbe44faadb87f88e092f3dac387f96070268137a
5 files changed
+9
-22
builtin/blame.c
+3
-12
@@ -26,6 +26,7 @@
26
#include "userdiff.h"
27
#include "line-range.h"
28
#include "line-log.h"
29
+#include "dir.h"
30
31
static char blame_usage[] = N_("git blame [<options>] [<rev-opts>] [<rev>] [--] file");
32
@@ -2151,16 +2152,6 @@ static void sanity_check_refcnt(struct scoreboard *sb)
2152
}
2153
}
2154
2154
-/*
2155
- * Used for the command line parsing; check if the path exists
2156
- * in the working tree.
2157
- */
2158
-static int has_string_in_work_tree(const char *path)
2159
-{
2160
- struct stat st;
2161
- return !lstat(path, &st);
2162
-}
2163
-
2155
static unsigned parse_score(const char *arg)
2156
{
2157
char *end;
@@ -2655,14 +2646,14 @@ parse_done:
2646
if (argc < 2)
2647
usage_with_options(blame_opt_usage, options);
2648
path = add_prefix(prefix, argv[argc - 1]);
2658
- if (argc == 3 && !has_string_in_work_tree(path)) { /* (2b) */
2649
+ if (argc == 3 && !file_exists(path)) { /* (2b) */
2650
path = add_prefix(prefix, argv[1]);
2651
argv[1] = argv[2];
2652
}
2653
argv[argc - 1] = "--";
2654
2655
setup_work_tree();
2665
- if (!has_string_in_work_tree(path))
2656
+ if (!file_exists(path))
2657
die_errno("cannot stat path '%s'", path);
2658
}
2659
builtin/rm.c
+1
-2
@@ -84,7 +84,6 @@ static int check_submodules_use_gitfiles(void)
84
const char *name = list.entry[i].name;
85
int pos;
86
const struct cache_entry *ce;
87
- struct stat st;
87
88
pos = cache_name_pos(name, strlen(name));
89
if (pos < 0) {
@@ -95,7 +94,7 @@ static int check_submodules_use_gitfiles(void)
94
ce = active_cache[pos];
95
96
if (!S_ISGITLINK(ce->ce_mode) ||
98
- (lstat(ce->name, &st) < 0) ||
97
+ !file_exists(ce->name) ||
98
is_empty_dir(name))
99
continue;
100
merge-recursive.c
+1
-2
@@ -611,7 +611,6 @@ static char *unique_path(struct merge_options *o, const char *path, const char *
611
{
612
struct strbuf newpath = STRBUF_INIT;
613
int suffix = 0;
614
- struct stat st;
614
size_t base_len;
615
616
strbuf_addf(&newpath, "%s~", path);
@@ -620,7 +619,7 @@ static char *unique_path(struct merge_options *o, const char *path, const char *
619
base_len = newpath.len;
620
while (string_list_has_string(&o->current_file_set, newpath.buf) ||
621
string_list_has_string(&o->current_directory_set, newpath.buf) ||
623
- lstat(newpath.buf, &st) == 0) {
622
+ file_exists(newpath.buf)) {
623
strbuf_setlen(&newpath, base_len);
624
strbuf_addf(&newpath, "_%d", suffix++);
625
}
sha1_name.c
+3
-4
@@ -6,6 +6,7 @@
6
#include "tree-walk.h"
7
#include "refs.h"
8
#include "remote.h"
9
+#include "dir.h"
10
11
static int get_sha1_oneline(const char *, unsigned char *, struct commit_list *);
12
@@ -1237,14 +1238,13 @@ static void diagnose_invalid_sha1_path(const char *prefix,
1238
const char *object_name,
1239
int object_name_len)
1240
{
1240
- struct stat st;
1241
unsigned char sha1[20];
1242
unsigned mode;
1243
1244
if (!prefix)
1245
prefix = "";
1246
1247
- if (!lstat(filename, &st))
1247
+ if (file_exists(filename))
1248
die("Path '%s' exists on disk, but not in '%.*s'.",
1249
filename, object_name_len, object_name);
1250
if (errno == ENOENT || errno == ENOTDIR) {
@@ -1271,7 +1271,6 @@ static void diagnose_invalid_index_path(int stage,
1271
const char *prefix,
1272
const char *filename)
1273
{
1274
- struct stat st;
1274
const struct cache_entry *ce;
1275
int pos;
1276
unsigned namelen = strlen(filename);
@@ -1314,7 +1313,7 @@ static void diagnose_invalid_index_path(int stage,
1313
ce_stage(ce), filename);
1314
}
1315
1317
- if (!lstat(filename, &st))
1316
+ if (file_exists(filename))
1317
die("Path '%s' exists on disk, but not in the index.", filename);
1318
if (errno == ENOENT || errno == ENOTDIR)
1319
die("Path '%s' does not exist (neither on disk nor in the index).",
submodule.c
+1
-2
@@ -891,7 +891,6 @@ int submodule_uses_gitfile(const char *path)
891
892
int ok_to_remove_submodule(const char *path)
893
{
894
- struct stat st;
894
ssize_t len;
895
struct child_process cp = CHILD_PROCESS_INIT;
896
const char *argv[] = {
@@ -904,7 +903,7 @@ int ok_to_remove_submodule(const char *path)
903
struct strbuf buf = STRBUF_INIT;
904
int ok_to_remove = 1;
905
907
- if ((lstat(path, &st) < 0) || is_empty_dir(path))
906
+ if (!file_exists(path) || is_empty_dir(path))
907
return 1;
908
909
if (!submodule_uses_gitfile(path))