Do not use GitAdminDir objects
[TortoiseGit.git] / src / Git / GitAdminDir.cpp
blob5559cfc8b73f7d99682ed3e10a2c7b43570cb461
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-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.
20 #include "stdafx.h"
21 #include "UnicodeUtils.h"
22 #include "GitAdminDir.h"
23 #include "Git.h"
24 #include <memory>
26 GitAdminDir::GitAdminDir()
30 GitAdminDir::~GitAdminDir()
34 CString GitAdminDir::GetSuperProjectRoot(const CString& path)
36 CString projectroot=path;
40 if (CGit::GitPathFileExists(projectroot + _T("\\.git")))
42 if (CGit::GitPathFileExists(projectroot + _T("\\.gitmodules")))
43 return projectroot;
44 else
45 return _T("");
48 projectroot = projectroot.Left(projectroot.ReverseFind('\\'));
50 // don't check for \\COMPUTERNAME\.git
51 if (projectroot[0] == _T('\\') && projectroot[1] == _T('\\') && projectroot.Find(_T('\\'), 2) < 0)
52 return _T("");
53 }while(projectroot.ReverseFind('\\')>0);
55 return _T("");
59 CString GitAdminDir::GetGitTopDir(const CString& path)
61 CString str;
62 HasAdminDir(path,!!PathIsDirectory(path),&str);
63 return str;
66 bool GitAdminDir::HasAdminDir(const CString& path)
68 return HasAdminDir(path, !!PathIsDirectory(path));
71 bool GitAdminDir::HasAdminDir(const CString& path,CString* ProjectTopDir)
73 return HasAdminDir(path, !!PathIsDirectory(path),ProjectTopDir);
76 bool GitAdminDir::HasAdminDir(const CString& path, bool bDir, CString* ProjectTopDir)
78 if (path.IsEmpty())
79 return false;
80 CString sDirName = path;
81 if (!bDir)
83 // e.g "C:\"
84 if (path.GetLength() <= 3)
85 return false;
86 sDirName = path.Left(path.ReverseFind(_T('\\')));
89 // a .git dir or anything inside it should be left out, only interested in working copy files -- Myagi
91 int n = 0;
92 for (;;)
94 n = sDirName.Find(_T("\\.git"), n);
95 if (n < 0)
97 break;
100 // check for actual .git dir (and not .gitignore or something else), continue search if false match
101 n += 5;
102 if (sDirName[n] == _T('\\') || sDirName[n] == 0)
104 return false;
109 for (;;)
111 if(CGit::GitPathFileExists(sDirName + _T("\\.git")))
113 if(ProjectTopDir)
115 *ProjectTopDir=sDirName;
116 // Make sure to add the trailing slash to root paths such as 'C:'
117 if (sDirName.GetLength() == 2 && sDirName[1] == _T(':'))
118 (*ProjectTopDir) += _T("\\");
120 return true;
122 else if (IsBareRepo(sDirName))
123 return false;
125 int x = sDirName.ReverseFind(_T('\\'));
126 if (x < 2)
127 break;
129 sDirName = sDirName.Left(x);
130 // don't check for \\COMPUTERNAME\.git
131 if (sDirName[0] == _T('\\') && sDirName[1] == _T('\\') && sDirName.Find(_T('\\'), 2) < 0)
132 break;
135 return false;
139 * Returns the .git-path (if .git is a file, read the repository path and return it)
140 * adminDir always ends with "\"
142 bool GitAdminDir::GetAdminDirPath(const CString &projectTopDir, CString& adminDir)
144 if (IsBareRepo(projectTopDir))
146 adminDir = projectTopDir;
147 adminDir.TrimRight('\\');
148 adminDir.Append(_T("\\"));
149 return true;
152 CString sDotGitPath = projectTopDir + _T("\\") + GetAdminDirName();
153 if (CTGitPath(sDotGitPath).IsDirectory())
155 sDotGitPath.TrimRight('\\');
156 sDotGitPath.Append(_T("\\"));
157 adminDir = sDotGitPath;
158 return true;
160 else
162 FILE *pFile;
163 _tfopen_s(&pFile, sDotGitPath, _T("r"));
165 if (!pFile)
166 return false;
168 int size = 65536;
169 std::unique_ptr<char[]> buffer(new char[size]);
170 int length = (int)fread(buffer.get(), sizeof(char), size, pFile);
171 fclose(pFile);
172 CStringA gitPathA(buffer.get(), length);
173 if (length < 8 || gitPathA.Left(8) != "gitdir: ")
174 return false;
175 CString gitPath = CUnicodeUtils::GetUnicode(gitPathA.Trim().Mid(8)); // 8 = len("gitdir: ")
176 gitPath.Replace('/', '\\');
177 gitPath.TrimRight('\\');
178 gitPath.Append(_T("\\"));
179 if (gitPath.GetLength() > 0 && gitPath[0] == _T('.'))
181 gitPath = projectTopDir + _T("\\") + gitPath;
182 PathCanonicalize(adminDir.GetBuffer(MAX_PATH), gitPath.GetBuffer());
183 adminDir.ReleaseBuffer();
184 gitPath.ReleaseBuffer();
185 return true;
187 adminDir = gitPath;
188 return true;
192 bool GitAdminDir::IsAdminDirPath(const CString& path)
194 if (path.IsEmpty())
195 return false;
196 bool bIsAdminDir = false;
197 CString lowerpath = path;
198 lowerpath.MakeLower();
199 int ind1 = 0;
200 while ((ind1 = lowerpath.Find(_T("\\.git"), ind1))>=0)
202 int ind = ind1++;
203 if (ind == (lowerpath.GetLength() - 5))
205 bIsAdminDir = true;
206 break;
208 else if (lowerpath.Find(_T("\\.git\\"), ind)>=0)
210 bIsAdminDir = true;
211 break;
215 return bIsAdminDir;
218 bool GitAdminDir::IsBareRepo(const CString& path)
220 if (path.IsEmpty())
221 return false;
223 if (IsAdminDirPath(path))
224 return false;
226 // don't check for \\COMPUTERNAME\HEAD
227 if (path[0] == _T('\\') && path[1] == _T('\\'))
229 if (path.Find(_T('\\'), 2) < 0)
230 return false;
233 if (!PathFileExists(path + _T("\\HEAD")) || !PathFileExists(path + _T("\\config")))
234 return false;
236 if (!PathFileExists(path + _T("\\objects\\")) || !PathFileExists(path + _T("\\refs\\")))
237 return false;
239 return true;