Code

Merge branch 'jn/gitweb-unspecified-action' into maint-1.7.8
[git.git] / abspath.c
index ff140689ed2453809e7c3794c9989918e90df392..f04ac18e33063f7e2cb9ab9eab9a6b86cb089b5a 100644 (file)
--- a/abspath.c
+++ b/abspath.c
@@ -14,7 +14,14 @@ int is_directory(const char *path)
 /* We allow "recursive" symbolic links. Only within reason, though. */
 #define MAXDEPTH 5
 
-const char *make_absolute_path(const char *path)
+/*
+ * Use this to get the real path, i.e. resolve links. If you want an
+ * absolute path but don't mind links, use absolute_path.
+ *
+ * If path is our buffer, then return path, as it's already what the
+ * user wants.
+ */
+const char *real_path(const char *path)
 {
        static char bufs[2][PATH_MAX + 1], *buf = bufs[0], *next_buf = bufs[1];
        char cwd[1024] = "";
@@ -33,7 +40,7 @@ const char *make_absolute_path(const char *path)
 
        while (depth--) {
                if (!is_directory(buf)) {
-                       char *last_slash = strrchr(buf, '/');
+                       char *last_slash = find_last_dir_sep(buf);
                        if (last_slash) {
                                *last_slash = '\0';
                                last_elem = xstrdup(last_slash + 1);
@@ -58,7 +65,7 @@ const char *make_absolute_path(const char *path)
                        if (len + strlen(last_elem) + 2 > PATH_MAX)
                                die ("Too long path name: '%s/%s'",
                                                buf, last_elem);
-                       if (len && buf[len-1] != '/')
+                       if (len && !is_dir_sep(buf[len-1]))
                                buf[len++] = '/';
                        strcpy(buf + len, last_elem);
                        free(last_elem);
@@ -95,7 +102,8 @@ static const char *get_pwd_cwd(void)
        pwd = getenv("PWD");
        if (pwd && strcmp(pwd, cwd)) {
                stat(cwd, &cwd_stat);
-               if (!stat(pwd, &pwd_stat) &&
+               if ((cwd_stat.st_dev || cwd_stat.st_ino) &&
+                   !stat(pwd, &pwd_stat) &&
                    pwd_stat.st_dev == cwd_stat.st_dev &&
                    pwd_stat.st_ino == cwd_stat.st_ino) {
                        strlcpy(cwd, pwd, PATH_MAX);
@@ -104,7 +112,14 @@ static const char *get_pwd_cwd(void)
        return cwd;
 }
 
-const char *make_nonrelative_path(const char *path)
+/*
+ * Use this to get an absolute path from a relative one. If you want
+ * to resolve links, you should use real_path.
+ *
+ * If the path is already absolute, then return path. As the user is
+ * never meant to free the return value, we're safe.
+ */
+const char *absolute_path(const char *path)
 {
        static char buf[PATH_MAX + 1];
 
@@ -124,3 +139,31 @@ const char *make_nonrelative_path(const char *path)
        }
        return buf;
 }
+
+/*
+ * Unlike prefix_path, this should be used if the named file does
+ * not have to interact with index entry; i.e. name of a random file
+ * on the filesystem.
+ */
+const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
+{
+       static char path[PATH_MAX];
+#ifndef WIN32
+       if (!pfx_len || is_absolute_path(arg))
+               return arg;
+       memcpy(path, pfx, pfx_len);
+       strcpy(path + pfx_len, arg);
+#else
+       char *p;
+       /* don't add prefix to absolute paths, but still replace '\' by '/' */
+       if (is_absolute_path(arg))
+               pfx_len = 0;
+       else if (pfx_len)
+               memcpy(path, pfx, pfx_len);
+       strcpy(path + pfx_len, arg);
+       for (p = path + pfx_len; *p; p++)
+               if (*p == '\\')
+                       *p = '/';
+#endif
+       return path;
+}