Skip to content

Commit 79ab3ef

Browse files
committed
repository: introduce is_worktree variable
1 parent c5f3da9 commit 79ab3ef

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

include/git2/repository.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,14 @@ GIT_EXTERN(int) git_repository_set_workdir(
467467
*/
468468
GIT_EXTERN(int) git_repository_is_bare(git_repository *repo);
469469

470+
/**
471+
* Check if a repository is a linked work tree
472+
*
473+
* @param repo Repo to test
474+
* @return 1 if the repository is a linked work tree, 0 otherwise.
475+
*/
476+
GIT_EXTERN(int) git_repository_is_worktree(git_repository *repo);
477+
470478
/**
471479
* Get the configuration file for this repository.
472480
*

src/repository.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ int git_repository_new(git_repository **out)
254254
GITERR_CHECK_ALLOC(repo);
255255

256256
repo->is_bare = 1;
257+
repo->is_worktree = 0;
257258

258259
return 0;
259260
}
@@ -558,6 +559,7 @@ int git_repository_open_bare(
558559

559560
/* of course we're bare! */
560561
repo->is_bare = 1;
562+
repo->is_worktree = 0;
561563
repo->workdir = NULL;
562564

563565
*repo_ptr = repo;
@@ -772,6 +774,9 @@ int git_repository_open_ext(
772774
GITERR_CHECK_ALLOC(repo->commondir);
773775
}
774776

777+
if (repo->path_gitlink && repo->commondir && strcmp(repo->path_gitlink, repo->commondir))
778+
repo->is_worktree = 1;
779+
775780
/*
776781
* We'd like to have the config, but git doesn't particularly
777782
* care if it's not there, so we need to deal with that.
@@ -2186,6 +2191,12 @@ int git_repository_is_bare(git_repository *repo)
21862191
return repo->is_bare;
21872192
}
21882193

2194+
int git_repository_is_worktree(git_repository *repo)
2195+
{
2196+
assert(repo);
2197+
return repo->is_worktree;
2198+
}
2199+
21892200
int git_repository_set_bare(git_repository *repo)
21902201
{
21912202
int error;

src/repository.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ struct git_repository {
138138
git_array_t(git_buf) reserved_names;
139139

140140
unsigned is_bare:1;
141+
unsigned is_worktree:1;
141142

142143
unsigned int lru_counter;
143144

tests/worktree/open.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "clar_libgit2.h"
2+
#include "repository.h"
23
#include "worktree_helpers.h"
34

45
#define WORKTREE_PARENT "submodules-worktree-parent"
@@ -13,6 +14,9 @@ void test_worktree_open__repository(void)
1314
cl_assert(git_repository_path(fixture.worktree) != NULL);
1415
cl_assert(git_repository_workdir(fixture.worktree) != NULL);
1516

17+
cl_assert(!fixture.repo->is_worktree);
18+
cl_assert(fixture.worktree->is_worktree);
19+
1620
cleanup_fixture_worktree(&fixture);
1721
}
1822

@@ -39,6 +43,9 @@ void test_worktree_open__submodule_worktree_parent(void)
3943
cl_assert(git_repository_path(fixture.worktree) != NULL);
4044
cl_assert(git_repository_workdir(fixture.worktree) != NULL);
4145

46+
cl_assert(!fixture.repo->is_worktree);
47+
cl_assert(fixture.worktree->is_worktree);
48+
4249
cleanup_fixture_worktree(&fixture);
4350
}
4451

@@ -55,6 +62,10 @@ void test_worktree_open__submodule_worktree_child(void)
5562
"submodules/testrepo/.git"));
5663
setup_fixture_worktree(&child_fixture);
5764

65+
cl_assert(!parent_fixture.repo->is_worktree);
66+
cl_assert(parent_fixture.worktree->is_worktree);
67+
cl_assert(child_fixture.worktree->is_worktree);
68+
5869
cleanup_fixture_worktree(&child_fixture);
5970
cleanup_fixture_worktree(&parent_fixture);
6071
}

0 commit comments

Comments
 (0)