summaryrefslogtreecommitdiff
path: root/src/path.c
diff options
context:
space:
mode:
authorVicent Marti <tanoku@gmail.com>2011-07-04 20:05:11 +0200
committerVicent Marti <tanoku@gmail.com>2011-07-05 02:06:26 +0200
commit5ad739e8328c665b629e2285abaec7e12ea8397c (patch)
treebe0677bcaeaa318bf20b8fa765e3bf478fb5bc55 /src/path.c
parentf79026b4912bcd2336667f4c1663c06e233f0b32 (diff)
downloadlibgit2-5ad739e8328c665b629e2285abaec7e12ea8397c.tar.gz
fileops: Drop `git_fileops_prettify_path`
The old `git_fileops_prettify_path` has been replaced with `git_path_prettify`. This is a much simpler method that uses the OS's `realpath` call to obtain the full path for directories and resolve symlinks. The `realpath` syscall is the original POSIX call in Unix system and an emulated version under Windows using the Windows API.
Diffstat (limited to 'src/path.c')
-rw-r--r--src/path.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/path.c b/src/path.c
index 766f67427..1f7a16679 100644
--- a/src/path.c
+++ b/src/path.c
@@ -1,4 +1,7 @@
#include "common.h"
+#include "path.h"
+#include "posix.h"
+
#include <stdarg.h>
#include <stdio.h>
#include <ctype.h>
@@ -202,3 +205,50 @@ void git_path_join_n(char *buffer_out, int count, ...)
*buffer_out = '\0';
}
+int git_path_root(const char *path)
+{
+ int offset = 0;
+
+#ifdef GIT_WIN32
+ /* Does the root of the path look like a windows drive ? */
+ if (isalpha(path[0]) && (path[1] == ':'))
+ offset += 2;
+#endif
+
+ if (*(path + offset) == '/')
+ return offset;
+
+ return -1; /* Not a real error. Rather a signal than the path is not rooted */
+}
+
+int git_path_prettify(char *path_out, const char *path, const char *base)
+{
+ char *result;
+
+ if (base == NULL || git_path_root(path) >= 0) {
+ result = p_realpath(path, path_out);
+ } else {
+ char aux_path[GIT_PATH_MAX];
+ git_path_join(aux_path, base, path);
+ result = p_realpath(aux_path, path_out);
+ }
+
+ return result ? GIT_SUCCESS : GIT_EOSERR;
+}
+
+int git_path_prettify_dir(char *path_out, const char *path, const char *base)
+{
+ size_t end;
+
+ if (git_path_prettify(path_out, path, base) < GIT_SUCCESS)
+ return GIT_EOSERR;
+
+ end = strlen(path_out);
+
+ if (end && path_out[end - 1] != '/') {
+ path_out[end] = '/';
+ path_out[end + 1] = '\0';
+ }
+
+ return GIT_SUCCESS;
+}