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() )
95 wxClipboardLocker lock;
99 if( !wxTheClipboard->IsSupported( wxDF_TEXT ) )
102 wxTextDataObject textData;
103 if( !wxTheClipboard->GetData( textData ) )
106 wxString clipboardText = textData.GetText();
107 if( clipboardText.empty() )
111 || std::get<0>(
isValidSSH( clipboardText ) ) )
113 m_txtURL->SetValue( clipboardText );
123 sshKey.SetPath( wxGetUserHome() );
126 sshKey.AppendDir(
".ssh" );
127 sshKey.SetFullName(
"id_rsa" );
129 if( sshKey.FileExists() )
131 retval = sshKey.GetFullPath();
133 else if( sshKey.SetFullName(
"id_dsa" ); sshKey.FileExists() )
135 retval = sshKey.GetFullPath();
137 else if( sshKey.SetFullName(
"id_ecdsa" ); sshKey.FileExists() )
139 retval = sshKey.GetFullPath();
142 if( !retval.empty() )
145 wxFileDirPickerEvent evt;
146 evt.SetPath( retval );
170 m_txtPassword->SetToolTip(
_(
"Enter the password for the SSH key" ) );
183 wxRegEx regex( R
"((https?:\/\/)(([^:]+)(:([^@]+))?@)?([^\/]+\/[^\s]+))" );
185 if( regex.Matches( url ) )
187 wxString username = regex.GetMatch( url, 3 );
188 wxString password = regex.GetMatch( url, 5 );
189 wxString repoAddress = regex.GetMatch( url, 1 ) + regex.GetMatch( url, 6 );
190 return std::make_tuple(
true, username, password, repoAddress );
193 return std::make_tuple(
false,
"",
"",
"" );
199 wxRegEx regex( R
"((?:ssh:\/\/)?([^@]+)@([^\/]+\/[^\s]+))" );
201 if( regex.Matches( url ) )
203 wxString username = regex.GetMatch( url, 1 );
204 wxString repoAddress = regex.GetMatch( url, 2 );
205 return std::make_tuple(
true, username, repoAddress );
208 return std::make_tuple(
false,
"",
"" );
214 wxString addr = aRepoAddr;
219 static wxRegEx webSuffix(
220 R
"((/-)?/(tree|blob|commits?|raw|releases|tags|branches|pulls|pull|issues|merge_requests|wiki|actions)/.*$)",
223 webSuffix.ReplaceAll( &addr, wxEmptyString );
225 while( addr.EndsWith(
"/" ) )
228 if( addr.EndsWith(
".git" ) )
229 addr.RemoveLast( 4 );
231 size_t last_slash = addr.find_last_of(
'/' );
233 if( last_slash == wxString::npos )
236 return addr.substr( last_slash + 1 );
250 wxString url =
m_txtURL->GetValue();
255 if( url.Contains(
"https://" ) || url.Contains(
"http://" ) )
257 auto [valid, username, password, repoAddress] =
isValidHTTPS( url );
270 else if( url.Contains(
"ssh://" ) || url.Contains(
"git@" ) )
272 auto [valid, username, repoAddress] =
isValidSSH( url );
292 size_t colonPos = url.find(
':' );
293 size_t slashPos = url.find(
'/' );
295 if( colonPos != wxString::npos && ( slashPos == wxString::npos || colonPos < slashPos ) )
308 if(
m_txtURL->GetValue().Trim().Trim(
false ).IsEmpty() )
312 bool success =
false;
313 git_remote* remote =
nullptr;
314 git_remote_callbacks callbacks;
315 git_remote_init_callbacks( &callbacks, GIT_REMOTE_CALLBACKS_VERSION );
328 callbacks.payload = &repoMixin;
330 wxString txtURL =
m_txtURL->GetValue();
331 git_remote_create_with_fetchspec( &remote,
m_repository,
"origin", txtURL.mbc_str(),
332 "+refs/heads/*:refs/remotes/origin/*" );
335 if( git_remote_connect( remote, GIT_DIRECTION_FETCH, &callbacks,
nullptr,
nullptr ) == GIT_OK )
340 git_remote_disconnect( remote );
343 wxOK | wxICON_INFORMATION );
347 dlg.SetMessage(
_(
"Connection successful" ) );
351 dlg.SetMessage( wxString::Format(
_(
"Could not connect to '%s' " ),
353 dlg.SetExtendedMessage( error );
362 wxString file = aEvent.GetPath();
364 if( file.ends_with( wxS(
".pub" ) ) )
365 file = file.Left( file.size() - 4 );
367 std::ifstream ifs( file.fn_str() );
369 if( !ifs.good() || !ifs.is_open() )
372 wxString::Format(
"%s: %d", std::strerror( errno ), errno ) );
377 std::getline( ifs, line );
379 bool isValid = ( line.find(
"PRIVATE KEY" ) != std::string::npos );
380 bool isEncrypted = ( line.find(
"ENCRYPTED" ) != std::string::npos );
385 _(
"The selected file is not a valid SSH private key" ) );
393 m_txtPassword->SetToolTip(
_(
"Enter the password for the SSH key" ) );
404 wxString pubFile = file + wxS(
".pub" );
405 std::ifstream pubIfs( pubFile.fn_str() );
407 if( !pubIfs.good() || !pubIfs.is_open() )
411 wxString::Format(
"%s: %d", std::strerror( errno ), errno ) );
412 aEvent.SetPath( wxEmptyString );
426 if(
m_txtURL->GetValue().IsEmpty() )
429 _(
"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.