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