summaryrefslogtreecommitdiff
path: root/src/path.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/path.c')
-rw-r--r--src/path.c46
1 files changed, 40 insertions, 6 deletions
diff --git a/src/path.c b/src/path.c
index 22391c52..15188850 100644
--- a/src/path.c
+++ b/src/path.c
@@ -147,6 +147,20 @@ char *git_path_basename(const char *path)
return basename;
}
+size_t git_path_basename_offset(git_buf *buffer)
+{
+ ssize_t slash;
+
+ if (!buffer || buffer->size <= 0)
+ return 0;
+
+ slash = git_buf_rfind_next(buffer, '/');
+
+ if (slash >= 0 && buffer->ptr[slash] == '/')
+ return (size_t)(slash + 1);
+
+ return 0;
+}
const char *git_path_topdir(const char *path)
{
@@ -193,6 +207,31 @@ int git_path_root(const char *path)
return -1; /* Not a real error - signals that path is not rooted */
}
+int git_path_join_unrooted(
+ git_buf *path_out, const char *path, const char *base, ssize_t *root_at)
+{
+ int error, root;
+
+ assert(path && path_out);
+
+ root = git_path_root(path);
+
+ if (base != NULL && root < 0) {
+ error = git_buf_joinpath(path_out, base, path);
+
+ if (root_at)
+ *root_at = (ssize_t)strlen(base);
+ }
+ else {
+ error = git_buf_sets(path_out, path);
+
+ if (root_at)
+ *root_at = (root < 0) ? 0 : (ssize_t)root;
+ }
+
+ return error;
+}
+
int git_path_prettify(git_buf *path_out, const char *path, const char *base)
{
char buf[GIT_PATH_MAX];
@@ -502,12 +541,7 @@ bool git_path_contains_file(git_buf *base, const char *file)
int git_path_find_dir(git_buf *dir, const char *path, const char *base)
{
- int error;
-
- if (base != NULL && git_path_root(path) < 0)
- error = git_buf_joinpath(dir, base, path);
- else
- error = git_buf_sets(dir, path);
+ int error = git_path_join_unrooted(dir, path, base, NULL);
if (!error) {
char buf[GIT_PATH_MAX];