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