Skip to content

Commit 1b9a946

Browse files
iabervongitster
authored andcommitted
Use nonrelative paths instead of absolute paths for cloned repositories
Particularly for the "alternates" file, if one will be created, we want a path that doesn't depend on the current directory, but we want to retain any symlinks in the path as given and any in the user's view of the current directory when the path was given. Signed-off-by: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 9e1f0a8 commit 1b9a946

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

builtin-clone.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ static char *get_repo_path(const char *repo, int *is_bundle)
7676
path = mkpath("%s%s", repo, suffix[i]);
7777
if (!stat(path, &st) && S_ISDIR(st.st_mode)) {
7878
*is_bundle = 0;
79-
return xstrdup(make_absolute_path(path));
79+
return xstrdup(make_nonrelative_path(path));
8080
}
8181
}
8282

@@ -85,7 +85,7 @@ static char *get_repo_path(const char *repo, int *is_bundle)
8585
path = mkpath("%s%s", repo, bundle_suffix[i]);
8686
if (!stat(path, &st) && S_ISREG(st.st_mode)) {
8787
*is_bundle = 1;
88-
return xstrdup(make_absolute_path(path));
88+
return xstrdup(make_nonrelative_path(path));
8989
}
9090
}
9191

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ static inline int is_absolute_path(const char *path)
524524
return path[0] == '/';
525525
}
526526
const char *make_absolute_path(const char *path);
527+
const char *make_nonrelative_path(const char *path);
527528

528529
/* Read and unpack a sha1 file into memory, write memory to a sha1 file */
529530
extern int sha1_object_info(const unsigned char *, unsigned long *);

path.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,42 @@ 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 (path[0] == '/') {
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+
294330
/* We allow "recursive" symbolic links. Only within reason, though. */
295331
#define MAXDEPTH 5
296332

0 commit comments

Comments
 (0)