* Normalize "path", prepending the "prefix" for relative paths. If * remaining_prefix is not NULL, return the actual prefix still * remains in the path. For example, prefix = sub1/sub2/ and path is * * foo -> sub1/sub2/foo (full prefix) * ../foo -> sub1/foo (remaining prefix is sub1/) * ../../bar -> bar (no remaining prefix) * ../../sub1/sub2/foo -> s
| 117 | * `pwd`/../bar -> sub1/bar (no remaining prefix) |
| 118 | */ |
| 119 | char *prefix_path_gently(struct repository *repo, |
| 120 | const char *prefix, int len, |
| 121 | int *remaining_prefix, const char *path) |
| 122 | { |
| 123 | const char *orig = path; |
| 124 | char *sanitized; |
| 125 | if (is_absolute_path(orig)) { |
| 126 | sanitized = xmallocz(strlen(path)); |
| 127 | if (remaining_prefix) |
| 128 | *remaining_prefix = 0; |
| 129 | if (normalize_path_copy_len(sanitized, path, remaining_prefix)) { |
| 130 | free(sanitized); |
| 131 | return NULL; |
| 132 | } |
| 133 | if (abspath_part_inside_repo(repo, sanitized)) { |
| 134 | free(sanitized); |
| 135 | return NULL; |
| 136 | } |
| 137 | } else { |
| 138 | sanitized = xstrfmt("%.*s%s", len, len ? prefix : "", path); |
| 139 | if (remaining_prefix) |
| 140 | *remaining_prefix = len; |
| 141 | if (normalize_path_copy_len(sanitized, sanitized, remaining_prefix)) { |
| 142 | free(sanitized); |
| 143 | return NULL; |
| 144 | } |
| 145 | } |
| 146 | return sanitized; |
| 147 | } |
| 148 | |
| 149 | char *prefix_path(struct repository *repo, const char *prefix, int len, const char *path) |
| 150 | { |