25#include <wx/filename.h>
60 bool aRemoveGitDir, wxString* aErrors )
67 return backend->
RemoveVCS( aRepo, aProjectPath, aRemoveGitDir, aErrors );
77 const auto read = [&]() -> ENVIRONMENT::VCS_VALUE
81 if( git_reference_name_to_id( &oid, aRepo,
"HEAD" ) != 0 )
84 char hash[GIT_OID_HEXSZ + 1];
85 git_oid_tostr( hash,
sizeof( hash ), &oid );
88 auto* environment = ENVIRONMENT::Current();
89 const char*
path = git_repository_path( aRepo );
90 const auto value = environment &&
path
91 ? environment->VcsValue( { ENVIRONMENT::VCS_QUERY::HEAD, wxString::FromUTF8(
path ),
"", 0,
false }, read )
95 if( value.text.empty() || git_oid_fromstr( &oid, value.text.c_str() ) != 0 )
104 const auto read = [&]
109 if( git_oid_is_zero( &oid ) )
110 return wxString(
"no hash" );
112 char hash[GIT_OID_HEXSZ + 1];
113 git_oid_tostr( hash,
sizeof( hash ), &oid );
114 return wxString::FromUTF8( hash );
117 const wxString hash = environment ? environment->GitHash( aProjectFile, read ) : read();
118 return aShort ? hash.Left( 8 ) : hash;
123 const wxString& aCanonicalWorkDir )
126 return aCanonicalWorkDir;
128 if( aUserProjectPath.IsEmpty() || aCanonicalWorkDir.IsEmpty() )
129 return aCanonicalWorkDir;
131 char resolvedPath[PATH_MAX];
133 if( realpath( aUserProjectPath.mb_str(), resolvedPath ) ==
nullptr )
134 return aCanonicalWorkDir;
136 wxString canonicalUserPath = wxString::FromUTF8( resolvedPath );
138 if( !canonicalUserPath.EndsWith( wxFileName::GetPathSeparator() ) )
139 canonicalUserPath += wxFileName::GetPathSeparator();
141 wxString canonicalWorkDirNorm = aCanonicalWorkDir;
143 if( !canonicalWorkDirNorm.EndsWith( wxFileName::GetPathSeparator() ) )
144 canonicalWorkDirNorm += wxFileName::GetPathSeparator();
150 if( canonicalUserPath == canonicalWorkDirNorm )
152 return aUserProjectPath.EndsWith( wxFileName::GetPathSeparator() )
154 : aUserProjectPath + wxFileName::GetPathSeparator();
161 userFn.AssignDir( aUserProjectPath );
163 wxFileName canonicalUserFn;
164 canonicalUserFn.AssignDir( canonicalUserPath );
166 wxFileName canonicalWorkDirFn;
167 canonicalWorkDirFn.AssignDir( canonicalWorkDirNorm );
169 while( canonicalUserFn.GetFullPath() != canonicalWorkDirFn.GetFullPath() )
171 if( canonicalUserFn.GetDirCount() == 0 || userFn.GetDirCount() == 0 )
172 return aCanonicalWorkDir;
174 canonicalUserFn.RemoveLastDir();
175 userFn.RemoveLastDir();
178 return userFn.GetPathWithSep();
185 if( aProjectPath.IsEmpty() )
188 wxString projectPath = aProjectPath;
190 if( !projectPath.EndsWith( wxT(
"/" ) ) && !projectPath.EndsWith( wxT(
"\\" ) ) )
191 projectPath += wxT(
"/" );
193 return aAbsPath.StartsWith( projectPath );
virtual int CreateBranch(git_repository *aRepo, const wxString &aBranchName)=0
virtual bool RemoveVCS(git_repository *&aRepo, const wxString &aProjectPath, bool aRemoveGitDir, wxString *aErrors)=0
virtual git_repository * GetRepositoryForFile(const char *aFilename)=0
static git_oid GetCapturedHeadOid(git_repository *aRepo)
Return HEAD for commit-based text queries.
static wxString GetCurrentHash(const wxString &aProjectFile, bool aShort)
Return the current HEAD commit hash for the repository containing aProjectFile.
static git_repository * GetRepositoryForFile(const char *aFilename)
Discover and open the repository that contains the given file.
static wxString ComputeSymlinkPreservingWorkDir(const wxString &aUserProjectPath, const wxString &aCanonicalWorkDir)
Compute a working directory path that preserves symlinks from the user's project path.
static bool IsWithinProjectPath(const wxString &aAbsPath, const wxString &aProjectPath)
Test whether an absolute file path lives inside the current project directory.
static bool RemoveVCS(git_repository *&aRepo, const wxString &aProjectPath=wxEmptyString, bool aRemoveGitDir=false, wxString *aErrors=nullptr)
Remove version control from a directory by freeing the repository and optionally removing the ....
static int CreateBranch(git_repository *aRepo, const wxString &aBranchName)
Create a new branch based on HEAD.
A text evaluation frame with one frozen clock and memoized external queries.
static ENVIRONMENT * Current()
GIT_BACKEND * GetGitBackend()
std::unique_ptr< git_repository, decltype([](git_repository *aRepo) { git_repository_free(aRepo); })> GitRepositoryPtr
A unique pointer for git_repository objects with automatic cleanup.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.