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