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.
21 #include "UnicodeUtils.h"
22 #include "GitAdminDir.h"
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")))
47 projectroot
= projectroot
.Left(projectroot
.ReverseFind('\\'));
49 }while(projectroot
.ReverseFind('\\')>0);
55 CString
GitAdminDir::GetGitTopDir(const CString
& path
)
59 HasAdminDir(path
,!!PathIsDirectory(path
),&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
77 CString sDirName
= path
;
81 if (path
.GetLength() <= 3)
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
91 n
= sDirName
.Find(_T("\\.git"), n
);
97 // check for actual .git dir (and not .gitignore or something else), continue search if false match
99 if (sDirName
[n
] == _T('\\') || sDirName
[n
] == 0)
108 if(CGit::GitPathFileExists(sDirName
+ _T("\\.git")))
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("\\");
119 else if (IsBareRepo(sDirName
))
122 int x
= sDirName
.ReverseFind(_T('\\'));
126 sDirName
= sDirName
.Left(x
);
133 * Returns the .git-path (if .git is a file, read the repository path and return it)
134 * adminDir always ends with "\"
136 bool GitAdminDir::GetAdminDirPath(const CString
&projectTopDir
, CString
&adminDir
) const
138 if (IsBareRepo(projectTopDir
))
140 adminDir
= projectTopDir
;
141 adminDir
.TrimRight('\\');
142 adminDir
.Append(_T("\\"));
146 CString sDotGitPath
= projectTopDir
+ _T("\\") + g_GitAdminDir
.GetAdminDirName();
147 if (CTGitPath(sDotGitPath
).IsDirectory())
149 sDotGitPath
.TrimRight('\\');
150 sDotGitPath
.Append(_T("\\"));
151 adminDir
= sDotGitPath
;
157 _tfopen_s(&pFile
, sDotGitPath
, _T("r"));
163 std::unique_ptr
<char[]> buffer(new char[size
]);
164 int length
= (int)fread(buffer
.get(), sizeof(char), size
, pFile
);
166 CStringA
gitPathA(buffer
.get(), length
);
167 if (length
< 8 || gitPathA
.Left(8) != "gitdir: ")
169 CString gitPath
= CUnicodeUtils::GetUnicode(gitPathA
.Trim().Mid(8)); // 8 = len("gitdir: ")
170 gitPath
.Replace('/', '\\');
171 gitPath
.TrimRight('\\');
172 gitPath
.Append(_T("\\"));
173 if (gitPath
.GetLength() > 0 && gitPath
[0] == _T('.'))
175 gitPath
= projectTopDir
+ _T("\\") + gitPath
;
176 PathCanonicalize(adminDir
.GetBuffer(MAX_PATH
), gitPath
.GetBuffer());
177 adminDir
.ReleaseBuffer();
178 gitPath
.ReleaseBuffer();
186 bool GitAdminDir::IsAdminDirPath(const CString
& path
) const
190 bool bIsAdminDir
= false;
191 CString lowerpath
= path
;
192 lowerpath
.MakeLower();
194 while ((ind1
= lowerpath
.Find(_T("\\.git"), ind1
))>=0)
197 if (ind
== (lowerpath
.GetLength() - 5))
202 else if (lowerpath
.Find(_T("\\.git\\"), ind
)>=0)
212 bool GitAdminDir::IsBareRepo(const CString
& path
) const
217 if (IsAdminDirPath(path
))
220 if (!PathFileExists(path
+ _T("\\HEAD")) || !PathFileExists(path
+ _T("\\config")))
223 if (!PathFileExists(path
+ _T("\\objects\\")) || !PathFileExists(path
+ _T("\\refs\\")))