worktree: add details to the worktree struct
In addition to the absolute path in the worktree struct, add the location of the git dir, the head ref (if not detached), the head revision sha1, whether or not head is detached, and whether or not the worktree is a bare repo. Signed-off-by: Michael Rappazzo <rappazzo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Michael Rappazzo committed
Oct 8, 2015 at 13:01 UTC
92718b7438799f4654304e2fa3b5b7e5a9fda882
2 files changed
+48
-11
worktree.c
+44
-11
@@ -9,6 +9,8 @@ void free_worktrees(struct worktree **worktrees)
9
10
for (i = 0; worktrees[i]; i++) {
11
free(worktrees[i]->path);
12
+ free(worktrees[i]->git_dir);
13
+ free(worktrees[i]->head_ref);
14
free(worktrees[i]);
15
}
16
free (worktrees);
@@ -49,6 +51,21 @@ static int parse_ref(char *path_to_ref, struct strbuf *ref, int *is_detached)
51
return 0;
52
}
53
54
+/**
55
+ * Add the head_sha1 and head_ref (if not detached) to the given worktree
56
+ */
57
+static void add_head_info(struct strbuf *head_ref, struct worktree *worktree)
58
+{
59
+ if (head_ref->len) {
60
+ if (worktree->is_detached) {
61
+ get_sha1_hex(head_ref->buf, worktree->head_sha1);
62
+ } else {
63
+ resolve_ref_unsafe(head_ref->buf, 0, worktree->head_sha1, NULL);
64
+ worktree->head_ref = strbuf_detach(head_ref, NULL);
65
+ }
66
+ }
67
+}
68
+
69
/**
70
* get the main worktree
71
*/
@@ -59,19 +76,29 @@ static struct worktree *get_main_worktree(void)
76
struct strbuf worktree_path = STRBUF_INIT;
77
struct strbuf gitdir = STRBUF_INIT;
78
struct strbuf head_ref = STRBUF_INIT;
79
+ int is_bare = 0;
80
+ int is_detached = 0;
81
82
strbuf_addf(&gitdir, "%s", absolute_path(get_git_common_dir()));
83
strbuf_addbuf(&worktree_path, &gitdir);
65
- if (!strbuf_strip_suffix(&worktree_path, "/.git"))
84
+ is_bare = !strbuf_strip_suffix(&worktree_path, "/.git");
85
+ if (is_bare)
86
strbuf_strip_suffix(&worktree_path, "/.");
87
88
strbuf_addf(&path, "%s/HEAD", get_git_common_dir());
89
70
- if (parse_ref(path.buf, &head_ref, NULL) >= 0) {
71
- worktree = xmalloc(sizeof(struct worktree));
72
- worktree->path = strbuf_detach(&worktree_path, NULL);
73
- worktree->git_dir = strbuf_detach(&gitdir, NULL);
74
- }
90
+ if (parse_ref(path.buf, &head_ref, &is_detached) < 0)
91
+ goto done;
92
+
93
+ worktree = xmalloc(sizeof(struct worktree));
94
+ worktree->path = strbuf_detach(&worktree_path, NULL);
95
+ worktree->git_dir = strbuf_detach(&gitdir, NULL);
96
+ worktree->is_bare = is_bare;
97
+ worktree->head_ref = NULL;
98
+ worktree->is_detached = is_detached;
99
+ add_head_info(&head_ref, worktree);
100
+
101
+done:
102
strbuf_release(&path);
103
strbuf_release(&gitdir);
104
strbuf_release(&worktree_path);
@@ -86,6 +113,7 @@ static struct worktree *get_linked_worktree(const char *id)
113
struct strbuf worktree_path = STRBUF_INIT;
114
struct strbuf gitdir = STRBUF_INIT;
115
struct strbuf head_ref = STRBUF_INIT;
116
+ int is_detached = 0;
117
118
if (!id)
119
die("Missing linked worktree name");
@@ -107,11 +135,16 @@ static struct worktree *get_linked_worktree(const char *id)
135
strbuf_reset(&path);
136
strbuf_addf(&path, "%s/worktrees/%s/HEAD", get_git_common_dir(), id);
137
110
- if (parse_ref(path.buf, &head_ref, NULL) >= 0) {
111
- worktree = xmalloc(sizeof(struct worktree));
112
- worktree->path = strbuf_detach(&worktree_path, NULL);
113
- worktree->git_dir = strbuf_detach(&gitdir, NULL);
114
- }
138
+ if (parse_ref(path.buf, &head_ref, &is_detached) < 0)
139
+ goto done;
140
+
141
+ worktree = xmalloc(sizeof(struct worktree));
142
+ worktree->path = strbuf_detach(&worktree_path, NULL);
143
+ worktree->git_dir = strbuf_detach(&gitdir, NULL);
144
+ worktree->is_bare = 0;
145
+ worktree->head_ref = NULL;
146
+ worktree->is_detached = is_detached;
147
+ add_head_info(&head_ref, worktree);
148
149
done:
150
strbuf_release(&path);
worktree.h
+4
@@ -4,6 +4,10 @@
4
struct worktree {
5
char *path;
6
char *git_dir;
7
+ char *head_ref;
8
+ unsigned char head_sha1[20];
9
+ int is_detached;
10
+ int is_bare;
11
};
12
13
/* Functions for acting on the information about worktrees. */