KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_git_repository.cpp
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 (C) 2023 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
25#include <confirm.h>
26
27#include <git2.h>
28#include <gestfich.h>
29
30#include <cerrno>
31#include <cstring>
32#include <fstream>
33
34#include <wx/clipbrd.h>
35#include <wx/msgdlg.h>
36#include <wx/regex.h>
37#include <wx/stdpaths.h>
38
39
40DIALOG_GIT_REPOSITORY::DIALOG_GIT_REPOSITORY( wxWindow* aParent, git_repository* aRepository,
41 wxString aURL ) :
43 m_repository( aRepository ),
44 m_prevFile( wxEmptyString ),
45 m_tested( 0 ),
46 m_failedTest( false ),
47 m_testError( wxEmptyString ),
48 m_tempRepo( false )
49{
50 m_txtURL->SetFocus();
51
52 if( !m_repository )
53 {
54 // Make a temporary repository to test the connection
55 m_tempRepo = true;
56 m_tempPath = wxFileName::CreateTempFileName( "kicadtestrepo" );
57
58 git_repository_init_options options;
59 git_repository_init_init_options( &options, GIT_REPOSITORY_INIT_OPTIONS_VERSION );
60 options.flags = GIT_REPOSITORY_INIT_MKPATH | GIT_REPOSITORY_INIT_NO_REINIT;
61 git_repository_init_ext( &m_repository, m_tempPath.ToStdString().c_str(), &options );
62 }
63
64 if( !aURL.empty() )
65 m_txtURL->SetValue( aURL );
66 else
68
69 if( !m_txtURL->GetValue().IsEmpty() )
71
74
75 Layout();
76}
77
79{
80 if( m_tempRepo )
81 {
82 git_repository_free( m_repository );
84 }
85}
86
87
89{
90 if( wxTheClipboard->Open() && wxTheClipboard->IsSupported( wxDF_TEXT ) )
91 {
92 wxString clipboardText;
93 wxTextDataObject textData;
94
95 if( wxTheClipboard->GetData( textData ) && !( clipboardText = textData.GetText() ).empty() )
96 {
97 if( std::get<0>( isValidHTTPS( clipboardText ) )
98 || std::get<0>( isValidSSH( clipboardText ) ) )
99 {
100 m_txtURL->SetValue( clipboardText );
101 }
102 }
103
104 wxTheClipboard->Close();
105 }
106
107 return false;
108}
109
110
112{
113 wxFileName sshKey;
114 sshKey.SetPath( wxGetUserHome() );
115 wxString retval;
116
117 sshKey.AppendDir( ".ssh" );
118 sshKey.SetFullName( "id_rsa" );
119
120 if( sshKey.FileExists() )
121 {
122 retval = sshKey.GetFullPath();
123 }
124 else if( sshKey.SetFullName( "id_dsa" ); sshKey.FileExists() )
125 {
126 retval = sshKey.GetFullPath();
127 }
128 else if( sshKey.SetFullName( "id_ecdsa" ); sshKey.FileExists() )
129 {
130 retval = sshKey.GetFullPath();
131 }
132
133 if( !retval.empty() )
134 {
135 m_fpSSHKey->SetFileName( retval );
136 wxFileDirPickerEvent evt;
137 evt.SetPath( retval );
138 OnFileUpdated( evt );
139 }
140}
141
142
143void DIALOG_GIT_REPOSITORY::OnUpdateUI( wxUpdateUIEvent& event )
144{
145 // event.Enable( !m_txtName->GetValue().IsEmpty() && !m_txtURL->GetValue().IsEmpty() );
146}
147
148
150{
151 if( aEncrypted )
152 {
153 m_txtPassword->Enable();
154 m_txtPassword->SetToolTip( _( "Enter the password for the SSH key" ) );
155 }
156 else
157 {
158 m_txtPassword->SetValue( wxEmptyString );
159 m_txtPassword->SetToolTip( wxEmptyString );
160 m_txtPassword->Disable();
161 }
162}
163
164std::tuple<bool,wxString,wxString,wxString> DIALOG_GIT_REPOSITORY::isValidHTTPS( const wxString& url )
165{
166 wxRegEx regex( R"((https?:\/\/)(([^:]+)(:([^@]+))?@)?([^\/]+\/[^\s]+))" );
167
168 if( regex.Matches( url ) )
169 {
170 wxString username = regex.GetMatch( url, 3 );
171 wxString password = regex.GetMatch( url, 5 );
172 wxString repoAddress = regex.GetMatch( url, 1 ) + regex.GetMatch( url, 6 );
173 return std::make_tuple( true, username, password, repoAddress );
174 }
175
176 return std::make_tuple( false, "", "", "" );
177}
178
179
180std::tuple<bool,wxString, wxString> DIALOG_GIT_REPOSITORY::isValidSSH( const wxString& url )
181{
182 wxRegEx regex( R"((?:ssh:\/\/)?([^@]+)@([^\/]+\/[^\s]+))" );
183
184 if( regex.Matches( url ) )
185 {
186 wxString username = regex.GetMatch( url, 1 );
187 wxString repoAddress = regex.GetMatch( url, 2 );
188 return std::make_tuple( true, username, repoAddress );
189 }
190
191 return std::make_tuple( false, "", "" );
192}
193
194
195static wxString get_repo_name( wxString& aRepoAddr )
196{
197 wxString retval;
198 size_t last_slash = aRepoAddr.find_last_of( '/' );
199 bool ends_with_dot_git = aRepoAddr.EndsWith( ".git" );
200
201 if( ends_with_dot_git )
202 retval = aRepoAddr.substr( last_slash + 1, aRepoAddr.size() - last_slash - 5 );
203 else
204 retval = aRepoAddr.substr( last_slash + 1, aRepoAddr.size() - last_slash );
205
206 return retval;
207}
208
209
210void DIALOG_GIT_REPOSITORY::OnLocationExit( wxFocusEvent& event )
211{
214 event.Skip();
215}
216
217
219{
220 wxString url = m_txtURL->GetValue();
221
222 if( url.IsEmpty() )
223 return;
224
225 if( url.Contains( "https://" ) || url.Contains( "http://" ) )
226 {
227 auto [valid, username, password, repoAddress] = isValidHTTPS( url );
228
229 if( valid )
230 {
231 m_ConnType->SetSelection( static_cast<int>( KIGIT_COMMON::GIT_CONN_TYPE::GIT_CONN_HTTPS ) );
232 SetUsername( username );
233 SetPassword( password );
234 m_txtURL->SetValue( repoAddress );
235
236 if( m_txtName->GetValue().IsEmpty() )
237 m_txtName->SetValue( get_repo_name( repoAddress ) );
238
239 }
240 }
241 else if( url.Contains( "ssh://" ) || url.Contains( "git@" ) )
242 {
243 auto [valid, username, repoAddress] = isValidSSH( url );
244
245 if( valid )
246 {
247 m_ConnType->SetSelection( static_cast<int>( KIGIT_COMMON::GIT_CONN_TYPE::GIT_CONN_SSH ) );
248 m_txtUsername->SetValue( username );
249 m_txtURL->SetValue( repoAddress );
250
251 if( m_txtName->GetValue().IsEmpty() )
252 m_txtName->SetValue( get_repo_name( repoAddress ) );
253
255 }
256 }
257}
258
259
260void DIALOG_GIT_REPOSITORY::OnTestClick( wxCommandEvent& event )
261{
262 git_remote* remote = nullptr;
263 git_remote_callbacks callbacks;
264 git_remote_init_callbacks( &callbacks, GIT_REMOTE_CALLBACKS_VERSION );
265
266 // We track if we have already tried to connect.
267 // If we have, the server may come back to offer another connection
268 // type, so we need to keep track of how many times we have tried.
269 m_tested = 0;
270
271 callbacks.credentials = []( git_cred** aOut, const char* aUrl, const char* aUsername,
272 unsigned int aAllowedTypes, void* aPayload ) -> int
273 {
274 DIALOG_GIT_REPOSITORY* dialog = static_cast<DIALOG_GIT_REPOSITORY*>( aPayload );
275
277 return GIT_PASSTHROUGH;
278
279 if( aAllowedTypes & GIT_CREDTYPE_USERNAME
280 && !( dialog->GetTested() & GIT_CREDTYPE_USERNAME ) )
281 {
282 wxString username = dialog->GetUsername().Trim().Trim( false );
283 git_cred_username_new( aOut, username.ToStdString().c_str() );
284 dialog->GetTested() |= GIT_CREDTYPE_USERNAME;
285 }
287 && ( aAllowedTypes & GIT_CREDTYPE_USERPASS_PLAINTEXT )
288 && !( dialog->GetTested() & GIT_CREDTYPE_USERPASS_PLAINTEXT ) )
289 {
290 wxString username = dialog->GetUsername().Trim().Trim( false );
291 wxString password = dialog->GetPassword().Trim().Trim( false );
292
293 git_cred_userpass_plaintext_new( aOut, username.ToStdString().c_str(),
294 password.ToStdString().c_str() );
295 dialog->GetTested() |= GIT_CREDTYPE_USERPASS_PLAINTEXT;
296 }
298 && ( aAllowedTypes & GIT_CREDTYPE_SSH_KEY )
299 && !( dialog->GetTested() & GIT_CREDTYPE_SSH_KEY ) )
300 {
301 // SSH key authentication
302 wxString sshKey = dialog->GetRepoSSHPath();
303 wxString sshPubKey = sshKey + ".pub";
304 wxString username = dialog->GetUsername().Trim().Trim( false );
305 wxString password = dialog->GetPassword().Trim().Trim( false );
306
307 git_cred_ssh_key_new( aOut, username.ToStdString().c_str(),
308 sshPubKey.ToStdString().c_str(), sshKey.ToStdString().c_str(),
309 password.ToStdString().c_str() );
310 dialog->GetTested() |= GIT_CREDTYPE_SSH_KEY;
311 }
312 else
313 {
314 return GIT_PASSTHROUGH;
315 }
316
317 return GIT_OK;
318 };
319
320 callbacks.payload = this;
321
322 wxString txtURL = m_txtURL->GetValue();
323 git_remote_create_with_fetchspec( &remote, m_repository, "origin", txtURL.ToStdString().c_str(),
324 "+refs/heads/*:refs/remotes/origin/*" );
325
326 if( git_remote_connect( remote, GIT_DIRECTION_FETCH, &callbacks, nullptr, nullptr ) != GIT_OK )
327 SetTestResult( true, git_error_last()->message );
328 else
329 SetTestResult( false, wxEmptyString );
330
331 git_remote_disconnect( remote );
332 git_remote_free( remote );
333
334 auto dlg = wxMessageDialog( this, wxEmptyString, _( "Test connection" ), wxOK | wxICON_INFORMATION );
335
336 if( !m_failedTest )
337 {
338 dlg.SetMessage( _( "Connection successful" ) );
339 }
340 else
341 {
342 dlg.SetMessage( wxString::Format( _( "Could not connect to '%s' " ), m_txtURL->GetValue() ) );
343 dlg.SetExtendedMessage( m_testError );
344 }
345
346 dlg.ShowModal();
347}
348
349
350void DIALOG_GIT_REPOSITORY::OnFileUpdated( wxFileDirPickerEvent& aEvent )
351{
352 wxString file = aEvent.GetPath();
353
354 if( file.ends_with( wxS( ".pub" ) ) )
355 file = file.Left( file.size() - 4 );
356
357 std::ifstream ifs( file.fn_str() );
358
359 if( !ifs.good() || !ifs.is_open() )
360 {
361 DisplayErrorMessage( this, wxString::Format( _( "Could not open private key '%s'" ), file ),
362 wxString::Format( "%s: %d", std::strerror( errno ), errno ) );
363 return;
364 }
365
366 std::string line;
367 std::getline( ifs, line );
368
369 bool isValid = ( line.find( "PRIVATE KEY" ) != std::string::npos );
370 bool isEncrypted = ( line.find( "ENCRYPTED" ) != std::string::npos );
371
372 if( !isValid )
373 {
374 DisplayErrorMessage( this, _( "Invalid SSH Key" ),
375 _( "The selected file is not a valid SSH private key" ) );
376 CallAfter( [this] { SetRepoSSHPath( m_prevFile ); } );
377 return;
378 }
379
380 if( isEncrypted )
381 {
382 m_txtPassword->Enable();
383 m_txtPassword->SetToolTip( _( "Enter the password for the SSH key" ) );
384 }
385 else
386 {
387 m_txtPassword->SetValue( wxEmptyString );
388 m_txtPassword->SetToolTip( wxEmptyString );
389 m_txtPassword->Disable();
390 }
391
392 ifs.close();
393
394 wxString pubFile = file + wxS( ".pub" );
395 std::ifstream pubIfs( pubFile.fn_str() );
396
397 if( !pubIfs.good() || !pubIfs.is_open() )
398 {
399 DisplayErrorMessage( this, wxString::Format( _( "Could not open public key '%s'" ),
400 file + ".pub" ),
401 wxString::Format( "%s: %d", std::strerror( errno ), errno ) );
402 aEvent.SetPath( wxEmptyString );
403 CallAfter( [this] { SetRepoSSHPath( m_prevFile ); } );
404 return;
405 }
406
407 m_prevFile = file;
408 pubIfs.close();
409}
410
411
412void DIALOG_GIT_REPOSITORY::OnOKClick( wxCommandEvent& event )
413{
414 // Save the repository details
415
416 if( m_txtName->GetValue().IsEmpty() )
417 {
418 DisplayErrorMessage( this, _( "Missing information" ),
419 _( "Please enter a name for the repository" ) );
420 return;
421 }
422
423 if( m_txtURL->GetValue().IsEmpty() )
424 {
425 DisplayErrorMessage( this, _( "Missing information" ),
426 _( "Please enter a URL for the repository" ) );
427 return;
428 }
429
430 EndModal( wxID_OK );
431}
432
433
435{
436 if( m_ConnType->GetSelection() == static_cast<int>( KIGIT_COMMON::GIT_CONN_TYPE::GIT_CONN_LOCAL ) )
437 {
438 m_panelAuth->Show( false );
439 }
440 else
441 {
442 m_panelAuth->Show( true );
443
444 if( m_ConnType->GetSelection() == static_cast<int>( KIGIT_COMMON::GIT_CONN_TYPE::GIT_CONN_SSH ) )
445 {
446 m_fpSSHKey->Show( true );
447 m_labelSSH->Show( true );
448 m_labelPass1->SetLabel( _( "SSH Key Password" ) );
449 }
450 else
451 {
452 m_fpSSHKey->Show( false );
453 m_labelSSH->Show( false );
454 m_labelPass1->SetLabel( _( "Password" ) );
456 }
457 }
458
459 Layout();
460}
461
462
463void DIALOG_GIT_REPOSITORY::OnSelectConnType( wxCommandEvent& event )
464{
466}
Class DIALOG_GIT_REPOSITORY_BASE.
void OnSelectConnType(wxCommandEvent &event) override
std::tuple< bool, wxString, wxString > isValidSSH(const wxString &url)
void SetRepoSSHPath(const wxString &aPath)
KIGIT_COMMON::GIT_CONN_TYPE GetRepoType() const
void OnFileUpdated(wxFileDirPickerEvent &event) override
void SetPassword(const wxString &aPassword)
wxString GetRepoSSHPath() const
void SetTestResult(bool aFailed, const wxString &aError)
void SetEncrypted(bool aEncrypted=true)
void OnLocationExit(wxFocusEvent &event) override
DIALOG_GIT_REPOSITORY(wxWindow *aParent, git_repository *aRepository, wxString aURL=wxEmptyString)
void OnTestClick(wxCommandEvent &event) override
wxString GetUsername() const
void OnOKClick(wxCommandEvent &event) override
void SetUsername(const wxString &aUsername)
std::tuple< bool, wxString, wxString, wxString > isValidHTTPS(const wxString &url)
void OnUpdateUI(wxUpdateUIEvent &event) override
wxString GetPassword() const
void SetupStandardButtons(std::map< int, wxString > aLabels={})
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:195
This file is part of the common library.
static wxString get_repo_name(wxString &aRepoAddr)
#define _(s)
bool RmDirRecursive(const wxString &aFileName, wxString *aErrors)
Removes the directory aDirName and all its contents including subdirectories and their files.
Definition: gestfich.cpp:326