KiCad PCB EDA Suite
Loading...
Searching...
No Matches
git_repo_mixin.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 "git_repo_mixin.h"
21
22#include <trace_helpers.h>
23
24#include <wx/log.h>
25
26
27void KIGIT_REPO_MIXIN::ReceiveSideband( const char* aData, size_t aLength )
28{
29 m_pendingSideband.append( aData, aLength );
30
31 size_t lineStart = 0;
32 size_t lineEnd;
33
34 // A multibyte character can straddle packets, so only whole lines are decoded
35 while( ( lineEnd = m_pendingSideband.find_first_of( "\r\n", lineStart ) ) != std::string::npos )
36 {
37 wxString line = KIGIT::DecodeServerText( m_pendingSideband.data() + lineStart, lineEnd + 1 - lineStart );
38 lineStart = lineEnd + 1;
39
40 // The rest of a line too long to keep, which may hold the password of a URL whose scheme is gone
42 {
44 continue;
45 }
46
47 addServerLine( line );
48
49 wxString progress = KIGIT::PrintableServerText( line ).Trim();
50
51 if( !progress.IsEmpty() )
52 UpdateProgress( 0, 0, progress );
53 }
54
55 m_pendingSideband.erase( 0, lineStart );
56
57 // UTF-8 takes at most four bytes a character, so this line is longer than the text kept
58 if( m_pendingSideband.length() > 4 * KIGIT::MAX_SERVER_TEXT )
59 {
60 m_pendingSideband.clear();
62 }
63}
64
65
66void KIGIT_REPO_MIXIN::addServerLine( const wxString& aLine )
67{
68 m_serverMessage << aLine;
69
70 // A long transfer streams progress through the same channel, and only the end can explain a refusal. Whole
71 // lines go, so the scrub still sees each URL from its scheme
72 while( m_serverMessage.length() > KIGIT::MAX_SERVER_TEXT )
73 {
74 size_t lineEnd = m_serverMessage.find_first_of( wxS( "\r\n" ) );
75 m_serverMessage.erase( 0, lineEnd == wxString::npos ? wxString::npos : lineEnd + 1 );
76 }
77}
78
79
80void KIGIT_REPO_MIXIN::SetPushRefusal( const wxString& aStatus )
81{
82 // Scrubbed before the cut, so the cut cannot leave half a URL that still names its password
84}
85
86
88 KIGIT_OPERATION aOperation, const wxString& aRemoteName )
89{
90 // A refusal can end the transfer without a final line break
93
94 m_pendingSideband.clear();
96
98 context.operation = aOperation;
99 context.remoteName = aRemoteName;
100 context.url = m_common->GetRemote();
101 context.hostKey = m_hostKeyFailure;
103 context.pushRefusal = m_pushRefusal;
105 context.authFailed = m_authFailed;
106
108
109 wxLogTrace( traceGit, "Transfer failed with code %d, class %d, kind %d: %s", aError.code, aError.klass,
110 static_cast<int>( report.kind ), KIGIT::PrintableServerText( aError.message ) );
111
112 if( KIGIT::IsCredentialFailure( report.kind ) )
113 m_authFailed = true;
114
115 m_failureKind = report.kind;
116 AddError( report.summary, report.details );
117 return report;
118}
void AddError(const wxString &aSummary, const wxString &aDetails)
A summary the user can act on, with the raw text that explains it kept apart for the details.
void SetPushRefusal(const wxString &aStatus)
std::optional< KIGIT_FAILURE > m_failureKind
std::optional< KIGIT::HOST_KEY_FAILURE > m_hostKeyFailure
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.
KIGIT_AUTH_ATTEMPT m_authAttempt
KIGIT_COMMON * m_common
wxString m_hostKeyRefusal
std::string m_pendingSideband
void addServerLine(const wxString &aLine)
void ReceiveSideband(const char *aData, size_t aLength)
Keep sideband text the server sent for the user, such as why it refused a pushed branch,...
void UpdateProgress(int aCurrent, int aTotal, const wxString &aMessage) override
const wxChar *const traceGit
Flag to enable Git debugging output.
KIGIT_OPERATION
The transfer that failed, so a message nothing more specific explains can say what did not happen.
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...
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 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.
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.
wxLogTrace helper definitions.