Fixed issue #849: Searching in log breaks graph/branch-line
[TortoiseGit.git] / src / Git / GitAdminDir.cpp
blob8d7c04aae6aedce3f49ea42cb0cf39a7537099cd
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2010 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "StdAfx.h"
20 #include "UnicodeUtils.h"
21 #include "GitAdminDir.h"
22 #include "Git.h"
23 GitAdminDir g_GitAdminDir;
25 GitAdminDir::GitAdminDir()
26 : m_nInit(0)
27 // , m_bVSNETHack(false)
28 // , m_pool(NULL)
32 GitAdminDir::~GitAdminDir()
34 // if (m_nInit)
35 // (m_pool);
38 bool GitAdminDir::Init()
40 if (m_nInit==0)
42 #if 0
43 m_bVSNETHack = false;
44 m_pool = svn_pool_create(NULL);
45 size_t ret = 0;
46 getenv_s(&ret, NULL, 0, "GIT_ASP_DOT_NET_HACK");
47 if (ret)
49 svn_error_clear(svn_wc_set_adm_dir("_git", m_pool));
50 m_bVSNETHack = true;
52 #endif
54 m_nInit++;
55 return true;
58 bool GitAdminDir::Close()
60 m_nInit--;
61 if (m_nInit>0)
62 return false;
63 #if 0
64 svn_pool_destroy(m_pool);
65 #endif
66 return true;
69 CString GitAdminDir::GetSuperProjectRoot(const CString& path)
71 CString projectroot=path;
75 if(CGit::GitPathFileExists(projectroot + _T("\\.gitmodules")))
77 return projectroot;
80 projectroot = projectroot.Left(projectroot.ReverseFind('\\'));
82 }while(projectroot.ReverseFind('\\')>0);
84 return _T("");
88 bool GitAdminDir::IsAdminDirName(const CString& name) const
90 #if 0
91 CStringA nameA = CUnicodeUtils::GetUTF8(name).MakeLower();
92 return !!svn_wc_is_adm_dir(nameA, m_pool);
93 #endif
94 return name == ".git";
96 CString GitAdminDir::GetGitTopDir(const CString& path)
98 CString str;
99 str=_T("");
100 HasAdminDir(path,!!PathIsDirectory(path),&str);
101 return str;
104 bool GitAdminDir::HasAdminDir(const CString& path) const
106 return HasAdminDir(path, !!PathIsDirectory(path));
109 bool GitAdminDir::HasAdminDir(const CString& path,CString *ProjectTopDir) const
111 return HasAdminDir(path, !!PathIsDirectory(path),ProjectTopDir);
114 bool GitAdminDir::HasAdminDir(const CString& path, bool bDir,CString *ProjectTopDir) const
116 if (path.IsEmpty())
117 return false;
118 CString sDirName = path;
119 if (!bDir)
121 sDirName = path.Left(path.ReverseFind(_T('\\')));
124 // a .git dir or anything inside it should be left out, only interested in working copy files -- Myagi
126 int n = 0;
127 for (;;)
129 n = sDirName.Find(_T("\\.git"), n);
130 if (n < 0)
132 break;
135 // check for actual .git dir (and not .gitignore or something else), continue search if false match
136 n += 5;
137 if (sDirName[n] == _T('\\') || sDirName[n] == 0)
139 return false;
144 for (;;)
146 if(CGit::GitPathFileExists(sDirName + _T("\\.git")))
148 if(ProjectTopDir)
150 *ProjectTopDir=sDirName;
151 // Make sure to add the trailing slash to root paths such as 'C:'
152 if (sDirName.GetLength() == 2 && sDirName[1] == _T(':'))
153 (*ProjectTopDir) += _T("\\");
155 return true;
158 int x = sDirName.ReverseFind(_T('\\'));
159 if (x < 2)
160 break;
162 sDirName = sDirName.Left(x);
165 return false;
169 bool GitAdminDir::IsAdminDirPath(const CString& path) const
171 if (path.IsEmpty())
172 return false;
173 bool bIsAdminDir = false;
174 CString lowerpath = path;
175 lowerpath.MakeLower();
176 int ind = -1;
177 int ind1 = 0;
178 while ((ind1 = lowerpath.Find(_T("\\.git"), ind1))>=0)
180 ind = ind1++;
181 if (ind == (lowerpath.GetLength() - 5))
183 bIsAdminDir = true;
184 break;
186 else if (lowerpath.Find(_T("\\.git\\"), ind)>=0)
188 bIsAdminDir = true;
189 break;
193 return bIsAdminDir;
196 bool GitAdminDir::IsBareRepo(const CString& path) const
198 if (path.IsEmpty())
199 return false;
201 if (IsAdminDirPath(path))
202 return false;
204 if (!PathFileExists(path + _T("\\HEAD")) || !PathFileExists(path + _T("\\config")))
205 return false;
207 if (!PathFileExists(path + _T("\\objects\\")) || !PathFileExists(path + _T("\\refs\\")))
208 return false;
210 return true;