KiCad PCB EDA Suite
Loading...
Searching...
No Matches
kicad_git_common.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 _GIT_COMMON_H_
21#define _GIT_COMMON_H_
22
24#include <import_export.h>
25
26#include <git2.h>
27#include <atomic>
28#include <functional>
29#include <mutex>
30#include <set>
31
32#include <wx/string.h>
33
34class LIBGIT_BACKEND;
35
37{
38
39public:
40 KIGIT_COMMON( git_repository* aRepo );
41 KIGIT_COMMON( const KIGIT_COMMON& aOther );
43
44 git_repository* GetRepo() const;
45
46 void SetRepo( git_repository* aRepo )
47 {
48 m_repo = aRepo;
49 }
50
51 wxString GetCurrentBranchName() const;
52
53 std::vector<wxString> GetBranchNames() const;
54
61 std::vector<wxString> GetProjectDirs();
62
68 std::pair<std::set<wxString>,std::set<wxString>> GetDifferentFiles() const;
69
70 enum class GIT_STATUS
71 {
74 GIT_STATUS_MODIFIED, // File changed but not committed to local repository
77 GIT_STATUS_BEHIND, // File changed in remote repository but not in local
78 GIT_STATUS_AHEAD, // File changed in local repository but not in remote
82 };
83
91
92 wxString GetUsername() const { return m_username; }
93 wxString GetPassword();
94 GIT_CONN_TYPE GetConnType() const;
95
96 void SetUsername( const wxString& aUsername ) { m_username = aUsername; m_secretFetched = false; }
97 void SetPassword( const wxString& aPassword ) { m_password = aPassword; m_secretFetched = true; }
98 void SetSSHKey( const wxString& aSSHKey );
99
100 // Holds a temporary variable that can be used by the authentication callback
101 // to remember which types of authentication have been tested so that we
102 // don't loop forever.
103 unsigned& TestedTypes() { return m_testedTypes; }
104
105 // Returns true if the repository has local commits that have not been pushed
106 bool HasLocalCommits() const;
107
108 // Returns true if the repository has a remote that can be pushed to pulled from
109 bool HasPushAndPullRemote() const;
110
111 // Updates the password and remote information for the repository given the current branch
112 void UpdateCurrentBranchInfo();
113
114 wxString GetGitRootDirectory() const;
115
116 wxString GetRemotename() const;
117
120 wxString GetRemoteNameOrDefault() const;
121
124 wxString GetUpstreamShorthand() const;
125
130 void SetProjectDir( const wxString& aProjectDir ) { m_projectDir = aProjectDir; }
131
136 wxString GetProjectDir() const;
137
139
141 {
142 if( m_nextPublicKey >= static_cast<int>( m_publicKeys.size() ) )
143 return wxEmptyString;
144
146 }
147
148 void SetRemote( const wxString& aRemote )
149 {
150 m_remote = aRemote;
151 m_password.clear();
152 m_secretFetched = false;
154 }
155
156 const wxString& GetRemote() const { return m_remote; }
157
158 int HandleSSHKeyAuthentication( git_cred** aOut, const wxString& aUsername );
159
160 int HandlePlaintextAuthentication( git_cred** aOut, const wxString& aUsername );
161
162 int HandleSSHAgentAuthentication( git_cred** aOut, const wxString& aUsername );
163
164 static wxString GetLastGitError()
165 {
166 const git_error* error = git_error_last();
167
168 if( error == nullptr )
169 return wxString( "No error" );
170
171 return wxString( error->message );
172 }
173
174 bool IsCancelled() const
175 {
176 return m_cancel.load();
177 }
178
179 void SetCancelled( bool aCancel )
180 {
181 m_cancel.store( aCancel );
182 }
183
184 bool WasAuthFailure() const { return m_authFailed; }
185 void ClearAuthFailure() { m_authFailed = false; }
186 void SetAuthFailure() { m_authFailed = true; }
187
188protected:
189 git_repository* m_repo;
190
191 wxString m_projectDir; // Project directory path preserving symlinks
192
194 wxString m_remote; // This is the full connection string
195 wxString m_hostname; // This is just the hostname without the protocol, username, or password
196 wxString m_username;
197 wxString m_password;
198
200
201 bool m_authFailed = false;
202
204
205 // Make git handlers friends so they can access the mutex
206 friend class GIT_PUSH_HANDLER;
207 friend class GIT_PULL_HANDLER;
208 friend class GIT_CLONE_HANDLER;
209 friend class LIBGIT_BACKEND;
210 friend class PROJECT_TREE_PANE;
211
212private:
213 void updatePublicKeys();
215
216 std::vector<wxString> m_publicKeys;
219
220 std::atomic<bool> m_cancel; // Set to true when the user cancels an operation
221
222 // Create a dummy flag to tell if we have tested ssh agent credentials separately
223 // from the ssh key credentials
224 static const unsigned KIGIT_CREDENTIAL_SSH_AGENT = 1 << sizeof( m_testedTypes - 1 );
225};
226
227extern "C" APIEXPORT int progress_cb( const char* str, int len, void* data );
228extern "C" APIEXPORT void clone_progress_cb( const char* str, size_t len, size_t total, void* data );
229extern "C" APIEXPORT int transfer_progress_cb( const git_transfer_progress* aStats, void* aPayload );
230extern "C" APIEXPORT int update_cb( const char* aRefname, const git_oid* aFirst, const git_oid* aSecond,
231 void* aPayload );
232extern "C" APIEXPORT int push_transfer_progress_cb( unsigned int aCurrent, unsigned int aTotal,
233 size_t aBytes, void* aPayload );
234extern "C" APIEXPORT int push_update_reference_cb( const char* aRefname, const char* aStatus,
235 void* aPayload );
236
237extern "C" APIEXPORT int fetchhead_foreach_cb( const char*, const char*,
238 const git_oid* aOID, unsigned int aIsMerge, void* aPayload );
239extern "C" APIEXPORT int credentials_cb( git_cred** aOut, const char* aUrl, const char* aUsername,
240 unsigned int aAllowedTypes, void* aPayload );
241
242
243namespace KIGIT
244{
245
258APIEXPORT git_tree* ResolveRefToTree( git_repository* aRepo, const wxString& aRef );
259
260
271APIEXPORT void CollectDiffDeltas( git_diff* aDiff,
272 const std::function<void( const git_diff_delta& )>& aCallback );
273
274} // namespace KIGIT
275
276
277#endif // _GIT_COMMON_H_
std::mutex m_gitActionMutex
git_repository * m_repo
const wxString & GetRemote() const
friend class PROJECT_TREE_PANE
unsigned m_testedTypes
std::atomic< bool > m_cancel
bool WasAuthFailure() const
static wxString GetLastGitError()
bool IsCancelled() const
static const unsigned KIGIT_CREDENTIAL_SSH_AGENT
void SetUsername(const wxString &aUsername)
git_repository * GetRepo() const
friend class GIT_CLONE_HANDLER
wxString GetNextPublicKey()
KIGIT_COMMON(git_repository *aRepo)
void SetProjectDir(const wxString &aProjectDir)
Set the project directory path, preserving any symlinks in the path.
std::vector< wxString > m_publicKeys
wxString m_projectDir
wxString GetUsername() const
GIT_CONN_TYPE m_connType
friend class GIT_PUSH_HANDLER
friend class GIT_PULL_HANDLER
friend class LIBGIT_BACKEND
void SetCancelled(bool aCancel)
unsigned & TestedTypes()
void SetPassword(const wxString &aPassword)
void SetRemote(const wxString &aRemote)
void SetRepo(git_repository *aRepo)
#define APIEXPORT
Macros which export functions from a DLL/DSO.
APIEXPORT int transfer_progress_cb(const git_transfer_progress *aStats, void *aPayload)
APIEXPORT int push_update_reference_cb(const char *aRefname, const char *aStatus, void *aPayload)
APIEXPORT int fetchhead_foreach_cb(const char *, const char *, const git_oid *aOID, unsigned int aIsMerge, void *aPayload)
APIEXPORT int credentials_cb(git_cred **aOut, const char *aUrl, const char *aUsername, unsigned int aAllowedTypes, void *aPayload)
APIEXPORT int push_transfer_progress_cb(unsigned int aCurrent, unsigned int aTotal, size_t aBytes, void *aPayload)
APIEXPORT int update_cb(const char *aRefname, const git_oid *aFirst, const git_oid *aSecond, void *aPayload)
APIEXPORT int progress_cb(const char *str, int len, void *data)
APIEXPORT void clone_progress_cb(const char *str, size_t len, size_t total, void *data)
git_tree * ResolveRefToTree(git_repository *aRepo, const wxString &aRef)
Resolve a string ref (branch name, short OID, full OID, tag) to its tree.
void CollectDiffDeltas(git_diff *aDiff, const std::function< void(const git_diff_delta &)> &aCallback)
Walk every delta in a computed diff, invoking aCallback once per delta.