Skip to content

Commit 750e8a6

Browse files
pcloudsgitster
authored andcommitted
worktree.c: mark current worktree
Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d3b9ac0 commit 750e8a6

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

worktree.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "refs.h"
33
#include "strbuf.h"
44
#include "worktree.h"
5+
#include "dir.h"
56

67
void free_worktrees(struct worktree **worktrees)
78
{
@@ -94,6 +95,7 @@ static struct worktree *get_main_worktree(void)
9495
worktree->is_bare = is_bare;
9596
worktree->head_ref = NULL;
9697
worktree->is_detached = is_detached;
98+
worktree->is_current = 0;
9799
add_head_info(&head_ref, worktree);
98100

99101
done:
@@ -138,6 +140,7 @@ static struct worktree *get_linked_worktree(const char *id)
138140
worktree->is_bare = 0;
139141
worktree->head_ref = NULL;
140142
worktree->is_detached = is_detached;
143+
worktree->is_current = 0;
141144
add_head_info(&head_ref, worktree);
142145

143146
done:
@@ -147,6 +150,25 @@ static struct worktree *get_linked_worktree(const char *id)
147150
return worktree;
148151
}
149152

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+
150172
struct worktree **get_worktrees(void)
151173
{
152174
struct worktree **list = NULL;
@@ -178,6 +200,8 @@ struct worktree **get_worktrees(void)
178200
}
179201
ALLOC_GROW(list, counter + 1, alloc);
180202
list[counter] = NULL;
203+
204+
mark_current_worktree(list);
181205
return list;
182206
}
183207

worktree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ struct worktree {
88
unsigned char head_sha1[20];
99
int is_detached;
1010
int is_bare;
11+
int is_current;
1112
};
1213

1314
/* Functions for acting on the information about worktrees. */

0 commit comments

Comments
 (0)