KiCad PCB EDA Suite
Loading...
Searching...
No Matches
kigit_failure.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include "kigit_failure.h"
21#include "git_repo_mixin.h"
22#include "kicad_git_compat.h"
23#include "kicad_git_memory.h"
24#include "kigit_ssh_keys.h"
25
26#include <trace_helpers.h>
27
28#include <git2.h>
29#include <wx/ffile.h>
30#include <wx/filename.h>
31#include <wx/log.h>
32#include <wx/regex.h>
33#include <wx/tokenzr.h>
34#include <wx/translation.h>
35
36#include <algorithm>
37#include <cstdlib>
38#include <filesystem>
39#include <memory>
40
41
42namespace
43{
44
45struct TEXT_PATTERN
46{
47 const char* text;
48 KIGIT_FAILURE kind;
49};
50
51
52// Resolver and connection texts libgit2 and libssh2 produce themselves, as captured from real failures. They are
53// English in every locale and never translated. Nothing a server sends is matched, only the codes it returns.
54const TEXT_PATTERN c_textPatterns[] = {
55 { "failed to resolve address for", KIGIT_FAILURE::SERVER_NOT_FOUND },
56 { "failed to connect to", KIGIT_FAILURE::HOST_UNREACHABLE },
57 { "failed getting banner", KIGIT_FAILURE::HOST_UNREACHABLE },
58 // WinHTTP's failures to reach a server. The system text after the prefix is localized and is not matched.
59 { "failed to send request", KIGIT_FAILURE::HOST_UNREACHABLE },
60 { "failed to receive response", KIGIT_FAILURE::HOST_UNREACHABLE },
61 { "error loading known_hosts", KIGIT_FAILURE::KNOWN_HOSTS_UNREADABLE },
62 { "unable to open public key file", KIGIT_FAILURE::KEY_LOAD_FAILED },
63 { "proxy returned unexpected status", KIGIT_FAILURE::PROXY },
64};
65
66
67// wxString::Lower follows the C library locale, which turns I into a dotless i in Turkish. The texts matched here are
68// ASCII, so only ASCII is folded.
69wxString asciiLower( const wxString& aText )
70{
71 wxString lower;
72
73 for( wxUniChar c : aText )
74 {
75 const wxUniChar::value_type value = c.GetValue();
76
77 lower += ( value >= 'A' && value <= 'Z' ) ? wxUniChar( value + ( 'a' - 'A' ) ) : c;
78 }
79
80 return lower;
81}
82
83
84// libgit2 and its WinHTTP transport word a refused request differently, but both end in the status
85int httpStatusOf( const KIGIT_ERROR_INFO& aError, const wxString& aLower )
86{
87 static const wxString marker = wxS( "status code: " );
88 const int at = aLower.Find( marker );
89
90 return ( aError.klass == GIT_ERROR_HTTP && at != wxNOT_FOUND ) ? wxAtoi( aLower.Mid( at + marker.length() ) ) : 0;
91}
92
93
94KIGIT_COMMON::GIT_CONN_TYPE connTypeOf( const KIGIT_AUTH_ATTEMPT& aAuth, const KIGIT_FAILURE_CONTEXT& aContext )
95{
97
98 // The prompt can choose a transport for an address that names none
100 connType = aAuth.connType;
101
102 return connType;
103}
104
105
106// The key to name in a rejection. The server accepted the public half of acceptedKey, so it is the one to fix
107wxString namedKey( const KIGIT_AUTH_ATTEMPT& aAuth )
108{
109 if( !aAuth.acceptedKey.IsEmpty() )
110 return aAuth.acceptedKey;
111
112 return aAuth.keysTried.empty() ? wxString() : aAuth.keysTried.back();
113}
114
115
116// Lines kept for the details, without git's own transfer progress, bounded to the end where a refusal explains itself.
117// Server text arrives framed by blank "error:" lines and progress carriage returns.
118wxString serverText( const wxString& aText )
119{
120 // The titles git's progress.c and pack-objects print, so a server's own "Error code: 403" survives
121 static thread_local const wxRegEx progress( wxS( "^(((Enumerating|Counting|Compressing|Writing|Receiving) objects"
122 "|Resolving deltas): .*|Total [0-9]+ \\(delta [0-9]+\\).*)$" ),
123 wxRE_ADVANCED );
124
125 wxArrayString lines;
126 wxStringTokenizer tokens( KIGIT::PrintableServerText( aText ), wxS( "\r\n" ), wxTOKEN_STRTOK );
127
128 while( tokens.HasMoreTokens() )
129 {
130 const wxString line = tokens.GetNextToken().Trim().Trim( false );
131
132 if( !line.IsEmpty() && line != wxS( "error:" ) && line != wxS( "remote:" ) && !progress.Matches( line ) )
133 lines.Add( line );
134 }
135
136 // Already scrubbed, so the cut cannot leave half a URL that still names its password
137 return wxJoin( lines, '\n', 0 ).Right( KIGIT::MAX_SERVER_TEXT );
138}
139
140} // namespace
141
142
144 const KIGIT_FAILURE_CONTEXT& aContext )
145{
146 const KIGIT_COMMON::GIT_CONN_TYPE connType = connTypeOf( aAuth, aContext );
147 const wxString lower = asciiLower( aError.message );
148
149 if( aError.code == GIT_EUSER )
151
152 if( aError.code == GIT_ENONFASTFORWARD )
154
155 if( aError.code == GIT_ECERTIFICATE )
156 {
157 if( aContext.hostKey )
158 {
159 KIGIT::KNOWN_HOST_STATE state = aContext.hostKey->state;
160
163
165 }
166
167 if( !aContext.hostKeyRefusal.IsEmpty() )
169
172 }
173
174 // git_remote_push reports a refused branch only through the callback, which fails the push with its status
175 if( !aContext.pushRefusal.IsEmpty() )
177
178 if( aError.code == GIT_EAUTH )
179 {
180 if( aError.klass == GIT_ERROR_HTTP && lower.Contains( wxS( "proxy" ) ) )
182
185
186 // An empty or absent agent fails exactly as a rejected agent key does, so neither is claimed
189
191 }
192
193 // libgit2 returns any SSH stderr as GIT_EEOF when stdout is empty. The whole message is then the server's text,
194 // which says that the server refused and is never matched for its wording.
195 if( aError.code == GIT_EEOF && aError.klass == GIT_ERROR_SSH )
196 {
197 return ( connType == KIGIT_COMMON::GIT_CONN_TYPE::GIT_CONN_SSH && !aContext.authFailed
198 && !serverText( aError.message ).IsEmpty() )
201 }
202
203 // mbedTLS fails an untrusted chain with -1 rather than GIT_ECERTIFICATE, and WinHTTP reports it as an HTTP error
204 if( ( aError.klass == GIT_ERROR_SSL || aError.klass == GIT_ERROR_HTTP ) && lower.Contains( wxS( "certificate" ) ) )
206
207 const int httpStatus = httpStatusOf( aError, lower );
208
209 if( httpStatus == 403 )
210 {
213 }
214
215 if( httpStatus == 404 )
217
218 for( const TEXT_PATTERN& pattern : c_textPatterns )
219 {
220 if( lower.Contains( wxString::FromUTF8( pattern.text ) ) )
221 {
222 // A key that failed to load was the last one the callback handed out
223 if( pattern.kind == KIGIT_FAILURE::KEY_LOAD_FAILED && aAuth.keysTried.empty() )
225
226 return pattern.kind;
227 }
228 }
229
231}
232
233
234namespace
235{
236
237wxString summaryFor( KIGIT_FAILURE aKind, const KIGIT_AUTH_ATTEMPT& aAuth, const KIGIT_FAILURE_CONTEXT& aContext )
238{
240 const wxString key = aKind == KIGIT_FAILURE::AUTH_SSH_KEY_REJECTED
241 ? namedKey( aAuth )
242 : ( aAuth.keysTried.empty() ? wxString() : aAuth.keysTried.back() );
243 wxString host = parsed.hostname;
244
245 // A lab or company server often listens on another port, and the bare host would name the wrong service
246 const int defaultPort = parsed.connType == KIGIT_COMMON::GIT_CONN_TYPE::GIT_CONN_SSH
247 ? 22
248 : ( aContext.url.Lower().StartsWith( wxS( "http://" ) ) ? 80 : 443 );
249
250 if( !host.IsEmpty() && parsed.port > 0 && parsed.port != defaultPort )
251 host = wxString::Format( host.Contains( wxS( ":" ) ) ? wxS( "[%s]:%d" ) : wxS( "%s:%d" ), host, parsed.port );
252
253 if( host.IsEmpty() )
254 host = aContext.remoteName.IsEmpty() ? KIGIT::StripEmbeddedUserinfo( aContext.url ) : aContext.remoteName;
255
256 switch( aKind )
257 {
258 case KIGIT_FAILURE::AUTH_SSH_KEY_REJECTED:
259 {
260 // An encrypted key with a wrong passphrase fails exactly as an unregistered one does, unless the
261 // server already refused its public half
262 const bool encrypted =
263 ( !aAuth.acceptedKey.IsEmpty() || !aAuth.lastKeyRefused ) && KIGIT::IsPrivateKeyFileEncrypted( key );
264
265 if( encrypted && !aAuth.passphraseGiven )
266 {
267 return wxString::Format( _( "The SSH key %s requires a passphrase to connect to %s." ), key, host );
268 }
269
270 if( encrypted )
271 {
272 return wxString::Format( _( "%s did not accept the SSH key %s. Check the key's passphrase, and that "
273 "%s.pub is added to your account on %s." ),
274 host, key, key, host );
275 }
276
277 return wxString::Format( _( "%s did not accept the SSH key %s. Check that %s.pub is added to your account "
278 "on %s, or choose another key." ),
279 host, key, key, host );
280 }
281
283 return wxString::Format( _( "%s did not accept any SSH key. KiCad found no key files, and your SSH agent "
284 "offered none that %s accepts. Create or choose a key, and add its .pub to your "
285 "account on %s." ),
286 host, host, host );
287
289 return wxString::Format( _( "%s did not accept the username and password. Many servers require a personal "
290 "access token in place of a password." ),
291 host );
292
294 return wxString::Format( _( "%s needs a username and password. Many servers require a personal access token "
295 "in place of a password." ),
296 host );
297
299 return wxString::Format( _( "The SSH key %s could not be opened. Check the passphrase, and that you chose "
300 "the private key file rather than the .pub file." ),
301 key );
302
304 return wxString::Format( _( "%s is not a known server yet. Use Get Changes to check its key and remember "
305 "it." ),
306 host );
307
309 return KIGIT::HostKeyRefusalMessage( *aContext.hostKey );
310
312 return wxString::Format( _( "The known hosts file %s has a line that could not be read. Fix or remove that "
313 "line, then try again." ),
315
317 return wxString::Format( _( "Could not find the server %s. Check the address, and that you are connected to "
318 "the network or VPN that reaches it." ),
319 host );
320
322 return wxString::Format( _( "%s did not answer. Check your network or VPN connection, then try again." ),
323 host );
324
326 return wxString::Format( _( "This computer does not trust the security certificate of %s. Ask your "
327 "administrator to install your organization's certificate." ),
328 host );
329
331 return wxString::Format( _( "The proxy refused the connection to %s. Check the proxy configured for Git "
332 "(http.proxy) or in your system settings." ),
333 host );
334
336 // Clone and Test Connection show the address in their own Location field
338 {
339 return wxString::Format( _( "%s has no repository at this address, or your account cannot see it. "
340 "Check the repository address." ),
341 host );
342 }
343
344 return wxString::Format( _( "%s has no repository at this address, or your account cannot see it. Check the "
345 "remote's address in the Advanced Git window, Remotes page." ),
346 host );
347
349 return wxString::Format( _( "%s refused the push. Your account may not have write access, or the branch is "
350 "protected." ),
351 host );
352
354 if( aContext.operation == KIGIT_OPERATION::PUSH )
355 return wxString::Format( _( "%s refused the push. The server's reason is shown in the details." ), host );
356
357 return wxString::Format( _( "%s refused the request. The server's reason is shown in the details." ), host );
358
360 return _( "The remote has changes you do not have yet. Get changes first, then push again." );
361
363 return _( "Cancelled." );
364
366 return aContext.hostKeyRefusal;
367
369 break;
370 }
371
372 const wxString remote = aContext.remoteName.IsEmpty() ? host : aContext.remoteName;
373
374 // Whole sentences, so translators never assemble a verb and its object
375 switch( aContext.operation )
376 {
378 return wxString::Format( _( "Could not clone from %s. The details below may help your administrator." ),
379 remote );
380
382 return wxString::Format( _( "Could not get changes from %s. The details below may help your administrator." ),
383 remote );
384
386 return wxString::Format( _( "Could not push to %s. The details below may help your administrator." ), remote );
387
389 return wxString::Format( _( "Could not set up the remote %s. The details below may help your administrator." ),
390 remote );
391
393 return wxString::Format( _( "Could not connect to %s. The details below may help your administrator." ),
394 remote );
395 }
396
397 return wxString();
398}
399
400
401wxString detailsFor( const KIGIT_ERROR_INFO& aError, const KIGIT_AUTH_ATTEMPT& aAuth,
402 const KIGIT_FAILURE_CONTEXT& aContext )
403{
404 // An SSH server's stderr arrives as libgit2's message, so it is treated as server text too
405 wxString details = serverText( aError.message );
406
407 auto addParagraph = [&]( const wxString& aText )
408 {
409 if( aText.IsEmpty() )
410 return;
411
412 if( !details.IsEmpty() )
413 details << wxS( "\n\n" );
414
415 details << aText;
416 };
417
418 const wxString pushRefusal = serverText( aContext.pushRefusal );
419
420 if( !pushRefusal.IsEmpty() && !details.Contains( pushRefusal ) )
421 addParagraph( pushRefusal );
422
423 addParagraph( serverText( aContext.serverMessage ) );
424
425 wxString offered = aAuth.agentTried ? _( "SSH agent" ) : wxString();
426
427 for( const wxString& key : aAuth.keysTried )
428 offered << ( offered.IsEmpty() ? wxString() : wxString( wxS( ", " ) ) ) << key;
429
430 if( !offered.IsEmpty() )
431 addParagraph( wxString::Format( _( "Keys offered: %s" ), offered ) );
432
433 if( !aContext.url.IsEmpty() )
434 addParagraph( wxString::Format( _( "Address: %s" ), KIGIT::StripEmbeddedUserinfo( aContext.url ) ) );
435
436 return KIGIT::StripEmbeddedUserinfo( details );
437}
438
439} // namespace
440
441
443 const KIGIT_FAILURE_CONTEXT& aContext )
444{
446 report.kind = classifyGitFailure( aError, aAuth, aContext );
447 report.summary = summaryFor( report.kind, aAuth, aContext );
448 report.details = detailsFor( aError, aAuth, aContext );
449 return report;
450}
451
452
453wxString KIGIT::UnexplainedFailure( KIGIT_OPERATION aOperation, const wxString& aUrl )
454{
455 KIGIT_FAILURE_CONTEXT context;
456 context.operation = aOperation;
457 context.url = aUrl;
458 return summaryFor( KIGIT_FAILURE::OTHER, {}, context );
459}
460
461
468
469
470wxString KIGIT::DecodeServerText( const char* aData, size_t aLength )
471{
472 wxString text = wxString::FromUTF8( aData, aLength );
473
474 // An empty result would drop the line, and with it the scheme the scrub looks for
475 if( text.IsEmpty() && aLength > 0 )
476 text = wxString::From8BitData( aData, aLength );
477
478 return text;
479}
480
481
482wxString KIGIT::StripEmbeddedUserinfo( const wxString& aText )
483{
484 wxString result;
485 size_t lineStart = 0;
486
487 result.reserve( aText.length() );
488
489 while( true )
490 {
491 size_t lineEnd = aText.find_first_of( wxS( "\r\n" ), lineStart );
492
493 if( lineEnd == wxString::npos )
494 lineEnd = aText.length();
495
496 // A password can hold anything but a line break, even what looks like another URL, so the whole line from
497 // its first "://" to its last "@" goes
498 wxString line = aText.Mid( lineStart, lineEnd - lineStart );
499 const size_t sep = line.find( wxS( "://" ) );
500 const size_t at = line.rfind( '@' );
501
502 if( sep != wxString::npos && at != wxString::npos && at >= sep + 3 )
503 line.erase( sep + 3, at - sep - 2 );
504
505 result << line;
506
507 if( lineEnd == aText.length() )
508 return result;
509
510 result << aText[lineEnd];
511 lineStart = lineEnd + 1;
512 }
513}
514
515
516wxString KIGIT::PrintableServerText( const wxString& aText )
517{
518 static thread_local const wxRegEx escapes( wxS( "\x1B\\[[0-?]*[ -/]*[@-~]" ), wxRE_ADVANCED );
519
520 wxString text = aText;
521
522 // wxRegEx stops replacing at a NUL, which would leave the rest of a password in place
523 text.Replace( wxString( wxUniChar( 0 ) ), wxEmptyString );
524
525 // An escape can end in the "@" that closes a userinfo, so the raw text is scrubbed first
527 escapes.ReplaceAll( &text, wxEmptyString );
528
529 wxString printable;
530
531 for( wxUniChar c : text )
532 {
533 const wxUint32 code = c.GetValue();
534 const bool control = code < 0x20 || code == 0x7F || ( code >= 0x80 && code <= 0x9F );
535 const bool bidi = ( code >= 0x202A && code <= 0x202E ) || ( code >= 0x2066 && code <= 0x2069 );
536
537 if( code == '\t' )
538 printable << wxS( " " );
539 else if( ( !control && !bidi ) || code == '\n' || code == '\r' )
540 printable << c;
541 }
542
543 // Removing escapes and controls can join a "://" the raw text split
544 return StripEmbeddedUserinfo( printable );
545}
546
547
549static wxFileName resolveLikeLibgit2( const wxString& aDir )
550{
551#ifdef __WINDOWS__
552 // win32 p_realpath is GetFullPathNameW then GetLongPathNameW, so links and junctions stay as written
553 wxFileName dir = wxFileName::DirName( aDir );
554 dir.Normalize( wxPATH_NORM_DOTS | wxPATH_NORM_ABSOLUTE | wxPATH_NORM_LONG );
555 return dir;
556#else
557 std::unique_ptr<char, decltype( &std::free )> resolved( realpath( aDir.utf8_str(), nullptr ), &std::free );
558 return wxFileName::DirName( resolved ? wxString::FromUTF8( resolved.get() ) : aDir );
559#endif
560}
561
562
564static std::filesystem::path canonicalDir( const wxString& aDir )
565{
566 std::error_code ec;
567
568#ifdef __WINDOWS__
569 std::filesystem::path path = std::filesystem::canonical( aDir.ToStdWstring(), ec );
570#else
571 std::filesystem::path path = std::filesystem::canonical( aDir.utf8_string(), ec );
572#endif
573
574 return ec ? std::filesystem::path() : path;
575}
576
577
579static wxString quoteForShell( const wxString& aPath )
580{
581#ifdef __WINDOWS__
582 // cmd and PowerShell each expand or end a quote on different characters, so only known-inert ones pass
583 static const wxString inert = wxS( " -_.,:/\\'()[]{}+=#@~" );
584
585 for( wxUniChar c : aPath )
586 {
587 if( !wxIsalnum( c ) && inert.Find( c ) == wxNOT_FOUND )
588 return wxEmptyString;
589 }
590
591 return wxS( "\"" ) + aPath + wxS( "\"" );
592#else
593 // Single quotes stop sh expanding $, `, " and \ in a folder name
594 wxString quoted = aPath;
595 quoted.Replace( wxS( "'" ), wxS( "'\\''" ) );
596 return wxS( "'" ) + quoted + wxS( "'" );
597#endif
598}
599
600
602static bool isRootedLikeLibgit2( const wxString& aPath )
603{
604 // libgit2 skips a drive prefix on every platform, not only Windows
605 const size_t offset = aPath.length() > 1 && aPath[1] == ':' ? 2 : 0;
606
607 if( offset >= aPath.length() )
608 return false;
609
610#ifdef __WINDOWS__
611 return aPath[offset] == '/' || aPath[offset] == '\\';
612#else
613 return aPath[offset] == '/';
614#endif
615}
616
617
618KIGIT_OWNERSHIP_REFUSAL KIGIT::DescribeOwnershipRefusal( const wxString& aStartDir, const wxString& aGitDir )
619{
620 const wxFileName gitDir = wxFileName::DirName( aGitDir );
621 wxFileName trusted = gitDir;
622 wxFileName dir = resolveLikeLibgit2( aStartDir );
623
624 // Reaching the git directory first means a bare repository, which is trusted by its own path
625 while( !dir.SameAs( gitDir ) )
626 {
627 if( wxFileName::Exists( dir.GetPathWithSep() + wxS( ".git" ) ) )
628 {
629 trusted = dir;
630 break;
631 }
632
633 if( dir.GetDirCount() == 0 )
634 break;
635
636 dir.RemoveLastDir();
637 }
638
639 // core.worktree moves the working folder git checks, except in a bare repository or linked worktree
640 git_config* rawConfig = nullptr;
641
642 if( !wxFileName::FileExists( gitDir.GetPathWithSep() + wxS( "commondir" ) )
643 && git_config_open_ondisk( &rawConfig, ( gitDir.GetPathWithSep() + wxS( "config" ) ).utf8_str() ) == GIT_OK )
644 {
645 KIGIT::GitConfigPtr config( rawConfig );
646 int error = GIT_OK;
647 int bare = 0;
648 git_buf worktree = GIT_BUF_INIT;
649
650#if KIGIT_HAS_WORKTREE_CONFIG
651 int worktreeConfig = 0;
652
653 // With the extension on, libgit2 lets config.worktree override config
654 if( git_config_get_bool( &worktreeConfig, config.get(), "extensions.worktreeConfig" ) == GIT_OK
655 && worktreeConfig )
656 {
657 error = git_config_add_file_ondisk( config.get(),
658 ( gitDir.GetPathWithSep() + wxS( "config.worktree" ) ).utf8_str(),
659 GIT_CONFIG_LEVEL_WORKTREE, nullptr, 0 );
660 }
661#endif
662
663 if( ( error == GIT_OK || error == GIT_ENOTFOUND )
664 && ( git_config_get_bool( &bare, config.get(), "core.bare" ) != GIT_OK || !bare )
665 && git_config_get_string_buf( &worktree, config.get(), "core.worktree" ) == GIT_OK )
666 {
667 KIGIT::GitBufPtr worktreePtr( &worktree );
668 wxString configured = wxString::FromUTF8( worktree.ptr );
669
670 // libgit2 joins a relative value onto the git directory and leaves .. and ~ to realpath
671 if( !isRootedLikeLibgit2( configured ) )
672 configured = gitDir.GetPathWithSep() + configured;
673
674 trusted = resolveLikeLibgit2( configured );
675 }
676 }
677
678 const std::filesystem::path repoPath = canonicalDir( trusted.GetPath() );
679
680 // A repository holding the home folder is someone else's, such as one an administrator made there
681 for( const wxString& home : KIGIT::HomeDirCandidates() )
682 {
683 const std::filesystem::path homePath = canonicalDir( home );
684
685 if( !homePath.empty() && !repoPath.empty()
686 && std::mismatch( repoPath.begin(), repoPath.end(), homePath.begin(), homePath.end() ).first
687 == repoPath.end() )
688 {
690 refusal.coversHome = true;
691 return refusal;
692 }
693 }
694
695 wxString path = trusted.GetPath();
696
697#ifdef __WINDOWS__
698 // git compares safe.directory against a forward-slash path, on Windows too
699 path.Replace( wxS( "\\" ), wxS( "/" ) );
700#endif
701
702 // git reads a trailing /* as every repository below the folder, so trusting this one would trust them all
703 if( path.EndsWith( wxS( "/*" ) ) )
704 {
705 return { wxString::Format( _( "The repository at '%s' belongs to another user, so git will not open it. "
706 "git reads a folder named '*' as a wildcard, so rename the folder before "
707 "trusting it." ),
708 trusted.GetPath() ),
709 wxEmptyString };
710 }
711
712 const wxString quoted = quoteForShell( path );
713
714 if( quoted.IsEmpty() )
715 {
716 return { wxString::Format( _( "The repository at '%s' belongs to another user, so git will not open it. To "
717 "trust it, add its folder to git's safe.directory setting and reopen the "
718 "project." ),
719 trusted.GetPath() ),
720 wxEmptyString };
721 }
722
723 return { wxString::Format( _( "The repository at '%s' belongs to another user, so git will not open it. To "
724 "trust it, run the command below and reopen the project." ),
725 trusted.GetPath() ),
726 wxS( "git config --global --add safe.directory " ) + quoted };
727}
static REMOTE_URL ParseRemoteURL(const wxString &aURL)
static bool empty(const wxTextEntryBase *aCtrl)
#define _(s)
static KIGIT::PROMPT_ANSWER refusal(const wxWeakRef< wxWindow > &aParent, bool aHadParent)
#define GIT_BUF_INIT
static std::filesystem::path canonicalDir(const wxString &aDir)
aDir with links and junctions resolved, which libgit2 leaves as written on Windows,...
static wxFileName resolveLikeLibgit2(const wxString &aDir)
aDir spelled as libgit2 1.9's p_realpath spells the working folder it checks safe....
static wxString quoteForShell(const wxString &aPath)
aPath quoted for the shell the trust command is pasted into, or empty when it cannot be done safely
static bool isRootedLikeLibgit2(const wxString &aPath)
True when libgit2's git_fs_path_root takes aPath as rooted rather than joining it onto a base folder.
static KIGIT_FAILURE classifyGitFailure(const KIGIT_ERROR_INFO &aError, const KIGIT_AUTH_ATTEMPT &aAuth, const KIGIT_FAILURE_CONTEXT &aContext)
KIGIT_FAILURE
Why a transfer failed, in terms of what the user can do about it.
@ SERVER_REFUSED
The server answered with text alone. Its wording is shown but never interpreted.
@ AUTH_HTTPS_NEEDED
The server asked for a login before any password was offered.
@ HOST_KEY_UNVERIFIED
A key KiCad could not check, so there is nothing to offer to trust.
KIGIT_OPERATION
The transfer that failed, so a message nothing more specific explains can say what did not happen.
APIEXPORT wxString StripEmbeddedUserinfo(const wxString &aText)
Remove the user and password from every URL quoted in a message, since the remote's can carry a passw...
APIEXPORT wxString PrintableServerText(const wxString &aText)
Text a server sent, made safe to show: no terminal escapes, control or bidi characters,...
constexpr size_t MAX_SERVER_TEXT
The longest server text a failure keeps. A refusal's explanation comes last, after any transfer progr...
KNOWN_HOST_STATE
How a server's host key stands against known_hosts.
@ MISMATCH
Entries name the host with another key of this type, and none with this one.
@ REVOKED
A @revoked entry carries this key.
std::unique_ptr< git_buf, decltype([](git_buf *aBuf) { git_buf_free(aBuf); })> GitBufPtr
A unique pointer for git_buf objects with automatic cleanup.
APIEXPORT KIGIT_OWNERSHIP_REFUSAL DescribeOwnershipRefusal(const wxString &aStartDir, const wxString &aGitDir)
The message for a repository git refuses to open because another user owns it.
std::unique_ptr< git_config, decltype([](git_config *aConfig) { git_config_free(aConfig); })> GitConfigPtr
A unique pointer for git_config objects with automatic cleanup.
APIEXPORT wxString DecodeServerText(const char *aData, size_t aLength)
Bytes a server sent, as UTF-8 or, when they are not valid UTF-8, one character for each byte.
APIEXPORT bool IsCredentialFailure(KIGIT_FAILURE aKind)
True for the failures another login, key or passphrase can fix, which the credential prompt answers.
APIEXPORT std::vector< wxString > HomeDirCandidates()
The folders that may be the user's home, most likely first.
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.
APIEXPORT wxString HostKeyRefusalMessage(const HOST_KEY_FAILURE &aFailure)
Why a key the user may not trust was refused, and what to do next.
APIEXPORT wxString KnownHostsPath()
The known_hosts file libgit2 checks host keys against. Empty when it has no home folder.
APIEXPORT wxString UnexplainedFailure(KIGIT_OPERATION aOperation, const wxString &aUrl)
The summary for an operation that failed without saying why, naming the server it was talking to.
What the credential callback handed out during one operation, kept to explain a rejection.
bool passwordGiven
A password went with the last username offered.
KIGIT_COMMON::GIT_CONN_TYPE connType
wxString acceptedKey
An encrypted key whose public half the server accepted.
std::vector< wxString > keysTried
Key files offered, in order.
bool agentTried
The SSH agent was offered before any key file.
A transport address with any embedded credentials extracted.
int port
0 when the address names none.
What libgit2 reported for a failed call, read before anything else can replace it.
int klass
git_error_t
int code
The failed call's return value.
What the failed operation was doing, as far as the message needs it.
KIGIT_OPERATION operation
std::optional< KIGIT::HOST_KEY_FAILURE > hostKey
The host key the operation refused, if any.
wxString hostKeyRefusal
Why a key that cannot be trusted was refused.
bool authFailed
The credential callback had nothing left to offer.
wxString url
The address connected to. Messages show only its host and its stripped form.
wxString remoteName
Empty when the operation has no named remote, as for a clone.
wxString pushRefusal
The status the server gave a refused branch.
wxString serverMessage
Text the server sent for the user.
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.
Why git will not open a repository another user owns, and the command that trusts it.
std::string path
wxString result
Test unit parsing edge cases and error handling.
wxLogTrace helper definitions.