Skip to content

Commit 10c4c88

Browse files
Johannes Sixtgitster
authored andcommitted
Allow add_path() to add non-existent directories to the path
This function had used make_absolute_path(); but this function dies if the directory that contains the entry whose relative path was supplied in the argument does not exist. This is a problem if the argument is, for example, "../libexec/git-core", and that "../libexec" does not exist. Since the resolution of symbolic links is not required for elements in PATH, we can fall back to using make_nonrelative_path(), which simply prepends $PWD to the path. We have to move make_nonrelative_path() alongside make_absolute_path() in abspath.c so that git-shell can be linked. See 5b8e6f8. Signed-off-by: Johannes Sixt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 49fa65a commit 10c4c88

File tree

3 files changed

+37
-37
lines changed

3 files changed

+37
-37
lines changed

abspath.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,39 @@ const char *make_absolute_path(const char *path)
6666

6767
return buf;
6868
}
69+
70+
static const char *get_pwd_cwd(void)
71+
{
72+
static char cwd[PATH_MAX + 1];
73+
char *pwd;
74+
struct stat cwd_stat, pwd_stat;
75+
if (getcwd(cwd, PATH_MAX) == NULL)
76+
return NULL;
77+
pwd = getenv("PWD");
78+
if (pwd && strcmp(pwd, cwd)) {
79+
stat(cwd, &cwd_stat);
80+
if (!stat(pwd, &pwd_stat) &&
81+
pwd_stat.st_dev == cwd_stat.st_dev &&
82+
pwd_stat.st_ino == cwd_stat.st_ino) {
83+
strlcpy(cwd, pwd, PATH_MAX);
84+
}
85+
}
86+
return cwd;
87+
}
88+
89+
const char *make_nonrelative_path(const char *path)
90+
{
91+
static char buf[PATH_MAX + 1];
92+
93+
if (is_absolute_path(path)) {
94+
if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
95+
die("Too long path: %.*s", 60, path);
96+
} else {
97+
const char *cwd = get_pwd_cwd();
98+
if (!cwd)
99+
die("Cannot determine the current working directory");
100+
if (snprintf(buf, PATH_MAX, "%s/%s", cwd, path) >= PATH_MAX)
101+
die("Too long path: %.*s", 60, path);
102+
}
103+
return buf;
104+
}

exec_cmd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static void add_path(struct strbuf *out, const char *path)
5050
if (is_absolute_path(path))
5151
strbuf_addstr(out, path);
5252
else
53-
strbuf_addstr(out, make_absolute_path(path));
53+
strbuf_addstr(out, make_nonrelative_path(path));
5454

5555
strbuf_addch(out, PATH_SEP);
5656
}

path.c

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -291,42 +291,6 @@ int adjust_shared_perm(const char *path)
291291
return 0;
292292
}
293293

294-
static const char *get_pwd_cwd(void)
295-
{
296-
static char cwd[PATH_MAX + 1];
297-
char *pwd;
298-
struct stat cwd_stat, pwd_stat;
299-
if (getcwd(cwd, PATH_MAX) == NULL)
300-
return NULL;
301-
pwd = getenv("PWD");
302-
if (pwd && strcmp(pwd, cwd)) {
303-
stat(cwd, &cwd_stat);
304-
if (!stat(pwd, &pwd_stat) &&
305-
pwd_stat.st_dev == cwd_stat.st_dev &&
306-
pwd_stat.st_ino == cwd_stat.st_ino) {
307-
strlcpy(cwd, pwd, PATH_MAX);
308-
}
309-
}
310-
return cwd;
311-
}
312-
313-
const char *make_nonrelative_path(const char *path)
314-
{
315-
static char buf[PATH_MAX + 1];
316-
317-
if (is_absolute_path(path)) {
318-
if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
319-
die ("Too long path: %.*s", 60, path);
320-
} else {
321-
const char *cwd = get_pwd_cwd();
322-
if (!cwd)
323-
die("Cannot determine the current working directory");
324-
if (snprintf(buf, PATH_MAX, "%s/%s", cwd, path) >= PATH_MAX)
325-
die ("Too long path: %.*s", 60, path);
326-
}
327-
return buf;
328-
}
329-
330294
const char *make_relative_path(const char *abs, const char *base)
331295
{
332296
static char buf[PATH_MAX + 1];

0 commit comments

Comments
 (0)