Skip to content

Commit dbe44fa

Browse files
rscharfegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent aaa7e0d commit dbe44fa

File tree

5 files changed

+9
-22
lines changed

5 files changed

+9
-22
lines changed

builtin/blame.c

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "userdiff.h"
2727
#include "line-range.h"
2828
#include "line-log.h"
29+
#include "dir.h"
2930

3031
static char blame_usage[] = N_("git blame [<options>] [<rev-opts>] [<rev>] [--] file");
3132

@@ -2151,16 +2152,6 @@ static void sanity_check_refcnt(struct scoreboard *sb)
21512152
}
21522153
}
21532154

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-
21642155
static unsigned parse_score(const char *arg)
21652156
{
21662157
char *end;
@@ -2655,14 +2646,14 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
26552646
if (argc < 2)
26562647
usage_with_options(blame_opt_usage, options);
26572648
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) */
26592650
path = add_prefix(prefix, argv[1]);
26602651
argv[1] = argv[2];
26612652
}
26622653
argv[argc - 1] = "--";
26632654

26642655
setup_work_tree();
2665-
if (!has_string_in_work_tree(path))
2656+
if (!file_exists(path))
26662657
die_errno("cannot stat path '%s'", path);
26672658
}
26682659

builtin/rm.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ static int check_submodules_use_gitfiles(void)
8484
const char *name = list.entry[i].name;
8585
int pos;
8686
const struct cache_entry *ce;
87-
struct stat st;
8887

8988
pos = cache_name_pos(name, strlen(name));
9089
if (pos < 0) {
@@ -95,7 +94,7 @@ static int check_submodules_use_gitfiles(void)
9594
ce = active_cache[pos];
9695

9796
if (!S_ISGITLINK(ce->ce_mode) ||
98-
(lstat(ce->name, &st) < 0) ||
97+
!file_exists(ce->name) ||
9998
is_empty_dir(name))
10099
continue;
101100

merge-recursive.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,6 @@ static char *unique_path(struct merge_options *o, const char *path, const char *
611611
{
612612
struct strbuf newpath = STRBUF_INIT;
613613
int suffix = 0;
614-
struct stat st;
615614
size_t base_len;
616615

617616
strbuf_addf(&newpath, "%s~", path);
@@ -620,7 +619,7 @@ static char *unique_path(struct merge_options *o, const char *path, const char *
620619
base_len = newpath.len;
621620
while (string_list_has_string(&o->current_file_set, newpath.buf) ||
622621
string_list_has_string(&o->current_directory_set, newpath.buf) ||
623-
lstat(newpath.buf, &st) == 0) {
622+
file_exists(newpath.buf)) {
624623
strbuf_setlen(&newpath, base_len);
625624
strbuf_addf(&newpath, "_%d", suffix++);
626625
}

sha1_name.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "tree-walk.h"
77
#include "refs.h"
88
#include "remote.h"
9+
#include "dir.h"
910

1011
static int get_sha1_oneline(const char *, unsigned char *, struct commit_list *);
1112

@@ -1237,14 +1238,13 @@ static void diagnose_invalid_sha1_path(const char *prefix,
12371238
const char *object_name,
12381239
int object_name_len)
12391240
{
1240-
struct stat st;
12411241
unsigned char sha1[20];
12421242
unsigned mode;
12431243

12441244
if (!prefix)
12451245
prefix = "";
12461246

1247-
if (!lstat(filename, &st))
1247+
if (file_exists(filename))
12481248
die("Path '%s' exists on disk, but not in '%.*s'.",
12491249
filename, object_name_len, object_name);
12501250
if (errno == ENOENT || errno == ENOTDIR) {
@@ -1271,7 +1271,6 @@ static void diagnose_invalid_index_path(int stage,
12711271
const char *prefix,
12721272
const char *filename)
12731273
{
1274-
struct stat st;
12751274
const struct cache_entry *ce;
12761275
int pos;
12771276
unsigned namelen = strlen(filename);
@@ -1314,7 +1313,7 @@ static void diagnose_invalid_index_path(int stage,
13141313
ce_stage(ce), filename);
13151314
}
13161315

1317-
if (!lstat(filename, &st))
1316+
if (file_exists(filename))
13181317
die("Path '%s' exists on disk, but not in the index.", filename);
13191318
if (errno == ENOENT || errno == ENOTDIR)
13201319
die("Path '%s' does not exist (neither on disk nor in the index).",

submodule.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,6 @@ int submodule_uses_gitfile(const char *path)
891891

892892
int ok_to_remove_submodule(const char *path)
893893
{
894-
struct stat st;
895894
ssize_t len;
896895
struct child_process cp = CHILD_PROCESS_INIT;
897896
const char *argv[] = {
@@ -904,7 +903,7 @@ int ok_to_remove_submodule(const char *path)
904903
struct strbuf buf = STRBUF_INIT;
905904
int ok_to_remove = 1;
906905

907-
if ((lstat(path, &st) < 0) || is_empty_dir(path))
906+
if (!file_exists(path) || is_empty_dir(path))
908907
return 1;
909908

910909
if (!submodule_uses_gitfile(path))

0 commit comments

Comments
 (0)