Skip to content

Commit b892913

Browse files
pcloudsgitster
authored andcommitted
Kill off get_relative_cwd()
Function dir_inside_of() does something similar (correctly), but looks easier to understand and does not bundle cwd to its business. Given get_relative_cwd's only user is is_inside_dir, we can kill it for good. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 9b125da commit b892913

File tree

2 files changed

+6
-50
lines changed

2 files changed

+6
-50
lines changed

dir.c

Lines changed: 6 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,53 +1001,6 @@ int file_exists(const char *f)
10011001
}
10021002

10031003
/*
1004-
* get_relative_cwd() gets the prefix of the current working directory
1005-
* relative to 'dir'. If we are not inside 'dir', it returns NULL.
1006-
*
1007-
* As a convenience, it also returns NULL if 'dir' is already NULL. The
1008-
* reason for this behaviour is that it is natural for functions returning
1009-
* directory names to return NULL to say "this directory does not exist"
1010-
* or "this directory is invalid". These cases are usually handled the
1011-
* same as if the cwd is not inside 'dir' at all, so get_relative_cwd()
1012-
* returns NULL for both of them.
1013-
*
1014-
* Most notably, get_relative_cwd(buffer, size, get_git_work_tree())
1015-
* unifies the handling of "outside work tree" with "no work tree at all".
1016-
*/
1017-
char *get_relative_cwd(char *buffer, int size, const char *dir)
1018-
{
1019-
char *cwd = buffer;
1020-
1021-
if (!dir)
1022-
return NULL;
1023-
if (!getcwd(buffer, size))
1024-
die_errno("can't find the current directory");
1025-
1026-
if (!is_absolute_path(dir))
1027-
dir = make_absolute_path(dir);
1028-
1029-
while (*dir && *dir == *cwd) {
1030-
dir++;
1031-
cwd++;
1032-
}
1033-
if (*dir)
1034-
return NULL;
1035-
switch (*cwd) {
1036-
case '\0':
1037-
return cwd;
1038-
case '/':
1039-
return cwd + 1;
1040-
default:
1041-
/*
1042-
* dir can end with a path separator when it's root
1043-
* directory. Return proper prefix in that case.
1044-
*/
1045-
if (dir[-1] == '/')
1046-
return cwd;
1047-
return NULL;
1048-
}
1049-
}
1050-
10511004
* Given two normalized paths (a trailing slash is ok), if subdir is
10521005
* outside dir, return -1. Otherwise return the offset in subdir that
10531006
* can be used as relative path to dir.
@@ -1081,8 +1034,12 @@ int dir_inside_of(const char *subdir, const char *dir)
10811034

10821035
int is_inside_dir(const char *dir)
10831036
{
1084-
char buffer[PATH_MAX];
1085-
return get_relative_cwd(buffer, sizeof(buffer), dir) != NULL;
1037+
char cwd[PATH_MAX];
1038+
if (!dir)
1039+
return 0;
1040+
if (!getcwd(cwd, sizeof(cwd)))
1041+
die_errno("can't find the current directory");
1042+
return dir_inside_of(cwd, dir) >= 0;
10861043
}
10871044

10881045
int is_empty_dir(const char *path)

dir.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ extern void add_exclude(const char *string, const char *base,
8181
extern void free_excludes(struct exclude_list *el);
8282
extern int file_exists(const char *);
8383

84-
extern char *get_relative_cwd(char *buffer, int size, const char *dir);
8584
extern int is_inside_dir(const char *dir);
8685
extern int dir_inside_of(const char *subdir, const char *dir);
8786

0 commit comments

Comments
 (0)