KiCad PCB EDA Suite
Loading...
Searching...
No Matches
project_git_utils.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
5 *
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 3
9 * of the License, or (at your option) any later version.
10 *
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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#ifndef PROJECT_GIT_UTILS_H
21#define PROJECT_GIT_UTILS_H
22
23#include <git2.h>
24#include <memory>
25#include <optional>
26#include <string>
27#include <vector>
28#include <wx/string.h>
29#include <wx/filename.h>
30#include <import_export.h>
31
32class KIGIT_COMMON;
33
34namespace KIGIT
35{
36
39{
40public:
49 static git_repository* GetRepositoryForFile( const char* aFilename, wxString* aError = nullptr,
50 wxString* aTrustCommand = nullptr );
51
59 static int CreateBranch( git_repository* aRepo, const wxString& aBranchName );
60
69 static wxString GetCurrentHash( const wxString& aProjectFile, bool aShort );
70
81 static git_oid GetCapturedHeadOid( git_repository* aRepo );
82
93 static bool RemoveVCS( git_repository*& aRepo, const wxString& aProjectPath = wxEmptyString,
94 bool aRemoveGitDir = false, wxString* aErrors = nullptr );
95
109 static wxString ComputeSymlinkPreservingWorkDir( const wxString& aUserProjectPath,
110 const wxString& aCanonicalWorkDir );
111
126 static bool IsWithinProjectPath( const wxString& aAbsPath, const wxString& aProjectPath );
127
135 static void SafeFreeRepository( std::shared_ptr<KIGIT_COMMON> aCommon, const std::string& aLabel );
136
138 static const std::vector<wxString>& TransientGitIgnorePatterns();
139
142 static bool IsTransientGitFile( const wxFileName& aFile );
143
150 static std::optional<wxString> ProjectPrefix( const wxString& aWorkDir, const wxString& aProjectPath );
151
155 static bool PathUsesGitLfs( git_repository* aRepo, const wxString& aPath, const git_oid* aCommit = nullptr );
156
163 static bool ProjectUsesGitLfs( const KIGIT_COMMON& aCommon, wxString* aExample = nullptr,
164 const git_oid* aCommit = nullptr );
165
170 static wxString LfsRefusal( KIGIT_COMMON* aCommon );
171
179 static bool WriteBlobToFile( git_repository* aRepo, const wxString& aRef, const wxString& aPath,
180 const wxString& aDestPath );
181
189 static bool WriteTreeToDir( git_repository* aRepo, const wxString& aRef, const wxString& aDestDir );
190};
191
192} // namespace KIGIT
193
194#endif // PROJECT_GIT_UTILS_H
Utility class with helper functions for project level git operations.
static bool WriteBlobToFile(git_repository *aRepo, const wxString &aRef, const wxString &aPath, const wxString &aDestPath)
Write a file as it exists at a git ref to a path on disk, so a committed version can be diffed.
static bool PathUsesGitLfs(git_repository *aRepo, const wxString &aPath, const git_oid *aCommit=nullptr)
Whether git routes aPath through the Git LFS filter.
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, wxString *aError=nullptr, wxString *aTrustCommand=nullptr)
Discover and open the repository that contains the given file.
static bool IsTransientGitFile(const wxFileName &aFile)
Match a project-relative path against the shared transient patterns.
static const std::vector< wxString > & TransientGitIgnorePatterns()
Shared patterns for new .gitignore files and the untracked-file filter.
static void SafeFreeRepository(std::shared_ptr< KIGIT_COMMON > aCommon, const std::string &aLabel)
Free the session's repository handle without pulling it out from under a running operation.
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 WriteTreeToDir(git_repository *aRepo, const wxString &aRef, const wxString &aDestDir)
Check out a whole tree into a directory, leaving the repository's working copy and index alone.
static bool ProjectUsesGitLfs(const KIGIT_COMMON &aCommon, wxString *aExample=nullptr, const git_oid *aCommit=nullptr)
Whether any committed file in the project's folder goes through Git LFS.
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 wxString LfsRefusal(KIGIT_COMMON *aCommon)
Return a refusal message for LFS files or an unknown project folder, otherwise empty.
static int CreateBranch(git_repository *aRepo, const wxString &aBranchName)
Create a new branch based on HEAD.
static std::optional< wxString > ProjectPrefix(const wxString &aWorkDir, const wxString &aProjectPath)
The project's folder inside the repository, ending in a slash.
The project's git session.
#define APIEXPORT
Macros which export functions from a DLL/DSO.