35#include <wx/clipbrd.h>
38#include <wx/stdpaths.h>
55 m_tempPath = wxFileName::CreateTempFileName(
"kicadtestrepo" );
60 git_repository_init_options options;
61 git_repository_init_init_options( &options, GIT_REPOSITORY_INIT_OPTIONS_VERSION );
62 options.flags = GIT_REPOSITORY_INIT_MKPATH | GIT_REPOSITORY_INIT_NO_REINIT;
74 [
this]( wxCommandEvent& aEvent )
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() )
113 && clipboardText.find_first_of(
" \t\r\n" ) == wxString::npos )
115 m_txtURL->SetValue( clipboardText );
125 std::vector<wxString> keys =
135 wxFileDirPickerEvent evt;
136 evt.SetPath( keys.front() );
159 m_txtPassword->SetToolTip(
_(
"Enter the password for the SSH key" ) );
172 wxString addr = aRepoAddr;
177 static wxRegEx webSuffix(
178 R
"((/-)?/(tree|blob|commits?|raw|releases|tags|branches|pulls|pull|issues|merge_requests|wiki|actions)/.*$)",
181 webSuffix.ReplaceAll( &addr, wxEmptyString );
183 while( addr.EndsWith(
"/" ) )
186 if( addr.EndsWith(
".git" ) )
187 addr.RemoveLast( 4 );
189 size_t last_slash = addr.find_last_of(
"/\\" );
191 if( last_slash == wxString::npos )
192 return addr.AfterLast(
':' );
194 return addr.substr( last_slash + 1 );
208 wxString url =
m_txtURL->GetValue();
227 if( parsed.
url != url )
243 bool success =
false;
244 git_remote* remote =
nullptr;
245 git_remote_callbacks callbacks;
246 git_remote_init_callbacks( &callbacks, GIT_REMOTE_CALLBACKS_VERSION );
262 callbacks.payload = &repoMixin;
266 git_proxy_options proxyOpts;
267 git_proxy_init_options( &proxyOpts, GIT_PROXY_OPTIONS_VERSION );
268 proxyOpts.type = GIT_PROXY_AUTO;
277 if(
const char* remoteUrl = git_remote_url( remote ) )
280 bool retriedAfterTrust =
false;
285 result = git_remote_connect( remote, GIT_DIRECTION_FETCH, &callbacks, &proxyOpts,
nullptr );
290 git_remote_disconnect( remote );
297 git_remote_disconnect( remote );
310 retriedAfterTrust =
true;
332 wxOK | wxICON_INFORMATION );
338static bool checkSSHKey( wxString aPath, wxString& aError, wxString& aDetails,
bool& aEncrypted )
340 if( aPath.ends_with( wxS(
".pub" ) ) )
341 aPath = aPath.Left( aPath.size() - 4 );
343 std::ifstream ifs( aPath.fn_str() );
345 if( !ifs.good() || !ifs.is_open() )
347 aError = wxString::Format(
_(
"Could not open private key '%s'" ), aPath );
348 aDetails = wxString::Format(
"%s: %d", std::strerror( errno ), errno );
352 std::string keyText( ( std::istreambuf_iterator<char>( ifs ) ), std::istreambuf_iterator<char>() );
353 wxString key = wxString::FromUTF8( keyText );
355 if( !key.BeforeFirst(
'\n' ).Contains( wxS(
"PRIVATE KEY" ) ) )
357 aError =
_(
"Invalid SSH Key" );
358 aDetails =
_(
"The selected file is not a valid SSH private key" );
364 aError = wxString::Format(
_(
"Could not open public key '%s'" ), aPath + wxS(
".pub" ) );
365 aDetails =
_(
"Put the key's .pub file beside it, or pick a key in OpenSSH format" );
378 bool encrypted =
false;
381 if(
checkSSHKey( aEvent.GetPath(), error, details, encrypted ) )
396 bool encrypted =
false;
409 git_remote* rawRemote =
nullptr;
411 if( git_remote_create_anonymous( &rawRemote, aRepo, aURL.utf8_str() ) != GIT_OK )
412 return wxString::Format(
_(
"'%s' is not a repository address." ), aURL );
417 wxString effective = wxString::FromUTF8( git_remote_url( remote.get() ) );
418 wxString checked = effective;
421 if( checked.Lower().StartsWith( wxS(
"git://" ) ) )
422 checked = wxS(
"https://" ) + checked.Mid( 6 );
428 const wxString& host = parsed.
hostname;
429 wxString authority = checked.AfterFirst(
':' ).Mid( 2 ).BeforeFirst(
'/' );
432 if( host.IsEmpty() || host.find_first_of( wxS(
" \t" ) ) != wxString::npos
433 || ( checked.Contains( wxS(
"://" ) ) && authority.Contains( wxS(
"[" ) )
434 && !authority.Contains( wxS(
"]" ) ) ) )
436 return wxString::Format(
_(
"'%s' does not name a server." ), aURL );
439 if( parsed.
port < 0 )
440 return wxString::Format(
_(
"The port in '%s' is not a number." ), aURL );
442 return wxEmptyString;
445 wxString lower = effective.Lower();
447 if( lower.Contains( wxS(
"://" ) ) && !lower.StartsWith( wxS(
"file://" ) ) )
448 return wxString::Format(
_(
"'%s' is not an HTTPS, SSH or folder address." ), aURL );
450 return wxEmptyString;
458 if(
m_txtURL->GetValue().IsEmpty() )
461 _(
"Please enter a URL for the repository" ) );
467 if( !problem.IsEmpty() )
470 _(
"Use an address such as https://server/owner/project.git or "
471 "git@server:owner/project.git, or the path of a repository folder." ) );
527 if( !
m_txtURL->GetValue().IsEmpty() )
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
KIGIT_COMMON::GIT_CONN_TYPE GetRepoType() const
void OnFileUpdated(wxFileDirPickerEvent &event) override
void SetPassword(const wxString &aPassword)
void onCbCustom(wxCommandEvent &event) override
wxString GetRepoSSHPath() const
The private key, also when its .pub was picked.
~DIALOG_GIT_REPOSITORY() override
void SetEncrypted(bool aEncrypted=true)
void OnLocationExit(wxFocusEvent &event) override
bool reportUnusableKey()
Show why the chosen SSH key cannot be used. False when there is no such problem.
DIALOG_GIT_REPOSITORY(wxWindow *aParent, git_repository *aRepository, wxString aURL=wxEmptyString)
void setDefaultSSHKey(const wxString &aHost)
void OnTestClick(wxCommandEvent &event) override
wxString GetRepoURL() const
wxString GetUsername() const
void OnOKClick(wxCommandEvent &event) override
void SetUsername(const wxString &aUsername)
virtual bool TransferDataToWindow() override
void updateAuthControls()
bool extractClipboardData()
void OnUpdateUI(wxUpdateUIEvent &event) override
git_repository * m_repository
void OptOut(wxWindow *aWindow)
Opt out of control state saving.
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...
The project's git session.
void FollowRewrittenUrl(const wxString &aRawUrl)
Bind the session to a URL that url.insteadOf rewrote, keeping the username, password and key entered ...
void SetSSHKey(const wxString &aSSHKey)
void SetUsername(const wxString &aUsername)
static KIGIT_ERROR_INFO GetLastGitErrorInfo(int aCode)
The error libgit2 holds for a call that returned aCode.
static REMOTE_URL ParseRemoteURL(const wxString &aURL)
void SetPassword(const wxString &aPassword)
void SetRemote(const wxString &aRemote)
std::optional< KIGIT::HOST_KEY_FAILURE > GetHostKeyFailure() const
The host key this operation's transfer refused, for the UI to offer to trust or to report.
KIGIT_FAILURE_REPORT ReportTransferFailure(const KIGIT_ERROR_INFO &aError, KIGIT_OPERATION aOperation, const wxString &aRemoteName)
Classify a failed libgit2 transfer call and queue its summary and details.
void RememberWorkingSSHKey()
Call once the transfer has connected, so the key that worked is offered first next time.
void ResetTransferState()
Start a transfer with nothing left from an earlier one on this handler or session: no keys or methods...
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 bool checkSSHKey(wxString aPath, wxString &aError, wxString &aDetails, bool &aEncrypted)
static wxString get_repo_name(const wxString &aRepoAddr)
static wxString describeUnusableURL(git_repository *aRepo, const wxString &aURL)
bool RmDirRecursive(const wxString &aFileName, wxString *aErrors)
Remove the directory aDirName and all its contents including subdirectories and their files.
int certificate_check_cb(git_cert *aCert, int aValid, const char *aHost, void *aPayload)
Check an SSH host key against known_hosts, which decides alone.
int credentials_cb(git_cred **aOut, const char *aUrl, const char *aUsername, unsigned int aAllowedTypes, void *aPayload)
bool IsPrivateKeyEncrypted(const wxString &aKeyText)
Report whether an SSH private key is protected by a passphrase.
APIEXPORT wxString EnsurePublicKeyFile(const wxString &aKey)
PublicKeyPath for aKey, first writing a public key file to KiCad's cache when the key has no ....
wxString ConfiguredSSHCommand(git_repository *aRepo)
git's core.sshCommand for aRepo, or from the user's git config when aRepo is null....
bool PromptToTrustHost(wxWindow *aParent, const HOST_KEY_FAILURE &aFailure, bool aRetriedAfterTrust)
Ask whether to trust a server seen for the first time, and remember its key when the user agrees.
APIEXPORT std::vector< wxString > DefaultSSHKeyCandidates(const wxString &aHomeDir, const wxString &aHost, const wxString &aSSHCommand=wxEmptyString)
Private keys to offer aHost, best first.
APIEXPORT wxString HomeDir()
The user's home folder, where ssh looks for its config and default keys: the first candidate with a ....
APIEXPORT wxString SSHKeyFolder()
The folder ssh keeps keys in, for a file picker to open at. Empty when the home folder is unknown.
APIEXPORT KIGIT_FAILURE_REPORT DescribeGitFailure(const KIGIT_ERROR_INFO &aError, const KIGIT_AUTH_ATTEMPT &aAuth, const KIGIT_FAILURE_CONTEXT &aContext)
Turn a failed transfer into a message the user can act on.
std::unique_ptr< git_remote, decltype([](git_remote *aRemote) { git_remote_free(aRemote); })> GitRemotePtr
A unique pointer for git_remote objects with automatic cleanup.
A transport address with any embedded credentials extracted.
int port
0 when the address names none.
wxString url
Keeps the SSH username, scheme and port, but never a password.
What the failed operation was doing, as far as the message needs it.
KIGIT_OPERATION operation
wxString url
The address connected to. Messages show only its host and its stripped form.
wxString summary
What happened and what to do next, in the user's terms. Never raw text.
wxString details
The raw error, the server's text and the address without its password.
wxString result
Test unit parsing edge cases and error handling.