1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
#include "cache.h"
#include "refs.h"
#include "strbuf.h"
#include "worktree.h"
/*
* read 'path_to_ref' into 'ref'. Also if is_detached is not NULL,
* set is_detached to 1 (0) if the ref is detatched (is not detached).
*
* $GIT_COMMON_DIR/$symref (e.g. HEAD) is practically outside $GIT_DIR so
* for linked worktrees, `resolve_ref_unsafe()` won't work (it uses
* git_path). Parse the ref ourselves.
*
* return -1 if the ref is not a proper ref, 0 otherwise (success)
*/
static int parse_ref(char *path_to_ref, struct strbuf *ref, int *is_detached)
{
if (is_detached)
*is_detached = 0;
if (!strbuf_readlink(ref, path_to_ref, 0)) {
/* HEAD is symbolic link */
if (!starts_with(ref->buf, "refs/") ||
check_refname_format(ref->buf, 0))
return -1;
} else if (strbuf_read_file(ref, path_to_ref, 0) >= 0) {
/* textual symref or detached */
if (!starts_with(ref->buf, "ref:")) {
if (is_detached)
*is_detached = 1;
} else {
strbuf_remove(ref, 0, strlen("ref:"));
strbuf_trim(ref);
if (check_refname_format(ref->buf, 0))
return -1;
}
} else
return -1;
return 0;
}
static char *find_main_symref(const char *symref, const char *branch)
{
struct strbuf sb = STRBUF_INIT;
struct strbuf path = STRBUF_INIT;
struct strbuf gitdir = STRBUF_INIT;
char *existing = NULL;
strbuf_addf(&path, "%s/%s", get_git_common_dir(), symref);
if (parse_ref(path.buf, &sb, NULL) < 0)
goto done;
if (strcmp(sb.buf, branch))
goto done;
strbuf_addstr(&gitdir, get_git_common_dir());
strbuf_strip_suffix(&gitdir, ".git");
existing = strbuf_detach(&gitdir, NULL);
done:
strbuf_release(&path);
strbuf_release(&sb);
strbuf_release(&gitdir);
return existing;
}
static char *find_linked_symref(const char *symref, const char *branch,
const char *id)
{
struct strbuf sb = STRBUF_INIT;
struct strbuf path = STRBUF_INIT;
struct strbuf gitdir = STRBUF_INIT;
char *existing = NULL;
if (!id)
die("Missing linked worktree name");
strbuf_addf(&path, "%s/worktrees/%s/%s", get_git_common_dir(), id, symref);
if (parse_ref(path.buf, &sb, NULL) < 0)
goto done;
if (strcmp(sb.buf, branch))
goto done;
strbuf_reset(&path);
strbuf_addf(&path, "%s/worktrees/%s/gitdir", get_git_common_dir(), id);
if (strbuf_read_file(&gitdir, path.buf, 0) <= 0)
goto done;
strbuf_rtrim(&gitdir);
strbuf_strip_suffix(&gitdir, ".git");
existing = strbuf_detach(&gitdir, NULL);
done:
strbuf_release(&path);
strbuf_release(&sb);
strbuf_release(&gitdir);
return existing;
}
char *find_shared_symref(const char *symref, const char *target)
{
struct strbuf path = STRBUF_INIT;
DIR *dir;
struct dirent *d;
char *existing;
if ((existing = find_main_symref(symref, target)))
return existing;
strbuf_addf(&path, "%s/worktrees", get_git_common_dir());
dir = opendir(path.buf);
strbuf_release(&path);
if (!dir)
return NULL;
while ((d = readdir(dir)) != NULL) {
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
continue;
existing = find_linked_symref(symref, target, d->d_name);
if (existing)
goto done;
}
done:
closedir(dir);
return existing;
}
|