| Linus Torvalds | 26c8a53 | 2005-07-08 23:20:59 | [diff] [blame] | 1 | /* |
| Torsten Bögershausen | 3a429d3 | 2013-03-30 09:53:32 | [diff] [blame] | 2 | * Utilities for paths and pathnames |
| Linus Torvalds | 26c8a53 | 2005-07-08 23:20:59 | [diff] [blame] | 3 | */ |
| Elijah Newren | 61a7b98 | 2023-03-21 06:26:06 | [diff] [blame] | 4 | #include "git-compat-util.h" |
| Elijah Newren | 0b027f6 | 2023-03-21 06:25:58 | [diff] [blame] | 5 | #include "abspath.h" |
| Elijah Newren | 32a8f51 | 2023-03-21 06:26:03 | [diff] [blame] | 6 | #include "environment.h" |
| Elijah Newren | f394e09 | 2023-03-21 06:25:54 | [diff] [blame] | 7 | #include "gettext.h" |
| Elijah Newren | 41771fa | 2023-02-24 00:09:27 | [diff] [blame] | 8 | #include "hex.h" |
| Brandon Williams | c14c234 | 2017-06-22 18:43:33 | [diff] [blame] | 9 | #include "repository.h" |
| Matthieu Moy | 395de25 | 2009-11-17 17:24:25 | [diff] [blame] | 10 | #include "strbuf.h" |
| Michael Haggerty | a5ccdbe | 2012-10-28 16:16:23 | [diff] [blame] | 11 | #include "string-list.h" |
| Nguyễn Thái Ngọc Duy | 77a6d84 | 2014-11-30 08:24:54 | [diff] [blame] | 12 | #include "dir.h" |
| Nguyễn Thái Ngọc Duy | 2e641d5 | 2016-04-22 13:01:29 | [diff] [blame] | 13 | #include "worktree.h" |
| Elijah Newren | e38da48 | 2023-03-21 06:26:05 | [diff] [blame] | 14 | #include "setup.h" |
| Jacob Keller | 99b43a6 | 2016-08-31 23:27:22 | [diff] [blame] | 15 | #include "submodule-config.h" |
| Brandon Williams | e7d72d0 | 2017-06-22 18:43:35 | [diff] [blame] | 16 | #include "path.h" |
| Jonathan Tan | 0abe14f | 2017-08-18 22:20:26 | [diff] [blame] | 17 | #include "packfile.h" |
| Elijah Newren | a034e91 | 2023-05-16 06:34:06 | [diff] [blame] | 18 | #include "object-store-ll.h" |
| Johannes Schindelin | 76a53d6 | 2019-10-28 12:57:18 | [diff] [blame] | 19 | #include "lockfile.h" |
| Johannes Schindelin | e394a16 | 2021-07-24 22:06:53 | [diff] [blame] | 20 | #include "exec-cmd.h" |
| Linus Torvalds | 26c8a53 | 2005-07-08 23:20:59 | [diff] [blame] | 21 | |
| Ramsay Jones | f66450a | 2013-06-22 19:42:47 | [diff] [blame] | 22 | static int get_st_mode_bits(const char *path, int *mode) |
| Torsten Bögershausen | 0117c2f | 2013-03-23 12:40:29 | [diff] [blame] | 23 | { |
| 24 | struct stat st; |
| 25 | if (lstat(path, &st) < 0) |
| 26 | return -1; |
| 27 | *mode = st.st_mode; |
| 28 | return 0; |
| 29 | } |
| Torsten Bögershausen | 0117c2f | 2013-03-23 12:40:29 | [diff] [blame] | 30 | |
| Linus Torvalds | 26c8a53 | 2005-07-08 23:20:59 | [diff] [blame] | 31 | static char bad_path[] = "/bad-path/"; |
| 32 | |
| Nguyễn Thái Ngọc Duy | 4ef9caf | 2014-11-30 08:24:26 | [diff] [blame] | 33 | static struct strbuf *get_pathname(void) |
| Linus Torvalds | e7676d2 | 2006-09-11 19:03:15 | [diff] [blame] | 34 | { |
| Nguyễn Thái Ngọc Duy | 4ef9caf | 2014-11-30 08:24:26 | [diff] [blame] | 35 | static struct strbuf pathname_array[4] = { |
| 36 | STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT |
| 37 | }; |
| Linus Torvalds | e7676d2 | 2006-09-11 19:03:15 | [diff] [blame] | 38 | static int index; |
| René Scharfe | bb84735 | 2016-10-23 17:57:30 | [diff] [blame] | 39 | struct strbuf *sb = &pathname_array[index]; |
| 40 | index = (index + 1) % ARRAY_SIZE(pathname_array); |
| Nguyễn Thái Ngọc Duy | 4ef9caf | 2014-11-30 08:24:26 | [diff] [blame] | 41 | strbuf_reset(sb); |
| 42 | return sb; |
| Linus Torvalds | e7676d2 | 2006-09-11 19:03:15 | [diff] [blame] | 43 | } |
| 44 | |
| Jeff King | 8262715 | 2017-10-03 23:30:40 | [diff] [blame] | 45 | static const char *cleanup_path(const char *path) |
| Linus Torvalds | 26c8a53 | 2005-07-08 23:20:59 | [diff] [blame] | 46 | { |
| 47 | /* Clean it up */ |
| Jeff King | 8262715 | 2017-10-03 23:30:40 | [diff] [blame] | 48 | if (skip_prefix(path, "./", &path)) { |
| Linus Torvalds | 26c8a53 | 2005-07-08 23:20:59 | [diff] [blame] | 49 | while (*path == '/') |
| 50 | path++; |
| 51 | } |
| 52 | return path; |
| 53 | } |
| 54 | |
| Nguyễn Thái Ngọc Duy | 4ef9caf | 2014-11-30 08:24:26 | [diff] [blame] | 55 | static void strbuf_cleanup_path(struct strbuf *sb) |
| 56 | { |
| Jeff King | 8262715 | 2017-10-03 23:30:40 | [diff] [blame] | 57 | const char *path = cleanup_path(sb->buf); |
| Nguyễn Thái Ngọc Duy | 4ef9caf | 2014-11-30 08:24:26 | [diff] [blame] | 58 | if (path > sb->buf) |
| 59 | strbuf_remove(sb, 0, path - sb->buf); |
| 60 | } |
| 61 | |
| Alex Riesen | 108bebe | 2008-10-26 21:59:13 | [diff] [blame] | 62 | char *mksnpath(char *buf, size_t n, const char *fmt, ...) |
| 63 | { |
| 64 | va_list args; |
| 65 | unsigned len; |
| 66 | |
| 67 | va_start(args, fmt); |
| 68 | len = vsnprintf(buf, n, fmt, args); |
| 69 | va_end(args); |
| 70 | if (len >= n) { |
| Daniel Lowe | 9db56f7 | 2008-11-10 21:07:52 | [diff] [blame] | 71 | strlcpy(buf, bad_path, n); |
| Alex Riesen | 108bebe | 2008-10-26 21:59:13 | [diff] [blame] | 72 | return buf; |
| 73 | } |
| Jeff King | 8262715 | 2017-10-03 23:30:40 | [diff] [blame] | 74 | return (char *)cleanup_path(buf); |
| Alex Riesen | 108bebe | 2008-10-26 21:59:13 | [diff] [blame] | 75 | } |
| 76 | |
| Nguyễn Thái Ngọc Duy | 557bd83 | 2014-11-30 08:24:31 | [diff] [blame] | 77 | static int dir_prefix(const char *buf, const char *dir) |
| Alex Riesen | fe2d777 | 2008-10-27 09:22:21 | [diff] [blame] | 78 | { |
| Nguyễn Thái Ngọc Duy | 557bd83 | 2014-11-30 08:24:31 | [diff] [blame] | 79 | int len = strlen(dir); |
| 80 | return !strncmp(buf, dir, len) && |
| 81 | (is_dir_sep(buf[len]) || buf[len] == '\0'); |
| Alex Riesen | fe2d777 | 2008-10-27 09:22:21 | [diff] [blame] | 82 | } |
| 83 | |
| Nguyễn Thái Ngọc Duy | 557bd83 | 2014-11-30 08:24:31 | [diff] [blame] | 84 | /* $buf =~ m|$dir/+$file| but without regex */ |
| 85 | static int is_dir_file(const char *buf, const char *dir, const char *file) |
| Alex Riesen | aba13e7 | 2008-10-27 10:17:51 | [diff] [blame] | 86 | { |
| Nguyễn Thái Ngọc Duy | 557bd83 | 2014-11-30 08:24:31 | [diff] [blame] | 87 | int len = strlen(dir); |
| 88 | if (strncmp(buf, dir, len) || !is_dir_sep(buf[len])) |
| 89 | return 0; |
| 90 | while (is_dir_sep(buf[len])) |
| 91 | len++; |
| 92 | return !strcmp(buf + len, file); |
| 93 | } |
| 94 | |
| 95 | static void replace_dir(struct strbuf *buf, int len, const char *newdir) |
| 96 | { |
| 97 | int newlen = strlen(newdir); |
| 98 | int need_sep = (buf->buf[len] && !is_dir_sep(buf->buf[len])) && |
| 99 | !is_dir_sep(newdir[newlen - 1]); |
| 100 | if (need_sep) |
| 101 | len--; /* keep one char, to be replaced with '/' */ |
| 102 | strbuf_splice(buf, 0, len, newdir, newlen); |
| 103 | if (need_sep) |
| 104 | buf->buf[newlen] = '/'; |
| 105 | } |
| 106 | |
| David Turner | 0701530 | 2015-09-01 02:13:09 | [diff] [blame] | 107 | struct common_dir { |
| 108 | /* Not considered garbage for report_linked_checkout_garbage */ |
| 109 | unsigned ignore_garbage:1; |
| 110 | unsigned is_dir:1; |
| SZEDER Gábor | c72fc40 | 2019-10-21 16:00:42 | [diff] [blame] | 111 | /* Belongs to the common dir, though it may contain paths that don't */ |
| 112 | unsigned is_common:1; |
| 113 | const char *path; |
| David Turner | 0701530 | 2015-09-01 02:13:09 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | static struct common_dir common_list[] = { |
| SZEDER Gábor | c72fc40 | 2019-10-21 16:00:42 | [diff] [blame] | 117 | { 0, 1, 1, "branches" }, |
| 118 | { 0, 1, 1, "common" }, |
| 119 | { 0, 1, 1, "hooks" }, |
| 120 | { 0, 1, 1, "info" }, |
| 121 | { 0, 0, 0, "info/sparse-checkout" }, |
| 122 | { 1, 1, 1, "logs" }, |
| 123 | { 1, 0, 0, "logs/HEAD" }, |
| 124 | { 0, 1, 0, "logs/refs/bisect" }, |
| 125 | { 0, 1, 0, "logs/refs/rewritten" }, |
| 126 | { 0, 1, 0, "logs/refs/worktree" }, |
| 127 | { 0, 1, 1, "lost-found" }, |
| 128 | { 0, 1, 1, "objects" }, |
| 129 | { 0, 1, 1, "refs" }, |
| 130 | { 0, 1, 0, "refs/bisect" }, |
| 131 | { 0, 1, 0, "refs/rewritten" }, |
| 132 | { 0, 1, 0, "refs/worktree" }, |
| 133 | { 0, 1, 1, "remotes" }, |
| 134 | { 0, 1, 1, "worktrees" }, |
| 135 | { 0, 1, 1, "rr-cache" }, |
| 136 | { 0, 1, 1, "svn" }, |
| 137 | { 0, 0, 1, "config" }, |
| 138 | { 1, 0, 1, "gc.pid" }, |
| 139 | { 0, 0, 1, "packed-refs" }, |
| 140 | { 0, 0, 1, "shallow" }, |
| David Turner | 0701530 | 2015-09-01 02:13:09 | [diff] [blame] | 141 | { 0, 0, 0, NULL } |
| Nguyễn Thái Ngọc Duy | c7b3a3d | 2014-11-30 08:24:36 | [diff] [blame] | 142 | }; |
| 143 | |
| David Turner | 4e09cf2 | 2015-09-01 02:13:10 | [diff] [blame] | 144 | /* |
| 145 | * A compressed trie. A trie node consists of zero or more characters that |
| 146 | * are common to all elements with this prefix, optionally followed by some |
| 147 | * children. If value is not NULL, the trie node is a terminal node. |
| 148 | * |
| 149 | * For example, consider the following set of strings: |
| 150 | * abc |
| 151 | * def |
| 152 | * definite |
| 153 | * definition |
| 154 | * |
| Li Peng | 832c0e5 | 2016-05-06 12:36:46 | [diff] [blame] | 155 | * The trie would look like: |
| David Turner | 4e09cf2 | 2015-09-01 02:13:10 | [diff] [blame] | 156 | * root: len = 0, children a and d non-NULL, value = NULL. |
| 157 | * a: len = 2, contents = bc, value = (data for "abc") |
| 158 | * d: len = 2, contents = ef, children i non-NULL, value = (data for "def") |
| 159 | * i: len = 3, contents = nit, children e and i non-NULL, value = NULL |
| 160 | * e: len = 0, children all NULL, value = (data for "definite") |
| 161 | * i: len = 2, contents = on, children all NULL, |
| 162 | * value = (data for "definition") |
| 163 | */ |
| 164 | struct trie { |
| 165 | struct trie *children[256]; |
| 166 | int len; |
| 167 | char *contents; |
| 168 | void *value; |
| 169 | }; |
| 170 | |
| 171 | static struct trie *make_trie_node(const char *key, void *value) |
| 172 | { |
| 173 | struct trie *new_node = xcalloc(1, sizeof(*new_node)); |
| 174 | new_node->len = strlen(key); |
| 175 | if (new_node->len) { |
| 176 | new_node->contents = xmalloc(new_node->len); |
| 177 | memcpy(new_node->contents, key, new_node->len); |
| 178 | } |
| 179 | new_node->value = value; |
| 180 | return new_node; |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * Add a key/value pair to a trie. The key is assumed to be \0-terminated. |
| 185 | * If there was an existing value for this key, return it. |
| 186 | */ |
| 187 | static void *add_to_trie(struct trie *root, const char *key, void *value) |
| 188 | { |
| 189 | struct trie *child; |
| 190 | void *old; |
| 191 | int i; |
| 192 | |
| 193 | if (!*key) { |
| 194 | /* we have reached the end of the key */ |
| 195 | old = root->value; |
| 196 | root->value = value; |
| 197 | return old; |
| 198 | } |
| 199 | |
| 200 | for (i = 0; i < root->len; i++) { |
| 201 | if (root->contents[i] == key[i]) |
| 202 | continue; |
| 203 | |
| 204 | /* |
| 205 | * Split this node: child will contain this node's |
| 206 | * existing children. |
| 207 | */ |
| Andrey Okoshkin | 55d7d15 | 2017-10-24 15:15:05 | [diff] [blame] | 208 | child = xmalloc(sizeof(*child)); |
| David Turner | 4e09cf2 | 2015-09-01 02:13:10 | [diff] [blame] | 209 | memcpy(child->children, root->children, sizeof(root->children)); |
| 210 | |
| 211 | child->len = root->len - i - 1; |
| 212 | if (child->len) { |
| 213 | child->contents = xstrndup(root->contents + i + 1, |
| 214 | child->len); |
| 215 | } |
| 216 | child->value = root->value; |
| 217 | root->value = NULL; |
| 218 | root->len = i; |
| 219 | |
| 220 | memset(root->children, 0, sizeof(root->children)); |
| 221 | root->children[(unsigned char)root->contents[i]] = child; |
| 222 | |
| 223 | /* This is the newly-added child. */ |
| 224 | root->children[(unsigned char)key[i]] = |
| 225 | make_trie_node(key + i + 1, value); |
| 226 | return NULL; |
| 227 | } |
| 228 | |
| 229 | /* We have matched the entire compressed section */ |
| 230 | if (key[i]) { |
| 231 | child = root->children[(unsigned char)key[root->len]]; |
| 232 | if (child) { |
| 233 | return add_to_trie(child, key + root->len + 1, value); |
| 234 | } else { |
| 235 | child = make_trie_node(key + root->len + 1, value); |
| 236 | root->children[(unsigned char)key[root->len]] = child; |
| 237 | return NULL; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | old = root->value; |
| 242 | root->value = value; |
| 243 | return old; |
| 244 | } |
| 245 | |
| SZEDER Gábor | 7cb8c92 | 2019-10-21 16:00:40 | [diff] [blame] | 246 | typedef int (*match_fn)(const char *unmatched, void *value, void *baton); |
| David Turner | 4e09cf2 | 2015-09-01 02:13:10 | [diff] [blame] | 247 | |
| 248 | /* |
| 249 | * Search a trie for some key. Find the longest /-or-\0-terminated |
| SZEDER Gábor | 7cb8c92 | 2019-10-21 16:00:40 | [diff] [blame] | 250 | * prefix of the key for which the trie contains a value. If there is |
| 251 | * no such prefix, return -1. Otherwise call fn with the unmatched |
| 252 | * portion of the key and the found value. If fn returns 0 or |
| 253 | * positive, then return its return value. If fn returns negative, |
| 254 | * then call fn with the next-longest /-terminated prefix of the key |
| 255 | * (i.e. a parent directory) for which the trie contains a value, and |
| 256 | * handle its return value the same way. If there is no shorter |
| 257 | * /-terminated prefix with a value left, then return the negative |
| 258 | * return value of the most recent fn invocation. |
| David Turner | 4e09cf2 | 2015-09-01 02:13:10 | [diff] [blame] | 259 | * |
| 260 | * The key is partially normalized: consecutive slashes are skipped. |
| 261 | * |
| SZEDER Gábor | 7cb8c92 | 2019-10-21 16:00:40 | [diff] [blame] | 262 | * For example, consider the trie containing only [logs, |
| 263 | * logs/refs/bisect], both with values, but not logs/refs. |
| David Turner | 4e09cf2 | 2015-09-01 02:13:10 | [diff] [blame] | 264 | * |
| SZEDER Gábor | 7cb8c92 | 2019-10-21 16:00:40 | [diff] [blame] | 265 | * | key | unmatched | prefix to node | return value | |
| 266 | * |--------------------|----------------|------------------|--------------| |
| 267 | * | a | not called | n/a | -1 | |
| 268 | * | logstore | not called | n/a | -1 | |
| 269 | * | logs | \0 | logs | as per fn | |
| 270 | * | logs/ | / | logs | as per fn | |
| 271 | * | logs/refs | /refs | logs | as per fn | |
| 272 | * | logs/refs/ | /refs/ | logs | as per fn | |
| 273 | * | logs/refs/b | /refs/b | logs | as per fn | |
| 274 | * | logs/refs/bisected | /refs/bisected | logs | as per fn | |
| 275 | * | logs/refs/bisect | \0 | logs/refs/bisect | as per fn | |
| 276 | * | logs/refs/bisect/ | / | logs/refs/bisect | as per fn | |
| 277 | * | logs/refs/bisect/a | /a | logs/refs/bisect | as per fn | |
| 278 | * | (If fn in the previous line returns -1, then fn is called once more:) | |
| 279 | * | logs/refs/bisect/a | /refs/bisect/a | logs | as per fn | |
| 280 | * |--------------------|----------------|------------------|--------------| |
| David Turner | 4e09cf2 | 2015-09-01 02:13:10 | [diff] [blame] | 281 | */ |
| 282 | static int trie_find(struct trie *root, const char *key, match_fn fn, |
| 283 | void *baton) |
| 284 | { |
| 285 | int i; |
| 286 | int result; |
| 287 | struct trie *child; |
| 288 | |
| 289 | if (!*key) { |
| 290 | /* we have reached the end of the key */ |
| 291 | if (root->value && !root->len) |
| 292 | return fn(key, root->value, baton); |
| 293 | else |
| 294 | return -1; |
| 295 | } |
| 296 | |
| 297 | for (i = 0; i < root->len; i++) { |
| 298 | /* Partial path normalization: skip consecutive slashes. */ |
| 299 | if (key[i] == '/' && key[i+1] == '/') { |
| 300 | key++; |
| 301 | continue; |
| 302 | } |
| 303 | if (root->contents[i] != key[i]) |
| 304 | return -1; |
| 305 | } |
| 306 | |
| 307 | /* Matched the entire compressed section */ |
| 308 | key += i; |
| SZEDER Gábor | f45f88b | 2019-10-21 16:00:43 | [diff] [blame] | 309 | if (!*key) { |
| David Turner | 4e09cf2 | 2015-09-01 02:13:10 | [diff] [blame] | 310 | /* End of key */ |
| SZEDER Gábor | f45f88b | 2019-10-21 16:00:43 | [diff] [blame] | 311 | if (root->value) |
| 312 | return fn(key, root->value, baton); |
| 313 | else |
| 314 | return -1; |
| 315 | } |
| David Turner | 4e09cf2 | 2015-09-01 02:13:10 | [diff] [blame] | 316 | |
| 317 | /* Partial path normalization: skip consecutive slashes */ |
| 318 | while (key[0] == '/' && key[1] == '/') |
| 319 | key++; |
| 320 | |
| 321 | child = root->children[(unsigned char)*key]; |
| 322 | if (child) |
| 323 | result = trie_find(child, key + 1, fn, baton); |
| 324 | else |
| 325 | result = -1; |
| 326 | |
| 327 | if (result >= 0 || (*key != '/' && *key != 0)) |
| 328 | return result; |
| 329 | if (root->value) |
| 330 | return fn(key, root->value, baton); |
| 331 | else |
| 332 | return -1; |
| 333 | } |
| 334 | |
| 335 | static struct trie common_trie; |
| 336 | static int common_trie_done_setup; |
| 337 | |
| 338 | static void init_common_trie(void) |
| 339 | { |
| 340 | struct common_dir *p; |
| 341 | |
| 342 | if (common_trie_done_setup) |
| 343 | return; |
| 344 | |
| SZEDER Gábor | c72fc40 | 2019-10-21 16:00:42 | [diff] [blame] | 345 | for (p = common_list; p->path; p++) |
| 346 | add_to_trie(&common_trie, p->path, p); |
| David Turner | 4e09cf2 | 2015-09-01 02:13:10 | [diff] [blame] | 347 | |
| 348 | common_trie_done_setup = 1; |
| 349 | } |
| 350 | |
| 351 | /* |
| 352 | * Helper function for update_common_dir: returns 1 if the dir |
| 353 | * prefix is common. |
| 354 | */ |
| Jeff King | d3dcfa0 | 2023-02-24 06:39:15 | [diff] [blame] | 355 | static int check_common(const char *unmatched, void *value, |
| 356 | void *baton UNUSED) |
| David Turner | 4e09cf2 | 2015-09-01 02:13:10 | [diff] [blame] | 357 | { |
| 358 | struct common_dir *dir = value; |
| 359 | |
| David Turner | 4e09cf2 | 2015-09-01 02:13:10 | [diff] [blame] | 360 | if (dir->is_dir && (unmatched[0] == 0 || unmatched[0] == '/')) |
| SZEDER Gábor | c72fc40 | 2019-10-21 16:00:42 | [diff] [blame] | 361 | return dir->is_common; |
| David Turner | 4e09cf2 | 2015-09-01 02:13:10 | [diff] [blame] | 362 | |
| 363 | if (!dir->is_dir && unmatched[0] == 0) |
| SZEDER Gábor | c72fc40 | 2019-10-21 16:00:42 | [diff] [blame] | 364 | return dir->is_common; |
| David Turner | 4e09cf2 | 2015-09-01 02:13:10 | [diff] [blame] | 365 | |
| 366 | return 0; |
| 367 | } |
| 368 | |
| Junio C Hamano | 1c630ba | 2015-10-15 22:43:31 | [diff] [blame] | 369 | static void update_common_dir(struct strbuf *buf, int git_dir_len, |
| 370 | const char *common_dir) |
| Nguyễn Thái Ngọc Duy | c7b3a3d | 2014-11-30 08:24:36 | [diff] [blame] | 371 | { |
| 372 | char *base = buf->buf + git_dir_len; |
| Johannes Schindelin | 76a53d6 | 2019-10-28 12:57:18 | [diff] [blame] | 373 | int has_lock_suffix = strbuf_strip_suffix(buf, LOCK_SUFFIX); |
| 374 | |
| David Turner | 4e09cf2 | 2015-09-01 02:13:10 | [diff] [blame] | 375 | init_common_trie(); |
| 376 | if (trie_find(&common_trie, base, check_common, NULL) > 0) |
| Junio C Hamano | 1c630ba | 2015-10-15 22:43:31 | [diff] [blame] | 377 | replace_dir(buf, git_dir_len, common_dir); |
| Johannes Schindelin | 76a53d6 | 2019-10-28 12:57:18 | [diff] [blame] | 378 | |
| 379 | if (has_lock_suffix) |
| 380 | strbuf_addstr(buf, LOCK_SUFFIX); |
| Nguyễn Thái Ngọc Duy | c7b3a3d | 2014-11-30 08:24:36 | [diff] [blame] | 381 | } |
| 382 | |
| Nguyễn Thái Ngọc Duy | 77a6d84 | 2014-11-30 08:24:54 | [diff] [blame] | 383 | void report_linked_checkout_garbage(void) |
| 384 | { |
| 385 | struct strbuf sb = STRBUF_INIT; |
| David Turner | 0701530 | 2015-09-01 02:13:09 | [diff] [blame] | 386 | const struct common_dir *p; |
| Nguyễn Thái Ngọc Duy | 77a6d84 | 2014-11-30 08:24:54 | [diff] [blame] | 387 | int len; |
| 388 | |
| Brandon Williams | c14c234 | 2017-06-22 18:43:33 | [diff] [blame] | 389 | if (!the_repository->different_commondir) |
| Nguyễn Thái Ngọc Duy | 77a6d84 | 2014-11-30 08:24:54 | [diff] [blame] | 390 | return; |
| 391 | strbuf_addf(&sb, "%s/", get_git_dir()); |
| 392 | len = sb.len; |
| SZEDER Gábor | c72fc40 | 2019-10-21 16:00:42 | [diff] [blame] | 393 | for (p = common_list; p->path; p++) { |
| 394 | const char *path = p->path; |
| David Turner | 0701530 | 2015-09-01 02:13:09 | [diff] [blame] | 395 | if (p->ignore_garbage) |
| Nguyễn Thái Ngọc Duy | 77a6d84 | 2014-11-30 08:24:54 | [diff] [blame] | 396 | continue; |
| 397 | strbuf_setlen(&sb, len); |
| 398 | strbuf_addstr(&sb, path); |
| 399 | if (file_exists(sb.buf)) |
| Junio C Hamano | 0a489b0 | 2015-08-13 18:02:52 | [diff] [blame] | 400 | report_garbage(PACKDIR_FILE_GARBAGE, sb.buf); |
| Nguyễn Thái Ngọc Duy | 77a6d84 | 2014-11-30 08:24:54 | [diff] [blame] | 401 | } |
| 402 | strbuf_release(&sb); |
| 403 | } |
| 404 | |
| Brandon Williams | f9a8a47 | 2017-06-22 18:43:38 | [diff] [blame] | 405 | static void adjust_git_path(const struct repository *repo, |
| 406 | struct strbuf *buf, int git_dir_len) |
| Nguyễn Thái Ngọc Duy | 557bd83 | 2014-11-30 08:24:31 | [diff] [blame] | 407 | { |
| 408 | const char *base = buf->buf + git_dir_len; |
| Brandon Williams | c14c234 | 2017-06-22 18:43:33 | [diff] [blame] | 409 | if (is_dir_file(base, "info", "grafts")) |
| Nguyễn Thái Ngọc Duy | 557bd83 | 2014-11-30 08:24:31 | [diff] [blame] | 410 | strbuf_splice(buf, 0, buf->len, |
| Brandon Williams | f9a8a47 | 2017-06-22 18:43:38 | [diff] [blame] | 411 | repo->graft_file, strlen(repo->graft_file)); |
| Brandon Williams | c14c234 | 2017-06-22 18:43:33 | [diff] [blame] | 412 | else if (!strcmp(base, "index")) |
| Nguyễn Thái Ngọc Duy | 557bd83 | 2014-11-30 08:24:31 | [diff] [blame] | 413 | strbuf_splice(buf, 0, buf->len, |
| Brandon Williams | f9a8a47 | 2017-06-22 18:43:38 | [diff] [blame] | 414 | repo->index_file, strlen(repo->index_file)); |
| Brandon Williams | c14c234 | 2017-06-22 18:43:33 | [diff] [blame] | 415 | else if (dir_prefix(base, "objects")) |
| Jeff King | f0eaf63 | 2018-11-12 14:50:39 | [diff] [blame] | 416 | replace_dir(buf, git_dir_len + 7, repo->objects->odb->path); |
| Johannes Schindelin | 9445b49 | 2016-08-16 13:14:27 | [diff] [blame] | 417 | else if (git_hooks_path && dir_prefix(base, "hooks")) |
| 418 | replace_dir(buf, git_dir_len + 5, git_hooks_path); |
| Brandon Williams | f9a8a47 | 2017-06-22 18:43:38 | [diff] [blame] | 419 | else if (repo->different_commondir) |
| 420 | update_common_dir(buf, git_dir_len, repo->commondir); |
| Nguyễn Thái Ngọc Duy | 557bd83 | 2014-11-30 08:24:31 | [diff] [blame] | 421 | } |
| 422 | |
| Brandon Williams | f9a8a47 | 2017-06-22 18:43:38 | [diff] [blame] | 423 | static void strbuf_worktree_gitdir(struct strbuf *buf, |
| 424 | const struct repository *repo, |
| 425 | const struct worktree *wt) |
| 426 | { |
| 427 | if (!wt) |
| 428 | strbuf_addstr(buf, repo->gitdir); |
| 429 | else if (!wt->id) |
| 430 | strbuf_addstr(buf, repo->commondir); |
| 431 | else |
| 432 | strbuf_git_common_path(buf, repo, "worktrees/%s", wt->id); |
| 433 | } |
| 434 | |
| 435 | static void do_git_path(const struct repository *repo, |
| 436 | const struct worktree *wt, struct strbuf *buf, |
| Nguyễn Thái Ngọc Duy | 2e641d5 | 2016-04-22 13:01:29 | [diff] [blame] | 437 | const char *fmt, va_list args) |
| Alex Riesen | fe2d777 | 2008-10-27 09:22:21 | [diff] [blame] | 438 | { |
| Nguyễn Thái Ngọc Duy | 557bd83 | 2014-11-30 08:24:31 | [diff] [blame] | 439 | int gitdir_len; |
| Brandon Williams | f9a8a47 | 2017-06-22 18:43:38 | [diff] [blame] | 440 | strbuf_worktree_gitdir(buf, repo, wt); |
| Nguyễn Thái Ngọc Duy | 4ef9caf | 2014-11-30 08:24:26 | [diff] [blame] | 441 | if (buf->len && !is_dir_sep(buf->buf[buf->len - 1])) |
| 442 | strbuf_addch(buf, '/'); |
| Nguyễn Thái Ngọc Duy | 557bd83 | 2014-11-30 08:24:31 | [diff] [blame] | 443 | gitdir_len = buf->len; |
| Nguyễn Thái Ngọc Duy | 4ef9caf | 2014-11-30 08:24:26 | [diff] [blame] | 444 | strbuf_vaddf(buf, fmt, args); |
| Brandon Williams | 5431073 | 2017-06-22 18:43:39 | [diff] [blame] | 445 | if (!wt) |
| 446 | adjust_git_path(repo, buf, gitdir_len); |
| Nguyễn Thái Ngọc Duy | 4ef9caf | 2014-11-30 08:24:26 | [diff] [blame] | 447 | strbuf_cleanup_path(buf); |
| Alex Riesen | fe2d777 | 2008-10-27 09:22:21 | [diff] [blame] | 448 | } |
| 449 | |
| Brandon Williams | 3181d86 | 2017-06-22 18:43:40 | [diff] [blame] | 450 | char *repo_git_path(const struct repository *repo, |
| 451 | const char *fmt, ...) |
| 452 | { |
| 453 | struct strbuf path = STRBUF_INIT; |
| 454 | va_list args; |
| 455 | va_start(args, fmt); |
| 456 | do_git_path(repo, NULL, &path, fmt, args); |
| 457 | va_end(args); |
| 458 | return strbuf_detach(&path, NULL); |
| 459 | } |
| 460 | |
| 461 | void strbuf_repo_git_path(struct strbuf *sb, |
| 462 | const struct repository *repo, |
| 463 | const char *fmt, ...) |
| 464 | { |
| 465 | va_list args; |
| 466 | va_start(args, fmt); |
| 467 | do_git_path(repo, NULL, sb, fmt, args); |
| 468 | va_end(args); |
| 469 | } |
| 470 | |
| Jeff King | bb3788c | 2015-09-24 21:05:40 | [diff] [blame] | 471 | char *git_path_buf(struct strbuf *buf, const char *fmt, ...) |
| 472 | { |
| 473 | va_list args; |
| 474 | strbuf_reset(buf); |
| 475 | va_start(args, fmt); |
| Brandon Williams | f9a8a47 | 2017-06-22 18:43:38 | [diff] [blame] | 476 | do_git_path(the_repository, NULL, buf, fmt, args); |
| Jeff King | bb3788c | 2015-09-24 21:05:40 | [diff] [blame] | 477 | va_end(args); |
| 478 | return buf->buf; |
| 479 | } |
| 480 | |
| Nguyễn Thái Ngọc Duy | 1a83c24 | 2014-11-30 08:24:28 | [diff] [blame] | 481 | void strbuf_git_path(struct strbuf *sb, const char *fmt, ...) |
| Alex Riesen | aba13e7 | 2008-10-27 10:17:51 | [diff] [blame] | 482 | { |
| Alex Riesen | aba13e7 | 2008-10-27 10:17:51 | [diff] [blame] | 483 | va_list args; |
| 484 | va_start(args, fmt); |
| Brandon Williams | f9a8a47 | 2017-06-22 18:43:38 | [diff] [blame] | 485 | do_git_path(the_repository, NULL, sb, fmt, args); |
| Alex Riesen | aba13e7 | 2008-10-27 10:17:51 | [diff] [blame] | 486 | va_end(args); |
| Alex Riesen | aba13e7 | 2008-10-27 10:17:51 | [diff] [blame] | 487 | } |
| 488 | |
| Nguyễn Thái Ngọc Duy | 57a23b7 | 2014-11-30 08:24:30 | [diff] [blame] | 489 | const char *git_path(const char *fmt, ...) |
| 490 | { |
| 491 | struct strbuf *pathname = get_pathname(); |
| 492 | va_list args; |
| 493 | va_start(args, fmt); |
| Brandon Williams | f9a8a47 | 2017-06-22 18:43:38 | [diff] [blame] | 494 | do_git_path(the_repository, NULL, pathname, fmt, args); |
| Nguyễn Thái Ngọc Duy | 57a23b7 | 2014-11-30 08:24:30 | [diff] [blame] | 495 | va_end(args); |
| 496 | return pathname->buf; |
| Alex Riesen | aba13e7 | 2008-10-27 10:17:51 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | char *git_pathdup(const char *fmt, ...) |
| 500 | { |
| Nguyễn Thái Ngọc Duy | 4ef9caf | 2014-11-30 08:24:26 | [diff] [blame] | 501 | struct strbuf path = STRBUF_INIT; |
| Alex Riesen | aba13e7 | 2008-10-27 10:17:51 | [diff] [blame] | 502 | va_list args; |
| 503 | va_start(args, fmt); |
| Brandon Williams | f9a8a47 | 2017-06-22 18:43:38 | [diff] [blame] | 504 | do_git_path(the_repository, NULL, &path, fmt, args); |
| Alex Riesen | aba13e7 | 2008-10-27 10:17:51 | [diff] [blame] | 505 | va_end(args); |
| Nguyễn Thái Ngọc Duy | 4ef9caf | 2014-11-30 08:24:26 | [diff] [blame] | 506 | return strbuf_detach(&path, NULL); |
| Alex Riesen | aba13e7 | 2008-10-27 10:17:51 | [diff] [blame] | 507 | } |
| 508 | |
| Huynh Khoi Nguyen Nguyen | 21cf322 | 2012-06-22 09:03:23 | [diff] [blame] | 509 | char *mkpathdup(const char *fmt, ...) |
| 510 | { |
| Huynh Khoi Nguyen Nguyen | 21cf322 | 2012-06-22 09:03:23 | [diff] [blame] | 511 | struct strbuf sb = STRBUF_INIT; |
| 512 | va_list args; |
| Huynh Khoi Nguyen Nguyen | 21cf322 | 2012-06-22 09:03:23 | [diff] [blame] | 513 | va_start(args, fmt); |
| 514 | strbuf_vaddf(&sb, fmt, args); |
| 515 | va_end(args); |
| Nguyễn Thái Ngọc Duy | 4ef9caf | 2014-11-30 08:24:26 | [diff] [blame] | 516 | strbuf_cleanup_path(&sb); |
| 517 | return strbuf_detach(&sb, NULL); |
| Huynh Khoi Nguyen Nguyen | 21cf322 | 2012-06-22 09:03:23 | [diff] [blame] | 518 | } |
| 519 | |
| Nguyễn Thái Ngọc Duy | dcf6926 | 2014-11-30 08:24:27 | [diff] [blame] | 520 | const char *mkpath(const char *fmt, ...) |
| Linus Torvalds | 26c8a53 | 2005-07-08 23:20:59 | [diff] [blame] | 521 | { |
| 522 | va_list args; |
| Nguyễn Thái Ngọc Duy | 4ef9caf | 2014-11-30 08:24:26 | [diff] [blame] | 523 | struct strbuf *pathname = get_pathname(); |
| Linus Torvalds | 26c8a53 | 2005-07-08 23:20:59 | [diff] [blame] | 524 | va_start(args, fmt); |
| Nguyễn Thái Ngọc Duy | 4ef9caf | 2014-11-30 08:24:26 | [diff] [blame] | 525 | strbuf_vaddf(pathname, fmt, args); |
| Linus Torvalds | 26c8a53 | 2005-07-08 23:20:59 | [diff] [blame] | 526 | va_end(args); |
| Nguyễn Thái Ngọc Duy | 4ef9caf | 2014-11-30 08:24:26 | [diff] [blame] | 527 | return cleanup_path(pathname->buf); |
| Linus Torvalds | 26c8a53 | 2005-07-08 23:20:59 | [diff] [blame] | 528 | } |
| Holger Eitzenberger | f2db68e | 2005-08-04 20:43:03 | [diff] [blame] | 529 | |
| Nguyễn Thái Ngọc Duy | 2e641d5 | 2016-04-22 13:01:29 | [diff] [blame] | 530 | const char *worktree_git_path(const struct worktree *wt, const char *fmt, ...) |
| 531 | { |
| 532 | struct strbuf *pathname = get_pathname(); |
| 533 | va_list args; |
| 534 | va_start(args, fmt); |
| Brandon Williams | f9a8a47 | 2017-06-22 18:43:38 | [diff] [blame] | 535 | do_git_path(the_repository, wt, pathname, fmt, args); |
| Nguyễn Thái Ngọc Duy | 2e641d5 | 2016-04-22 13:01:29 | [diff] [blame] | 536 | va_end(args); |
| 537 | return pathname->buf; |
| 538 | } |
| 539 | |
| Brandon Williams | b42b0c0 | 2017-06-22 18:43:41 | [diff] [blame] | 540 | static void do_worktree_path(const struct repository *repo, |
| 541 | struct strbuf *buf, |
| 542 | const char *fmt, va_list args) |
| 543 | { |
| 544 | strbuf_addstr(buf, repo->worktree); |
| 545 | if(buf->len && !is_dir_sep(buf->buf[buf->len - 1])) |
| 546 | strbuf_addch(buf, '/'); |
| 547 | |
| 548 | strbuf_vaddf(buf, fmt, args); |
| 549 | strbuf_cleanup_path(buf); |
| 550 | } |
| 551 | |
| 552 | char *repo_worktree_path(const struct repository *repo, const char *fmt, ...) |
| 553 | { |
| 554 | struct strbuf path = STRBUF_INIT; |
| 555 | va_list args; |
| 556 | |
| 557 | if (!repo->worktree) |
| 558 | return NULL; |
| 559 | |
| 560 | va_start(args, fmt); |
| 561 | do_worktree_path(repo, &path, fmt, args); |
| 562 | va_end(args); |
| 563 | |
| 564 | return strbuf_detach(&path, NULL); |
| 565 | } |
| 566 | |
| 567 | void strbuf_repo_worktree_path(struct strbuf *sb, |
| 568 | const struct repository *repo, |
| 569 | const char *fmt, ...) |
| 570 | { |
| 571 | va_list args; |
| 572 | |
| 573 | if (!repo->worktree) |
| 574 | return; |
| 575 | |
| 576 | va_start(args, fmt); |
| 577 | do_worktree_path(repo, sb, fmt, args); |
| 578 | va_end(args); |
| 579 | } |
| 580 | |
| Jacob Keller | 99b43a6 | 2016-08-31 23:27:22 | [diff] [blame] | 581 | /* Returns 0 on success, negative on failure. */ |
| Jacob Keller | 99b43a6 | 2016-08-31 23:27:22 | [diff] [blame] | 582 | static int do_submodule_path(struct strbuf *buf, const char *path, |
| 583 | const char *fmt, va_list args) |
| Heiko Voigt | 0bad611 | 2010-07-07 13:39:11 | [diff] [blame] | 584 | { |
| Max Kirillov | 11f9dd7 | 2015-09-13 22:17:42 | [diff] [blame] | 585 | struct strbuf git_submodule_common_dir = STRBUF_INIT; |
| 586 | struct strbuf git_submodule_dir = STRBUF_INIT; |
| Nguyễn Thái Ngọc Duy | bbbb7de | 2017-03-26 02:42:30 | [diff] [blame] | 587 | int ret; |
| Heiko Voigt | 0bad611 | 2010-07-07 13:39:11 | [diff] [blame] | 588 | |
| Nguyễn Thái Ngọc Duy | bbbb7de | 2017-03-26 02:42:30 | [diff] [blame] | 589 | ret = submodule_to_gitdir(&git_submodule_dir, path); |
| 590 | if (ret) |
| 591 | goto cleanup; |
| Heiko Voigt | 0bad611 | 2010-07-07 13:39:11 | [diff] [blame] | 592 | |
| Nguyễn Thái Ngọc Duy | bbbb7de | 2017-03-26 02:42:30 | [diff] [blame] | 593 | strbuf_complete(&git_submodule_dir, '/'); |
| 594 | strbuf_addbuf(buf, &git_submodule_dir); |
| Nguyễn Thái Ngọc Duy | 4ef9caf | 2014-11-30 08:24:26 | [diff] [blame] | 595 | strbuf_vaddf(buf, fmt, args); |
| Max Kirillov | 11f9dd7 | 2015-09-13 22:17:42 | [diff] [blame] | 596 | |
| 597 | if (get_common_dir_noenv(&git_submodule_common_dir, git_submodule_dir.buf)) |
| 598 | update_common_dir(buf, git_submodule_dir.len, git_submodule_common_dir.buf); |
| 599 | |
| Nguyễn Thái Ngọc Duy | 4ef9caf | 2014-11-30 08:24:26 | [diff] [blame] | 600 | strbuf_cleanup_path(buf); |
| Max Kirillov | 11f9dd7 | 2015-09-13 22:17:42 | [diff] [blame] | 601 | |
| Jacob Keller | 99b43a6 | 2016-08-31 23:27:22 | [diff] [blame] | 602 | cleanup: |
| Max Kirillov | 11f9dd7 | 2015-09-13 22:17:42 | [diff] [blame] | 603 | strbuf_release(&git_submodule_dir); |
| 604 | strbuf_release(&git_submodule_common_dir); |
| Nguyễn Thái Ngọc Duy | bbbb7de | 2017-03-26 02:42:30 | [diff] [blame] | 605 | return ret; |
| Jeff King | f5895fd | 2015-08-10 09:32:22 | [diff] [blame] | 606 | } |
| 607 | |
| Jeff King | f5895fd | 2015-08-10 09:32:22 | [diff] [blame] | 608 | char *git_pathdup_submodule(const char *path, const char *fmt, ...) |
| 609 | { |
| Jacob Keller | 99b43a6 | 2016-08-31 23:27:22 | [diff] [blame] | 610 | int err; |
| Jeff King | f5895fd | 2015-08-10 09:32:22 | [diff] [blame] | 611 | va_list args; |
| 612 | struct strbuf buf = STRBUF_INIT; |
| 613 | va_start(args, fmt); |
| Jacob Keller | 99b43a6 | 2016-08-31 23:27:22 | [diff] [blame] | 614 | err = do_submodule_path(&buf, path, fmt, args); |
| Jeff King | f5895fd | 2015-08-10 09:32:22 | [diff] [blame] | 615 | va_end(args); |
| Jacob Keller | 99b43a6 | 2016-08-31 23:27:22 | [diff] [blame] | 616 | if (err) { |
| 617 | strbuf_release(&buf); |
| 618 | return NULL; |
| 619 | } |
| Jeff King | f5895fd | 2015-08-10 09:32:22 | [diff] [blame] | 620 | return strbuf_detach(&buf, NULL); |
| 621 | } |
| 622 | |
| Jacob Keller | 99b43a6 | 2016-08-31 23:27:22 | [diff] [blame] | 623 | int strbuf_git_path_submodule(struct strbuf *buf, const char *path, |
| 624 | const char *fmt, ...) |
| Jeff King | f5895fd | 2015-08-10 09:32:22 | [diff] [blame] | 625 | { |
| Jacob Keller | 99b43a6 | 2016-08-31 23:27:22 | [diff] [blame] | 626 | int err; |
| Jeff King | f5895fd | 2015-08-10 09:32:22 | [diff] [blame] | 627 | va_list args; |
| 628 | va_start(args, fmt); |
| Jacob Keller | 99b43a6 | 2016-08-31 23:27:22 | [diff] [blame] | 629 | err = do_submodule_path(buf, path, fmt, args); |
| Jeff King | f5895fd | 2015-08-10 09:32:22 | [diff] [blame] | 630 | va_end(args); |
| Jacob Keller | 99b43a6 | 2016-08-31 23:27:22 | [diff] [blame] | 631 | |
| 632 | return err; |
| Jeff King | f5895fd | 2015-08-10 09:32:22 | [diff] [blame] | 633 | } |
| 634 | |
| Brandon Williams | b337172 | 2017-06-22 18:43:37 | [diff] [blame] | 635 | static void do_git_common_path(const struct repository *repo, |
| 636 | struct strbuf *buf, |
| Nguyễn Thái Ngọc Duy | 15cdfea | 2016-04-22 13:01:25 | [diff] [blame] | 637 | const char *fmt, |
| 638 | va_list args) |
| 639 | { |
| Brandon Williams | b337172 | 2017-06-22 18:43:37 | [diff] [blame] | 640 | strbuf_addstr(buf, repo->commondir); |
| Nguyễn Thái Ngọc Duy | 15cdfea | 2016-04-22 13:01:25 | [diff] [blame] | 641 | if (buf->len && !is_dir_sep(buf->buf[buf->len - 1])) |
| 642 | strbuf_addch(buf, '/'); |
| 643 | strbuf_vaddf(buf, fmt, args); |
| 644 | strbuf_cleanup_path(buf); |
| 645 | } |
| 646 | |
| 647 | const char *git_common_path(const char *fmt, ...) |
| 648 | { |
| 649 | struct strbuf *pathname = get_pathname(); |
| 650 | va_list args; |
| 651 | va_start(args, fmt); |
| Brandon Williams | b337172 | 2017-06-22 18:43:37 | [diff] [blame] | 652 | do_git_common_path(the_repository, pathname, fmt, args); |
| Nguyễn Thái Ngọc Duy | 15cdfea | 2016-04-22 13:01:25 | [diff] [blame] | 653 | va_end(args); |
| 654 | return pathname->buf; |
| 655 | } |
| 656 | |
| Brandon Williams | b337172 | 2017-06-22 18:43:37 | [diff] [blame] | 657 | void strbuf_git_common_path(struct strbuf *sb, |
| 658 | const struct repository *repo, |
| 659 | const char *fmt, ...) |
| Nguyễn Thái Ngọc Duy | 15cdfea | 2016-04-22 13:01:25 | [diff] [blame] | 660 | { |
| 661 | va_list args; |
| 662 | va_start(args, fmt); |
| Brandon Williams | b337172 | 2017-06-22 18:43:37 | [diff] [blame] | 663 | do_git_common_path(repo, sb, fmt, args); |
| Nguyễn Thái Ngọc Duy | 15cdfea | 2016-04-22 13:01:25 | [diff] [blame] | 664 | va_end(args); |
| 665 | } |
| 666 | |
| Junio C Hamano | c847f53 | 2007-01-02 07:31:08 | [diff] [blame] | 667 | int validate_headref(const char *path) |
| Junio C Hamano | 0870ca7 | 2005-11-18 22:59:34 | [diff] [blame] | 668 | { |
| 669 | struct stat st; |
| Jeff King | 7eb4b9d | 2017-09-27 06:17:26 | [diff] [blame] | 670 | char buffer[256]; |
| 671 | const char *refname; |
| Jeff King | 0bca165 | 2017-09-27 06:17:36 | [diff] [blame] | 672 | struct object_id oid; |
| Heikki Orsila | 0104ca0 | 2008-04-27 18:21:58 | [diff] [blame] | 673 | int fd; |
| 674 | ssize_t len; |
| Junio C Hamano | 0870ca7 | 2005-11-18 22:59:34 | [diff] [blame] | 675 | |
| 676 | if (lstat(path, &st) < 0) |
| 677 | return -1; |
| 678 | |
| 679 | /* Make sure it is a "refs/.." symlink */ |
| 680 | if (S_ISLNK(st.st_mode)) { |
| 681 | len = readlink(path, buffer, sizeof(buffer)-1); |
| Junio C Hamano | 222b167 | 2009-02-12 21:02:09 | [diff] [blame] | 682 | if (len >= 5 && !memcmp("refs/", buffer, 5)) |
| Junio C Hamano | 0870ca7 | 2005-11-18 22:59:34 | [diff] [blame] | 683 | return 0; |
| 684 | return -1; |
| 685 | } |
| 686 | |
| 687 | /* |
| 688 | * Anything else, just open it and try to see if it is a symbolic ref. |
| 689 | */ |
| 690 | fd = open(path, O_RDONLY); |
| 691 | if (fd < 0) |
| 692 | return -1; |
| Andy Whitcroft | 93d26e4 | 2007-01-08 15:58:08 | [diff] [blame] | 693 | len = read_in_full(fd, buffer, sizeof(buffer)-1); |
| Junio C Hamano | 0870ca7 | 2005-11-18 22:59:34 | [diff] [blame] | 694 | close(fd); |
| 695 | |
| Jeff King | 6e68c91 | 2017-09-27 06:17:23 | [diff] [blame] | 696 | if (len < 0) |
| 697 | return -1; |
| 698 | buffer[len] = '\0'; |
| 699 | |
| Junio C Hamano | 0870ca7 | 2005-11-18 22:59:34 | [diff] [blame] | 700 | /* |
| 701 | * Is it a symbolic ref? |
| 702 | */ |
| Jeff King | 7eb4b9d | 2017-09-27 06:17:26 | [diff] [blame] | 703 | if (skip_prefix(buffer, "ref:", &refname)) { |
| 704 | while (isspace(*refname)) |
| 705 | refname++; |
| 706 | if (starts_with(refname, "refs/")) |
| Junio C Hamano | c847f53 | 2007-01-02 07:31:08 | [diff] [blame] | 707 | return 0; |
| 708 | } |
| 709 | |
| 710 | /* |
| 711 | * Is this a detached HEAD? |
| 712 | */ |
| Jeff King | 0bca165 | 2017-09-27 06:17:36 | [diff] [blame] | 713 | if (!get_oid_hex(buffer, &oid)) |
| Junio C Hamano | 0870ca7 | 2005-11-18 22:59:34 | [diff] [blame] | 714 | return 0; |
| Junio C Hamano | c847f53 | 2007-01-02 07:31:08 | [diff] [blame] | 715 | |
| Junio C Hamano | 0870ca7 | 2005-11-18 22:59:34 | [diff] [blame] | 716 | return -1; |
| 717 | } |
| 718 | |
| Matthieu Moy | 395de25 | 2009-11-17 17:24:25 | [diff] [blame] | 719 | static struct passwd *getpw_str(const char *username, size_t len) |
| Andreas Ericsson | 54f4b87 | 2005-11-17 19:37:14 | [diff] [blame] | 720 | { |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 721 | struct passwd *pw; |
| René Scharfe | 5c0b13f | 2014-07-19 15:35:34 | [diff] [blame] | 722 | char *username_z = xmemdupz(username, len); |
| Matthieu Moy | 395de25 | 2009-11-17 17:24:25 | [diff] [blame] | 723 | pw = getpwnam(username_z); |
| 724 | free(username_z); |
| 725 | return pw; |
| 726 | } |
| Andreas Ericsson | 54f4b87 | 2005-11-17 19:37:14 | [diff] [blame] | 727 | |
| Matthieu Moy | 395de25 | 2009-11-17 17:24:25 | [diff] [blame] | 728 | /* |
| Johannes Schindelin | 789f6f2 | 2021-07-24 22:06:50 | [diff] [blame] | 729 | * Return a string with ~ and ~user expanded via getpw*. Returns NULL on getpw |
| 730 | * failure or if path is NULL. |
| Nguyễn Thái Ngọc Duy | 4aad2f1 | 2017-04-05 10:24:38 | [diff] [blame] | 731 | * |
| Johannes Schindelin | 644e6b2 | 2021-07-24 22:06:51 | [diff] [blame] | 732 | * If real_home is true, strbuf_realpath($HOME) is used in the `~/` expansion. |
| Johannes Schindelin | e394a16 | 2021-07-24 22:06:53 | [diff] [blame] | 733 | * |
| 734 | * If the path starts with `%(prefix)/`, the remainder is interpreted as |
| 735 | * relative to where Git is installed, and expanded to the absolute path. |
| Matthieu Moy | 395de25 | 2009-11-17 17:24:25 | [diff] [blame] | 736 | */ |
| Johannes Schindelin | a03b097 | 2021-07-24 22:06:52 | [diff] [blame] | 737 | char *interpolate_path(const char *path, int real_home) |
| Matthieu Moy | 395de25 | 2009-11-17 17:24:25 | [diff] [blame] | 738 | { |
| 739 | struct strbuf user_path = STRBUF_INIT; |
| Matthieu Moy | 395de25 | 2009-11-17 17:24:25 | [diff] [blame] | 740 | const char *to_copy = path; |
| 741 | |
| Junio C Hamano | afe8a90 | 2022-05-02 16:50:37 | [diff] [blame] | 742 | if (!path) |
| Matthieu Moy | 395de25 | 2009-11-17 17:24:25 | [diff] [blame] | 743 | goto return_null; |
| Johannes Schindelin | e394a16 | 2021-07-24 22:06:53 | [diff] [blame] | 744 | |
| 745 | if (skip_prefix(path, "%(prefix)/", &path)) |
| 746 | return system_path(path); |
| 747 | |
| Matthieu Moy | 395de25 | 2009-11-17 17:24:25 | [diff] [blame] | 748 | if (path[0] == '~') { |
| Jeff King | 53ec551 | 2014-01-28 01:36:12 | [diff] [blame] | 749 | const char *first_slash = strchrnul(path, '/'); |
| Matthieu Moy | 395de25 | 2009-11-17 17:24:25 | [diff] [blame] | 750 | const char *username = path + 1; |
| 751 | size_t username_len = first_slash - username; |
| Matthieu Moy | df2a79f | 2009-11-19 15:21:15 | [diff] [blame] | 752 | if (username_len == 0) { |
| 753 | const char *home = getenv("HOME"); |
| Jonathan Nieder | 79bf149 | 2010-07-26 15:06:51 | [diff] [blame] | 754 | if (!home) |
| 755 | goto return_null; |
| Nguyễn Thái Ngọc Duy | 4aad2f1 | 2017-04-05 10:24:38 | [diff] [blame] | 756 | if (real_home) |
| René Scharfe | fa2bb34 | 2017-10-01 14:44:06 | [diff] [blame] | 757 | strbuf_add_real_path(&user_path, home); |
| Nguyễn Thái Ngọc Duy | 4aad2f1 | 2017-04-05 10:24:38 | [diff] [blame] | 758 | else |
| 759 | strbuf_addstr(&user_path, home); |
| Johannes Schindelin | 5ca6b7b | 2016-03-23 10:55:00 | [diff] [blame] | 760 | #ifdef GIT_WINDOWS_NATIVE |
| 761 | convert_slashes(user_path.buf); |
| 762 | #endif |
| Matthieu Moy | df2a79f | 2009-11-19 15:21:15 | [diff] [blame] | 763 | } else { |
| 764 | struct passwd *pw = getpw_str(username, username_len); |
| 765 | if (!pw) |
| 766 | goto return_null; |
| René Scharfe | cedc61a | 2014-07-16 23:38:18 | [diff] [blame] | 767 | strbuf_addstr(&user_path, pw->pw_dir); |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 768 | } |
| Matthieu Moy | 395de25 | 2009-11-17 17:24:25 | [diff] [blame] | 769 | to_copy = first_slash; |
| Junio C Hamano | 0870ca7 | 2005-11-18 22:59:34 | [diff] [blame] | 770 | } |
| René Scharfe | cedc61a | 2014-07-16 23:38:18 | [diff] [blame] | 771 | strbuf_addstr(&user_path, to_copy); |
| Matthieu Moy | 395de25 | 2009-11-17 17:24:25 | [diff] [blame] | 772 | return strbuf_detach(&user_path, NULL); |
| 773 | return_null: |
| 774 | strbuf_release(&user_path); |
| 775 | return NULL; |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 776 | } |
| Andreas Ericsson | 54f4b87 | 2005-11-17 19:37:14 | [diff] [blame] | 777 | |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 778 | /* |
| 779 | * First, one directory to try is determined by the following algorithm. |
| 780 | * |
| 781 | * (0) If "strict" is given, the path is used as given and no DWIM is |
| 782 | * done. Otherwise: |
| 783 | * (1) "~/path" to mean path under the running user's home directory; |
| 784 | * (2) "~user/path" to mean path under named user's home directory; |
| 785 | * (3) "relative/path" to mean cwd relative directory; or |
| 786 | * (4) "/absolute/path" to mean absolute directory. |
| 787 | * |
| Paul Tan | c8c3f1d | 2015-03-31 13:39:27 | [diff] [blame] | 788 | * Unless "strict" is given, we check "%s/.git", "%s", "%s.git/.git", "%s.git" |
| 789 | * in this order. We select the first one that is a valid git repository, and |
| 790 | * chdir() to it. If none match, or we fail to chdir, we return NULL. |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 791 | * |
| 792 | * If all goes well, we return the directory we used to chdir() (but |
| 793 | * before ~user is expanded), avoiding getcwd() resolving symbolic |
| 794 | * links. User relative paths are also returned as they are given, |
| 795 | * except DWIM suffixing. |
| 796 | */ |
| Erik Faye-Lund | 1c64b48 | 2011-10-04 20:02:00 | [diff] [blame] | 797 | const char *enter_repo(const char *path, int strict) |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 798 | { |
| Jeff King | e9ba678 | 2015-09-24 21:07:45 | [diff] [blame] | 799 | static struct strbuf validated_path = STRBUF_INIT; |
| 800 | static struct strbuf used_path = STRBUF_INIT; |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 801 | |
| 802 | if (!path) |
| 803 | return NULL; |
| 804 | |
| 805 | if (!strict) { |
| 806 | static const char *suffix[] = { |
| Jeff King | b3256eb | 2012-02-02 21:59:13 | [diff] [blame] | 807 | "/.git", "", ".git/.git", ".git", NULL, |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 808 | }; |
| Phil Hord | 0310676 | 2011-10-04 20:05:17 | [diff] [blame] | 809 | const char *gitfile; |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 810 | int len = strlen(path); |
| 811 | int i; |
| Erik Faye-Lund | 1c64b48 | 2011-10-04 20:02:00 | [diff] [blame] | 812 | while ((1 < len) && (path[len-1] == '/')) |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 813 | len--; |
| Erik Faye-Lund | 1c64b48 | 2011-10-04 20:02:00 | [diff] [blame] | 814 | |
| Jeff King | e9ba678 | 2015-09-24 21:07:45 | [diff] [blame] | 815 | /* |
| 816 | * We can handle arbitrary-sized buffers, but this remains as a |
| 817 | * sanity check on untrusted input. |
| 818 | */ |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 819 | if (PATH_MAX <= len) |
| 820 | return NULL; |
| Erik Faye-Lund | 1c64b48 | 2011-10-04 20:02:00 | [diff] [blame] | 821 | |
| Jeff King | e9ba678 | 2015-09-24 21:07:45 | [diff] [blame] | 822 | strbuf_reset(&used_path); |
| 823 | strbuf_reset(&validated_path); |
| 824 | strbuf_add(&used_path, path, len); |
| 825 | strbuf_add(&validated_path, path, len); |
| 826 | |
| 827 | if (used_path.buf[0] == '~') { |
| Johannes Schindelin | a03b097 | 2021-07-24 22:06:52 | [diff] [blame] | 828 | char *newpath = interpolate_path(used_path.buf, 0); |
| Jeff King | e9ba678 | 2015-09-24 21:07:45 | [diff] [blame] | 829 | if (!newpath) |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 830 | return NULL; |
| Jeff King | e9ba678 | 2015-09-24 21:07:45 | [diff] [blame] | 831 | strbuf_attach(&used_path, newpath, strlen(newpath), |
| 832 | strlen(newpath)); |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 833 | } |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 834 | for (i = 0; suffix[i]; i++) { |
| Jeff King | b3256eb | 2012-02-02 21:59:13 | [diff] [blame] | 835 | struct stat st; |
| Jeff King | e9ba678 | 2015-09-24 21:07:45 | [diff] [blame] | 836 | size_t baselen = used_path.len; |
| 837 | strbuf_addstr(&used_path, suffix[i]); |
| 838 | if (!stat(used_path.buf, &st) && |
| Jeff King | b3256eb | 2012-02-02 21:59:13 | [diff] [blame] | 839 | (S_ISREG(st.st_mode) || |
| Jeff King | e9ba678 | 2015-09-24 21:07:45 | [diff] [blame] | 840 | (S_ISDIR(st.st_mode) && is_git_directory(used_path.buf)))) { |
| 841 | strbuf_addstr(&validated_path, suffix[i]); |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 842 | break; |
| 843 | } |
| Jeff King | e9ba678 | 2015-09-24 21:07:45 | [diff] [blame] | 844 | strbuf_setlen(&used_path, baselen); |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 845 | } |
| Phil Hord | 0310676 | 2011-10-04 20:05:17 | [diff] [blame] | 846 | if (!suffix[i]) |
| 847 | return NULL; |
| Junio C Hamano | 7889179 | 2015-10-20 22:24:00 | [diff] [blame] | 848 | gitfile = read_gitfile(used_path.buf); |
| Jeff King | e9ba678 | 2015-09-24 21:07:45 | [diff] [blame] | 849 | if (gitfile) { |
| 850 | strbuf_reset(&used_path); |
| 851 | strbuf_addstr(&used_path, gitfile); |
| 852 | } |
| 853 | if (chdir(used_path.buf)) |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 854 | return NULL; |
| Jeff King | e9ba678 | 2015-09-24 21:07:45 | [diff] [blame] | 855 | path = validated_path.buf; |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 856 | } |
| Nguyễn Thái Ngọc Duy | 1f5fbe1 | 2015-09-28 13:06:14 | [diff] [blame] | 857 | else { |
| 858 | const char *gitfile = read_gitfile(path); |
| 859 | if (gitfile) |
| 860 | path = gitfile; |
| 861 | if (chdir(path)) |
| 862 | return NULL; |
| 863 | } |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 864 | |
| Nguyễn Thái Ngọc Duy | 0f64cc4 | 2015-09-28 13:06:13 | [diff] [blame] | 865 | if (is_git_directory(".")) { |
| Alexandr Miloslavskiy | 0915a5b | 2020-03-06 19:03:13 | [diff] [blame] | 866 | set_git_dir(".", 0); |
| brian m. carlson | cfe3917 | 2020-02-22 20:17:37 | [diff] [blame] | 867 | check_repository_format(NULL); |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 868 | return path; |
| Andreas Ericsson | 54f4b87 | 2005-11-17 19:37:14 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | return NULL; |
| 872 | } |
| Junio C Hamano | 138086a | 2006-06-10 05:07:23 | [diff] [blame] | 873 | |
| Torsten Bögershausen | cbe43b8 | 2013-03-30 09:53:47 | [diff] [blame] | 874 | static int calc_shared_perm(int mode) |
| Junio C Hamano | 138086a | 2006-06-10 05:07:23 | [diff] [blame] | 875 | { |
| Torsten Bögershausen | cbe43b8 | 2013-03-30 09:53:47 | [diff] [blame] | 876 | int tweak; |
| Junio C Hamano | 138086a | 2006-06-10 05:07:23 | [diff] [blame] | 877 | |
| Jeff King | 7875acb | 2016-03-11 22:36:49 | [diff] [blame] | 878 | if (get_shared_repository() < 0) |
| 879 | tweak = -get_shared_repository(); |
| Junio C Hamano | 5a688fe | 2009-03-25 23:19:36 | [diff] [blame] | 880 | else |
| Jeff King | 7875acb | 2016-03-11 22:36:49 | [diff] [blame] | 881 | tweak = get_shared_repository(); |
| Junio C Hamano | 94df250 | 2006-06-10 06:09:49 | [diff] [blame] | 882 | |
| Junio C Hamano | 5a688fe | 2009-03-25 23:19:36 | [diff] [blame] | 883 | if (!(mode & S_IWUSR)) |
| 884 | tweak &= ~0222; |
| 885 | if (mode & S_IXUSR) |
| 886 | /* Copy read bits to execute bits */ |
| 887 | tweak |= (tweak & 0444) >> 2; |
| Jeff King | 7875acb | 2016-03-11 22:36:49 | [diff] [blame] | 888 | if (get_shared_repository() < 0) |
| Junio C Hamano | 5a688fe | 2009-03-25 23:19:36 | [diff] [blame] | 889 | mode = (mode & ~0777) | tweak; |
| 890 | else |
| Petr Baudis | 8c6202d | 2008-07-12 01:15:03 | [diff] [blame] | 891 | mode |= tweak; |
| Junio C Hamano | 94df250 | 2006-06-10 06:09:49 | [diff] [blame] | 892 | |
| Torsten Bögershausen | cbe43b8 | 2013-03-30 09:53:47 | [diff] [blame] | 893 | return mode; |
| 894 | } |
| 895 | |
| 896 | |
| 897 | int adjust_shared_perm(const char *path) |
| 898 | { |
| 899 | int old_mode, new_mode; |
| 900 | |
| Jeff King | 7875acb | 2016-03-11 22:36:49 | [diff] [blame] | 901 | if (!get_shared_repository()) |
| Torsten Bögershausen | cbe43b8 | 2013-03-30 09:53:47 | [diff] [blame] | 902 | return 0; |
| 903 | if (get_st_mode_bits(path, &old_mode) < 0) |
| 904 | return -1; |
| 905 | |
| 906 | new_mode = calc_shared_perm(old_mode); |
| 907 | if (S_ISDIR(old_mode)) { |
| Heikki Orsila | 06cbe85 | 2008-04-16 08:34:24 | [diff] [blame] | 908 | /* Copy read bits to execute bits */ |
| Torsten Bögershausen | cbe43b8 | 2013-03-30 09:53:47 | [diff] [blame] | 909 | new_mode |= (new_mode & 0444) >> 2; |
| Junio C Hamano | 671bbf7 | 2022-10-28 21:16:09 | [diff] [blame] | 910 | |
| 911 | /* |
| 912 | * g+s matters only if any extra access is granted |
| 913 | * based on group membership. |
| 914 | */ |
| 915 | if (FORCE_DIR_SET_GID && (new_mode & 060)) |
| 916 | new_mode |= FORCE_DIR_SET_GID; |
| Heikki Orsila | 06cbe85 | 2008-04-16 08:34:24 | [diff] [blame] | 917 | } |
| 918 | |
| Torsten Bögershausen | cbe43b8 | 2013-03-30 09:53:47 | [diff] [blame] | 919 | if (((old_mode ^ new_mode) & ~S_IFMT) && |
| 920 | chmod(path, (new_mode & ~S_IFMT)) < 0) |
| Junio C Hamano | 138086a | 2006-06-10 05:07:23 | [diff] [blame] | 921 | return -2; |
| 922 | return 0; |
| 923 | } |
| Johannes Schindelin | e5392c5 | 2007-08-01 00:28:59 | [diff] [blame] | 924 | |
| David Turner | eb33876 | 2015-11-10 11:42:38 | [diff] [blame] | 925 | void safe_create_dir(const char *dir, int share) |
| 926 | { |
| 927 | if (mkdir(dir, 0777) < 0) { |
| 928 | if (errno != EEXIST) { |
| 929 | perror(dir); |
| 930 | exit(1); |
| 931 | } |
| 932 | } |
| 933 | else if (share && adjust_shared_perm(dir)) |
| 934 | die(_("Could not make %s writable by group"), dir); |
| 935 | } |
| 936 | |
| Jiang Xin | 7fbd422 | 2013-10-14 02:29:39 | [diff] [blame] | 937 | static int have_same_root(const char *path1, const char *path2) |
| 938 | { |
| 939 | int is_abs1, is_abs2; |
| 940 | |
| 941 | is_abs1 = is_absolute_path(path1); |
| 942 | is_abs2 = is_absolute_path(path2); |
| 943 | return (is_abs1 && is_abs2 && tolower(path1[0]) == tolower(path2[0])) || |
| 944 | (!is_abs1 && !is_abs2); |
| 945 | } |
| 946 | |
| Jiang Xin | e02ca72 | 2013-06-25 15:53:43 | [diff] [blame] | 947 | /* |
| 948 | * Give path as relative to prefix. |
| 949 | * |
| 950 | * The strbuf may or may not be used, so do not assume it contains the |
| 951 | * returned path. |
| 952 | */ |
| 953 | const char *relative_path(const char *in, const char *prefix, |
| 954 | struct strbuf *sb) |
| Linus Torvalds | 044bbbc | 2008-06-19 19:34:06 | [diff] [blame] | 955 | { |
| Jiang Xin | e02ca72 | 2013-06-25 15:53:43 | [diff] [blame] | 956 | int in_len = in ? strlen(in) : 0; |
| 957 | int prefix_len = prefix ? strlen(prefix) : 0; |
| 958 | int in_off = 0; |
| 959 | int prefix_off = 0; |
| Junio C Hamano | 288123f | 2010-01-22 03:05:19 | [diff] [blame] | 960 | int i = 0, j = 0; |
| 961 | |
| Jiang Xin | e02ca72 | 2013-06-25 15:53:43 | [diff] [blame] | 962 | if (!in_len) |
| 963 | return "./"; |
| 964 | else if (!prefix_len) |
| 965 | return in; |
| 966 | |
| Johannes Schindelin | 2f36eed | 2016-01-12 07:57:22 | [diff] [blame] | 967 | if (have_same_root(in, prefix)) |
| Jiang Xin | 7fbd422 | 2013-10-14 02:29:39 | [diff] [blame] | 968 | /* bypass dos_drive, for "c:" is identical to "C:" */ |
| Johannes Schindelin | 2f36eed | 2016-01-12 07:57:22 | [diff] [blame] | 969 | i = j = has_dos_drive_prefix(in); |
| 970 | else { |
| Jiang Xin | 7fbd422 | 2013-10-14 02:29:39 | [diff] [blame] | 971 | return in; |
| 972 | } |
| 973 | |
| Jiang Xin | e02ca72 | 2013-06-25 15:53:43 | [diff] [blame] | 974 | while (i < prefix_len && j < in_len && prefix[i] == in[j]) { |
| 975 | if (is_dir_sep(prefix[i])) { |
| 976 | while (is_dir_sep(prefix[i])) |
| Junio C Hamano | 288123f | 2010-01-22 03:05:19 | [diff] [blame] | 977 | i++; |
| Jiang Xin | e02ca72 | 2013-06-25 15:53:43 | [diff] [blame] | 978 | while (is_dir_sep(in[j])) |
| Junio C Hamano | 288123f | 2010-01-22 03:05:19 | [diff] [blame] | 979 | j++; |
| Jiang Xin | e02ca72 | 2013-06-25 15:53:43 | [diff] [blame] | 980 | prefix_off = i; |
| 981 | in_off = j; |
| 982 | } else { |
| 983 | i++; |
| 984 | j++; |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | if ( |
| 989 | /* "prefix" seems like prefix of "in" */ |
| 990 | i >= prefix_len && |
| 991 | /* |
| 992 | * but "/foo" is not a prefix of "/foobar" |
| 993 | * (i.e. prefix not end with '/') |
| 994 | */ |
| 995 | prefix_off < prefix_len) { |
| 996 | if (j >= in_len) { |
| 997 | /* in="/a/b", prefix="/a/b" */ |
| 998 | in_off = in_len; |
| 999 | } else if (is_dir_sep(in[j])) { |
| 1000 | /* in="/a/b/c", prefix="/a/b" */ |
| 1001 | while (is_dir_sep(in[j])) |
| 1002 | j++; |
| 1003 | in_off = j; |
| 1004 | } else { |
| 1005 | /* in="/a/bbb/c", prefix="/a/b" */ |
| 1006 | i = prefix_off; |
| 1007 | } |
| 1008 | } else if ( |
| 1009 | /* "in" is short than "prefix" */ |
| 1010 | j >= in_len && |
| 1011 | /* "in" not end with '/' */ |
| 1012 | in_off < in_len) { |
| 1013 | if (is_dir_sep(prefix[i])) { |
| 1014 | /* in="/a/b", prefix="/a/b/c/" */ |
| 1015 | while (is_dir_sep(prefix[i])) |
| 1016 | i++; |
| 1017 | in_off = in_len; |
| 1018 | } |
| 1019 | } |
| 1020 | in += in_off; |
| 1021 | in_len -= in_off; |
| 1022 | |
| 1023 | if (i >= prefix_len) { |
| 1024 | if (!in_len) |
| 1025 | return "./"; |
| 1026 | else |
| 1027 | return in; |
| 1028 | } |
| 1029 | |
| 1030 | strbuf_reset(sb); |
| 1031 | strbuf_grow(sb, in_len); |
| 1032 | |
| 1033 | while (i < prefix_len) { |
| 1034 | if (is_dir_sep(prefix[i])) { |
| 1035 | strbuf_addstr(sb, "../"); |
| 1036 | while (is_dir_sep(prefix[i])) |
| 1037 | i++; |
| Junio C Hamano | 288123f | 2010-01-22 03:05:19 | [diff] [blame] | 1038 | continue; |
| Junio C Hamano | 288123f | 2010-01-22 03:05:19 | [diff] [blame] | 1039 | } |
| 1040 | i++; |
| Junio C Hamano | 288123f | 2010-01-22 03:05:19 | [diff] [blame] | 1041 | } |
| Jiang Xin | e02ca72 | 2013-06-25 15:53:43 | [diff] [blame] | 1042 | if (!is_dir_sep(prefix[prefix_len - 1])) |
| 1043 | strbuf_addstr(sb, "../"); |
| 1044 | |
| 1045 | strbuf_addstr(sb, in); |
| 1046 | |
| 1047 | return sb->buf; |
| Linus Torvalds | 044bbbc | 2008-06-19 19:34:06 | [diff] [blame] | 1048 | } |
| David Reiss | ae299be | 2008-05-20 06:48:54 | [diff] [blame] | 1049 | |
| 1050 | /* |
| Jiang Xin | 41894ae | 2013-10-14 02:29:40 | [diff] [blame] | 1051 | * A simpler implementation of relative_path |
| 1052 | * |
| 1053 | * Get relative path by removing "prefix" from "in". This function |
| 1054 | * first appears in v1.5.6-1-g044bbbc, and makes git_dir shorter |
| 1055 | * to increase performance when traversing the path to work_tree. |
| 1056 | */ |
| 1057 | const char *remove_leading_path(const char *in, const char *prefix) |
| 1058 | { |
| Jeff King | 4635768 | 2015-09-24 21:07:47 | [diff] [blame] | 1059 | static struct strbuf buf = STRBUF_INIT; |
| Jiang Xin | 41894ae | 2013-10-14 02:29:40 | [diff] [blame] | 1060 | int i = 0, j = 0; |
| 1061 | |
| 1062 | if (!prefix || !prefix[0]) |
| 1063 | return in; |
| 1064 | while (prefix[i]) { |
| 1065 | if (is_dir_sep(prefix[i])) { |
| 1066 | if (!is_dir_sep(in[j])) |
| 1067 | return in; |
| 1068 | while (is_dir_sep(prefix[i])) |
| 1069 | i++; |
| 1070 | while (is_dir_sep(in[j])) |
| 1071 | j++; |
| 1072 | continue; |
| 1073 | } else if (in[j] != prefix[i]) { |
| 1074 | return in; |
| 1075 | } |
| 1076 | i++; |
| 1077 | j++; |
| 1078 | } |
| 1079 | if ( |
| 1080 | /* "/foo" is a prefix of "/foo" */ |
| 1081 | in[j] && |
| 1082 | /* "/foo" is not a prefix of "/foobar" */ |
| 1083 | !is_dir_sep(prefix[i-1]) && !is_dir_sep(in[j]) |
| 1084 | ) |
| 1085 | return in; |
| 1086 | while (is_dir_sep(in[j])) |
| 1087 | j++; |
| Jeff King | 4635768 | 2015-09-24 21:07:47 | [diff] [blame] | 1088 | |
| 1089 | strbuf_reset(&buf); |
| Jiang Xin | 41894ae | 2013-10-14 02:29:40 | [diff] [blame] | 1090 | if (!in[j]) |
| Jeff King | 4635768 | 2015-09-24 21:07:47 | [diff] [blame] | 1091 | strbuf_addstr(&buf, "."); |
| Jiang Xin | 41894ae | 2013-10-14 02:29:40 | [diff] [blame] | 1092 | else |
| Jeff King | 4635768 | 2015-09-24 21:07:47 | [diff] [blame] | 1093 | strbuf_addstr(&buf, in + j); |
| 1094 | return buf.buf; |
| Jiang Xin | 41894ae | 2013-10-14 02:29:40 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | /* |
| Johannes Sixt | f2a782b | 2009-02-07 15:08:31 | [diff] [blame] | 1098 | * It is okay if dst == src, but they should not overlap otherwise. |
| Jeff King | 9734b74 | 2020-01-30 09:52:19 | [diff] [blame] | 1099 | * The "dst" buffer must be at least as long as "src"; normalizing may shrink |
| 1100 | * the size of the path, but will never grow it. |
| David Reiss | ae299be | 2008-05-20 06:48:54 | [diff] [blame] | 1101 | * |
| Johannes Sixt | f2a782b | 2009-02-07 15:08:31 | [diff] [blame] | 1102 | * Performs the following normalizations on src, storing the result in dst: |
| 1103 | * - Ensures that components are separated by '/' (Windows only) |
| Johannes Sixt | 7814fbe | 2016-12-14 19:37:38 | [diff] [blame] | 1104 | * - Squashes sequences of '/' except "//server/share" on Windows |
| David Reiss | ae299be | 2008-05-20 06:48:54 | [diff] [blame] | 1105 | * - Removes "." components. |
| 1106 | * - Removes ".." components, and the components the precede them. |
| Johannes Sixt | f2a782b | 2009-02-07 15:08:31 | [diff] [blame] | 1107 | * Returns failure (non-zero) if a ".." component appears as first path |
| 1108 | * component anytime during the normalization. Otherwise, returns success (0). |
| David Reiss | ae299be | 2008-05-20 06:48:54 | [diff] [blame] | 1109 | * |
| 1110 | * Note that this function is purely textual. It does not follow symlinks, |
| 1111 | * verify the existence of the path, or make any system calls. |
| Nguyễn Thái Ngọc Duy | 645a29c | 2013-07-14 08:36:03 | [diff] [blame] | 1112 | * |
| 1113 | * prefix_len != NULL is for a specific case of prefix_pathspec(): |
| 1114 | * assume that src == dst and src[0..prefix_len-1] is already |
| 1115 | * normalized, any time "../" eats up to the prefix_len part, |
| 1116 | * prefix_len is reduced. In the end prefix_len is the remaining |
| 1117 | * prefix that has not been overridden by user pathspec. |
| Ray Donnelly | b2a7123 | 2015-10-01 19:04:17 | [diff] [blame] | 1118 | * |
| 1119 | * NEEDSWORK: This function doesn't perform normalization w.r.t. trailing '/'. |
| 1120 | * For everything but the root folder itself, the normalized path should not |
| 1121 | * end with a '/', then the callers need to be fixed up accordingly. |
| 1122 | * |
| David Reiss | ae299be | 2008-05-20 06:48:54 | [diff] [blame] | 1123 | */ |
| Nguyễn Thái Ngọc Duy | 645a29c | 2013-07-14 08:36:03 | [diff] [blame] | 1124 | int normalize_path_copy_len(char *dst, const char *src, int *prefix_len) |
| David Reiss | ae299be | 2008-05-20 06:48:54 | [diff] [blame] | 1125 | { |
| Johannes Sixt | f3cad0a | 2009-02-07 15:08:28 | [diff] [blame] | 1126 | char *dst0; |
| Johannes Sixt | 7814fbe | 2016-12-14 19:37:38 | [diff] [blame] | 1127 | const char *end; |
| David Reiss | ae299be | 2008-05-20 06:48:54 | [diff] [blame] | 1128 | |
| Johannes Sixt | 7814fbe | 2016-12-14 19:37:38 | [diff] [blame] | 1129 | /* |
| 1130 | * Copy initial part of absolute path: "/", "C:/", "//server/share/". |
| 1131 | */ |
| 1132 | end = src + offset_1st_component(src); |
| 1133 | while (src < end) { |
| 1134 | char c = *src++; |
| 1135 | if (is_dir_sep(c)) |
| 1136 | c = '/'; |
| 1137 | *dst++ = c; |
| 1138 | } |
| Johannes Sixt | f3cad0a | 2009-02-07 15:08:28 | [diff] [blame] | 1139 | dst0 = dst; |
| David Reiss | ae299be | 2008-05-20 06:48:54 | [diff] [blame] | 1140 | |
| Johannes Sixt | 7814fbe | 2016-12-14 19:37:38 | [diff] [blame] | 1141 | while (is_dir_sep(*src)) |
| 1142 | src++; |
| David Reiss | ae299be | 2008-05-20 06:48:54 | [diff] [blame] | 1143 | |
| Johannes Sixt | f3cad0a | 2009-02-07 15:08:28 | [diff] [blame] | 1144 | for (;;) { |
| 1145 | char c = *src; |
| David Reiss | ae299be | 2008-05-20 06:48:54 | [diff] [blame] | 1146 | |
| Johannes Sixt | f3cad0a | 2009-02-07 15:08:28 | [diff] [blame] | 1147 | /* |
| 1148 | * A path component that begins with . could be |
| 1149 | * special: |
| 1150 | * (1) "." and ends -- ignore and terminate. |
| 1151 | * (2) "./" -- ignore them, eat slash and continue. |
| 1152 | * (3) ".." and ends -- strip one and terminate. |
| 1153 | * (4) "../" -- strip one, eat slash and continue. |
| 1154 | */ |
| 1155 | if (c == '.') { |
| 1156 | if (!src[1]) { |
| 1157 | /* (1) */ |
| 1158 | src++; |
| 1159 | } else if (is_dir_sep(src[1])) { |
| 1160 | /* (2) */ |
| 1161 | src += 2; |
| 1162 | while (is_dir_sep(*src)) |
| 1163 | src++; |
| 1164 | continue; |
| 1165 | } else if (src[1] == '.') { |
| 1166 | if (!src[2]) { |
| 1167 | /* (3) */ |
| 1168 | src += 2; |
| 1169 | goto up_one; |
| 1170 | } else if (is_dir_sep(src[2])) { |
| 1171 | /* (4) */ |
| 1172 | src += 3; |
| 1173 | while (is_dir_sep(*src)) |
| 1174 | src++; |
| 1175 | goto up_one; |
| 1176 | } |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | /* copy up to the next '/', and eat all '/' */ |
| 1181 | while ((c = *src++) != '\0' && !is_dir_sep(c)) |
| 1182 | *dst++ = c; |
| 1183 | if (is_dir_sep(c)) { |
| 1184 | *dst++ = '/'; |
| 1185 | while (is_dir_sep(c)) |
| 1186 | c = *src++; |
| 1187 | src--; |
| 1188 | } else if (!c) |
| 1189 | break; |
| 1190 | continue; |
| 1191 | |
| 1192 | up_one: |
| 1193 | /* |
| 1194 | * dst0..dst is prefix portion, and dst[-1] is '/'; |
| 1195 | * go up one level. |
| 1196 | */ |
| Johannes Sixt | f42302b | 2009-02-07 15:08:30 | [diff] [blame] | 1197 | dst--; /* go to trailing '/' */ |
| 1198 | if (dst <= dst0) |
| Johannes Sixt | f3cad0a | 2009-02-07 15:08:28 | [diff] [blame] | 1199 | return -1; |
| Johannes Sixt | f42302b | 2009-02-07 15:08:30 | [diff] [blame] | 1200 | /* Windows: dst[-1] cannot be backslash anymore */ |
| 1201 | while (dst0 < dst && dst[-1] != '/') |
| 1202 | dst--; |
| Nguyễn Thái Ngọc Duy | 645a29c | 2013-07-14 08:36:03 | [diff] [blame] | 1203 | if (prefix_len && *prefix_len > dst - dst0) |
| 1204 | *prefix_len = dst - dst0; |
| Johannes Sixt | f3cad0a | 2009-02-07 15:08:28 | [diff] [blame] | 1205 | } |
| David Reiss | ae299be | 2008-05-20 06:48:54 | [diff] [blame] | 1206 | *dst = '\0'; |
| Johannes Sixt | f3cad0a | 2009-02-07 15:08:28 | [diff] [blame] | 1207 | return 0; |
| David Reiss | ae299be | 2008-05-20 06:48:54 | [diff] [blame] | 1208 | } |
| David Reiss | 0454dd9 | 2008-05-20 06:49:26 | [diff] [blame] | 1209 | |
| Nguyễn Thái Ngọc Duy | 645a29c | 2013-07-14 08:36:03 | [diff] [blame] | 1210 | int normalize_path_copy(char *dst, const char *src) |
| 1211 | { |
| 1212 | return normalize_path_copy_len(dst, src, NULL); |
| 1213 | } |
| 1214 | |
| Calvin Wan | aba0706 | 2023-06-06 19:48:42 | [diff] [blame] | 1215 | int strbuf_normalize_path(struct strbuf *src) |
| 1216 | { |
| 1217 | struct strbuf dst = STRBUF_INIT; |
| 1218 | |
| 1219 | strbuf_grow(&dst, src->len); |
| 1220 | if (normalize_path_copy(dst.buf, src->buf) < 0) { |
| 1221 | strbuf_release(&dst); |
| 1222 | return -1; |
| 1223 | } |
| 1224 | |
| 1225 | /* |
| 1226 | * normalize_path does not tell us the new length, so we have to |
| 1227 | * compute it by looking for the new NUL it placed |
| 1228 | */ |
| 1229 | strbuf_setlen(&dst, strlen(dst.buf)); |
| 1230 | strbuf_swap(src, &dst); |
| 1231 | strbuf_release(&dst); |
| 1232 | return 0; |
| 1233 | } |
| 1234 | |
| David Reiss | 0454dd9 | 2008-05-20 06:49:26 | [diff] [blame] | 1235 | /* |
| 1236 | * path = Canonical absolute path |
| Michael Haggerty | 9e2326c | 2012-10-28 16:16:25 | [diff] [blame] | 1237 | * prefixes = string_list containing normalized, absolute paths without |
| 1238 | * trailing slashes (except for the root directory, which is denoted by "/"). |
| David Reiss | 0454dd9 | 2008-05-20 06:49:26 | [diff] [blame] | 1239 | * |
| Michael Haggerty | 9e2326c | 2012-10-28 16:16:25 | [diff] [blame] | 1240 | * Determines, for each path in prefixes, whether the "prefix" |
| David Reiss | 0454dd9 | 2008-05-20 06:49:26 | [diff] [blame] | 1241 | * is an ancestor directory of path. Returns the length of the longest |
| 1242 | * ancestor directory, excluding any trailing slashes, or -1 if no prefix |
| Michael Haggerty | 31171d9 | 2012-10-28 16:16:24 | [diff] [blame] | 1243 | * is an ancestor. (Note that this means 0 is returned if prefixes is |
| 1244 | * ["/"].) "/foo" is not considered an ancestor of "/foobar". Directories |
| David Reiss | 0454dd9 | 2008-05-20 06:49:26 | [diff] [blame] | 1245 | * are not considered to be their own ancestors. path must be in a |
| 1246 | * canonical form: empty components, or "." or ".." components are not |
| Michael Haggerty | 9e2326c | 2012-10-28 16:16:25 | [diff] [blame] | 1247 | * allowed. |
| David Reiss | 0454dd9 | 2008-05-20 06:49:26 | [diff] [blame] | 1248 | */ |
| Michael Haggerty | 31171d9 | 2012-10-28 16:16:24 | [diff] [blame] | 1249 | int longest_ancestor_length(const char *path, struct string_list *prefixes) |
| David Reiss | 0454dd9 | 2008-05-20 06:49:26 | [diff] [blame] | 1250 | { |
| Michael Haggerty | a5ccdbe | 2012-10-28 16:16:23 | [diff] [blame] | 1251 | int i, max_len = -1; |
| David Reiss | 0454dd9 | 2008-05-20 06:49:26 | [diff] [blame] | 1252 | |
| Michael Haggerty | 31171d9 | 2012-10-28 16:16:24 | [diff] [blame] | 1253 | if (!strcmp(path, "/")) |
| David Reiss | 0454dd9 | 2008-05-20 06:49:26 | [diff] [blame] | 1254 | return -1; |
| 1255 | |
| Michael Haggerty | 31171d9 | 2012-10-28 16:16:24 | [diff] [blame] | 1256 | for (i = 0; i < prefixes->nr; i++) { |
| 1257 | const char *ceil = prefixes->items[i].string; |
| Michael Haggerty | a5ccdbe | 2012-10-28 16:16:23 | [diff] [blame] | 1258 | int len = strlen(ceil); |
| 1259 | |
| Johannes Schindelin | fdcad5a | 2022-03-23 22:00:41 | [diff] [blame] | 1260 | /* |
| 1261 | * For root directories (`/`, `C:/`, `//server/share/`) |
| 1262 | * adjust the length to exclude the trailing slash. |
| 1263 | */ |
| 1264 | if (len > 0 && ceil[len - 1] == '/') |
| 1265 | len--; |
| 1266 | |
| 1267 | if (strncmp(path, ceil, len) || |
| 1268 | path[len] != '/' || !path[len + 1]) |
| Michael Haggerty | 9e2326c | 2012-10-28 16:16:25 | [diff] [blame] | 1269 | continue; /* no match */ |
| David Reiss | 0454dd9 | 2008-05-20 06:49:26 | [diff] [blame] | 1270 | |
| Michael Haggerty | 9e2326c | 2012-10-28 16:16:25 | [diff] [blame] | 1271 | if (len > max_len) |
| David Reiss | 0454dd9 | 2008-05-20 06:49:26 | [diff] [blame] | 1272 | max_len = len; |
| David Reiss | 0454dd9 | 2008-05-20 06:49:26 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | return max_len; |
| 1276 | } |
| Johannes Schindelin | 4fcc86b | 2009-02-19 19:10:49 | [diff] [blame] | 1277 | |
| 1278 | /* strip arbitrary amount of directory separators at end of path */ |
| 1279 | static inline int chomp_trailing_dir_sep(const char *path, int len) |
| 1280 | { |
| 1281 | while (len && is_dir_sep(path[len - 1])) |
| 1282 | len--; |
| 1283 | return len; |
| 1284 | } |
| 1285 | |
| 1286 | /* |
| brian m. carlson | ce17feb | 2019-08-25 23:33:39 | [diff] [blame] | 1287 | * If path ends with suffix (complete path components), returns the offset of |
| 1288 | * the last character in the path before the suffix (sans trailing directory |
| 1289 | * separators), and -1 otherwise. |
| 1290 | */ |
| 1291 | static ssize_t stripped_path_suffix_offset(const char *path, const char *suffix) |
| 1292 | { |
| 1293 | int path_len = strlen(path), suffix_len = strlen(suffix); |
| 1294 | |
| 1295 | while (suffix_len) { |
| 1296 | if (!path_len) |
| 1297 | return -1; |
| 1298 | |
| 1299 | if (is_dir_sep(path[path_len - 1])) { |
| 1300 | if (!is_dir_sep(suffix[suffix_len - 1])) |
| 1301 | return -1; |
| 1302 | path_len = chomp_trailing_dir_sep(path, path_len); |
| 1303 | suffix_len = chomp_trailing_dir_sep(suffix, suffix_len); |
| 1304 | } |
| 1305 | else if (path[--path_len] != suffix[--suffix_len]) |
| 1306 | return -1; |
| 1307 | } |
| 1308 | |
| 1309 | if (path_len && !is_dir_sep(path[path_len - 1])) |
| 1310 | return -1; |
| 1311 | return chomp_trailing_dir_sep(path, path_len); |
| 1312 | } |
| 1313 | |
| 1314 | /* |
| 1315 | * Returns true if the path ends with components, considering only complete path |
| 1316 | * components, and false otherwise. |
| 1317 | */ |
| 1318 | int ends_with_path_components(const char *path, const char *components) |
| 1319 | { |
| 1320 | return stripped_path_suffix_offset(path, components) != -1; |
| 1321 | } |
| 1322 | |
| 1323 | /* |
| Johannes Schindelin | 4fcc86b | 2009-02-19 19:10:49 | [diff] [blame] | 1324 | * If path ends with suffix (complete path components), returns the |
| 1325 | * part before suffix (sans trailing directory separators). |
| 1326 | * Otherwise returns NULL. |
| 1327 | */ |
| 1328 | char *strip_path_suffix(const char *path, const char *suffix) |
| 1329 | { |
| brian m. carlson | ce17feb | 2019-08-25 23:33:39 | [diff] [blame] | 1330 | ssize_t offset = stripped_path_suffix_offset(path, suffix); |
| Johannes Schindelin | 4fcc86b | 2009-02-19 19:10:49 | [diff] [blame] | 1331 | |
| brian m. carlson | ce17feb | 2019-08-25 23:33:39 | [diff] [blame] | 1332 | return offset == -1 ? NULL : xstrndup(path, offset); |
| Johannes Schindelin | 4fcc86b | 2009-02-19 19:10:49 | [diff] [blame] | 1333 | } |
| Shawn O. Pearce | 34b6cb8 | 2009-11-09 19:26:43 | [diff] [blame] | 1334 | |
| 1335 | int daemon_avoid_alias(const char *p) |
| 1336 | { |
| 1337 | int sl, ndot; |
| 1338 | |
| 1339 | /* |
| 1340 | * This resurrects the belts and suspenders paranoia check by HPA |
| 1341 | * done in <435560F7.4080006@zytor.com> thread, now enter_repo() |
| Junio C Hamano | 9517e6b | 2010-02-04 05:23:18 | [diff] [blame] | 1342 | * does not do getcwd() based path canonicalization. |
| Shawn O. Pearce | 34b6cb8 | 2009-11-09 19:26:43 | [diff] [blame] | 1343 | * |
| 1344 | * sl becomes true immediately after seeing '/' and continues to |
| 1345 | * be true as long as dots continue after that without intervening |
| 1346 | * non-dot character. |
| 1347 | */ |
| 1348 | if (!p || (*p != '/' && *p != '~')) |
| 1349 | return -1; |
| 1350 | sl = 1; ndot = 0; |
| 1351 | p++; |
| 1352 | |
| 1353 | while (1) { |
| 1354 | char ch = *p++; |
| 1355 | if (sl) { |
| 1356 | if (ch == '.') |
| 1357 | ndot++; |
| 1358 | else if (ch == '/') { |
| 1359 | if (ndot < 3) |
| 1360 | /* reject //, /./ and /../ */ |
| 1361 | return -1; |
| 1362 | ndot = 0; |
| 1363 | } |
| 1364 | else if (ch == 0) { |
| 1365 | if (0 < ndot && ndot < 3) |
| 1366 | /* reject /.$ and /..$ */ |
| 1367 | return -1; |
| 1368 | return 0; |
| 1369 | } |
| 1370 | else |
| 1371 | sl = ndot = 0; |
| 1372 | } |
| 1373 | else if (ch == 0) |
| 1374 | return 0; |
| 1375 | else if (ch == '/') { |
| 1376 | sl = 1; |
| 1377 | ndot = 0; |
| 1378 | } |
| 1379 | } |
| 1380 | } |
| Nguyễn Thái Ngọc Duy | 4bb43de | 2010-02-16 05:22:08 | [diff] [blame] | 1381 | |
| Johannes Schindelin | 525e7fb | 2019-09-16 18:44:31 | [diff] [blame] | 1382 | /* |
| 1383 | * On NTFS, we need to be careful to disallow certain synonyms of the `.git/` |
| 1384 | * directory: |
| 1385 | * |
| 1386 | * - For historical reasons, file names that end in spaces or periods are |
| 1387 | * automatically trimmed. Therefore, `.git . . ./` is a valid way to refer |
| 1388 | * to `.git/`. |
| 1389 | * |
| 1390 | * - For other historical reasons, file names that do not conform to the 8.3 |
| 1391 | * format (up to eight characters for the basename, three for the file |
| 1392 | * extension, certain characters not allowed such as `+`, etc) are associated |
| 1393 | * with a so-called "short name", at least on the `C:` drive by default. |
| 1394 | * Which means that `git~1/` is a valid way to refer to `.git/`. |
| 1395 | * |
| 1396 | * Note: Technically, `.git/` could receive the short name `git~2` if the |
| 1397 | * short name `git~1` were already used. In Git, however, we guarantee that |
| 1398 | * `.git` is the first item in a directory, therefore it will be associated |
| 1399 | * with the short name `git~1` (unless short names are disabled). |
| 1400 | * |
| Johannes Schindelin | 7c3745f | 2019-08-28 10:22:17 | [diff] [blame] | 1401 | * - For yet other historical reasons, NTFS supports so-called "Alternate Data |
| 1402 | * Streams", i.e. metadata associated with a given file, referred to via |
| 1403 | * `<filename>:<stream-name>:<stream-type>`. There exists a default stream |
| 1404 | * type for directories, allowing `.git/` to be accessed via |
| 1405 | * `.git::$INDEX_ALLOCATION/`. |
| 1406 | * |
| Johannes Schindelin | 525e7fb | 2019-09-16 18:44:31 | [diff] [blame] | 1407 | * When this function returns 1, it indicates that the specified file/directory |
| 1408 | * name refers to a `.git` file or directory, or to any of these synonyms, and |
| 1409 | * Git should therefore not track it. |
| 1410 | * |
| Johannes Schindelin | 7c3745f | 2019-08-28 10:22:17 | [diff] [blame] | 1411 | * For performance reasons, _all_ Alternate Data Streams of `.git/` are |
| 1412 | * forbidden, not just `::$INDEX_ALLOCATION`. |
| 1413 | * |
| Johannes Schindelin | 525e7fb | 2019-09-16 18:44:31 | [diff] [blame] | 1414 | * This function is intended to be used by `git fsck` even on platforms where |
| 1415 | * the backslash is a regular filename character, therefore it needs to handle |
| 1416 | * backlash characters in the provided `name` specially: they are interpreted |
| 1417 | * as directory separators. |
| 1418 | */ |
| Johannes Schindelin | 1d1d69b | 2014-12-16 22:31:03 | [diff] [blame] | 1419 | int is_ntfs_dotgit(const char *name) |
| 1420 | { |
| Johannes Schindelin | 3a85dc7 | 2019-09-06 19:09:35 | [diff] [blame] | 1421 | char c; |
| Johannes Schindelin | 1d1d69b | 2014-12-16 22:31:03 | [diff] [blame] | 1422 | |
| Johannes Schindelin | 3a85dc7 | 2019-09-06 19:09:35 | [diff] [blame] | 1423 | /* |
| 1424 | * Note that when we don't find `.git` or `git~1` we end up with `name` |
| 1425 | * advanced partway through the string. That's okay, though, as we |
| 1426 | * return immediately in those cases, without looking at `name` any |
| 1427 | * further. |
| 1428 | */ |
| 1429 | c = *(name++); |
| 1430 | if (c == '.') { |
| 1431 | /* .git */ |
| 1432 | if (((c = *(name++)) != 'g' && c != 'G') || |
| 1433 | ((c = *(name++)) != 'i' && c != 'I') || |
| 1434 | ((c = *(name++)) != 't' && c != 'T')) |
| Johannes Schindelin | 288a74b | 2019-09-23 06:58:11 | [diff] [blame] | 1435 | return 0; |
| Johannes Schindelin | 3a85dc7 | 2019-09-06 19:09:35 | [diff] [blame] | 1436 | } else if (c == 'g' || c == 'G') { |
| 1437 | /* git ~1 */ |
| 1438 | if (((c = *(name++)) != 'i' && c != 'I') || |
| 1439 | ((c = *(name++)) != 't' && c != 'T') || |
| 1440 | *(name++) != '~' || |
| 1441 | *(name++) != '1') |
| 1442 | return 0; |
| 1443 | } else |
| 1444 | return 0; |
| 1445 | |
| 1446 | for (;;) { |
| 1447 | c = *(name++); |
| Ævar Arnfjörð Bjarmason | 9fd512c | 2022-05-16 20:10:59 | [diff] [blame] | 1448 | if (!c || is_xplatform_dir_sep(c) || c == ':') |
| Johannes Schindelin | 3a85dc7 | 2019-09-06 19:09:35 | [diff] [blame] | 1449 | return 1; |
| 1450 | if (c != '.' && c != ' ') |
| 1451 | return 0; |
| 1452 | } |
| Johannes Schindelin | 1d1d69b | 2014-12-16 22:31:03 | [diff] [blame] | 1453 | } |
| Paul Tan | ea19289 | 2015-04-21 04:06:27 | [diff] [blame] | 1454 | |
| Johannes Schindelin | e7cb0b4 | 2018-05-11 14:03:54 | [diff] [blame] | 1455 | static int is_ntfs_dot_generic(const char *name, |
| 1456 | const char *dotgit_name, |
| 1457 | size_t len, |
| 1458 | const char *dotgit_ntfs_shortname_prefix) |
| 1459 | { |
| 1460 | int saw_tilde; |
| 1461 | size_t i; |
| 1462 | |
| 1463 | if ((name[0] == '.' && !strncasecmp(name + 1, dotgit_name, len))) { |
| 1464 | i = len + 1; |
| 1465 | only_spaces_and_periods: |
| 1466 | for (;;) { |
| 1467 | char c = name[i++]; |
| Johannes Schindelin | 91bd465 | 2019-08-28 10:22:17 | [diff] [blame] | 1468 | if (!c || c == ':') |
| Johannes Schindelin | e7cb0b4 | 2018-05-11 14:03:54 | [diff] [blame] | 1469 | return 1; |
| 1470 | if (c != ' ' && c != '.') |
| 1471 | return 0; |
| 1472 | } |
| 1473 | } |
| 1474 | |
| 1475 | /* |
| 1476 | * Is it a regular NTFS short name, i.e. shortened to 6 characters, |
| 1477 | * followed by ~1, ... ~4? |
| 1478 | */ |
| 1479 | if (!strncasecmp(name, dotgit_name, 6) && name[6] == '~' && |
| 1480 | name[7] >= '1' && name[7] <= '4') { |
| 1481 | i = 8; |
| 1482 | goto only_spaces_and_periods; |
| 1483 | } |
| 1484 | |
| 1485 | /* |
| 1486 | * Is it a fall-back NTFS short name (for details, see |
| 1487 | * https://en.wikipedia.org/wiki/8.3_filename? |
| 1488 | */ |
| 1489 | for (i = 0, saw_tilde = 0; i < 8; i++) |
| 1490 | if (name[i] == '\0') |
| 1491 | return 0; |
| 1492 | else if (saw_tilde) { |
| 1493 | if (name[i] < '0' || name[i] > '9') |
| 1494 | return 0; |
| 1495 | } else if (name[i] == '~') { |
| 1496 | if (name[++i] < '1' || name[i] > '9') |
| 1497 | return 0; |
| 1498 | saw_tilde = 1; |
| 1499 | } else if (i >= 6) |
| 1500 | return 0; |
| Torsten Bögershausen | 3063477 | 2018-10-25 16:13:08 | [diff] [blame] | 1501 | else if (name[i] & 0x80) { |
| Johannes Schindelin | e7cb0b4 | 2018-05-11 14:03:54 | [diff] [blame] | 1502 | /* |
| 1503 | * We know our needles contain only ASCII, so we clamp |
| 1504 | * here to make the results of tolower() sane. |
| 1505 | */ |
| 1506 | return 0; |
| 1507 | } else if (tolower(name[i]) != dotgit_ntfs_shortname_prefix[i]) |
| 1508 | return 0; |
| 1509 | |
| 1510 | goto only_spaces_and_periods; |
| 1511 | } |
| 1512 | |
| 1513 | /* |
| 1514 | * Inline helper to make sure compiler resolves strlen() on literals at |
| 1515 | * compile time. |
| 1516 | */ |
| 1517 | static inline int is_ntfs_dot_str(const char *name, const char *dotgit_name, |
| 1518 | const char *dotgit_ntfs_shortname_prefix) |
| 1519 | { |
| 1520 | return is_ntfs_dot_generic(name, dotgit_name, strlen(dotgit_name), |
| 1521 | dotgit_ntfs_shortname_prefix); |
| 1522 | } |
| 1523 | |
| 1524 | int is_ntfs_dotgitmodules(const char *name) |
| 1525 | { |
| 1526 | return is_ntfs_dot_str(name, "gitmodules", "gi7eba"); |
| 1527 | } |
| 1528 | |
| 1529 | int is_ntfs_dotgitignore(const char *name) |
| 1530 | { |
| 1531 | return is_ntfs_dot_str(name, "gitignore", "gi250a"); |
| 1532 | } |
| 1533 | |
| 1534 | int is_ntfs_dotgitattributes(const char *name) |
| 1535 | { |
| 1536 | return is_ntfs_dot_str(name, "gitattributes", "gi7d29"); |
| 1537 | } |
| 1538 | |
| Jeff King | 801ed01 | 2021-05-03 20:43:22 | [diff] [blame] | 1539 | int is_ntfs_dotmailmap(const char *name) |
| 1540 | { |
| 1541 | return is_ntfs_dot_str(name, "mailmap", "maba30"); |
| 1542 | } |
| 1543 | |
| Jeff King | 2491f77 | 2017-07-28 19:25:45 | [diff] [blame] | 1544 | int looks_like_command_line_option(const char *str) |
| 1545 | { |
| 1546 | return str && str[0] == '-'; |
| 1547 | } |
| 1548 | |
| Lénaïc Huard | cb7db5b | 2021-09-04 20:54:58 | [diff] [blame] | 1549 | char *xdg_config_home_for(const char *subdir, const char *filename) |
| Paul Tan | ea19289 | 2015-04-21 04:06:27 | [diff] [blame] | 1550 | { |
| 1551 | const char *home, *config_home; |
| 1552 | |
| Lénaïc Huard | cb7db5b | 2021-09-04 20:54:58 | [diff] [blame] | 1553 | assert(subdir); |
| Paul Tan | ea19289 | 2015-04-21 04:06:27 | [diff] [blame] | 1554 | assert(filename); |
| 1555 | config_home = getenv("XDG_CONFIG_HOME"); |
| 1556 | if (config_home && *config_home) |
| Lénaïc Huard | cb7db5b | 2021-09-04 20:54:58 | [diff] [blame] | 1557 | return mkpathdup("%s/%s/%s", config_home, subdir, filename); |
| Paul Tan | ea19289 | 2015-04-21 04:06:27 | [diff] [blame] | 1558 | |
| 1559 | home = getenv("HOME"); |
| 1560 | if (home) |
| Lénaïc Huard | cb7db5b | 2021-09-04 20:54:58 | [diff] [blame] | 1561 | return mkpathdup("%s/.config/%s/%s", home, subdir, filename); |
| 1562 | |
| Paul Tan | ea19289 | 2015-04-21 04:06:27 | [diff] [blame] | 1563 | return NULL; |
| 1564 | } |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 1565 | |
| Lénaïc Huard | cb7db5b | 2021-09-04 20:54:58 | [diff] [blame] | 1566 | char *xdg_config_home(const char *filename) |
| 1567 | { |
| 1568 | return xdg_config_home_for("git", filename); |
| 1569 | } |
| 1570 | |
| Devin Lehmacher | e7f136b | 2017-03-13 20:43:54 | [diff] [blame] | 1571 | char *xdg_cache_home(const char *filename) |
| 1572 | { |
| 1573 | const char *home, *cache_home; |
| 1574 | |
| 1575 | assert(filename); |
| 1576 | cache_home = getenv("XDG_CACHE_HOME"); |
| 1577 | if (cache_home && *cache_home) |
| 1578 | return mkpathdup("%s/git/%s", cache_home, filename); |
| 1579 | |
| 1580 | home = getenv("HOME"); |
| 1581 | if (home) |
| 1582 | return mkpathdup("%s/.cache/git/%s", home, filename); |
| 1583 | return NULL; |
| 1584 | } |
| 1585 | |
| Stefan Beller | 102de88 | 2018-05-17 22:51:51 | [diff] [blame] | 1586 | REPO_GIT_PATH_FUNC(squash_msg, "SQUASH_MSG") |
| 1587 | REPO_GIT_PATH_FUNC(merge_msg, "MERGE_MSG") |
| 1588 | REPO_GIT_PATH_FUNC(merge_rr, "MERGE_RR") |
| 1589 | REPO_GIT_PATH_FUNC(merge_mode, "MERGE_MODE") |
| 1590 | REPO_GIT_PATH_FUNC(merge_head, "MERGE_HEAD") |
| Denton Liu | a03b555 | 2020-04-07 14:28:07 | [diff] [blame] | 1591 | REPO_GIT_PATH_FUNC(merge_autostash, "MERGE_AUTOSTASH") |
| Elijah Newren | 5291828 | 2021-03-20 00:03:52 | [diff] [blame] | 1592 | REPO_GIT_PATH_FUNC(auto_merge, "AUTO_MERGE") |
| Stefan Beller | 102de88 | 2018-05-17 22:51:51 | [diff] [blame] | 1593 | REPO_GIT_PATH_FUNC(fetch_head, "FETCH_HEAD") |
| 1594 | REPO_GIT_PATH_FUNC(shallow, "shallow") |