Make sure we also kill AsyncReadStdErrThread when we kill a thread
[TortoiseGit.git] / src / Git / GitAdminDir.cpp
blob17b29cf5e7cb6ad64e777bae9240005a6f81c454
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 "SmartHandle.h"
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("");
58 CString GitAdminDir::GetGitTopDir(const CString& path)
60 CString str;
61 HasAdminDir(path,!!PathIsDirectory(path),&str);
62 return str;
65 bool GitAdminDir::IsWorkingTreeOrBareRepo(const CString& path)
67 return HasAdminDir(path) || IsBareRepo(path);
70 bool GitAdminDir::HasAdminDir(const CString& path)
72 return HasAdminDir(path, !!PathIsDirectory(path));
75 bool GitAdminDir::HasAdminDir(const CString& path,CString* ProjectTopDir)
77 return HasAdminDir(path, !!PathIsDirectory(path),ProjectTopDir);
80 bool GitAdminDir::HasAdminDir(const CString& path, bool bDir, CString* ProjectTopDir)
82 if (path.IsEmpty())
83 return false;
84 CString sDirName = path;
85 if (!bDir)
87 // e.g "C:\"
88 if (path.GetLength() <= 3)
89 return false;
90 sDirName = path.Left(path.ReverseFind(_T('\\')));
93 // a .git dir or anything inside it should be left out, only interested in working copy files -- Myagi
94 int n = 0;
95 for (;;)
97 n = sDirName.Find(_T("\\.git"), n);
98 if (n < 0)
99 break;
101 // check for actual .git dir (and not .gitignore or something else), continue search if false match
102 n += 5;
103 if (sDirName[n] == _T('\\') || sDirName[n] == 0)
104 return false;
107 for (;;)
109 if(CGit::GitPathFileExists(sDirName + _T("\\.git")))
111 if(ProjectTopDir)
113 *ProjectTopDir=sDirName;
114 // Make sure to add the trailing slash to root paths such as 'C:'
115 if (sDirName.GetLength() == 2 && sDirName[1] == _T(':'))
116 (*ProjectTopDir) += _T("\\");
118 return true;
120 else if (IsBareRepo(sDirName))
121 return false;
123 int x = sDirName.ReverseFind(_T('\\'));
124 if (x < 2)
125 break;
127 sDirName = sDirName.Left(x);
128 // don't check for \\COMPUTERNAME\.git
129 if (sDirName[0] == _T('\\') && sDirName[1] == _T('\\') && sDirName.Find(_T('\\'), 2) < 0)
130 break;
133 return false;
136 * Returns the .git-path (if .git is a file, read the repository path and return it)
137 * adminDir always ends with "\"
139 bool GitAdminDir::GetAdminDirPath(const CString &projectTopDir, CString& adminDir)
141 if (IsBareRepo(projectTopDir))
143 adminDir = projectTopDir;
144 adminDir.TrimRight('\\');
145 adminDir.Append(_T("\\"));
146 return true;
149 CString sDotGitPath = projectTopDir + _T("\\") + GetAdminDirName();
150 if (CTGitPath(sDotGitPath).IsDirectory())
152 sDotGitPath.TrimRight('\\');
153 sDotGitPath.Append(_T("\\"));
154 adminDir = sDotGitPath;
155 return true;
157 else
159 CString result = ReadGitLink(projectTopDir, sDotGitPath);
160 if (result.IsEmpty())
161 return false;
162 adminDir = result + _T("\\");
163 return true;
167 CString GitAdminDir::ReadGitLink(const CString& topDir, const CString& dotGitPath)
169 CAutoFILE pFile = _tfsopen(dotGitPath, _T("r"), SH_DENYWR);
171 if (!pFile)
172 return _T("");
174 int size = 65536;
175 auto buffer = std::make_unique<char[]>(size);
176 int length = (int)fread(buffer.get(), sizeof(char), size, pFile);
177 CStringA gitPathA(buffer.get(), length);
178 if (length < 8 || gitPathA.Left(8) != "gitdir: ")
179 return _T("");
180 CString gitPath = CUnicodeUtils::GetUnicode(gitPathA);
181 // trim after converting to UTF-16, because CStringA trim does not work when having UTF-8 chars
182 gitPath = gitPath.Trim().Mid(8); // 8 = len("gitdir: ")
183 gitPath.Replace('/', '\\');
184 gitPath.TrimRight('\\');
185 if (!gitPath.IsEmpty() && gitPath[0] == _T('.'))
187 gitPath = topDir + _T("\\") + gitPath;
188 CString adminDir;
189 PathCanonicalize(CStrBuf(adminDir, MAX_PATH), gitPath);
190 return adminDir;
192 return gitPath;
195 bool GitAdminDir::IsAdminDirPath(const CString& path)
197 if (path.IsEmpty())
198 return false;
199 bool bIsAdminDir = false;
200 CString lowerpath = path;
201 lowerpath.MakeLower();
202 int ind1 = 0;
203 while ((ind1 = lowerpath.Find(_T("\\.git"), ind1))>=0)
205 int ind = ind1++;
206 if (ind == (lowerpath.GetLength() - 5))
208 bIsAdminDir = true;
209 break;
211 else if (lowerpath.Find(_T("\\.git\\"), ind)>=0)
213 bIsAdminDir = true;
214 break;
218 return bIsAdminDir;
221 bool GitAdminDir::IsBareRepo(const CString& path)
223 if (path.IsEmpty())
224 return false;
226 if (IsAdminDirPath(path))
227 return false;
229 // don't check for \\COMPUTERNAME\HEAD
230 if (path[0] == _T('\\') && path[1] == _T('\\'))
232 if (path.Find(_T('\\'), 2) < 0)
233 return false;
236 if (!PathFileExists(path + _T("\\HEAD")) || !PathFileExists(path + _T("\\config")))
237 return false;
239 if (!PathFileExists(path + _T("\\objects\\")) || !PathFileExists(path + _T("\\refs\\")))
240 return false;
242 return true;