|
10 | 10 |
|
11 | 11 | #include "git2_util.h" |
12 | 12 | #include "vector.h" |
| 13 | +#include "fs_path.h" |
13 | 14 |
|
14 | 15 | #include "common.h" |
15 | 16 | #include "error.h" |
@@ -124,3 +125,44 @@ int cli_repository_open( |
124 | 125 | *out = repo; |
125 | 126 | return 0; |
126 | 127 | } |
| 128 | + |
| 129 | +/* |
| 130 | + * This resolves paths - not _pathspecs_ like git - it accepts an absolute |
| 131 | + * path (to a path within the repository working directory) or a path |
| 132 | + * relative to the current directory. |
| 133 | + */ |
| 134 | +int cli_resolve_path(git_str *out, git_repository *repo, const char *given_path) |
| 135 | +{ |
| 136 | + git_str path = GIT_STR_INIT; |
| 137 | + int error = 0; |
| 138 | + |
| 139 | + if (!git_fs_path_is_absolute(given_path)) { |
| 140 | + char cwd[GIT_PATH_MAX]; |
| 141 | + |
| 142 | + if (p_getcwd(cwd, GIT_PATH_MAX) < 0) |
| 143 | + error = cli_error_os(); |
| 144 | + else if (git_str_puts(&path, cwd) < 0 || |
| 145 | + git_fs_path_apply_relative(&path, given_path) < 0) |
| 146 | + error = cli_error_git(); |
| 147 | + |
| 148 | + if (error) |
| 149 | + goto done; |
| 150 | + } else if (git_str_puts(&path, given_path) < 0) { |
| 151 | + error = cli_error_git(); |
| 152 | + goto done; |
| 153 | + } |
| 154 | + |
| 155 | + error = git_fs_path_make_relative(&path, git_repository_workdir(repo)); |
| 156 | + |
| 157 | + if (error == GIT_ENOTFOUND) |
| 158 | + error = cli_error("path '%s' is not inside the git repository '%s'", |
| 159 | + given_path, git_repository_workdir(repo)); |
| 160 | + else if (error < 0) |
| 161 | + error = cli_error_git(); |
| 162 | + else |
| 163 | + git_str_swap(out, &path); |
| 164 | + |
| 165 | +done: |
| 166 | + git_str_dispose(&path); |
| 167 | + return error; |
| 168 | +} |
0 commit comments