Skip to content

Commit b36fdf6

Browse files
committed
refactor: rename is_directory() to dir_exists() and use it in clone.c
The original is_directory() checks whether the given path exists as a directory, which makes dir_exists() a more suitable name. However, there is already an existing function called dir_exists(), while it doesn't check if the path is a directory. We decided to do the following: - remove the original dir_exists() - rename the original is_directory() to dir_exists() - use the new dir_exists() where the original dir_exists() is called Hope it can reduce some confusion. Signed-off-by: John Lin <johnlinp@gmail.com>
1 parent da72936 commit b36fdf6

17 files changed

Lines changed: 27 additions & 33 deletions

abspath.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* symlink to a directory, we do not want to say it is a directory when
66
* dealing with tracked content in the working tree.
77
*/
8-
int is_directory(const char *path)
8+
int dir_exists(const char *path)
99
{
1010
struct stat st;
1111
return (!stat(path, &st) && S_ISDIR(st.st_mode));

builtin/am.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ static int detect_patch_format(const char **paths)
576576
/*
577577
* We default to mbox format if input is from stdin and for directories
578578
*/
579-
if (!*paths || !strcmp(*paths, "-") || is_directory(*paths))
579+
if (!*paths || !strcmp(*paths, "-") || dir_exists(*paths))
580580
return PATCH_FORMAT_MBOX;
581581

582582
/*

builtin/clone.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -899,12 +899,6 @@ static void dissociate_from_references(void)
899899
free(alternates);
900900
}
901901

902-
static int dir_exists(const char *path)
903-
{
904-
struct stat sb;
905-
return !stat(path, &sb);
906-
}
907-
908902
int cmd_clone(int argc, const char **argv, const char *prefix)
909903
{
910904
int is_bundle = 0, is_local;

builtin/mv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
152152
* "git mv directory no-such-dir/".
153153
*/
154154
flags = KEEP_TRAILING_SLASH;
155-
if (argc == 1 && is_directory(argv[0]) && !is_directory(argv[1]))
155+
if (argc == 1 && dir_exists(argv[0]) && !dir_exists(argv[1]))
156156
flags = 0;
157157
dest_path = internal_prefix_pathspec(prefix, argv + argc, 1, flags);
158158
submodule_gitfile = xcalloc(argc, sizeof(char *));

builtin/rebase.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ static int init_basic_state(struct replay_opts *opts, const char *head_name,
275275
{
276276
FILE *interactive;
277277

278-
if (!is_directory(merge_dir()) && mkdir_in_gitdir(merge_dir()))
278+
if (!dir_exists(merge_dir()) && mkdir_in_gitdir(merge_dir()))
279279
return error_errno(_("could not create temporary %s"), merge_dir());
280280

281281
delete_reflog("REBASE_HEAD");
@@ -1068,7 +1068,7 @@ static int run_am(struct rebase_options *opts)
10681068
return move_to_original_branch(opts);
10691069
}
10701070

1071-
if (is_directory(opts->state_dir))
1071+
if (dir_exists(opts->state_dir))
10721072
rebase_write_basic_state(opts);
10731073

10741074
return status;
@@ -1529,13 +1529,13 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
15291529
if(file_exists(buf.buf))
15301530
die(_("It looks like 'git am' is in progress. Cannot rebase."));
15311531

1532-
if (is_directory(apply_dir())) {
1532+
if (dir_exists(apply_dir())) {
15331533
options.type = REBASE_AM;
15341534
options.state_dir = apply_dir();
1535-
} else if (is_directory(merge_dir())) {
1535+
} else if (dir_exists(merge_dir())) {
15361536
strbuf_reset(&buf);
15371537
strbuf_addf(&buf, "%s/rewritten", merge_dir());
1538-
if (is_directory(buf.buf)) {
1538+
if (dir_exists(buf.buf)) {
15391539
options.type = REBASE_PRESERVE_MERGES;
15401540
options.flags |= REBASE_INTERACTIVE_EXPLICIT;
15411541
} else {

builtin/submodule--helper.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,7 @@ static void deinit_submodule(const char *path, const char *prefix,
10961096
displaypath = get_submodule_displaypath(path, prefix);
10971097

10981098
/* remove the submodule work tree (unless the user already did it) */
1099-
if (is_directory(path)) {
1099+
if (dir_exists(path)) {
11001100
struct strbuf sb_rm = STRBUF_INIT;
11011101
const char *format;
11021102

@@ -1105,7 +1105,7 @@ static void deinit_submodule(const char *path, const char *prefix,
11051105
* NEEDSWORK: instead of dying, automatically call
11061106
* absorbgitdirs and (possibly) warn.
11071107
*/
1108-
if (is_directory(sub_git_dir))
1108+
if (dir_exists(sub_git_dir))
11091109
die(_("Submodule work tree '%s' contains a .git "
11101110
"directory (use 'rm -rf' if you really want "
11111111
"to remove it including all of its history)"),

builtin/worktree.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ static int prune_worktree(const char *id, struct strbuf *reason)
7575
size_t len;
7676
ssize_t read_result;
7777

78-
if (!is_directory(git_path("worktrees/%s", id))) {
78+
if (!dir_exists(git_path("worktrees/%s", id))) {
7979
strbuf_addf(reason, _("Removing worktrees/%s: not a valid directory"), id);
8080
return 1;
8181
}
@@ -738,7 +738,7 @@ static void validate_no_submodules(const struct worktree *wt)
738738
struct strbuf path = STRBUF_INIT;
739739
int i, found_submodules = 0;
740740

741-
if (is_directory(worktree_git_path(wt, "modules"))) {
741+
if (dir_exists(worktree_git_path(wt, "modules"))) {
742742
/*
743743
* There could be false positives, e.g. the "modules"
744744
* directory exists but is empty. But it's a rare case and
@@ -799,7 +799,7 @@ static int move_worktree(int ac, const char **av, const char *prefix)
799799
die(_("'%s' is not a working tree"), av[0]);
800800
if (is_main_worktree(wt))
801801
die(_("'%s' is a main working tree"), av[0]);
802-
if (is_directory(dst.buf)) {
802+
if (dir_exists(dst.buf)) {
803803
const char *sep = find_last_dir_sep(wt->path);
804804

805805
if (!sep)

cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1274,7 +1274,7 @@ static inline int is_absolute_path(const char *path)
12741274
{
12751275
return is_dir_sep(path[0]) || has_dos_drive_prefix(path);
12761276
}
1277-
int is_directory(const char *);
1277+
int dir_exists(const char *);
12781278
char *strbuf_realpath(struct strbuf *resolved, const char *path,
12791279
int die_on_error);
12801280
const char *real_path(const char *path);

daemon.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1455,7 +1455,7 @@ int cmd_main(int argc, const char **argv)
14551455
if (strict_paths && (!ok_paths || !*ok_paths))
14561456
die("option --strict-paths requires a whitelist");
14571457

1458-
if (base_path && !is_directory(base_path))
1458+
if (base_path && !dir_exists(base_path))
14591459
die("base-path '%s' does not exist or is not a directory",
14601460
base_path);
14611461

diff-no-index.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,8 @@ static void fixup_paths(const char **path, struct strbuf *replacement)
221221
if (path[0] == file_from_standard_input ||
222222
path[1] == file_from_standard_input)
223223
return;
224-
isdir0 = is_directory(path[0]);
225-
isdir1 = is_directory(path[1]);
224+
isdir0 = dir_exists(path[0]);
225+
isdir1 = dir_exists(path[1]);
226226
if (isdir0 == isdir1)
227227
return;
228228
if (isdir0) {

0 commit comments

Comments
 (0)