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#include <import_export.h>
29
30#include <git2.h>
31#include <atomic>
32#include <mutex>
33#include <set>
34
35#include <wx/string.h>
36
37class LIBGIT_BACKEND;
38
40{
41
42public:
43 KIGIT_COMMON( git_repository* aRepo );
44 KIGIT_COMMON( const KIGIT_COMMON& aOther );
46
47 git_repository* GetRepo() const;
48
49 void SetRepo( git_repository* aRepo )
50 {
51 m_repo = aRepo;
52 }
53
54 wxString GetCurrentBranchName() const;
55
56 std::vector<wxString> GetBranchNames() const;
57
64 std::vector<wxString> GetProjectDirs();
65
71 std::pair<std::set<wxString>,std::set<wxString>> GetDifferentFiles() const;
72
73 enum class GIT_STATUS
74 {
77 GIT_STATUS_MODIFIED, // File changed but not committed to local repository
80 GIT_STATUS_BEHIND, // File changed in remote repository but not in local
81 GIT_STATUS_AHEAD, // File changed in local repository but not in remote
85 };
86
94
95 wxString GetUsername() const { return m_username; }
96 wxString GetPassword() const { return m_password; }
97 GIT_CONN_TYPE GetConnType() const;
98
99 void SetUsername( const wxString& aUsername ) { m_username = aUsername; }
100 void SetPassword( const wxString& aPassword ) { m_password = aPassword; }
101 void SetSSHKey( const wxString& aSSHKey );
102
103 // Holds a temporary variable that can be used by the authentication callback
104 // to remember which types of authentication have been tested so that we
105 // don't loop forever.
106 unsigned& TestedTypes() { return m_testedTypes; }
107
108 // Returns true if the repository has local commits that have not been pushed
109 bool HasLocalCommits() const;
110
111 // Returns true if the repository has a remote that can be pushed to pulled from
112 bool HasPushAndPullRemote() const;
113
114 // Updates the password and remote information for the repository given the current branch
115 void UpdateCurrentBranchInfo();
116
117 wxString GetGitRootDirectory() const;
118
119 wxString GetRemotename() const;
120
122
124 {
125 if( m_nextPublicKey >= static_cast<int>( m_publicKeys.size() ) )
126 return wxEmptyString;
127
129 }
130
131 void SetRemote( const wxString& aRemote )
132 {
133 m_remote = aRemote;
135 }
136
137 int HandleSSHKeyAuthentication( git_cred** aOut, const wxString& aUsername );
138
139 int HandlePlaintextAuthentication( git_cred** aOut, const wxString& aUsername );
140
141 int HandleSSHAgentAuthentication( git_cred** aOut, const wxString& aUsername );
142
143 static wxString GetLastGitError()
144 {
145 const git_error* error = git_error_last();
146
147 if( error == nullptr )
148 return wxString( "No error" );
149
150 return wxString( error->message );
151 }
152
153 bool IsCancelled() const
154 {
155 return m_cancel.load();
156 }
157
158 void SetCancelled( bool aCancel )
159 {
160 m_cancel.store( aCancel );
161 }
162
163protected:
164 git_repository* m_repo;
165
167 wxString m_remote; // This is the full connection string
168 wxString m_hostname; // This is just the hostname without the protocol, username, or password
169 wxString m_username;
170 wxString m_password;
171
173
175
176 // Make git handlers friends so they can access the mutex
177 friend class GIT_PUSH_HANDLER;
178 friend class GIT_PULL_HANDLER;
179 friend class GIT_CLONE_HANDLER;
180 friend class LIBGIT_BACKEND;
181 friend class PROJECT_TREE_PANE;
182
183private:
184 void updatePublicKeys();
186
187 std::vector<wxString> m_publicKeys;
189
190 std::atomic<bool> m_cancel; // Set to true when the user cancels an operation
191
192 // Create a dummy flag to tell if we have tested ssh agent credentials separately
193 // from the ssh key credentials
194 static const unsigned KIGIT_CREDENTIAL_SSH_AGENT = 1 << sizeof( m_testedTypes - 1 );
195};
196
197extern "C" APIEXPORT int progress_cb( const char* str, int len, void* data );
198extern "C" APIEXPORT void clone_progress_cb( const char* str, size_t len, size_t total, void* data );
199extern "C" APIEXPORT int transfer_progress_cb( const git_transfer_progress* aStats, void* aPayload );
200extern "C" APIEXPORT int update_cb( const char* aRefname, const git_oid* aFirst, const git_oid* aSecond,
201 void* aPayload );
202extern "C" APIEXPORT int push_transfer_progress_cb( unsigned int aCurrent, unsigned int aTotal,
203 size_t aBytes, void* aPayload );
204extern "C" APIEXPORT int push_update_reference_cb( const char* aRefname, const char* aStatus,
205 void* aPayload );
206
207extern "C" APIEXPORT int fetchhead_foreach_cb( const char*, const char*,
208 const git_oid* aOID, unsigned int aIsMerge, void* aPayload );
209extern "C" APIEXPORT int credentials_cb( git_cred** aOut, const char* aUrl, const char* aUsername,
210 unsigned int aAllowedTypes, void* aPayload );
211
212#endif // _GIT_COMMON_H_
std::mutex m_gitActionMutex
git_repository * m_repo
friend class PROJECT_TREE_PANE
unsigned m_testedTypes
std::atomic< bool > m_cancel
static wxString GetLastGitError()
bool IsCancelled() const
static const unsigned KIGIT_CREDENTIAL_SSH_AGENT
void SetUsername(const wxString &aUsername)
wxString GetPassword() const
git_repository * GetRepo() const
friend class GIT_CLONE_HANDLER
wxString GetNextPublicKey()
KIGIT_COMMON(git_repository *aRepo)
std::vector< wxString > m_publicKeys
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)