Deduplicate code to read .git file
[TortoiseGit.git] / src / Git / GitAdminDir.cpp
blob2ff0daa4a49c21fecbb76f9a56372fc27d07456f
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::IsWorkingTreeOrBareRepo(const CString& path)
68 return HasAdminDir(path) || IsBareRepo(path);
71 bool GitAdminDir::HasAdminDir(const CString& path)
73 return HasAdminDir(path, !!PathIsDirectory(path));
76 bool GitAdminDir::HasAdminDir(const CString& path,CString* ProjectTopDir)
78 return HasAdminDir(path, !!PathIsDirectory(path),ProjectTopDir);
81 bool GitAdminDir::HasAdminDir(const CString& path, bool bDir, CString* ProjectTopDir)
83 if (path.IsEmpty())
84 return false;
85 CString sDirName = path;
86 if (!bDir)
88 // e.g "C:\"
89 if (path.GetLength() <= 3)
90 return false;
91 sDirName = path.Left(path.ReverseFind(_T('\\')));
94 // a .git dir or anything inside it should be left out, only interested in working copy files -- Myagi
96 int n = 0;
97 for (;;)
99 n = sDirName.Find(_T("\\.git"), n);
100 if (n < 0)
102 break;
105 // check for actual .git dir (and not .gitignore or something else), continue search if false match
106 n += 5;
107 if (sDirName[n] == _T('\\') || sDirName[n] == 0)
109 return false;
114 for (;;)
116 if(CGit::GitPathFileExists(sDirName + _T("\\.git")))
118 if(ProjectTopDir)
120 *ProjectTopDir=sDirName;
121 // Make sure to add the trailing slash to root paths such as 'C:'
122 if (sDirName.GetLength() == 2 && sDirName[1] == _T(':'))
123 (*ProjectTopDir) += _T("\\");
125 return true;
127 else if (IsBareRepo(sDirName))
128 return false;
130 int x = sDirName.ReverseFind(_T('\\'));
131 if (x < 2)
132 break;
134 sDirName = sDirName.Left(x);
135 // don't check for \\COMPUTERNAME\.git
136 if (sDirName[0] == _T('\\') && sDirName[1] == _T('\\') && sDirName.Find(_T('\\'), 2) < 0)
137 break;
140 return false;
144 * Returns the .git-path (if .git is a file, read the repository path and return it)
145 * adminDir always ends with "\"
147 bool GitAdminDir::GetAdminDirPath(const CString &projectTopDir, CString& adminDir)
149 if (IsBareRepo(projectTopDir))
151 adminDir = projectTopDir;
152 adminDir.TrimRight('\\');
153 adminDir.Append(_T("\\"));
154 return true;
157 CString sDotGitPath = projectTopDir + _T("\\") + GetAdminDirName();
158 if (CTGitPath(sDotGitPath).IsDirectory())
160 sDotGitPath.TrimRight('\\');
161 sDotGitPath.Append(_T("\\"));
162 adminDir = sDotGitPath;
163 return true;
165 else
167 CString result = ReadGitLink(projectTopDir, sDotGitPath);
168 if (result.IsEmpty())
169 return false;
170 adminDir = result + _T("\\");
171 return true;
175 CString GitAdminDir::ReadGitLink(const CString& topDir, const CString& dotGitPath)
177 FILE* pFile = _tfsopen(dotGitPath, _T("r"), SH_DENYWR);
179 if (!pFile)
180 return _T("");
182 int size = 65536;
183 std::unique_ptr<char[]> buffer(new char[size]);
184 int length = (int)fread(buffer.get(), sizeof(char), size, pFile);
185 fclose(pFile);
186 CStringA gitPathA(buffer.get(), length);
187 if (length < 8 || gitPathA.Left(8) != "gitdir: ")
188 return _T("");
189 CString gitPath = CUnicodeUtils::GetUnicode(gitPathA);
190 // trim after converting to UTF-16, because CStringA trim does not work when having UTF-8 chars
191 gitPath = gitPath.Trim().Mid(8); // 8 = len("gitdir: ")
192 gitPath.Replace('/', '\\');
193 gitPath.TrimRight('\\');
194 if (!gitPath.IsEmpty() && gitPath[0] == _T('.'))
196 gitPath = topDir + _T("\\") + gitPath;
197 CString adminDir;
198 PathCanonicalize(adminDir.GetBuffer(MAX_PATH), gitPath);
199 adminDir.ReleaseBuffer();
200 return adminDir;
202 return gitPath;
205 bool GitAdminDir::IsAdminDirPath(const CString& path)
207 if (path.IsEmpty())
208 return false;
209 bool bIsAdminDir = false;
210 CString lowerpath = path;
211 lowerpath.MakeLower();
212 int ind1 = 0;
213 while ((ind1 = lowerpath.Find(_T("\\.git"), ind1))>=0)
215 int ind = ind1++;
216 if (ind == (lowerpath.GetLength() - 5))
218 bIsAdminDir = true;
219 break;
221 else if (lowerpath.Find(_T("\\.git\\"), ind)>=0)
223 bIsAdminDir = true;
224 break;
228 return bIsAdminDir;
231 bool GitAdminDir::IsBareRepo(const CString& path)
233 if (path.IsEmpty())
234 return false;
236 if (IsAdminDirPath(path))
237 return false;
239 // don't check for \\COMPUTERNAME\HEAD
240 if (path[0] == _T('\\') && path[1] == _T('\\'))
242 if (path.Find(_T('\\'), 2) < 0)
243 return false;
246 if (!PathFileExists(path + _T("\\HEAD")) || !PathFileExists(path + _T("\\config")))
247 return false;
249 if (!PathFileExists(path + _T("\\objects\\")) || !PathFileExists(path + _T("\\refs\\")))
250 return false;
252 return true;