KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 <mutex>
31#include <set>
32
33#include <wx/string.h>
34
36{
37
38public:
39 KIGIT_COMMON( git_repository* aRepo );
40 KIGIT_COMMON( const KIGIT_COMMON& aOther );
42
43 git_repository* GetRepo() const;
44
45 void SetRepo( git_repository* aRepo )
46 {
47 m_repo = aRepo;
48 }
49
50 wxString GetCurrentBranchName() const;
51
52 std::vector<wxString> GetBranchNames() const;
53
60 std::vector<wxString> GetProjectDirs();
61
67 std::pair<std::set<wxString>,std::set<wxString>> GetDifferentFiles() const;
68
69 enum class GIT_STATUS
70 {
73 GIT_STATUS_MODIFIED, // File changed but not committed to local repository
76 GIT_STATUS_BEHIND, // File changed in remote repository but not in local
77 GIT_STATUS_AHEAD, // File changed in local repository but not in remote
81 };
82
83 enum class GIT_CONN_TYPE
84 {
89 };
90
91 wxString GetUsername() const { return m_username; }
92 wxString GetPassword() const { return m_password; }
94
95 void SetUsername( const wxString& aUsername ) { m_username = aUsername; }
96 void SetPassword( const wxString& aPassword ) { m_password = aPassword; }
97 void SetSSHKey( const wxString& aSSHKey );
98
99 // Holds a temporary variable that can be used by the authentication callback
100 // to remember which types of authentication have been tested so that we
101 // don't loop forever.
102 unsigned& TestedTypes() { return m_testedTypes; }
103
104 // Returns true if the repository has local commits that have not been pushed
105 bool HasLocalCommits() const;
106
107 // Returns true if the repository has a remote that can be pushed to pulled from
108 bool HasPushAndPullRemote() const;
109
110 // Updates the password and remote information for the repository given the current branch
112
113 wxString GetGitRootDirectory() const;
114
115 wxString GetRemotename() const;
116
118
120 {
121 if( m_nextPublicKey >= static_cast<int>( m_publicKeys.size() ) )
122 return wxEmptyString;
123
125 }
126
127 void SetRemote( const wxString& aRemote )
128 {
129 m_remote = aRemote;
131 }
132
133 int HandleSSHKeyAuthentication( git_cred** aOut, const wxString& aUsername );
134
135 int HandlePlaintextAuthentication( git_cred** aOut, const wxString& aUsername );
136
137 int HandleSSHAgentAuthentication( git_cred** aOut, const wxString& aUsername );
138
139 static wxString GetLastGitError()
140 {
141 const git_error* error = git_error_last();
142
143 if( error == nullptr )
144 return wxString( "No error" );
145
146 return wxString( error->message );
147 }
148
149protected:
150 git_repository* m_repo;
151
153 wxString m_remote; // This is the full connection string
154 wxString m_hostname; // This is just the hostname without the protocol, username, or password
155 wxString m_username;
156 wxString m_password;
157
159
161
162 // Make git handlers friends so they can access the mutex
163 friend class GIT_PUSH_HANDLER;
164 friend class GIT_PULL_HANDLER;
165 friend class GIT_CLONE_HANDLER;
166
167private:
168 void updatePublicKeys();
170
171 std::vector<wxString> m_publicKeys;
173
174 // Create a dummy flag to tell if we have tested ssh agent credentials separately
175 // from the ssh key credentials
176 static const unsigned KIGIT_CREDENTIAL_SSH_AGENT = 1 << sizeof( m_testedTypes - 1 );
177};
178
179extern "C" int progress_cb( const char* str, int len, void* data );
180extern "C" void clone_progress_cb( const char* str, size_t len, size_t total, void* data );
181extern "C" int transfer_progress_cb( const git_transfer_progress* aStats, void* aPayload );
182extern "C" int update_cb( const char* aRefname, const git_oid* aFirst, const git_oid* aSecond,
183 void* aPayload );
184extern "C" int push_transfer_progress_cb( unsigned int aCurrent, unsigned int aTotal,
185 size_t aBytes, void* aPayload );
186extern "C" int push_update_reference_cb( const char* aRefname, const char* aStatus,
187 void* aPayload );
188
189extern "C" int fetchhead_foreach_cb( const char*, const char*,
190 const git_oid* aOID, unsigned int aIsMerge, void* aPayload );
191extern "C" int credentials_cb( git_cred** aOut, const char* aUrl, const char* aUsername,
192 unsigned int aAllowedTypes, void* aPayload );
193
194#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
static wxString GetLastGitError()
wxString GetCurrentBranchName() const
wxString GetGitRootDirectory() const
void SetSSHKey(const wxString &aSSHKey)
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)
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)
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)