1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2015 - TortoiseGit
4 // Copyright (C) 2003-2008 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "GitAdminDir.h"
23 #include "StringUtils.h"
25 TEST(CGitAdminDir
, IsBareRepo
)
29 EXPECT_FALSE(GitAdminDir::IsBareRepo(tmpDir
.GetTempDir()));
31 CAutoRepository repo
= nullptr;
32 ASSERT_TRUE(git_repository_init(repo
.GetPointer(), CUnicodeUtils::GetUTF8(tmpDir
.GetTempDir()), true) == 0);
34 EXPECT_TRUE(GitAdminDir::IsBareRepo(tmpDir
.GetTempDir()));
37 TEST(CGitAdminDir
, IsBareRepo_normalRepo
)
41 EXPECT_FALSE(GitAdminDir::IsBareRepo(tmpDir
.GetTempDir()));
43 CAutoRepository repo
= nullptr;
44 ASSERT_TRUE(git_repository_init(repo
.GetPointer(), CUnicodeUtils::GetUTF8(tmpDir
.GetTempDir()), false) == 0);
46 EXPECT_FALSE(GitAdminDir::IsBareRepo(tmpDir
.GetTempDir()));
48 EXPECT_FALSE(GitAdminDir::IsBareRepo(tmpDir
.GetTempDir() + _T("\\.git")));
50 EXPECT_FALSE(GitAdminDir::IsBareRepo(tmpDir
.GetTempDir() + _T("\\.git\\objects")));
53 TEST(CGitAdminDir
, IsBareRepo_ReferencedRepo
)
57 CString gitFile
= tmpDir
.GetTempDir() + L
"\\.git";
58 EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR
)gitFile
, L
"gitdir: dontcare"));
60 EXPECT_FALSE(GitAdminDir::IsBareRepo(tmpDir
.GetTempDir()));
63 TEST(CGitAdminDir
, IsAdminDirPath
)
67 EXPECT_FALSE(GitAdminDir::IsAdminDirPath(tmpDir
.GetTempDir()));
69 CAutoRepository repo
= nullptr;
70 ASSERT_TRUE(git_repository_init(repo
.GetPointer(), CUnicodeUtils::GetUTF8(tmpDir
.GetTempDir()), false) == 0);
72 EXPECT_FALSE(GitAdminDir::IsAdminDirPath(tmpDir
.GetTempDir()));
74 EXPECT_TRUE(GitAdminDir::IsAdminDirPath(tmpDir
.GetTempDir() + L
"\\.git"));
76 EXPECT_TRUE(GitAdminDir::IsAdminDirPath(tmpDir
.GetTempDir() + L
"\\.git\\config"));
78 EXPECT_TRUE(GitAdminDir::IsAdminDirPath(tmpDir
.GetTempDir() + L
"\\.git\\objects"));
80 EXPECT_FALSE(GitAdminDir::IsAdminDirPath(tmpDir
.GetTempDir() + L
"\\.gitmodules"));
82 ASSERT_TRUE(CreateDirectory(tmpDir
.GetTempDir() + L
"\\.gitted", nullptr));
83 EXPECT_FALSE(GitAdminDir::IsAdminDirPath(tmpDir
.GetTempDir() + L
"\\.gitted"));
86 TEST(CGitAdminDir
, IsAdminDirPath_BareRepo
)
90 EXPECT_FALSE(GitAdminDir::IsAdminDirPath(tmpDir
.GetTempDir()));
92 CAutoRepository repo
= nullptr;
93 ASSERT_TRUE(git_repository_init(repo
.GetPointer(), CUnicodeUtils::GetUTF8(tmpDir
.GetTempDir()), true) == 0);
95 EXPECT_FALSE(GitAdminDir::IsAdminDirPath(tmpDir
.GetTempDir()));
98 TEST(CGitAdminDir
, GetAdminDirPath_BareRepo
)
103 EXPECT_FALSE(GitAdminDir::GetAdminDirPath(tmpDir
.GetTempDir(), adminDir
));
104 EXPECT_TRUE(adminDir
.IsEmpty());
106 CAutoRepository repo
= nullptr;
107 ASSERT_TRUE(git_repository_init(repo
.GetPointer(), CUnicodeUtils::GetUTF8(tmpDir
.GetTempDir()), true) == 0);
109 EXPECT_TRUE(GitAdminDir::GetAdminDirPath(tmpDir
.GetTempDir(), adminDir
));
110 EXPECT_STREQ(tmpDir
.GetTempDir() + _T("\\"), adminDir
);
113 EXPECT_FALSE(GitAdminDir::GetAdminDirPath(tmpDir
.GetTempDir() + L
"\\objects", adminDir
));
114 EXPECT_TRUE(adminDir
.IsEmpty());
117 TEST(CGitAdminDir
, GetAdminDirPath_ReferencedRepo
)
121 CString gitFile
= tmpDir
.GetTempDir() + L
"\\.git";
122 EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR
)gitFile
, L
""));
125 EXPECT_FALSE(GitAdminDir::GetAdminDirPath(tmpDir
.GetTempDir(), adminDir
));
127 EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR
)gitFile
, L
"gitdir: dontcare\n"));
129 EXPECT_TRUE(GitAdminDir::GetAdminDirPath(tmpDir
.GetTempDir(), adminDir
));
130 EXPECT_STREQ(L
"dontcare\\", adminDir
);
132 ASSERT_TRUE(CreateDirectory(tmpDir
.GetTempDir() + L
"\\anotherdir", nullptr));
133 gitFile
= tmpDir
.GetTempDir() + L
"\\anotherdir\\.git";
134 EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR
)gitFile
, L
"gitdir: ../something\n"));
136 EXPECT_TRUE(GitAdminDir::GetAdminDirPath(tmpDir
.GetTempDir() + L
"\\anotherdir", adminDir
));
137 EXPECT_STREQ(tmpDir
.GetTempDir() + L
"\\something\\", adminDir
);
139 ASSERT_TRUE(CreateDirectory(tmpDir
.GetTempDir() + L
"\\\x6587", nullptr));
140 gitFile
= tmpDir
.GetTempDir() + L
"\\\x6587\\.git";
142 EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR
)gitFile
, L
"gitdir: ../\x4e2d\n"));
144 EXPECT_TRUE(GitAdminDir::GetAdminDirPath(tmpDir
.GetTempDir() + L
"\\\x6587", adminDir
));
145 EXPECT_STREQ(tmpDir
.GetTempDir() + L
"\\\x4e2d\\", adminDir
);
148 TEST(CGitAdminDir
, HasAdminDir
)
152 EXPECT_FALSE(GitAdminDir::HasAdminDir(tmpDir
.GetTempDir()));
154 CString gitFile
= tmpDir
.GetTempDir() + L
"\\.gitted";
155 EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR
)gitFile
, L
"something"));
157 EXPECT_FALSE(GitAdminDir::HasAdminDir(tmpDir
.GetTempDir()));
159 CAutoRepository repo
= nullptr;
160 ASSERT_TRUE(git_repository_init(repo
.GetPointer(), CUnicodeUtils::GetUTF8(tmpDir
.GetTempDir()), false) == 0);
163 EXPECT_TRUE(GitAdminDir::HasAdminDir(tmpDir
.GetTempDir(), &repoRoot
));
164 EXPECT_STREQ(tmpDir
.GetTempDir(), repoRoot
);
166 EXPECT_FALSE(GitAdminDir::HasAdminDir(tmpDir
.GetTempDir() + _T("\\.git"), &repoRoot
));
169 EXPECT_TRUE(GitAdminDir::HasAdminDir(tmpDir
.GetTempDir() + _T("\\.gitmodules"), &repoRoot
));
170 EXPECT_STREQ(tmpDir
.GetTempDir(), repoRoot
);
173 EXPECT_TRUE(GitAdminDir::HasAdminDir(tmpDir
.GetTempDir() + _T("\\something"), &repoRoot
));
174 EXPECT_STREQ(tmpDir
.GetTempDir(), repoRoot
);
176 ASSERT_TRUE(CreateDirectory(tmpDir
.GetTempDir() + L
"\\anotherdir", nullptr));
178 EXPECT_TRUE(GitAdminDir::HasAdminDir(tmpDir
.GetTempDir() + _T("\\anotherdir"), &repoRoot
));
179 EXPECT_STREQ(tmpDir
.GetTempDir(), repoRoot
);
181 CAutoRepository subrepo
= nullptr;
182 ASSERT_TRUE(git_repository_init(subrepo
.GetPointer(), CUnicodeUtils::GetUTF8(tmpDir
.GetTempDir() + _T("\\anotherdir")), false) == 0);
185 EXPECT_TRUE(GitAdminDir::HasAdminDir(tmpDir
.GetTempDir() + _T("\\anotherdir"), &repoRoot
));
186 EXPECT_STREQ(tmpDir
.GetTempDir() + _T("\\anotherdir"), repoRoot
);
189 EXPECT_TRUE(GitAdminDir::HasAdminDir(tmpDir
.GetTempDir() + _T("\\anotherdir"), true, &repoRoot
));
190 EXPECT_STREQ(tmpDir
.GetTempDir() + _T("\\anotherdir"), repoRoot
);
193 EXPECT_TRUE(GitAdminDir::HasAdminDir(tmpDir
.GetTempDir() + _T("\\anotherdir"), false, &repoRoot
));
194 EXPECT_STREQ(tmpDir
.GetTempDir(), repoRoot
);
197 TEST(CGitAdminDir
, HasAdminDir_bareRepo
)
201 EXPECT_FALSE(GitAdminDir::HasAdminDir(tmpDir
.GetTempDir()));
203 CAutoRepository repo
= nullptr;
204 ASSERT_TRUE(git_repository_init(repo
.GetPointer(), CUnicodeUtils::GetUTF8(tmpDir
.GetTempDir()), true) == 0);
206 EXPECT_FALSE(GitAdminDir::HasAdminDir(tmpDir
.GetTempDir()));
208 EXPECT_FALSE(GitAdminDir::IsBareRepo(tmpDir
.GetTempDir() + _T("\\objects")));
211 TEST(CGitAdminDir
, HasAdminDir_ReferencedRepo
)
215 CString gitFile
= tmpDir
.GetTempDir() + L
"\\.git";
216 EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR
)gitFile
, L
"gitdir: dontcare"));
219 EXPECT_TRUE(GitAdminDir::HasAdminDir(tmpDir
.GetTempDir(), &repoRoot
));
220 EXPECT_STREQ(tmpDir
.GetTempDir(), repoRoot
);
223 TEST(CGitAdminDir
, IsWorkingTreeOrBareRepo
)
227 EXPECT_FALSE(GitAdminDir::IsWorkingTreeOrBareRepo(tmpDir
.GetTempDir()));
229 CAutoRepository repo
= nullptr;
230 ASSERT_TRUE(git_repository_init(repo
.GetPointer(), CUnicodeUtils::GetUTF8(tmpDir
.GetTempDir()), true) == 0);
232 EXPECT_TRUE(GitAdminDir::IsWorkingTreeOrBareRepo(tmpDir
.GetTempDir()));
235 TEST(CGitAdminDir
, IsWorkingTreeOrBareRepo_normalRepo
)
239 EXPECT_FALSE(GitAdminDir::IsWorkingTreeOrBareRepo(tmpDir
.GetTempDir()));
241 CAutoRepository repo
= nullptr;
242 ASSERT_TRUE(git_repository_init(repo
.GetPointer(), CUnicodeUtils::GetUTF8(tmpDir
.GetTempDir()), false) == 0);
244 EXPECT_TRUE(GitAdminDir::IsWorkingTreeOrBareRepo(tmpDir
.GetTempDir()));
246 EXPECT_FALSE(GitAdminDir::IsWorkingTreeOrBareRepo(tmpDir
.GetTempDir() + _T("\\.git")));
248 EXPECT_FALSE(GitAdminDir::IsWorkingTreeOrBareRepo(tmpDir
.GetTempDir() + _T("\\.git\\objects")));
251 TEST(CGitAdminDir
, IsWorkingTreeOrBareRepo_ReferencedRepo
)
255 CString gitFile
= tmpDir
.GetTempDir() + L
"\\.git";
256 EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR
)gitFile
, L
"gitdir: dontcare"));
258 EXPECT_TRUE(GitAdminDir::IsWorkingTreeOrBareRepo(tmpDir
.GetTempDir()));
261 TEST(CGitAdminDir
, ReadGitLink
)
265 CString gitFile
= tmpDir
.GetTempDir() + L
"\\.git";
266 EXPECT_STREQ(L
"", GitAdminDir::ReadGitLink(L
"C:\\somerepo", gitFile
));
268 EXPECT_TRUE(::CreateDirectory(gitFile
, nullptr));
269 EXPECT_STREQ(L
"", GitAdminDir::ReadGitLink(L
"C:\\somerepo", gitFile
));
270 EXPECT_TRUE(::RemoveDirectory(gitFile
));
272 EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR
)gitFile
, L
"broken"));
273 EXPECT_STREQ(L
"", GitAdminDir::ReadGitLink(L
"C:\\somerepo", gitFile
));
275 EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR
)gitFile
, L
"gitdir: dontcare"));
276 EXPECT_STREQ(L
"dontcare", GitAdminDir::ReadGitLink(L
"C:\\somerepo", gitFile
));
278 EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR
)gitFile
, L
"gitdir: ./dontcare"));
279 EXPECT_STREQ(L
"C:\\somerepo\\dontcare", GitAdminDir::ReadGitLink(L
"C:\\somerepo", gitFile
));
281 EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR
)gitFile
, L
"gitdir: .dontcare"));
282 EXPECT_STREQ(L
"C:\\somerepo\\.dontcare", GitAdminDir::ReadGitLink(L
"C:\\somerepo", gitFile
));
284 EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR
)gitFile
, L
"gitdir: ..dontcare"));
285 EXPECT_STREQ(L
"C:\\somerepo\\..dontcare", GitAdminDir::ReadGitLink(L
"C:\\somerepo", gitFile
));
287 EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR
)gitFile
, L
"gitdir: ../.git/modules/dontcare"));
288 EXPECT_STREQ(L
"C:\\.git\\modules\\dontcare", GitAdminDir::ReadGitLink(L
"C:\\somerepo", gitFile
));
291 TEST(CGitAdminDir
, GetGitTopDir
)
295 EXPECT_STREQ(L
"", GitAdminDir::GetGitTopDir(tmpDir
.GetTempDir()));
297 CString gitFile
= tmpDir
.GetTempDir() + L
"\\.git";
298 CString testFile
= tmpDir
.GetTempDir() + L
"\\.git\\test";
299 EXPECT_TRUE(::CreateDirectory(gitFile
, nullptr));
300 EXPECT_STREQ(L
"", GitAdminDir::GetGitTopDir(gitFile
));
301 EXPECT_STREQ(L
"", GitAdminDir::GetGitTopDir(gitFile
+ L
"\\test"));
302 EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR
)testFile
, L
"something"));
303 EXPECT_STREQ(L
"", GitAdminDir::GetGitTopDir(gitFile
+ L
"\\test"));
304 EXPECT_TRUE(::DeleteFile(testFile
));
305 EXPECT_TRUE(::RemoveDirectory(gitFile
));
307 // should not matter whether .git is a directory or file
308 EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR
)gitFile
, L
"gitdir: dontcare"));
310 EXPECT_STREQ(tmpDir
.GetTempDir(), GitAdminDir::GetGitTopDir(gitFile
));
311 EXPECT_STREQ(tmpDir
.GetTempDir(), GitAdminDir::GetGitTopDir(tmpDir
.GetTempDir()));
312 testFile
= tmpDir
.GetTempDir() + L
"\\test";
313 EXPECT_STREQ(tmpDir
.GetTempDir(), GitAdminDir::GetGitTopDir(testFile
));
314 EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR
)testFile
, L
"something"));
315 EXPECT_STREQ(tmpDir
.GetTempDir(), GitAdminDir::GetGitTopDir(testFile
));
317 EXPECT_TRUE(::CreateDirectory(tmpDir
.GetTempDir() + L
"\\subdir", nullptr));
318 EXPECT_STREQ(tmpDir
.GetTempDir(), GitAdminDir::GetGitTopDir(tmpDir
.GetTempDir() + L
"\\subdir"));
319 testFile
= tmpDir
.GetTempDir() + L
"\\subdir\\test";
320 EXPECT_STREQ(tmpDir
.GetTempDir(), GitAdminDir::GetGitTopDir(testFile
));
321 EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR
)testFile
, L
"something"));
322 EXPECT_STREQ(tmpDir
.GetTempDir(), GitAdminDir::GetGitTopDir(testFile
));
324 // make subdir a nested repository
325 gitFile
= tmpDir
.GetTempDir() + L
"\\subdir\\.git";
326 EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR
)gitFile
, L
"gitdir: dontcare"));
327 EXPECT_STREQ(tmpDir
.GetTempDir() + L
"\\subdir", GitAdminDir::GetGitTopDir(tmpDir
.GetTempDir() + L
"\\subdir"));
328 EXPECT_STREQ(tmpDir
.GetTempDir() + L
"\\subdir", GitAdminDir::GetGitTopDir(testFile
));
331 TEST(CGitAdminDir
, GetSuperProjectRoot
)
335 EXPECT_STREQ(L
"", GitAdminDir::GetSuperProjectRoot(tmpDir
.GetTempDir()));
337 EXPECT_TRUE(::CreateDirectory(tmpDir
.GetTempDir() + L
"\\.git", nullptr));
338 EXPECT_STREQ(L
"", GitAdminDir::GetSuperProjectRoot(tmpDir
.GetTempDir()));
340 EXPECT_TRUE(::CreateDirectory(tmpDir
.GetTempDir() + L
"\\subdir", nullptr));
341 EXPECT_STREQ(L
"", GitAdminDir::GetSuperProjectRoot(tmpDir
.GetTempDir()));
343 CString gitmodules
= tmpDir
.GetTempDir() + L
"\\.gitmodules";
344 EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR
)gitmodules
, L
"something"));
345 EXPECT_STREQ(tmpDir
.GetTempDir(), GitAdminDir::GetSuperProjectRoot(tmpDir
.GetTempDir()));
346 EXPECT_STREQ(tmpDir
.GetTempDir(), GitAdminDir::GetSuperProjectRoot(tmpDir
.GetTempDir() + L
"\\subdir"));
348 // make subdir a nested repository
349 EXPECT_TRUE(::CreateDirectory(tmpDir
.GetTempDir() + L
"\\subdir\\.git", nullptr));
350 EXPECT_STREQ(L
"", GitAdminDir::GetSuperProjectRoot(tmpDir
.GetTempDir() + L
"\\subdir"));
352 EXPECT_TRUE(::DeleteFile(gitmodules
));
353 EXPECT_STREQ(L
"", GitAdminDir::GetSuperProjectRoot(tmpDir
.GetTempDir() + L
"\\subdir"));
355 gitmodules
= tmpDir
.GetTempDir() + L
"\\subdir\\.gitmodules";
356 EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR
)gitmodules
, L
"something"));
357 EXPECT_STREQ(tmpDir
.GetTempDir() + L
"\\subdir", GitAdminDir::GetSuperProjectRoot(tmpDir
.GetTempDir() + L
"\\subdir"));