summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 9b125da)
raw | patch | inline | side by side (parent: 9b125da)
author | Nguyễn Thái Ngọc Duy <pclouds@gmail.com> | |
Sat, 26 Mar 2011 09:04:25 +0000 (16:04 +0700) | ||
committer | Junio C Hamano <gitster@pobox.com> | |
Tue, 29 Mar 2011 00:02:57 +0000 (17:02 -0700) |
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>
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>
dir.c | patch | blob | history | |
dir.h | patch | blob | history |
index b0100f553323998e74005edacdab44b87b5ca315..522a6fe9da5e95b44f19b5ffa804ce44d9c52cc7 100644 (file)
--- a/dir.c
+++ b/dir.c
}
/*
- * get_relative_cwd() gets the prefix of the current working directory
- * relative to 'dir'. If we are not inside 'dir', it returns NULL.
- *
- * As a convenience, it also returns NULL if 'dir' is already NULL. The
- * reason for this behaviour is that it is natural for functions returning
- * directory names to return NULL to say "this directory does not exist"
- * or "this directory is invalid". These cases are usually handled the
- * same as if the cwd is not inside 'dir' at all, so get_relative_cwd()
- * returns NULL for both of them.
- *
- * Most notably, get_relative_cwd(buffer, size, get_git_work_tree())
- * unifies the handling of "outside work tree" with "no work tree at all".
- */
-char *get_relative_cwd(char *buffer, int size, const char *dir)
-{
- char *cwd = buffer;
-
- if (!dir)
- return NULL;
- if (!getcwd(buffer, size))
- die_errno("can't find the current directory");
-
- if (!is_absolute_path(dir))
- dir = make_absolute_path(dir);
-
- while (*dir && *dir == *cwd) {
- dir++;
- cwd++;
- }
- if (*dir)
- return NULL;
- switch (*cwd) {
- case '\0':
- return cwd;
- case '/':
- return cwd + 1;
- default:
- /*
- * dir can end with a path separator when it's root
- * directory. Return proper prefix in that case.
- */
- if (dir[-1] == '/')
- return cwd;
- return NULL;
- }
-}
-
* Given two normalized paths (a trailing slash is ok), if subdir is
* outside dir, return -1. Otherwise return the offset in subdir that
* can be used as relative path to dir.
int is_inside_dir(const char *dir)
{
- char buffer[PATH_MAX];
- return get_relative_cwd(buffer, sizeof(buffer), dir) != NULL;
+ char cwd[PATH_MAX];
+ if (!dir)
+ return 0;
+ if (!getcwd(cwd, sizeof(cwd)))
+ die_errno("can't find the current directory");
+ return dir_inside_of(cwd, dir) >= 0;
}
int is_empty_dir(const char *path)
index 86a99ed9c1be3dce3434f989cde8f1501f50472a..1cf271098a9c346b273f742da09143a6d3d91c50 100644 (file)
--- a/dir.h
+++ b/dir.h
extern void free_excludes(struct exclude_list *el);
extern int file_exists(const char *);
-extern char *get_relative_cwd(char *buffer, int size, const char *dir);
extern int is_inside_dir(const char *dir);
extern int dir_inside_of(const char *subdir, const char *dir);