37#include <wx/clipbrd.h>
40#include <wx/stdpaths.h>
46 m_repository( aRepository ),
47 m_prevFile( wxEmptyString ),
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 if( wxTheClipboard->Open() && wxTheClipboard->IsSupported( wxDF_TEXT ) )
97 wxString clipboardText;
98 wxTextDataObject textData;
100 if( wxTheClipboard->GetData( textData ) && !( clipboardText = textData.GetText() ).empty() )
103 || std::get<0>(
isValidSSH( clipboardText ) ) )
105 m_txtURL->SetValue( clipboardText );
109 wxTheClipboard->Close();
119 sshKey.SetPath( wxGetUserHome() );
122 sshKey.AppendDir(
".ssh" );
123 sshKey.SetFullName(
"id_rsa" );
125 if( sshKey.FileExists() )
127 retval = sshKey.GetFullPath();
129 else if( sshKey.SetFullName(
"id_dsa" ); sshKey.FileExists() )
131 retval = sshKey.GetFullPath();
133 else if( sshKey.SetFullName(
"id_ecdsa" ); sshKey.FileExists() )
135 retval = sshKey.GetFullPath();
138 if( !retval.empty() )
141 wxFileDirPickerEvent evt;
142 evt.SetPath( retval );
166 m_txtPassword->SetToolTip(
_(
"Enter the password for the SSH key" ) );
179 wxRegEx regex( R
"((https?:\/\/)(([^:]+)(:([^@]+))?@)?([^\/]+\/[^\s]+))" );
181 if( regex.Matches( url ) )
183 wxString username = regex.GetMatch( url, 3 );
184 wxString password = regex.GetMatch( url, 5 );
185 wxString repoAddress = regex.GetMatch( url, 1 ) + regex.GetMatch( url, 6 );
186 return std::make_tuple(
true, username, password, repoAddress );
189 return std::make_tuple(
false,
"",
"",
"" );
195 wxRegEx regex( R
"((?:ssh:\/\/)?([^@]+)@([^\/]+\/[^\s]+))" );
197 if( regex.Matches( url ) )
199 wxString username = regex.GetMatch( url, 1 );
200 wxString repoAddress = regex.GetMatch( url, 2 );
201 return std::make_tuple(
true, username, repoAddress );
204 return std::make_tuple(
false,
"",
"" );
211 size_t last_slash = aRepoAddr.find_last_of(
'/' );
212 bool ends_with_dot_git = aRepoAddr.EndsWith(
".git" );
214 if( ends_with_dot_git )
215 retval = aRepoAddr.substr( last_slash + 1, aRepoAddr.size() - last_slash - 5 );
217 retval = aRepoAddr.substr( last_slash + 1, aRepoAddr.size() - last_slash );
233 wxString url =
m_txtURL->GetValue();
238 if( url.Contains(
"https://" ) || url.Contains(
"http://" ) )
240 auto [valid, username, password, repoAddress] =
isValidHTTPS( url );
254 else if( url.Contains(
"ssh://" ) || url.Contains(
"git@" ) )
256 auto [valid, username, repoAddress] =
isValidSSH( url );
276 if(
m_txtURL->GetValue().Trim().Trim(
false ).IsEmpty() )
280 bool success =
false;
281 git_remote* remote =
nullptr;
282 git_remote_callbacks callbacks;
283 git_remote_init_callbacks( &callbacks, GIT_REMOTE_CALLBACKS_VERSION );
296 callbacks.payload = &repoMixin;
298 wxString txtURL =
m_txtURL->GetValue();
299 git_remote_create_with_fetchspec( &remote,
m_repository,
"origin", txtURL.mbc_str(),
300 "+refs/heads/*:refs/remotes/origin/*" );
303 if( git_remote_connect( remote, GIT_DIRECTION_FETCH, &callbacks,
nullptr,
nullptr ) == GIT_OK )
308 git_remote_disconnect( remote );
310 auto dlg = wxMessageDialog(
this, wxEmptyString,
_(
"Test Connection" ),
311 wxOK | wxICON_INFORMATION );
315 dlg.SetMessage(
_(
"Connection successful" ) );
319 dlg.SetMessage( wxString::Format(
_(
"Could not connect to '%s' " ),
321 dlg.SetExtendedMessage( error );
330 wxString file = aEvent.GetPath();
332 if( file.ends_with( wxS(
".pub" ) ) )
333 file = file.Left( file.size() - 4 );
335 std::ifstream ifs( file.fn_str() );
337 if( !ifs.good() || !ifs.is_open() )
340 wxString::Format(
"%s: %d", std::strerror( errno ), errno ) );
345 std::getline( ifs, line );
347 bool isValid = ( line.find(
"PRIVATE KEY" ) != std::string::npos );
348 bool isEncrypted = ( line.find(
"ENCRYPTED" ) != std::string::npos );
353 _(
"The selected file is not a valid SSH private key" ) );
361 m_txtPassword->SetToolTip(
_(
"Enter the password for the SSH key" ) );
372 wxString pubFile = file + wxS(
".pub" );
373 std::ifstream pubIfs( pubFile.fn_str() );
375 if( !pubIfs.good() || !pubIfs.is_open() )
379 wxString::Format(
"%s: %d", std::strerror( errno ), errno ) );
380 aEvent.SetPath( wxEmptyString );
394 if(
m_txtURL->GetValue().IsEmpty() )
397 _(
"Please enter a URL for the repository" ) );
Class DIALOG_GIT_REPOSITORY_BASE.
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.
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.