@@ -9,6 +9,8 @@ void free_worktrees(struct worktree **worktrees)
99
1010 for (i = 0 ; worktrees [i ]; i ++ ) {
1111 free (worktrees [i ]-> path );
12+ free (worktrees [i ]-> git_dir );
13+ free (worktrees [i ]-> head_ref );
1214 free (worktrees [i ]);
1315 }
1416 free (worktrees );
@@ -49,6 +51,21 @@ static int parse_ref(char *path_to_ref, struct strbuf *ref, int *is_detached)
4951 return 0 ;
5052}
5153
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+
5269/**
5370 * get the main worktree
5471 */
@@ -59,19 +76,29 @@ static struct worktree *get_main_worktree(void)
5976 struct strbuf worktree_path = STRBUF_INIT ;
6077 struct strbuf gitdir = STRBUF_INIT ;
6178 struct strbuf head_ref = STRBUF_INIT ;
79+ int is_bare = 0 ;
80+ int is_detached = 0 ;
6281
6382 strbuf_addf (& gitdir , "%s" , absolute_path (get_git_common_dir ()));
6483 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 )
6686 strbuf_strip_suffix (& worktree_path , "/." );
6787
6888 strbuf_addf (& path , "%s/HEAD" , get_git_common_dir ());
6989
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 :
75102 strbuf_release (& path );
76103 strbuf_release (& gitdir );
77104 strbuf_release (& worktree_path );
@@ -86,6 +113,7 @@ static struct worktree *get_linked_worktree(const char *id)
86113 struct strbuf worktree_path = STRBUF_INIT ;
87114 struct strbuf gitdir = STRBUF_INIT ;
88115 struct strbuf head_ref = STRBUF_INIT ;
116+ int is_detached = 0 ;
89117
90118 if (!id )
91119 die ("Missing linked worktree name" );
@@ -107,11 +135,16 @@ static struct worktree *get_linked_worktree(const char *id)
107135 strbuf_reset (& path );
108136 strbuf_addf (& path , "%s/worktrees/%s/HEAD" , get_git_common_dir (), id );
109137
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 );
115148
116149done :
117150 strbuf_release (& path );
0 commit comments