worktree.c: mark current worktree

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Apr 22, 2016 at 20:01 UTC 750e8a60d69274acd820f812704c75927d82728e
2 files changed +25
worktree.c
+24
@@ -2,6 +2,7 @@
2 #include "refs.h"
3 #include "strbuf.h"
4 #include "worktree.h"
5 +#include "dir.h"
6
7 void free_worktrees(struct worktree **worktrees)
8 {
@@ -94,6 +95,7 @@ static struct worktree *get_main_worktree(void)
95 worktree->is_bare = is_bare;
96 worktree->head_ref = NULL;
97 worktree->is_detached = is_detached;
98 + worktree->is_current = 0;
99 add_head_info(&head_ref, worktree);
100
101 done:
@@ -138,6 +140,7 @@ static struct worktree *get_linked_worktree(const char *id)
140 worktree->is_bare = 0;
141 worktree->head_ref = NULL;
142 worktree->is_detached = is_detached;
143 + worktree->is_current = 0;
144 add_head_info(&head_ref, worktree);
145
146 done:
@@ -147,6 +150,25 @@ done:
150 return worktree;
151 }
152
153 +static void mark_current_worktree(struct worktree **worktrees)
154 +{
155 + struct strbuf git_dir = STRBUF_INIT;
156 + struct strbuf path = STRBUF_INIT;
157 + int i;
158 +
159 + strbuf_addstr(&git_dir, absolute_path(get_git_dir()));
160 + for (i = 0; worktrees[i]; i++) {
161 + struct worktree *wt = worktrees[i];
162 + strbuf_addstr(&path, absolute_path(get_worktree_git_dir(wt)));
163 + wt->is_current = !fspathcmp(git_dir.buf, path.buf);
164 + strbuf_reset(&path);
165 + if (wt->is_current)
166 + break;
167 + }
168 + strbuf_release(&git_dir);
169 + strbuf_release(&path);
170 +}
171 +
172 struct worktree **get_worktrees(void)
173 {
174 struct worktree **list = NULL;
@@ -178,6 +200,8 @@ struct worktree **get_worktrees(void)
200 }
201 ALLOC_GROW(list, counter + 1, alloc);
202 list[counter] = NULL;
203 +
204 + mark_current_worktree(list);
205 return list;
206 }
207
worktree.h
+1
@@ -8,6 +8,7 @@ struct worktree {
8 unsigned char head_sha1[20];
9 int is_detached;
10 int is_bare;
11 + int is_current;
12 };
13
14 /* Functions for acting on the information about worktrees. */