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, you may find one here:
18 * http://www.gnu.org/licenses/gpl-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#ifndef _GIT_COMMON_H_
25#define _GIT_COMMON_H_
26
28
29#include <git2.h>
30#include <atomic>
31#include <mutex>
32#include <set>
33
34#include <wx/string.h>
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
84 enum class GIT_CONN_TYPE
85 {
90 };
91
92 wxString GetUsername() const { return m_username; }
93 wxString GetPassword() const { return m_password; }
95
96 void SetUsername( const wxString& aUsername ) { m_username = aUsername; }
97 void SetPassword( const wxString& aPassword ) { m_password = aPassword; }
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
113
114 wxString GetGitRootDirectory() const;
115
116 wxString GetRemotename() const;
117
119
121 {
122 if( m_nextPublicKey >= static_cast<int>( m_publicKeys.size() ) )
123 return wxEmptyString;
124
126 }
127
128 void SetRemote( const wxString& aRemote )
129 {
130 m_remote = aRemote;
132 }
133
134 int HandleSSHKeyAuthentication( git_cred** aOut, const wxString& aUsername );
135
136 int HandlePlaintextAuthentication( git_cred** aOut, const wxString& aUsername );
137
138 int HandleSSHAgentAuthentication( git_cred** aOut, const wxString& aUsername );
139
140 static wxString GetLastGitError()
141 {
142 const git_error* error = git_error_last();
143
144 if( error == nullptr )
145 return wxString( "No error" );
146
147 return wxString( error->message );
148 }
149
150 bool IsCancelled() const
151 {
152 return m_cancel.load();
153 }
154
155 void SetCancelled( bool aCancel )
156 {
157 m_cancel.store( aCancel );
158 }
159
160protected:
161 git_repository* m_repo;
162
164 wxString m_remote; // This is the full connection string
165 wxString m_hostname; // This is just the hostname without the protocol, username, or password
166 wxString m_username;
167 wxString m_password;
168
170
172
173 // Make git handlers friends so they can access the mutex
174 friend class GIT_PUSH_HANDLER;
175 friend class GIT_PULL_HANDLER;
176 friend class GIT_CLONE_HANDLER;
177 friend class PROJECT_TREE_PANE;
178
179private:
180 void updatePublicKeys();
182
183 std::vector<wxString> m_publicKeys;
185
186 std::atomic<bool> m_cancel; // Set to true when the user cancels an operation
187
188 // Create a dummy flag to tell if we have tested ssh agent credentials separately
189 // from the ssh key credentials
190 static const unsigned KIGIT_CREDENTIAL_SSH_AGENT = 1 << sizeof( m_testedTypes - 1 );
191};
192
193extern "C" int progress_cb( const char* str, int len, void* data );
194extern "C" void clone_progress_cb( const char* str, size_t len, size_t total, void* data );
195extern "C" int transfer_progress_cb( const git_transfer_progress* aStats, void* aPayload );
196extern "C" int update_cb( const char* aRefname, const git_oid* aFirst, const git_oid* aSecond,
197 void* aPayload );
198extern "C" int push_transfer_progress_cb( unsigned int aCurrent, unsigned int aTotal,
199 size_t aBytes, void* aPayload );
200extern "C" int push_update_reference_cb( const char* aRefname, const char* aStatus,
201 void* aPayload );
202
203extern "C" int fetchhead_foreach_cb( const char*, const char*,
204 const git_oid* aOID, unsigned int aIsMerge, void* aPayload );
205extern "C" int credentials_cb( git_cred** aOut, const char* aUrl, const char* aUsername,
206 unsigned int aAllowedTypes, void* aPayload );
207
208#endif // _GIT_COMMON_H_
wxString m_username
std::mutex m_gitActionMutex
git_repository * m_repo
std::vector< wxString > GetBranchNames() const
void updateConnectionType()
GIT_CONN_TYPE GetConnType() const
unsigned m_testedTypes
std::atomic< bool > m_cancel
static wxString GetLastGitError()
wxString GetCurrentBranchName() const
wxString GetGitRootDirectory() const
void SetSSHKey(const wxString &aSSHKey)
bool IsCancelled() const
static const unsigned KIGIT_CREDENTIAL_SSH_AGENT
int HandlePlaintextAuthentication(git_cred **aOut, const wxString &aUsername)
void SetUsername(const wxString &aUsername)
std::vector< wxString > GetProjectDirs()
Return a vector of project files in the repository.
wxString GetPassword() const
git_repository * GetRepo() const
wxString m_hostname
wxString m_password
wxString GetNextPublicKey()
std::pair< std::set< wxString >, std::set< wxString > > GetDifferentFiles() const
Return a pair of sets of files that differ locally from the remote repository The first set is files ...
std::vector< wxString > m_publicKeys
bool HasPushAndPullRemote() const
wxString GetUsername() const
GIT_CONN_TYPE m_connType
void UpdateCurrentBranchInfo()
bool HasLocalCommits() const
int HandleSSHAgentAuthentication(git_cred **aOut, const wxString &aUsername)
void SetCancelled(bool aCancel)
unsigned & TestedTypes()
void SetPassword(const wxString &aPassword)
wxString GetRemotename() const
void SetRemote(const wxString &aRemote)
void SetRepo(git_repository *aRepo)
int HandleSSHKeyAuthentication(git_cred **aOut, const wxString &aUsername)
PROJECT_TREE_PANE Window to display the tree files.
int fetchhead_foreach_cb(const char *, const char *, const git_oid *aOID, unsigned int aIsMerge, void *aPayload)
int push_update_reference_cb(const char *aRefname, const char *aStatus, void *aPayload)
int update_cb(const char *aRefname, const git_oid *aFirst, const git_oid *aSecond, void *aPayload)
int transfer_progress_cb(const git_transfer_progress *aStats, void *aPayload)
int credentials_cb(git_cred **aOut, const char *aUrl, const char *aUsername, unsigned int aAllowedTypes, void *aPayload)
void clone_progress_cb(const char *str, size_t len, size_t total, void *data)
int push_transfer_progress_cb(unsigned int aCurrent, unsigned int aTotal, size_t aBytes, void *aPayload)
int progress_cb(const char *str, int len, void *data)