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.
21 #include "UnicodeUtils.h"
22 #include "GitAdminDir.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")))
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)
53 }while(projectroot
.ReverseFind('\\')>0);
58 CString
GitAdminDir::GetGitTopDir(const CString
& path
)
61 HasAdminDir(path
,!!PathIsDirectory(path
),&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
)
84 CString sDirName
= path
;
88 if (path
.GetLength() <= 3)
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
97 n
= sDirName
.Find(_T("\\.git"), n
);
101 // check for actual .git dir (and not .gitignore or something else), continue search if false match
103 if (sDirName
[n
] == _T('\\') || sDirName
[n
] == 0)
109 if(CGit::GitPathFileExists(sDirName
+ _T("\\.git")))
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("\\");
120 else if (IsBareRepo(sDirName
))
123 int x
= sDirName
.ReverseFind(_T('\\'));
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)
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("\\"));
149 CString sDotGitPath
= projectTopDir
+ _T("\\") + GetAdminDirName();
150 if (CTGitPath(sDotGitPath
).IsDirectory())
152 sDotGitPath
.TrimRight('\\');
153 sDotGitPath
.Append(_T("\\"));
154 adminDir
= sDotGitPath
;
159 CString result
= ReadGitLink(projectTopDir
, sDotGitPath
);
160 if (result
.IsEmpty())
162 adminDir
= result
+ _T("\\");
167 CString
GitAdminDir::ReadGitLink(const CString
& topDir
, const CString
& dotGitPath
)
169 CAutoFILE pFile
= _tfsopen(dotGitPath
, _T("r"), SH_DENYWR
);
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: ")
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
;
189 PathCanonicalize(CStrBuf(adminDir
, MAX_PATH
), gitPath
);
195 bool GitAdminDir::IsAdminDirPath(const CString
& path
)
199 bool bIsAdminDir
= false;
200 CString lowerpath
= path
;
201 lowerpath
.MakeLower();
203 while ((ind1
= lowerpath
.Find(_T("\\.git"), ind1
))>=0)
206 if (ind
== (lowerpath
.GetLength() - 5))
211 else if (lowerpath
.Find(_T("\\.git\\"), ind
)>=0)
221 bool GitAdminDir::IsBareRepo(const CString
& path
)
226 if (IsAdminDirPath(path
))
229 // don't check for \\COMPUTERNAME\HEAD
230 if (path
[0] == _T('\\') && path
[1] == _T('\\'))
232 if (path
.Find(_T('\\'), 2) < 0)
236 if (!PathFileExists(path
+ _T("\\HEAD")) || !PathFileExists(path
+ _T("\\config")))
239 if (!PathFileExists(path
+ _T("\\objects\\")) || !PathFileExists(path
+ _T("\\refs\\")))