36#include <wx/clipbrd.h>
39#include <wx/stdpaths.h>
56 m_tempPath = wxFileName::CreateTempFileName(
"kicadtestrepo" );
61 git_repository_init_options options;
62 git_repository_init_init_options( &options, GIT_REPOSITORY_INIT_OPTIONS_VERSION );
63 options.flags = GIT_REPOSITORY_INIT_MKPATH | GIT_REPOSITORY_INIT_NO_REINIT;
75 [
this]( wxCommandEvent& aEvent )
96 wxClipboardLocker lock;
100 if( !wxTheClipboard->IsSupported( wxDF_TEXT ) )
103 wxTextDataObject textData;
104 if( !wxTheClipboard->GetData( textData ) )
107 wxString clipboardText = textData.GetText();
108 if( clipboardText.empty() )
114 && clipboardText.find_first_of(
" \t\r\n" ) == wxString::npos )
116 m_txtURL->SetValue( clipboardText );
126 std::vector<wxString> keys =
136 wxFileDirPickerEvent evt;
137 evt.SetPath( keys.front() );
160 m_txtPassword->SetToolTip(
_(
"Enter the password for the SSH key" ) );
173 wxString addr = aRepoAddr;
178 static wxRegEx webSuffix(
179 R
"((/-)?/(tree|blob|commits?|raw|releases|tags|branches|pulls|pull|issues|merge_requests|wiki|actions)/.*$)",
182 webSuffix.ReplaceAll( &addr, wxEmptyString );
184 while( addr.EndsWith(
"/" ) )
187 if( addr.EndsWith(
".git" ) )
188 addr.RemoveLast( 4 );
190 size_t last_slash = addr.find_last_of(
"/\\" );
192 if( last_slash == wxString::npos )
193 return addr.AfterLast(
':' );
195 return addr.substr( last_slash + 1 );
209 wxString url =
m_txtURL->GetValue();
228 if( parsed.
url != url )
244 bool success =
false;
245 git_remote* remote =
nullptr;
264 if(
const char* remoteUrl = git_remote_url( remote ) )
268 bool retriedAfterTrust =
false;
272 if( transport.
Check() == GIT_OK )
292 retriedAfterTrust =
true;
314 wxOK | wxICON_INFORMATION );
320static bool checkSSHKey( wxString aPath, wxString& aError, wxString& aDetails,
bool& aEncrypted )
322 if( aPath.ends_with( wxS(
".pub" ) ) )
323 aPath = aPath.Left( aPath.size() - 4 );
325 std::ifstream ifs( aPath.fn_str() );
327 if( !ifs.good() || !ifs.is_open() )
329 aError = wxString::Format(
_(
"Could not open private key '%s'" ), aPath );
330 aDetails = wxString::Format(
"%s: %d", std::strerror( errno ), errno );
334 std::string keyText( ( std::istreambuf_iterator<char>( ifs ) ), std::istreambuf_iterator<char>() );
335 wxString key = wxString::FromUTF8( keyText );
337 if( !key.BeforeFirst(
'\n' ).Contains( wxS(
"PRIVATE KEY" ) ) )
339 aError =
_(
"Invalid SSH Key" );
340 aDetails =
_(
"The selected file is not a valid SSH private key" );
346 aError = wxString::Format(
_(
"Could not open public key '%s'" ), aPath + wxS(
".pub" ) );
347 aDetails =
_(
"Put the key's .pub file beside it, or pick a key in OpenSSH format" );
360 bool encrypted =
false;
363 if(
checkSSHKey( aEvent.GetPath(), error, details, encrypted ) )
378 bool encrypted =
false;
391 git_remote* rawRemote =
nullptr;
393 if( git_remote_create_anonymous( &rawRemote, aRepo, aURL.utf8_str() ) != GIT_OK )
394 return wxString::Format(
_(
"'%s' is not a repository address." ), aURL );
399 wxString effective = wxString::FromUTF8( git_remote_url( remote.get() ) );
400 wxString checked = effective;
403 if( checked.Lower().StartsWith( wxS(
"git://" ) ) )
404 checked = wxS(
"https://" ) + checked.Mid( 6 );
410 const wxString& host = parsed.
hostname;
411 wxString authority = checked.AfterFirst(
':' ).Mid( 2 ).BeforeFirst(
'/' );
414 if( host.IsEmpty() || host.find_first_of( wxS(
" \t" ) ) != wxString::npos
415 || ( checked.Contains( wxS(
"://" ) ) && authority.Contains( wxS(
"[" ) )
416 && !authority.Contains( wxS(
"]" ) ) ) )
418 return wxString::Format(
_(
"'%s' does not name a server." ), aURL );
421 if( parsed.
port < 0 )
422 return wxString::Format(
_(
"The port in '%s' is not a number." ), aURL );
424 return wxEmptyString;
427 wxString lower = effective.Lower();
429 if( lower.Contains( wxS(
"://" ) ) && !lower.StartsWith( wxS(
"file://" ) ) )
430 return wxString::Format(
_(
"'%s' is not an HTTPS, SSH or folder address." ), aURL );
432 return wxEmptyString;
440 if(
m_txtURL->GetValue().IsEmpty() )
443 _(
"Please enter a URL for the repository" ) );
449 if( !problem.IsEmpty() )
452 _(
"Use an address such as https://server/owner/project.git or "
453 "git@server:owner/project.git, or the path of a repository folder." ) );
509 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...
const KIGIT_ERROR_INFO & Error() const
libgit2's error for the last call that failed, read before anything else could replace it
int Check()
Connect, which proves the credentials and the host key without transferring anything.
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 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.
The one place a fetch, push, clone or connection check reaches a remote.
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....
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.
PROMPT_ANSWER 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 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.