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>
Nguyễn Thái Ngọc Duy committed
Mar 26, 2011 at 16:04 UTC
b892913d51795ee66d825bedd23e5c4ffe85ebb2
2 files changed
+6
-50
dir.c
+6
-49
@@ -1001,53 +1001,6 @@ int file_exists(const char *f)
1001
}
1002
1003
/*
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
-
1004
* Given two normalized paths (a trailing slash is ok), if subdir is
1005
* outside dir, return -1. Otherwise return the offset in subdir that
1006
* can be used as relative path to dir.
@@ -1081,8 +1034,12 @@ int dir_inside_of(const char *subdir, const char *dir)
1034
1035
int is_inside_dir(const char *dir)
1036
{
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;
1043
}
1044
1045
int is_empty_dir(const char *path)
dir.h
-1
@@ -81,7 +81,6 @@ extern void add_exclude(const char *string, const char *base,
81
extern void free_excludes(struct exclude_list *el);
82
extern int file_exists(const char *);
83
84
-extern char *get_relative_cwd(char *buffer, int size, const char *dir);
84
extern int is_inside_dir(const char *dir);
85
extern int dir_inside_of(const char *subdir, const char *dir);
86