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