@@ -9,6 +9,8 @@ void free_worktrees(struct worktree **worktrees)
9
9
10
10
for (i = 0 ; worktrees [i ]; i ++ ) {
11
11
free (worktrees [i ]-> path );
12
+ free (worktrees [i ]-> git_dir );
13
+ free (worktrees [i ]-> head_ref );
12
14
free (worktrees [i ]);
13
15
}
14
16
free (worktrees );
@@ -49,6 +51,21 @@ static int parse_ref(char *path_to_ref, struct strbuf *ref, int *is_detached)
49
51
return 0 ;
50
52
}
51
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
+
52
69
/**
53
70
* get the main worktree
54
71
*/
@@ -59,19 +76,29 @@ static struct worktree *get_main_worktree(void)
59
76
struct strbuf worktree_path = STRBUF_INIT ;
60
77
struct strbuf gitdir = STRBUF_INIT ;
61
78
struct strbuf head_ref = STRBUF_INIT ;
79
+ int is_bare = 0 ;
80
+ int is_detached = 0 ;
62
81
63
82
strbuf_addf (& gitdir , "%s" , absolute_path (get_git_common_dir ()));
64
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 )
66
86
strbuf_strip_suffix (& worktree_path , "/." );
67
87
68
88
strbuf_addf (& path , "%s/HEAD" , get_git_common_dir ());
69
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 :
75
102
strbuf_release (& path );
76
103
strbuf_release (& gitdir );
77
104
strbuf_release (& worktree_path );
@@ -86,6 +113,7 @@ static struct worktree *get_linked_worktree(const char *id)
86
113
struct strbuf worktree_path = STRBUF_INIT ;
87
114
struct strbuf gitdir = STRBUF_INIT ;
88
115
struct strbuf head_ref = STRBUF_INIT ;
116
+ int is_detached = 0 ;
89
117
90
118
if (!id )
91
119
die ("Missing linked worktree name" );
@@ -107,11 +135,16 @@ static struct worktree *get_linked_worktree(const char *id)
107
135
strbuf_reset (& path );
108
136
strbuf_addf (& path , "%s/worktrees/%s/HEAD" , get_git_common_dir (), id );
109
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 );
115
148
116
149
done :
117
150
strbuf_release (& path );
0 commit comments