diff options
author | nulltoken <emeric.fermas@gmail.com> | 2011-12-27 16:03:28 +0100 |
---|---|---|
committer | nulltoken <emeric.fermas@gmail.com> | 2011-12-28 20:35:09 +0100 |
commit | 2017a15d6ca7f756dcf036499a02e15393609c83 (patch) | |
tree | eed9dfa76f244ed6846c78ec6a0fff4fe2178067 /src | |
parent | 459e2dcd7deb379b9a013ab70aa70206fc17f16a (diff) | |
download | libgit2-2017a15d6ca7f756dcf036499a02e15393609c83.tar.gz |
path: add git_path_fromurl()
Diffstat (limited to 'src')
-rw-r--r-- | src/path.c | 35 | ||||
-rw-r--r-- | src/path.h | 1 |
2 files changed, 36 insertions, 0 deletions
diff --git a/src/path.c b/src/path.c index bd62a3e4d..53f0f3dc6 100644 --- a/src/path.c +++ b/src/path.c @@ -272,3 +272,38 @@ append: return error; } + +int git_path_fromurl(git_buf *local_path_out, const char *file_url) +{ + int error = GIT_SUCCESS, offset = 0, len; + + assert(local_path_out && file_url); + + if (git__prefixcmp(file_url, "file://") != 0) + return git__throw(GIT_EINVALIDPATH, "Parsing of '%s' failed. A file Uri is expected (ie. with 'file://' scheme).", file_url); + + offset += 7; + len = strlen(file_url); + + if (offset < len && file_url[offset] == '/') + offset++; + else if (offset < len && git__prefixcmp(file_url + offset, "localhost/") == 0) + offset += 10; + else + return git__throw(GIT_EINVALIDPATH, "Parsing of '%s' failed. A local file Uri is expected.", file_url); + + if (offset >= len || file_url[offset] == '/') + return git__throw(GIT_EINVALIDPATH, "Parsing of '%s' failed. Invalid file Uri format.", file_url); + +#ifndef _MSC_VER + offset--; /* A *nix absolute path starts with a forward slash */ +#endif + + git_buf_clear(local_path_out); + + error = git__percent_decode(local_path_out, file_url + offset); + if (error < GIT_SUCCESS) + return git__rethrow(error, "Parsing of '%s' failed.", file_url); + + return error; +} diff --git a/src/path.h b/src/path.h index 6397feedf..c308c5bd4 100644 --- a/src/path.h +++ b/src/path.h @@ -75,5 +75,6 @@ GIT_INLINE(void) git_path_mkposix(char *path) #endif extern int git__percent_decode(git_buf *decoded_out, const char *input); +extern int git_path_fromurl(git_buf *local_path_out, const char *file_url); #endif |