]> git.proxmox.com Git - libgit2.git/blob - tests/libgit2/worktree/open.c
New upstream version 1.5.0+ds
[libgit2.git] / tests / libgit2 / worktree / open.c
1 #include "clar_libgit2.h"
2 #include "repository.h"
3 #include "worktree.h"
4 #include "worktree_helpers.h"
5
6 #define COMMON_REPO "testrepo"
7 #define WORKTREE_REPO "testrepo-worktree"
8
9 static worktree_fixture fixture =
10 WORKTREE_FIXTURE_INIT(COMMON_REPO, WORKTREE_REPO);
11
12 static void assert_worktree_valid(git_repository *wt, const char *parentdir, const char *wtdir)
13 {
14 cl_assert(wt->is_worktree);
15
16 cl_assert_equal_s(wt->workdir, cl_git_sandbox_path(1, wtdir, NULL));
17 cl_assert_equal_s(wt->gitlink, cl_git_sandbox_path(0, wtdir, ".git", NULL));
18 cl_assert_equal_s(wt->gitdir, cl_git_sandbox_path(1, parentdir, ".git", "worktrees", wtdir, NULL));
19 }
20
21 void test_worktree_open__initialize(void)
22 {
23 setup_fixture_worktree(&fixture);
24 }
25
26 void test_worktree_open__cleanup(void)
27 {
28 cleanup_fixture_worktree(&fixture);
29 }
30
31 void test_worktree_open__repository(void)
32 {
33 assert_worktree_valid(fixture.worktree, COMMON_REPO, WORKTREE_REPO);
34 }
35
36 void test_worktree_open__repository_through_workdir(void)
37 {
38 git_repository *wt;
39
40 cl_git_pass(git_repository_open(&wt, WORKTREE_REPO));
41 assert_worktree_valid(wt, COMMON_REPO, WORKTREE_REPO);
42
43 git_repository_free(wt);
44 }
45
46 void test_worktree_open__repository_through_gitlink(void)
47 {
48 git_repository *wt;
49
50 cl_git_pass(git_repository_open(&wt, WORKTREE_REPO "/.git"));
51 assert_worktree_valid(wt, COMMON_REPO, WORKTREE_REPO);
52
53 git_repository_free(wt);
54 }
55
56 void test_worktree_open__repository_through_gitdir(void)
57 {
58 git_str gitdir_path = GIT_STR_INIT;
59 git_repository *wt;
60
61 cl_git_pass(git_str_joinpath(&gitdir_path, COMMON_REPO, ".git"));
62 cl_git_pass(git_str_joinpath(&gitdir_path, gitdir_path.ptr, "worktrees"));
63 cl_git_pass(git_str_joinpath(&gitdir_path, gitdir_path.ptr, "testrepo-worktree"));
64
65 cl_git_pass(git_repository_open(&wt, gitdir_path.ptr));
66 assert_worktree_valid(wt, COMMON_REPO, WORKTREE_REPO);
67
68 git_str_dispose(&gitdir_path);
69 git_repository_free(wt);
70 }
71
72 void test_worktree_open__open_discovered_worktree(void)
73 {
74 git_buf path = GIT_BUF_INIT;
75 git_repository *repo;
76
77 cl_git_pass(git_repository_discover(&path,
78 git_repository_workdir(fixture.worktree), false, NULL));
79 cl_git_pass(git_repository_open(&repo, path.ptr));
80 cl_assert_equal_s(git_repository_workdir(fixture.worktree),
81 git_repository_workdir(repo));
82
83 git_buf_dispose(&path);
84 git_repository_free(repo);
85 }
86
87 void test_worktree_open__repository_with_nonexistent_parent(void)
88 {
89 git_repository *repo;
90
91 cleanup_fixture_worktree(&fixture);
92
93 cl_fixture_sandbox(WORKTREE_REPO);
94 cl_git_pass(p_chdir(WORKTREE_REPO));
95 cl_git_pass(cl_rename(".gitted", ".git"));
96 cl_git_pass(p_chdir(".."));
97
98 cl_git_fail(git_repository_open(&repo, WORKTREE_REPO));
99
100 cl_fixture_cleanup(WORKTREE_REPO);
101 }
102
103 void test_worktree_open__open_from_repository(void)
104 {
105 git_worktree *opened, *lookedup;
106
107 cl_git_pass(git_worktree_open_from_repository(&opened, fixture.worktree));
108 cl_git_pass(git_worktree_lookup(&lookedup, fixture.repo, WORKTREE_REPO));
109
110 cl_assert_equal_s(opened->name, lookedup->name);
111 cl_assert_equal_s(opened->gitdir_path, lookedup->gitdir_path);
112 cl_assert_equal_s(opened->gitlink_path, lookedup->gitlink_path);
113 cl_assert_equal_s(opened->parent_path, lookedup->parent_path);
114 cl_assert_equal_s(opened->commondir_path, lookedup->commondir_path);
115 cl_assert_equal_i(opened->locked, lookedup->locked);
116
117 git_worktree_free(opened);
118 git_worktree_free(lookedup);
119 }
120
121 void test_worktree_open__open_from_nonworktree_fails(void)
122 {
123 git_worktree *wt;
124
125 cl_git_fail(git_worktree_open_from_repository(&wt, fixture.repo));
126 }