*.js files: keep the style consistent
[TortoiseGit.git] / src / Git / GitAdminDir.cpp
blob8bd780b42903675c85bfa7c95e392d67df26807d
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2013 - 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 g_GitAdminDir;
28 GitAdminDir::GitAdminDir()
32 GitAdminDir::~GitAdminDir()
36 CString GitAdminDir::GetSuperProjectRoot(const CString& path)
38 CString projectroot=path;
42 if(CGit::GitPathFileExists(projectroot + _T("\\.gitmodules")))
44 return projectroot;
47 projectroot = projectroot.Left(projectroot.ReverseFind('\\'));
49 }while(projectroot.ReverseFind('\\')>0);
51 return _T("");
55 CString GitAdminDir::GetGitTopDir(const CString& path)
57 CString str;
58 str=_T("");
59 HasAdminDir(path,!!PathIsDirectory(path),&str);
60 return str;
63 bool GitAdminDir::HasAdminDir(const CString& path) const
65 return HasAdminDir(path, !!PathIsDirectory(path));
68 bool GitAdminDir::HasAdminDir(const CString& path,CString *ProjectTopDir) const
70 return HasAdminDir(path, !!PathIsDirectory(path),ProjectTopDir);
73 bool GitAdminDir::HasAdminDir(const CString& path, bool bDir,CString *ProjectTopDir) const
75 if (path.IsEmpty())
76 return false;
77 CString sDirName = path;
78 if (!bDir)
80 // e.g "C:\"
81 if (path.GetLength() <= 3)
82 return false;
83 sDirName = path.Left(path.ReverseFind(_T('\\')));
86 // a .git dir or anything inside it should be left out, only interested in working copy files -- Myagi
88 int n = 0;
89 for (;;)
91 n = sDirName.Find(_T("\\.git"), n);
92 if (n < 0)
94 break;
97 // check for actual .git dir (and not .gitignore or something else), continue search if false match
98 n += 5;
99 if (sDirName[n] == _T('\\') || sDirName[n] == 0)
101 return false;
106 for (;;)
108 if(CGit::GitPathFileExists(sDirName + _T("\\.git")))
110 if(ProjectTopDir)
112 *ProjectTopDir=sDirName;
113 // Make sure to add the trailing slash to root paths such as 'C:'
114 if (sDirName.GetLength() == 2 && sDirName[1] == _T(':'))
115 (*ProjectTopDir) += _T("\\");
117 return true;
120 int x = sDirName.ReverseFind(_T('\\'));
121 if (x < 2)
122 break;
124 sDirName = sDirName.Left(x);
127 return false;
131 * Returns the .git-path (if .git is a file, read the repository path and return it)
132 * adminDir always ends with "\"
134 bool GitAdminDir::GetAdminDirPath(const CString &projectTopDir, CString &adminDir) const
136 if (IsBareRepo(projectTopDir))
138 adminDir = projectTopDir;
139 adminDir.TrimRight('\\');
140 adminDir.Append(_T("\\"));
141 return true;
144 CString sDotGitPath = projectTopDir + _T("\\") + g_GitAdminDir.GetAdminDirName();
145 if (CTGitPath(sDotGitPath).IsDirectory())
147 sDotGitPath.TrimRight('\\');
148 sDotGitPath.Append(_T("\\"));
149 adminDir = sDotGitPath;
150 return true;
152 else
154 FILE *pFile;
155 _tfopen_s(&pFile, sDotGitPath, _T("r"));
157 if (!pFile)
158 return false;
160 int size = 65536;
161 std::unique_ptr<char[]> buffer(new char[size]);
162 SecureZeroMemory(buffer.get(), size);
163 fread(buffer.get(), sizeof(char), size, pFile);
164 fclose(pFile);
165 CStringA gitPathA(buffer.get());
166 if (gitPathA.Left(8) != "gitdir: ")
167 return false;
168 CString gitPath = CUnicodeUtils::GetUnicode(gitPathA.Trim().Mid(8)); // 8 = len("gitdir: ")
169 gitPath.Replace('/', '\\');
170 gitPath.TrimRight('\\');
171 gitPath.Append(_T("\\"));
172 if (gitPath.GetLength() > 0 && gitPath[0] == _T('.'))
174 gitPath = projectTopDir + _T("\\") + gitPath;
175 PathCanonicalize(adminDir.GetBuffer(MAX_PATH), gitPath.GetBuffer());
176 adminDir.ReleaseBuffer();
177 gitPath.ReleaseBuffer();
178 return true;
180 adminDir = gitPath;
181 return true;
185 bool GitAdminDir::IsAdminDirPath(const CString& path) const
187 if (path.IsEmpty())
188 return false;
189 bool bIsAdminDir = false;
190 CString lowerpath = path;
191 lowerpath.MakeLower();
192 int ind = -1;
193 int ind1 = 0;
194 while ((ind1 = lowerpath.Find(_T("\\.git"), ind1))>=0)
196 ind = ind1++;
197 if (ind == (lowerpath.GetLength() - 5))
199 bIsAdminDir = true;
200 break;
202 else if (lowerpath.Find(_T("\\.git\\"), ind)>=0)
204 bIsAdminDir = true;
205 break;
209 return bIsAdminDir;
212 bool GitAdminDir::IsBareRepo(const CString& path) const
214 if (path.IsEmpty())
215 return false;
217 if (IsAdminDirPath(path))
218 return false;
220 if (!PathFileExists(path + _T("\\HEAD")) || !PathFileExists(path + _T("\\config")))
221 return false;
223 if (!PathFileExists(path + _T("\\objects\\")) || !PathFileExists(path + _T("\\refs\\")))
224 return false;
226 return true;