Merge branch 'nd/maint-setup'

* nd/maint-setup:
  Kill off get_relative_cwd()
  setup: return correct prefix if worktree is '/'

Conflicts:
	dir.c
	setup.c
This commit is contained in:
Junio C Hamano
2011-05-02 15:58:30 -07:00
3 changed files with 34 additions and 45 deletions

70
dir.c
View File

@@ -1105,57 +1105,45 @@ int file_exists(const char *f)
} }
/* /*
* get_relative_cwd() gets the prefix of the current working directory * Given two normalized paths (a trailing slash is ok), if subdir is
* relative to 'dir'. If we are not inside 'dir', it returns NULL. * outside dir, return -1. Otherwise return the offset in subdir that
* * can be used as relative path to dir.
* 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) int dir_inside_of(const char *subdir, const char *dir)
{ {
char *cwd = buffer; int offset = 0;
if (!dir) assert(dir && subdir && *dir && *subdir);
return NULL;
if (!getcwd(buffer, size))
die_errno("can't find the current directory");
if (!is_absolute_path(dir)) while (*dir && *subdir && *dir == *subdir) {
dir = real_path(dir);
while (*dir && *dir == *cwd) {
dir++; dir++;
cwd++; subdir++;
} offset++;
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;
} }
/* hel[p]/me vs hel[l]/yeah */
if (*dir && *subdir)
return -1;
if (!*subdir)
return !*dir ? offset : -1; /* same dir */
/* foo/[b]ar vs foo/[] */
if (is_dir_sep(dir[-1]))
return is_dir_sep(subdir[-1]) ? offset : -1;
/* foo[/]bar vs foo[] */
return is_dir_sep(*subdir) ? offset + 1 : -1;
} }
int is_inside_dir(const char *dir) int is_inside_dir(const char *dir)
{ {
char buffer[PATH_MAX]; char cwd[PATH_MAX];
return get_relative_cwd(buffer, sizeof(buffer), dir) != NULL; 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) int is_empty_dir(const char *path)

2
dir.h
View File

@@ -85,8 +85,8 @@ extern void add_exclude(const char *string, const char *base,
extern void free_excludes(struct exclude_list *el); extern void free_excludes(struct exclude_list *el);
extern int file_exists(const char *); 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 is_inside_dir(const char *dir);
extern int dir_inside_of(const char *subdir, const char *dir);
static inline int is_dot_or_dotdot(const char *name) static inline int is_dot_or_dotdot(const char *name)
{ {

View File

@@ -325,6 +325,7 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT); const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
const char *worktree; const char *worktree;
char *gitfile; char *gitfile;
int offset;
if (PATH_MAX - 40 < strlen(gitdirenv)) if (PATH_MAX - 40 < strlen(gitdirenv))
die("'$%s' too big", GIT_DIR_ENVIRONMENT); die("'$%s' too big", GIT_DIR_ENVIRONMENT);
@@ -390,15 +391,15 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
return NULL; return NULL;
} }
if (!prefixcmp(cwd, worktree) && offset = dir_inside_of(cwd, worktree);
cwd[strlen(worktree)] == '/') { /* cwd inside worktree */ if (offset >= 0) { /* cwd inside worktree? */
set_git_dir(real_path(gitdirenv)); set_git_dir(real_path(gitdirenv));
if (chdir(worktree)) if (chdir(worktree))
die_errno("Could not chdir to '%s'", worktree); die_errno("Could not chdir to '%s'", worktree);
cwd[len++] = '/'; cwd[len++] = '/';
cwd[len] = '\0'; cwd[len] = '\0';
free(gitfile); free(gitfile);
return cwd + strlen(worktree) + 1; return cwd + offset;
} }
/* cwd outside worktree */ /* cwd outside worktree */