37#include <wx/clipbrd.h>
40#include <wx/stdpaths.h>
56 m_tempPath = wxFileName::CreateTempFileName(
"kicadtestrepo" );
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;
69 if( !
m_txtURL->GetValue().IsEmpty() )
82 [
this]( wxCommandEvent& aEvent )
103 wxClipboardLocker lock;
107 if( !wxTheClipboard->IsSupported( wxDF_TEXT ) )
110 wxTextDataObject textData;
111 if( !wxTheClipboard->GetData( textData ) )
114 wxString clipboardText = textData.GetText();
115 if( clipboardText.empty() )
119 || std::get<0>(
isValidSSH( clipboardText ) ) )
121 m_txtURL->SetValue( clipboardText );
131 sshKey.SetPath( wxGetUserHome() );
134 sshKey.AppendDir(
".ssh" );
135 sshKey.SetFullName(
"id_rsa" );
137 if( sshKey.FileExists() )
139 retval = sshKey.GetFullPath();
141 else if( sshKey.SetFullName(
"id_dsa" ); sshKey.FileExists() )
143 retval = sshKey.GetFullPath();
145 else if( sshKey.SetFullName(
"id_ecdsa" ); sshKey.FileExists() )
147 retval = sshKey.GetFullPath();
150 if( !retval.empty() )
153 wxFileDirPickerEvent evt;
154 evt.SetPath( retval );
178 m_txtPassword->SetToolTip(
_(
"Enter the password for the SSH key" ) );
191 wxRegEx regex( R
"((https?:\/\/)(([^:]+)(:([^@]+))?@)?([^\/]+\/[^\s]+))" );
193 if( regex.Matches( url ) )
195 wxString username = regex.GetMatch( url, 3 );
196 wxString password = regex.GetMatch( url, 5 );
197 wxString repoAddress = regex.GetMatch( url, 1 ) + regex.GetMatch( url, 6 );
198 return std::make_tuple(
true, username, password, repoAddress );
201 return std::make_tuple(
false,
"",
"",
"" );
207 wxRegEx regex( R
"((?:ssh:\/\/)?([^@]+)@([^\/]+\/[^\s]+))" );
209 if( regex.Matches( url ) )
211 wxString username = regex.GetMatch( url, 1 );
212 wxString repoAddress = regex.GetMatch( url, 2 );
213 return std::make_tuple(
true, username, repoAddress );
216 return std::make_tuple(
false,
"",
"" );
222 wxString addr = aRepoAddr;
227 static wxRegEx webSuffix(
228 R
"((/-)?/(tree|blob|commits?|raw|releases|tags|branches|pulls|pull|issues|merge_requests|wiki|actions)/.*$)",
231 webSuffix.ReplaceAll( &addr, wxEmptyString );
233 while( addr.EndsWith(
"/" ) )
236 if( addr.EndsWith(
".git" ) )
237 addr.RemoveLast( 4 );
239 size_t last_slash = addr.find_last_of(
'/' );
241 if( last_slash == wxString::npos )
244 return addr.substr( last_slash + 1 );
258 wxString url =
m_txtURL->GetValue();
263 if( url.Contains(
"https://" ) || url.Contains(
"http://" ) )
265 auto [valid, username, password, repoAddress] =
isValidHTTPS( url );
273 m_txtURL->ChangeValue( repoAddress );
278 else if( url.Contains(
"ssh://" ) || url.Contains(
"git@" ) )
280 auto [valid, username, repoAddress] =
isValidSSH( url );
287 m_txtURL->ChangeValue( repoAddress );
300 size_t colonPos = url.find(
':' );
301 size_t slashPos = url.find(
'/' );
303 if( colonPos != wxString::npos && ( slashPos == wxString::npos || colonPos < slashPos ) )
316 if(
m_txtURL->GetValue().Trim().Trim(
false ).IsEmpty() )
320 bool success =
false;
321 git_remote* remote =
nullptr;
322 git_remote_callbacks callbacks;
323 git_remote_init_callbacks( &callbacks, GIT_REMOTE_CALLBACKS_VERSION );
338 callbacks.payload = &repoMixin;
340 git_proxy_options proxyOpts;
341 git_proxy_init_options( &proxyOpts, GIT_PROXY_OPTIONS_VERSION );
342 proxyOpts.type = GIT_PROXY_AUTO;
344 git_remote_create_anonymous( &remote,
m_repository, fullURL.mbc_str() );
347 if( git_remote_connect( remote, GIT_DIRECTION_FETCH, &callbacks, &proxyOpts,
nullptr ) == GIT_OK )
352 git_remote_disconnect( remote );
355 wxOK | wxICON_INFORMATION );
359 dlg.SetMessage(
_(
"Connection successful" ) );
363 dlg.SetMessage( wxString::Format(
_(
"Could not connect to '%s' " ),
365 dlg.SetExtendedMessage( error );
374 wxString file = aEvent.GetPath();
376 if( file.ends_with( wxS(
".pub" ) ) )
377 file = file.Left( file.size() - 4 );
379 std::ifstream ifs( file.fn_str() );
381 if( !ifs.good() || !ifs.is_open() )
384 wxString::Format(
"%s: %d", std::strerror( errno ), errno ) );
389 std::getline( ifs, line );
391 bool isValid = ( line.find(
"PRIVATE KEY" ) != std::string::npos );
392 bool isEncrypted = ( line.find(
"ENCRYPTED" ) != std::string::npos );
397 _(
"The selected file is not a valid SSH private key" ) );
405 m_txtPassword->SetToolTip(
_(
"Enter the password for the SSH key" ) );
416 wxString pubFile = file + wxS(
".pub" );
417 std::ifstream pubIfs( pubFile.fn_str() );
419 if( !pubIfs.good() || !pubIfs.is_open() )
423 wxString::Format(
"%s: %d", std::strerror( errno ), errno ) );
424 aEvent.SetPath( wxEmptyString );
438 if(
m_txtURL->GetValue().IsEmpty() )
441 _(
"Please enter a URL for the repository" ) );
DIALOG_GIT_REPOSITORY_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Git Repository"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxCAPTION|wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
wxTextCtrl * m_txtPassword
wxStaticText * m_labelPass1
wxTextCtrl * m_txtUsername
wxFilePickerCtrl * m_fpSSHKey
void OnSelectConnType(wxCommandEvent &event) override
std::tuple< bool, wxString, wxString > isValidSSH(const wxString &url)
void SetRepoSSHPath(const wxString &aPath)
void OnFileUpdated(wxFileDirPickerEvent &event) override
void SetPassword(const wxString &aPassword)
void onCbCustom(wxCommandEvent &event) override
~DIALOG_GIT_REPOSITORY() override
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
void OnOKClick(wxCommandEvent &event) override
void SetUsername(const wxString &aUsername)
std::tuple< bool, wxString, wxString, wxString > isValidHTTPS(const wxString &url)
void updateAuthControls()
bool extractClipboardData()
void OnUpdateUI(wxUpdateUIEvent &event) override
git_repository * m_repository
void SetupStandardButtons(std::map< int, wxString > aLabels={})
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
static wxString GetLastGitError()
void SetSSHKey(const wxString &aSSHKey)
void SetUsername(const wxString &aUsername)
void SetPassword(const wxString &aPassword)
void SetRemote(const wxString &aRemote)
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
This file is part of the common library.
#define KICAD_MESSAGE_DIALOG
static wxString get_repo_name(wxString &aRepoAddr)
bool RmDirRecursive(const wxString &aFileName, wxString *aErrors)
Remove the directory aDirName and all its contents including subdirectories and their files.
int credentials_cb(git_cred **aOut, const char *aUrl, const char *aUsername, unsigned int aAllowedTypes, void *aPayload)
std::unique_ptr< git_remote, decltype([](git_remote *aRemote) { git_remote_free(aRemote); })> GitRemotePtr
A unique pointer for git_remote objects with automatic cleanup.