KiCad PCB EDA Suite
Loading...
Searching...
No Matches
kicad_git_common.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, you may find one here:
18 * http://www.gnu.org/licenses/gpl-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include "kicad_git_common.h"
25#include "kicad_git_memory.h"
26#include "git_repo_mixin.h"
27
28#include <git/git_progress.h>
30#include <kiplatform/secrets.h>
31#include <trace_helpers.h>
32
33#include <git2.h>
34#include <wx/filename.h>
35#include <wx/log.h>
36#include <wx/textfile.h>
37#include <wx/utils.h>
38#include <map>
39#include <vector>
40
41KIGIT_COMMON::KIGIT_COMMON( git_repository* aRepo ) :
43 m_nextPublicKey( 0 ), m_secretFetched( false )
44{}
45
46
48 // Initialize base class and member variables
49 m_repo( aOther.m_repo ),
50 m_projectDir( aOther.m_projectDir ),
51 m_connType( aOther.m_connType ),
52 m_remote( aOther.m_remote ),
53 m_hostname( aOther.m_hostname ),
54 m_username( aOther.m_username ),
55 m_password( aOther.m_password ),
57
58 // The mutex is default-initialized, not copied
60 m_publicKeys( aOther.m_publicKeys ),
63{
64}
65
66
69
70
71git_repository* KIGIT_COMMON::GetRepo() const
72{
73 return m_repo;
74}
75
76
78{
79 if( !m_projectDir.IsEmpty() )
80 return m_projectDir;
81
82 if( m_repo )
83 {
84 const char* workdir = git_repository_workdir( m_repo );
85
86 if( workdir )
87 return wxString( workdir );
88 }
89
90 return wxEmptyString;
91}
92
93
95{
96 wxCHECK( m_repo, wxEmptyString );
97
98 git_reference* head = nullptr;
99
100 if( git_repository_head( &head, m_repo ) != GIT_OK )
101 return wxEmptyString;
102
103 KIGIT::GitReferencePtr headPtr( head );
104
105 if( !git_reference_is_branch( head ) )
106 return wxEmptyString;
107
108 git_reference* upstream = nullptr;
109
110 if( git_branch_upstream( &upstream, head ) == GIT_OK )
111 {
112 KIGIT::GitReferencePtr upstreamPtr( upstream );
113 const char* shorthand = git_reference_shorthand( upstream );
114
115 if( shorthand )
116 return wxString::FromUTF8( shorthand );
117 }
118
119 // No upstream configured. Synthesise the target that PerformPull's fallback
120 // and the first push will use.
121 const char* branch_shorthand = git_reference_shorthand( head );
122
123 if( !branch_shorthand )
124 return wxEmptyString;
125
126 return wxString::Format( "%s/%s", GetRemoteNameOrDefault(), branch_shorthand );
127}
128
129
131{
132 wxCHECK( m_repo, wxEmptyString );
133 git_reference* head = nullptr;
134
135 int retval = git_repository_head( &head, m_repo );
136
137 if( retval && retval != GIT_EUNBORNBRANCH && retval != GIT_ENOTFOUND )
138 return wxEmptyString;
139
140 KIGIT::GitReferencePtr headPtr( head );
141 git_reference* branch;
142
143 if( git_reference_resolve( &branch, head ) )
144 {
145 wxLogTrace( traceGit, "Failed to resolve branch" );
146 return wxEmptyString;
147 }
148
149 KIGIT::GitReferencePtr branchPtr( branch );
150 const char* branchName = "";
151
152 if( git_branch_name( &branchName, branch ) )
153 {
154 wxLogTrace( traceGit, "Failed to get branch name" );
155 return wxEmptyString;
156 }
157
158 return wxString( branchName );
159}
160
161
163{
164 if( !m_secretFetched )
165 {
167 {
168 wxString secret;
169
171 m_password = secret;
172 }
173
174 m_secretFetched = true;
175 }
176
177 return m_password;
178}
179
180
181std::vector<wxString> KIGIT_COMMON::GetBranchNames() const
182{
183 if( !m_repo )
184 return {};
185
186 std::vector<wxString> branchNames;
187 std::map<git_time_t, wxString> branchNamesMap;
188 wxString firstName;
189
190 git_branch_iterator* branchIterator = nullptr;
191
192 if( git_branch_iterator_new( &branchIterator, m_repo, GIT_BRANCH_LOCAL ) )
193 {
194 wxLogTrace( traceGit, "Failed to get branch iterator" );
195 return branchNames;
196 }
197
198 KIGIT::GitBranchIteratorPtr branchIteratorPtr( branchIterator );
199 git_reference* branchReference = nullptr;
200 git_branch_t branchType;
201
202 while( git_branch_next( &branchReference, &branchType, branchIterator ) != GIT_ITEROVER )
203 {
204 const char* branchName = "";
205 KIGIT::GitReferencePtr branchReferencePtr( branchReference );
206
207 if( git_branch_name( &branchName, branchReference ) )
208 {
209 wxLogTrace( traceGit, "Failed to get branch name in iter loop" );
210 continue;
211 }
212
213 const git_oid* commitId = git_reference_target( branchReference );
214
215 git_commit* commit = nullptr;
216
217 if( git_commit_lookup( &commit, m_repo, commitId ) )
218 {
219 wxLogTrace( traceGit, "Failed to get commit in iter loop" );
220 continue;
221 }
222
223 KIGIT::GitCommitPtr commitPtr( commit );
224 git_time_t commitTime = git_commit_time( commit );
225
226 if( git_branch_is_head( branchReference ) )
227 firstName = branchName;
228 else
229 branchNamesMap.emplace( commitTime, branchName );
230 }
231
232 // Add the current branch to the top of the list
233 if( !firstName.IsEmpty() )
234 branchNames.push_back( firstName );
235
236 // Add the remaining branches in order from newest to oldest
237 for( auto rit = branchNamesMap.rbegin(); rit != branchNamesMap.rend(); ++rit )
238 branchNames.push_back( rit->second );
239
240 return branchNames;
241}
242
243
244std::vector<wxString> KIGIT_COMMON::GetProjectDirs()
245{
246 wxCHECK( m_repo, {} );
247 std::vector<wxString> projDirs;
248
249 git_oid oid;
250 git_commit* commit;
251 git_tree *tree;
252
253 if( git_reference_name_to_id( &oid, m_repo, "HEAD" ) != GIT_OK )
254 {
255 wxLogTrace( traceGit, "An error occurred: %s", KIGIT_COMMON::GetLastGitError() );
256 return projDirs;
257 }
258
259 if( git_commit_lookup( &commit, m_repo, &oid ) != GIT_OK )
260 {
261 wxLogTrace( traceGit, "An error occurred: %s", KIGIT_COMMON::GetLastGitError() );
262 return projDirs;
263 }
264
265 KIGIT::GitCommitPtr commitPtr( commit );
266
267 if( git_commit_tree( &tree, commit ) != GIT_OK )
268 {
269 wxLogTrace( traceGit, "An error occurred: %s", KIGIT_COMMON::GetLastGitError() );
270 return projDirs;
271 }
272
273 KIGIT::GitTreePtr treePtr( tree );
274
275 // Define callback
276 git_tree_walk(
277 tree, GIT_TREEWALK_PRE,
278 []( const char* root, const git_tree_entry* entry, void* payload )
279 {
280 std::vector<wxString>* prjs = static_cast<std::vector<wxString>*>( payload );
281 wxFileName root_fn( git_tree_entry_name( entry ) );
282
283 root_fn.SetPath( root );
284
285 if( git_tree_entry_type( entry ) == GIT_OBJECT_BLOB
286 && ( ( root_fn.GetExt() == "kicad_pro" ) || ( root_fn.GetExt() == "pro" ) ) )
287 {
288 prjs->push_back( root_fn.GetFullPath() );
289 }
290
291 return 0; // continue walking
292 },
293 &projDirs );
294
295 std::sort( projDirs.begin(), projDirs.end(),
296 []( const wxString& a, const wxString& b )
297 {
298 int a_freq = a.Freq( wxFileName::GetPathSeparator() );
299 int b_freq = b.Freq( wxFileName::GetPathSeparator() );
300
301 if( a_freq == b_freq )
302 return a < b;
303 else
304 return a_freq < b_freq;
305
306 } );
307
308 return projDirs;
309}
310
311
312std::pair<std::set<wxString>, std::set<wxString>> KIGIT_COMMON::GetDifferentFiles() const
313{
314 std::pair<std::set<wxString>, std::set<wxString>> modified_files;
315
316 if( !m_repo || IsCancelled() )
317 return modified_files;
318
319 git_reference* head = nullptr;
320 git_reference* remote_head = nullptr;
321
322 if( git_repository_head( &head, m_repo ) != GIT_OK )
323 {
324 wxLogTrace( traceGit, "Failed to get modified HEAD" );
325 return modified_files;
326 }
327
328 KIGIT::GitReferencePtr headPtr( head );
329
330 if( git_branch_upstream( &remote_head, head ) != GIT_OK )
331 {
332 // Branch has no upstream tracking ref configured. Without an upstream there is
333 // nothing to compare against, so leave both sets empty. Walking commits unbounded
334 // would otherwise dump the entire history's root tree into the result.
335 wxLogTrace( traceGit, "Failed to get modified remote HEAD" );
336 return modified_files;
337 }
338
339 KIGIT::GitReferencePtr remoteHeadPtr( remote_head );
340
341 const git_oid* head_oid = git_reference_target( head );
342 const git_oid* remote_oid = git_reference_target( remote_head );
343
344 if( !head_oid || !remote_oid )
345 return modified_files;
346
347 auto load_tree =
348 [this]( const git_oid* aOid ) -> git_tree*
349 {
350 git_commit* commit = nullptr;
351
352 if( git_commit_lookup( &commit, m_repo, aOid ) != GIT_OK )
353 {
354 wxLogTrace( traceGit, "Failed to lookup commit for diff: %s",
356 return nullptr;
357 }
358
359 KIGIT::GitCommitPtr commitPtr( commit );
360 git_tree* tree = nullptr;
361
362 if( git_commit_tree( &tree, commit ) != GIT_OK )
363 {
364 wxLogTrace( traceGit, "Failed to get commit tree for diff: %s",
366 return nullptr;
367 }
368
369 return tree;
370 };
371
372 git_tree* head_tree = load_tree( head_oid );
373 KIGIT::GitTreePtr headTreePtr( head_tree );
374
375 git_tree* remote_tree = load_tree( remote_oid );
376 KIGIT::GitTreePtr remoteTreePtr( remote_tree );
377
378 if( !head_tree || !remote_tree )
379 return modified_files;
380
381 // Find the merge-base so AHEAD and BEHIND can be distinguished. AHEAD = files that
382 // changed between merge-base and HEAD (only in local commits). BEHIND = files that
383 // changed between merge-base and the remote tip (only in remote commits). Without a
384 // shared history the merge-base lookup fails; in that case we treat both sets as empty
385 // because there is no meaningful "ahead vs behind" partition to compute.
386 git_oid base_oid;
387
388 if( git_merge_base( &base_oid, m_repo, head_oid, remote_oid ) != GIT_OK )
389 {
390 wxLogTrace( traceGit, "No merge base between local and remote: %s",
392 return modified_files;
393 }
394
395 git_tree* base_tree = load_tree( &base_oid );
396 KIGIT::GitTreePtr baseTreePtr( base_tree );
397
398 if( !base_tree )
399 return modified_files;
400
401 auto collect_paths =
402 [this]( git_tree* aOldTree, git_tree* aNewTree, std::set<wxString>& aOut )
403 {
404 if( IsCancelled() )
405 return;
406
407 git_diff_options localOpts;
408 git_diff_init_options( &localOpts, GIT_DIFF_OPTIONS_VERSION );
409
410 git_diff* diff = nullptr;
411
412 if( git_diff_tree_to_tree( &diff, m_repo, aOldTree, aNewTree, &localOpts )
413 != GIT_OK )
414 {
415 wxLogTrace( traceGit, "Failed to diff trees: %s",
417 return;
418 }
419
420 size_t numDeltas = git_diff_num_deltas( diff );
421
422 for( size_t ii = 0; ii < numDeltas; ++ii )
423 {
424 const git_diff_delta* delta = git_diff_get_delta( diff, ii );
425
426 if( delta->new_file.path )
427 aOut.insert( wxString::FromUTF8( delta->new_file.path ) );
428
429 if( delta->old_file.path )
430 aOut.insert( wxString::FromUTF8( delta->old_file.path ) );
431 }
432
433 git_diff_free( diff );
434 };
435
436 collect_paths( base_tree, head_tree, modified_files.first ); // AHEAD
437 collect_paths( base_tree, remote_tree, modified_files.second ); // BEHIND
438
439 // Filter both sets to files whose content actually differs between HEAD and remote.
440 // Without this, a file touched by a commit that has since been replaced with an
441 // identical-tree commit (e.g. a message-only amend) keeps an AHEAD marker even
442 // though its blob matches the remote's blob.
443 std::set<wxString> actuallyDifferent;
444 collect_paths( head_tree, remote_tree, actuallyDifferent );
445
446 auto filterToDifferent = [&]( std::set<wxString>& aSet )
447 {
448 for( auto it = aSet.begin(); it != aSet.end(); )
449 it = actuallyDifferent.count( *it ) ? std::next( it ) : aSet.erase( it );
450 };
451
452 filterToDifferent( modified_files.first );
453 filterToDifferent( modified_files.second );
454
455 return modified_files;
456}
457
458
460{
461 if( !m_repo )
462 return false;
463
464 git_reference* head = nullptr;
465 git_reference* remote_head = nullptr;
466
467 if( git_repository_head( &head, m_repo ) != GIT_OK )
468 {
469 wxLogTrace( traceGit, "Failed to get HEAD: %s", KIGIT_COMMON::GetLastGitError() );
470 return false;
471 }
472
473 KIGIT::GitReferencePtr headPtr( head );
474
475 if( git_branch_upstream( &remote_head, head ) != GIT_OK )
476 {
477 // No remote branch, so we have local commits (new repo?)
478 wxLogTrace( traceGit, "Failed to get remote HEAD: %s", KIGIT_COMMON::GetLastGitError() );
479 return true;
480 }
481
482 KIGIT::GitReferencePtr remoteHeadPtr( remote_head );
483 const git_oid* head_oid = git_reference_target( head );
484 const git_oid* remote_oid = git_reference_target( remote_head );
485 git_revwalk* walker = nullptr;
486
487 if( git_revwalk_new( &walker, m_repo ) != GIT_OK )
488 {
489 wxLogTrace( traceGit, "Failed to create revwalker: %s", KIGIT_COMMON::GetLastGitError() );
490 return false;
491 }
492
493 KIGIT::GitRevWalkPtr walkerPtr( walker );
494
495 if( !head_oid || git_revwalk_push( walker, head_oid ) != GIT_OK )
496 {
497 wxLogTrace( traceGit, "Failed to push commits: %s", KIGIT_COMMON::GetLastGitError() );
498 return false;
499 }
500
501 if( remote_oid && git_revwalk_hide( walker, remote_oid ) != GIT_OK )
502 {
503 wxLogTrace( traceGit, "Failed to push/hide commits: %s", KIGIT_COMMON::GetLastGitError() );
504 return false;
505 }
506
507 git_oid oid;
508
509 // If we can't walk to the next commit, then we are at or behind the remote
510 if( git_revwalk_next( &oid, walker ) != GIT_OK )
511 {
512 wxLogTrace( traceGit, "Failed to walk to next commit: %s", KIGIT_COMMON::GetLastGitError() );
513 return false;
514 }
515
516 return true;
517}
518
519
521{
522 wxCHECK( m_repo, false );
523
524 // Prefer the upstream remote configured for the current branch. Fall back to
525 // "origin" and then to any remote that exposes a fetch URL so that repos
526 // cloned with non-default remote names also enable push/pull operations.
527 auto checkRemote =
528 [this]( const char* aName ) -> bool
529 {
530 git_remote* remote = nullptr;
531
532 if( git_remote_lookup( &remote, m_repo, aName ) != GIT_OK )
533 return false;
534
535 KIGIT::GitRemotePtr remotePtr( remote );
536
537 const char* fetch_url = git_remote_url( remote );
538 const char* push_url = git_remote_pushurl( remote );
539
540 // libgit2 defaults to the fetch URL for pushing when no push URL is set
541 if( !push_url )
542 push_url = fetch_url;
543
544 return fetch_url && push_url;
545 };
546
547 std::string preferred = GetRemoteNameOrDefault().utf8_string();
548
549 if( checkRemote( preferred.c_str() ) )
550 return true;
551
552 if( preferred != "origin" && checkRemote( "origin" ) )
553 return true;
554
555 git_strarray remotes = { nullptr, 0 };
556
557 if( git_remote_list( &remotes, m_repo ) != GIT_OK )
558 {
559 wxLogTrace( traceGit, "Failed to enumerate remotes for haspushpull" );
560 return false;
561 }
562
563 KIGIT::GitStrArrayPtr remotesPtr( &remotes );
564
565 for( size_t ii = 0; ii < remotes.count; ++ii )
566 {
567 if( checkRemote( remotes.strings[ii] ) )
568 return true;
569 }
570
571 return false;
572}
573
574
576{
577 wxString remoteName = GetRemotename();
578
579 if( remoteName.IsEmpty() )
580 remoteName = wxS( "origin" );
581
582 return remoteName;
583}
584
585
587{
588 wxCHECK( m_repo, wxEmptyString );
589
590 wxString retval;
591 git_reference* head = nullptr;
592 git_reference* upstream = nullptr;
593
594 if( git_repository_head( &head, m_repo ) != GIT_OK )
595 {
596 wxLogTrace( traceGit, "Failed to get remote name: %s", KIGIT_COMMON::GetLastGitError() );
597 return retval;
598 }
599
600 KIGIT::GitReferencePtr headPtr( head );
601
602 if( git_branch_upstream( &upstream, head ) != GIT_OK )
603 {
604 wxLogTrace( traceGit, "Failed to get upstream branch: %s", KIGIT_COMMON::GetLastGitError() );
605 git_strarray remotes = { nullptr, 0 };
606
607 if( git_remote_list( &remotes, m_repo ) == GIT_OK )
608 {
609 // No upstream tracking branch. Prefer "origin" when present, otherwise pick the
610 // single configured remote. Returning empty for ambiguous (multiple) remotes
611 // tells callers to fall back to whatever default they want.
612 if( remotes.count == 1 )
613 {
614 retval = remotes.strings[0];
615 }
616 else
617 {
618 for( size_t ii = 0; ii < remotes.count; ++ii )
619 {
620 if( strcmp( remotes.strings[ii], "origin" ) == 0 )
621 {
622 retval = remotes.strings[ii];
623 break;
624 }
625 }
626 }
627
628 git_strarray_dispose( &remotes );
629 }
630 else
631 {
632 wxLogTrace( traceGit, "Failed to list remotes: %s", KIGIT_COMMON::GetLastGitError() );
633
634 // If we can't get the remote name from the upstream branch or the list of remotes,
635 // just return the default remote name
636
637 git_remote* remote = nullptr;
638
639 if( git_remote_lookup( &remote, m_repo, "origin" ) == GIT_OK )
640 {
641 retval = git_remote_name( remote );
642 git_remote_free( remote );
643 }
644 else
645 {
646 wxLogTrace( traceGit, "Failed to get remote name from default remote: %s",
648 }
649 }
650
651 return retval;
652 }
653
654 KIGIT::GitReferencePtr upstreamPtr( upstream );
655 git_buf remote_name = GIT_BUF_INIT_CONST( nullptr, 0 );
656
657 if( git_branch_remote_name( &remote_name, m_repo, git_reference_name( upstream ) ) == GIT_OK )
658 {
659 retval = remote_name.ptr;
660 git_buf_dispose( &remote_name );
661 }
662 else
663 {
664 wxLogTrace( traceGit,
665 "Failed to get remote name from upstream branch: %s",
667 }
668
669 return retval;
670}
671
672
673void KIGIT_COMMON::SetSSHKey( const wxString& aKey )
674{
675 auto it = std::find( m_publicKeys.begin(), m_publicKeys.end(), aKey );
676
677 if( it != m_publicKeys.end() )
678 m_publicKeys.erase( it );
679
680 m_publicKeys.insert( m_publicKeys.begin(), aKey );
681}
682
683
685{
686 if( !m_repo )
687 return wxEmptyString;
688
689 // Prefer the working directory (the user-visible project root). Fall back to
690 // git_repository_path (the .git directory) only for bare repositories.
691 if( const char* workdir = git_repository_workdir( m_repo ) )
692 return wxString::FromUTF8( workdir );
693
694 if( const char* path = git_repository_path( m_repo ) )
695 return wxString::FromUTF8( path );
696
697 return wxEmptyString;
698}
699
700
702{
703 m_publicKeys.clear();
704
705 wxFileName keyFile( wxGetHomeDir(), wxEmptyString );
706 keyFile.AppendDir( ".ssh" );
707 keyFile.SetFullName( "id_rsa" );
708
709 if( keyFile.FileExists() )
710 m_publicKeys.push_back( keyFile.GetFullPath() );
711
712 keyFile.SetFullName( "id_dsa" );
713
714 if( keyFile.FileExists() )
715 m_publicKeys.push_back( keyFile.GetFullPath() );
716
717 keyFile.SetFullName( "id_ecdsa" );
718
719 if( keyFile.FileExists() )
720 m_publicKeys.push_back( keyFile.GetFullPath() );
721
722 keyFile.SetFullName( "id_ed25519" );
723
724 if( keyFile.FileExists() )
725 m_publicKeys.push_back( keyFile.GetFullPath() );
726
727 // Parse SSH config file for hostname information
728 wxFileName sshConfig( wxGetHomeDir(), wxEmptyString );
729 sshConfig.AppendDir( ".ssh" );
730 sshConfig.SetFullName( "config" );
731
732 if( sshConfig.FileExists() )
733 {
734 wxTextFile configFile( sshConfig.GetFullPath() );
735 configFile.Open();
736
737 bool match = false;
738
739 for( wxString line = configFile.GetFirstLine(); !configFile.Eof(); line = configFile.GetNextLine() )
740 {
741 line.Trim( false ).Trim( true );
742
743 if( line.StartsWith( "Host " ) )
744 match = false;
745
746 // The difference here is that we are matching either "Hostname" or "Host" to get the
747 // match. This is because in the absence of a "Hostname" line, the "Host" line is used
748 if( line.StartsWith( "Host" ) && line.Contains( m_hostname ) )
749 match = true;
750
751 if( match && line.StartsWith( "IdentityFile" ) )
752 {
753 wxString keyPath = line.AfterFirst( ' ' ).Trim( false ).Trim( true );
754
755 // Expand ~ to home directory if present
756 if( keyPath.StartsWith( "~" ) )
757 keyPath.Replace( "~", wxGetHomeDir(), false );
758
759 // Add the public key to the beginning of the list
760 if( wxFileName::FileExists( keyPath ) )
761 SetSSHKey( keyPath );
762 }
763 }
764
765 configFile.Close();
766 }
767}
768
769
771{
772 wxCHECK( m_repo, /* void */ );
773
774 // We want to get the current branch's upstream url as well as the stored password
775 // if one exists given the url and username.
776
777 wxString remote_name = GetRemotename();
778 git_remote* remote = nullptr;
779
780 m_remote.clear();
781 m_password.clear();
782 m_secretFetched = false;
783
784 if( git_remote_lookup( &remote, m_repo, remote_name.ToStdString().c_str() ) == GIT_OK )
785 {
786 const char* url = git_remote_url( remote );
787
788 if( url )
789 m_remote = url;
790
791 git_remote_free( remote );
792 }
793
796}
797
798
800{
801 wxString remote = m_remote;
802
803 if( remote.IsEmpty() )
804 remote = GetRemotename();
805
806 if( remote.StartsWith( "https://" ) || remote.StartsWith( "http://" ) )
807 {
809 }
810 else if( remote.StartsWith( "ssh://" ) || remote.StartsWith( "git@" ) || remote.StartsWith( "git+ssh://" )
811 || remote.EndsWith( ".git" ) )
812 {
814 }
815
817}
818
819
821{
822 if( m_remote.StartsWith( "https://" ) || m_remote.StartsWith( "http://" ) )
824 else if( m_remote.StartsWith( "ssh://" ) || m_remote.StartsWith( "git@" ) || m_remote.StartsWith( "git+ssh://" ) )
826 else
828
830 {
831 wxString uri = m_remote;
832 size_t atPos = uri.find( '@' );
833
834 if( atPos != wxString::npos )
835 {
836 size_t protoEnd = uri.find( "//" );
837
838 if( protoEnd != wxString::npos )
839 {
840 wxString credentials = uri.Mid( protoEnd + 2, atPos - protoEnd - 2 );
841 size_t colonPos = credentials.find( ':' );
842
843 if( colonPos != wxString::npos )
844 {
845 m_username = credentials.Left( colonPos );
846 m_password = credentials.Mid( colonPos + 1, credentials.Length() - colonPos - 1 );
847 }
848 else
849 {
850 m_username = credentials;
851 }
852 }
853 else
854 {
855 m_username = uri.Left( atPos );
856 }
857 }
858
859 if( m_remote.StartsWith( "git@" ) )
860 {
861 // SSH format: git@hostname:path
862 size_t colonPos = m_remote.find( ':' );
863
864 if( colonPos != wxString::npos )
865 m_hostname = m_remote.Mid( 4, colonPos - 4 );
866 }
867 else
868 {
869 // other URL format: proto://[user@]hostname/path
870 size_t hostStart = m_remote.find( "://" ) + 2;
871 size_t hostEnd = m_remote.find( '/', hostStart );
872 wxString host;
873
874 if( hostEnd != wxString::npos )
875 host = m_remote.Mid( hostStart, hostEnd - hostStart );
876 else
877 host = m_remote.Mid( hostStart );
878
879 atPos = host.find( '@' );
880
881 if( atPos != wxString::npos )
882 m_hostname = host.Mid( atPos + 1 );
883 else
884 m_hostname = host;
885 }
886 }
887
888 m_secretFetched = !m_password.IsEmpty();
889}
890
891
892int KIGIT_COMMON::HandleSSHKeyAuthentication( git_cred** aOut, const wxString& aUsername )
893{
895 {
896 if( HandleSSHAgentAuthentication( aOut, aUsername ) == GIT_OK )
897 return GIT_OK;
898 // Agent unavailable or has no matching key; fall through to configured key.
899 }
900
901 // SSH key authentication with password
902 wxString sshKey = GetNextPublicKey();
903
904 if( sshKey.IsEmpty() )
905 {
906 wxLogTrace( traceGit, "Finished testing all possible ssh keys" );
907 m_testedTypes |= GIT_CREDENTIAL_SSH_KEY;
908 return GIT_PASSTHROUGH;
909 }
910
911 wxString sshPubKey = sshKey + ".pub";
912 wxString password = GetPassword();
913
914 wxLogTrace( traceGit, "Testing %s\n", sshKey );
915
916 if( git_credential_ssh_key_new( aOut, aUsername.mbc_str(), sshPubKey.mbc_str(), sshKey.mbc_str(),
917 password.mbc_str() ) != GIT_OK )
918 {
919 wxLogTrace( traceGit, "Failed to create SSH key credential for %s: %s",
920 aUsername, KIGIT_COMMON::GetLastGitError() );
921 return GIT_PASSTHROUGH;
922 }
923
924 return GIT_OK;
925}
926
927
928int KIGIT_COMMON::HandlePlaintextAuthentication( git_cred** aOut, const wxString& aUsername )
929{
930 wxString password = GetPassword();
931
932 git_credential_userpass_plaintext_new( aOut, aUsername.mbc_str(), password.mbc_str() );
933 m_testedTypes |= GIT_CREDENTIAL_USERPASS_PLAINTEXT;
934
935 return GIT_OK;
936}
937
938
939int KIGIT_COMMON::HandleSSHAgentAuthentication( git_cred** aOut, const wxString& aUsername )
940{
942
943 if( git_credential_ssh_key_from_agent( aOut, aUsername.mbc_str() ) != GIT_OK )
944 {
945 wxLogTrace( traceGit, "Failed to create SSH agent credential for %s: %s",
946 aUsername, KIGIT_COMMON::GetLastGitError() );
947 return GIT_PASSTHROUGH;
948 }
949
950 return GIT_OK;
951}
952
953
954extern "C" int fetchhead_foreach_cb( const char*, const char*,
955 const git_oid* aOID, unsigned int aIsMerge, void* aPayload )
956{
957 if( aIsMerge )
958 git_oid_cpy( (git_oid*) aPayload, aOID );
959
960 return 0;
961}
962
963
964extern "C" void clone_progress_cb( const char* aStr, size_t aLen, size_t aTotal, void* aPayload )
965{
966 KIGIT_REPO_MIXIN* parent = reinterpret_cast<KIGIT_REPO_MIXIN*>( aPayload );
967
968 wxString progressMessage( aStr );
969 parent->UpdateProgress( aLen, aTotal, progressMessage );
970}
971
972
973extern "C" int progress_cb( const char* str, int len, void* aPayload )
974{
975 KIGIT_REPO_MIXIN* parent = reinterpret_cast<KIGIT_REPO_MIXIN*>( aPayload );
976
977 if( parent->GetCommon()->IsCancelled() )
978 {
979 wxLogTrace( traceGit, "Progress CB cancelled" );
980 return GIT_EUSER;
981 }
982
983 wxString progressMessage( str, len );
984 parent->UpdateProgress( 0, 0, progressMessage );
985
986 return 0;
987}
988
989
990extern "C" int transfer_progress_cb( const git_transfer_progress* aStats, void* aPayload )
991{
992 KIGIT_REPO_MIXIN* parent = reinterpret_cast<KIGIT_REPO_MIXIN*>( aPayload );
993
994 wxString progressMessage = wxString::Format( _( "Received %u of %u objects" ),
995 aStats->received_objects,
996 aStats->total_objects );
997 if( parent->GetCommon()->IsCancelled() )
998 {
999 wxLogTrace( traceGit, "Transfer progress cancelled" );
1000 return GIT_EUSER;
1001 }
1002
1003 parent->UpdateProgress( aStats->received_objects, aStats->total_objects, progressMessage );
1004
1005 return 0;
1006}
1007
1008
1009extern "C" int update_cb( const char* aRefname, const git_oid* aFirst, const git_oid* aSecond,
1010 void* aPayload )
1011{
1012 constexpr int cstring_len = 8;
1013 char a_str[cstring_len + 1];
1014 char b_str[cstring_len + 1];
1015
1016 KIGIT_REPO_MIXIN* parent = reinterpret_cast<KIGIT_REPO_MIXIN*>( aPayload );
1017 wxString status;
1018
1019 git_oid_tostr( b_str, cstring_len, aSecond );
1020
1021#if ( LIBGIT2_VER_MAJOR >= 1 ) || ( LIBGIT2_VER_MINOR >= 99 )
1022 if( !git_oid_is_zero( aFirst ) )
1023#else
1024 if( !git_oid_iszero( aFirst ) )
1025#endif
1026 {
1027 git_oid_tostr( a_str, cstring_len, aFirst );
1028 status = wxString::Format( _( "* [updated] %s..%s %s" ), a_str, b_str, aRefname );
1029 }
1030 else
1031 {
1032 status = wxString::Format( _( "* [new] %s %s" ), b_str, aRefname );
1033 }
1034
1035 parent->UpdateProgress( 0, 0, status );
1036
1037 return 0;
1038}
1039
1040
1041extern "C" int push_transfer_progress_cb( unsigned int aCurrent, unsigned int aTotal, size_t aBytes,
1042 void* aPayload )
1043{
1044 long long progress = 100;
1045 KIGIT_REPO_MIXIN* parent = reinterpret_cast<KIGIT_REPO_MIXIN*>( aPayload );
1046
1047 if( aTotal != 0 )
1048 {
1049 progress = ( aCurrent * 100ll ) / aTotal;
1050 }
1051
1052 wxString progressMessage = wxString::Format( _( "Writing objects: %lld%% (%u/%u), %zu bytes" ),
1053 progress, aCurrent, aTotal, aBytes );
1054 parent->UpdateProgress( aCurrent, aTotal, progressMessage );
1055
1056 return 0;
1057}
1058
1059
1060extern "C" int push_update_reference_cb( const char* aRefname, const char* aStatus, void* aPayload )
1061{
1062 KIGIT_REPO_MIXIN* parent = reinterpret_cast<KIGIT_REPO_MIXIN*>( aPayload );
1063 wxString status( aStatus );
1064
1065 if( !status.IsEmpty() )
1066 {
1067 wxString statusMessage = wxString::Format( _( "* [rejected] %s (%s)" ), aRefname, aStatus );
1068 parent->UpdateProgress( 0, 0, statusMessage );
1069 }
1070 else
1071 {
1072 wxString statusMessage = wxString::Format( _( "[updated] %s" ), aRefname );
1073 parent->UpdateProgress( 0, 0, statusMessage );
1074 }
1075
1076 return 0;
1077}
1078
1079
1080extern "C" int credentials_cb( git_cred** aOut, const char* aUrl, const char* aUsername,
1081 unsigned int aAllowedTypes, void* aPayload )
1082{
1083 KIGIT_REPO_MIXIN* parent = reinterpret_cast<KIGIT_REPO_MIXIN*>( aPayload );
1084 KIGIT_COMMON* common = parent->GetCommon();
1085
1086 wxLogTrace( traceGit, "Credentials callback for %s, testing %d", aUrl, aAllowedTypes );
1087
1089 {
1090 wxLogTrace( traceGit, "Local repository, no credentials needed" );
1091 return GIT_PASSTHROUGH;
1092 }
1093
1094 if( aAllowedTypes & GIT_CREDENTIAL_USERNAME
1095 && !( parent->TestedTypes() & GIT_CREDENTIAL_USERNAME ) )
1096 {
1097 wxString username = parent->GetUsername().Trim().Trim( false );
1098 wxLogTrace( traceGit, "Username credential for %s at %s with allowed type %d",
1099 username, aUrl, aAllowedTypes );
1100
1101 if( git_credential_username_new( aOut, username.ToStdString().c_str() ) != GIT_OK )
1102 {
1103 wxLogTrace( traceGit, "Failed to create username credential for %s: %s",
1104 username, KIGIT_COMMON::GetLastGitError() );
1105 }
1106 else
1107 {
1108 wxLogTrace( traceGit, "Created username credential for %s", username );
1109 }
1110
1111 parent->TestedTypes() |= GIT_CREDENTIAL_USERNAME;
1112 }
1114 && ( aAllowedTypes & GIT_CREDENTIAL_USERPASS_PLAINTEXT )
1115 && !( parent->TestedTypes() & GIT_CREDENTIAL_USERPASS_PLAINTEXT )
1116 && !parent->GetUsername().IsEmpty() )
1117 {
1118 // Plaintext authentication
1119 wxLogTrace( traceGit, "Plaintext authentication for %s at %s with allowed type %d",
1120 parent->GetUsername(), aUrl, aAllowedTypes );
1121 return common->HandlePlaintextAuthentication( aOut, parent->GetUsername() );
1122 }
1124 && ( aAllowedTypes & GIT_CREDENTIAL_SSH_KEY )
1125 && !( parent->TestedTypes() & GIT_CREDENTIAL_SSH_KEY ) )
1126 {
1127 // SSH key authentication
1128 int result = common->HandleSSHKeyAuthentication( aOut, parent->GetUsername() );
1129
1130 // Translate exhausted-keys PASSTHROUGH into a proper auth error so the
1131 // retry loop runs and libgit2 doesn't emit "no callback set".
1132 if( result == GIT_PASSTHROUGH )
1133 {
1134 git_error_clear();
1135 git_error_set_str( GIT_ERROR_NET, _( "Unable to authenticate" ).mbc_str() );
1136 common->SetAuthFailure();
1137 return GIT_EAUTH;
1138 }
1139
1140 return result;
1141 }
1142 else
1143 {
1144 // If we didn't find anything to try, then we don't have a callback set that the
1145 // server likes
1146 if( !parent->TestedTypes() )
1147 return GIT_PASSTHROUGH;
1148
1149 git_error_clear();
1150 git_error_set_str( GIT_ERROR_NET, _( "Unable to authenticate" ).mbc_str() );
1151
1152 // Otherwise, we did try something but we failed, so return an authentication error
1153 common->SetAuthFailure();
1154 return GIT_EAUTH;
1155 }
1156
1157 return GIT_OK;
1158};
virtual void UpdateProgress(int aCurrent, int aTotal, const wxString &aMessage)
std::mutex m_gitActionMutex
git_repository * m_repo
std::vector< wxString > GetBranchNames() const
GIT_CONN_TYPE GetConnType() const
unsigned m_testedTypes
static wxString GetLastGitError()
wxString GetCurrentBranchName() const
wxString GetGitRootDirectory() const
void SetSSHKey(const wxString &aSSHKey)
bool IsCancelled() const
static const unsigned KIGIT_CREDENTIAL_SSH_AGENT
int HandlePlaintextAuthentication(git_cred **aOut, const wxString &aUsername)
std::vector< wxString > GetProjectDirs()
Return a vector of project files in the repository.
wxString GetRemoteNameOrDefault() const
Returns GetRemotename() when non-empty, otherwise "origin".
git_repository * GetRepo() const
wxString GetProjectDir() const
Get the project directory path.
wxString GetNextPublicKey()
KIGIT_COMMON(git_repository *aRepo)
std::pair< std::set< wxString >, std::set< wxString > > GetDifferentFiles() const
Return a pair of sets of files that differ locally from the remote repository The first set is files ...
std::vector< wxString > m_publicKeys
wxString m_projectDir
bool HasPushAndPullRemote() const
GIT_CONN_TYPE m_connType
void UpdateCurrentBranchInfo()
bool HasLocalCommits() const
wxString GetPassword()
int HandleSSHAgentAuthentication(git_cred **aOut, const wxString &aUsername)
wxString GetUpstreamShorthand() const
Returns the upstream shorthand for the current branch (e.g.
wxString GetRemotename() const
int HandleSSHKeyAuthentication(git_cred **aOut, const wxString &aUsername)
wxString GetUsername() const
Get the username.
unsigned & TestedTypes()
Return the connection types that have been tested for authentication.
KIGIT_COMMON * GetCommon() const
Get the common object.
KIGIT_COMMON::GIT_CONN_TYPE GetConnType() const
Get the connection type.
#define _(s)
const wxChar *const traceGit
Flag to enable Git debugging output.
int fetchhead_foreach_cb(const char *, const char *, const git_oid *aOID, unsigned int aIsMerge, void *aPayload)
int progress_cb(const char *str, int len, void *aPayload)
int push_update_reference_cb(const char *aRefname, const char *aStatus, void *aPayload)
int update_cb(const char *aRefname, const git_oid *aFirst, const git_oid *aSecond, void *aPayload)
int transfer_progress_cb(const git_transfer_progress *aStats, void *aPayload)
int credentials_cb(git_cred **aOut, const char *aUrl, const char *aUsername, unsigned int aAllowedTypes, void *aPayload)
void clone_progress_cb(const char *aStr, size_t aLen, size_t aTotal, void *aPayload)
int push_transfer_progress_cb(unsigned int aCurrent, unsigned int aTotal, size_t aBytes, void *aPayload)
std::unique_ptr< git_tree, decltype([](git_tree *aTree) { git_tree_free(aTree); })> GitTreePtr
A unique pointer for git_tree objects with automatic cleanup.
std::unique_ptr< git_revwalk, decltype([](git_revwalk *aWalker) { git_revwalk_free(aWalker); })> GitRevWalkPtr
A unique pointer for git_revwalk objects with automatic cleanup.
std::unique_ptr< git_commit, decltype([](git_commit *aCommit) { git_commit_free(aCommit); })> GitCommitPtr
A unique pointer for git_commit objects with automatic cleanup.
std::unique_ptr< git_reference, decltype([](git_reference *aRef) { git_reference_free(aRef); })> GitReferencePtr
A unique pointer for git_reference objects with automatic cleanup.
std::unique_ptr< git_strarray, decltype([](git_strarray *aArray) { git_strarray_free(aArray); })> GitStrArrayPtr
A unique pointer for git_strarray objects with automatic cleanup.
std::unique_ptr< git_branch_iterator, decltype([](git_branch_iterator *aIter) { git_branch_iterator_free(aIter); })> GitBranchIteratorPtr
A unique pointer for git_branch_iterator objects with automatic cleanup.
std::unique_ptr< git_remote, decltype([](git_remote *aRemote) { git_remote_free(aRemote); })> GitRemotePtr
A unique pointer for git_remote objects with automatic cleanup.
bool GetSecret(const wxString &aService, const wxString &aKey, wxString &aSecret)
std::string path
wxString result
Test unit parsing edge cases and error handling.
int delta
wxLogTrace helper definitions.