Skip to content

Commit c67e367

Browse files
committed
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
2 parents 2db8926 + b892913 commit c67e367

File tree

3 files changed

+34
-45
lines changed

3 files changed

+34
-45
lines changed

dir.c

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,57 +1105,45 @@ int file_exists(const char *f)
11051105
}
11061106

11071107
/*
1108-
* get_relative_cwd() gets the prefix of the current working directory
1109-
* relative to 'dir'. If we are not inside 'dir', it returns NULL.
1110-
*
1111-
* As a convenience, it also returns NULL if 'dir' is already NULL. The
1112-
* reason for this behaviour is that it is natural for functions returning
1113-
* directory names to return NULL to say "this directory does not exist"
1114-
* or "this directory is invalid". These cases are usually handled the
1115-
* same as if the cwd is not inside 'dir' at all, so get_relative_cwd()
1116-
* returns NULL for both of them.
1117-
*
1118-
* Most notably, get_relative_cwd(buffer, size, get_git_work_tree())
1119-
* unifies the handling of "outside work tree" with "no work tree at all".
1108+
* Given two normalized paths (a trailing slash is ok), if subdir is
1109+
* outside dir, return -1. Otherwise return the offset in subdir that
1110+
* can be used as relative path to dir.
11201111
*/
1121-
char *get_relative_cwd(char *buffer, int size, const char *dir)
1112+
int dir_inside_of(const char *subdir, const char *dir)
11221113
{
1123-
char *cwd = buffer;
1124-
1125-
if (!dir)
1126-
return NULL;
1127-
if (!getcwd(buffer, size))
1128-
die_errno("can't find the current directory");
1114+
int offset = 0;
11291115

1130-
if (!is_absolute_path(dir))
1131-
dir = real_path(dir);
1116+
assert(dir && subdir && *dir && *subdir);
11321117

1133-
while (*dir && *dir == *cwd) {
1118+
while (*dir && *subdir && *dir == *subdir) {
11341119
dir++;
1135-
cwd++;
1136-
}
1137-
if (*dir)
1138-
return NULL;
1139-
switch (*cwd) {
1140-
case '\0':
1141-
return cwd;
1142-
case '/':
1143-
return cwd + 1;
1144-
default:
1145-
/*
1146-
* dir can end with a path separator when it's root
1147-
* directory. Return proper prefix in that case.
1148-
*/
1149-
if (dir[-1] == '/')
1150-
return cwd;
1151-
return NULL;
1120+
subdir++;
1121+
offset++;
11521122
}
1123+
1124+
/* hel[p]/me vs hel[l]/yeah */
1125+
if (*dir && *subdir)
1126+
return -1;
1127+
1128+
if (!*subdir)
1129+
return !*dir ? offset : -1; /* same dir */
1130+
1131+
/* foo/[b]ar vs foo/[] */
1132+
if (is_dir_sep(dir[-1]))
1133+
return is_dir_sep(subdir[-1]) ? offset : -1;
1134+
1135+
/* foo[/]bar vs foo[] */
1136+
return is_dir_sep(*subdir) ? offset + 1 : -1;
11531137
}
11541138

11551139
int is_inside_dir(const char *dir)
11561140
{
1157-
char buffer[PATH_MAX];
1158-
return get_relative_cwd(buffer, sizeof(buffer), dir) != NULL;
1141+
char cwd[PATH_MAX];
1142+
if (!dir)
1143+
return 0;
1144+
if (!getcwd(cwd, sizeof(cwd)))
1145+
die_errno("can't find the current directory");
1146+
return dir_inside_of(cwd, dir) >= 0;
11591147
}
11601148

11611149
int is_empty_dir(const char *path)

dir.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ extern void add_exclude(const char *string, const char *base,
8585
extern void free_excludes(struct exclude_list *el);
8686
extern int file_exists(const char *);
8787

88-
extern char *get_relative_cwd(char *buffer, int size, const char *dir);
8988
extern int is_inside_dir(const char *dir);
89+
extern int dir_inside_of(const char *subdir, const char *dir);
9090

9191
static inline int is_dot_or_dotdot(const char *name)
9292
{

setup.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
325325
const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
326326
const char *worktree;
327327
char *gitfile;
328+
int offset;
328329

329330
if (PATH_MAX - 40 < strlen(gitdirenv))
330331
die("'$%s' too big", GIT_DIR_ENVIRONMENT);
@@ -390,15 +391,15 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
390391
return NULL;
391392
}
392393

393-
if (!prefixcmp(cwd, worktree) &&
394-
cwd[strlen(worktree)] == '/') { /* cwd inside worktree */
394+
offset = dir_inside_of(cwd, worktree);
395+
if (offset >= 0) { /* cwd inside worktree? */
395396
set_git_dir(real_path(gitdirenv));
396397
if (chdir(worktree))
397398
die_errno("Could not chdir to '%s'", worktree);
398399
cwd[len++] = '/';
399400
cwd[len] = '\0';
400401
free(gitfile);
401-
return cwd + strlen(worktree) + 1;
402+
return cwd + offset;
402403
}
403404

404405
/* cwd outside worktree */

0 commit comments

Comments
 (0)