30#include <wx/filename.h>
32#include <wx/textfile.h>
80 const char* workdir = git_repository_workdir(
m_repo );
83 return wxString( workdir );
92 wxCHECK(
m_repo, wxEmptyString );
94 git_reference* head =
nullptr;
96 if( git_repository_head( &head,
m_repo ) != GIT_OK )
101 if( !git_reference_is_branch( head ) )
102 return wxEmptyString;
104 git_reference* upstream =
nullptr;
106 if( git_branch_upstream( &upstream, head ) == GIT_OK )
109 const char* shorthand = git_reference_shorthand( upstream );
112 return wxString::FromUTF8( shorthand );
117 const char* branch_shorthand = git_reference_shorthand( head );
119 if( !branch_shorthand )
120 return wxEmptyString;
128 wxCHECK(
m_repo, wxEmptyString );
129 git_reference* head =
nullptr;
131 int retval = git_repository_head( &head,
m_repo );
133 if( retval && retval != GIT_EUNBORNBRANCH && retval != GIT_ENOTFOUND )
134 return wxEmptyString;
137 git_reference* branch;
139 if( git_reference_resolve( &branch, head ) )
141 wxLogTrace(
traceGit,
"Failed to resolve branch" );
142 return wxEmptyString;
146 const char* branchName =
"";
148 if( git_branch_name( &branchName, branch ) )
150 wxLogTrace(
traceGit,
"Failed to get branch name" );
151 return wxEmptyString;
154 return wxString( branchName );
182 std::vector<wxString> branchNames;
183 std::map<git_time_t, wxString> branchNamesMap;
186 git_branch_iterator* branchIterator =
nullptr;
188 if( git_branch_iterator_new( &branchIterator,
m_repo, GIT_BRANCH_LOCAL ) )
190 wxLogTrace(
traceGit,
"Failed to get branch iterator" );
195 git_reference* branchReference =
nullptr;
196 git_branch_t branchType;
198 while( git_branch_next( &branchReference, &branchType, branchIterator ) != GIT_ITEROVER )
200 const char* branchName =
"";
203 if( git_branch_name( &branchName, branchReference ) )
205 wxLogTrace(
traceGit,
"Failed to get branch name in iter loop" );
209 const git_oid* commitId = git_reference_target( branchReference );
211 git_commit* commit =
nullptr;
213 if( git_commit_lookup( &commit,
m_repo, commitId ) )
215 wxLogTrace(
traceGit,
"Failed to get commit in iter loop" );
220 git_time_t commitTime = git_commit_time( commit );
222 if( git_branch_is_head( branchReference ) )
223 firstName = branchName;
225 branchNamesMap.emplace( commitTime, branchName );
229 if( !firstName.IsEmpty() )
230 branchNames.push_back( firstName );
233 for(
auto rit = branchNamesMap.rbegin(); rit != branchNamesMap.rend(); ++rit )
234 branchNames.push_back( rit->second );
243 std::vector<wxString> projDirs;
249 if( git_reference_name_to_id( &oid,
m_repo,
"HEAD" ) != GIT_OK )
255 if( git_commit_lookup( &commit,
m_repo, &oid ) != GIT_OK )
263 if( git_commit_tree( &tree, commit ) != GIT_OK )
273 tree, GIT_TREEWALK_PRE,
274 [](
const char* root,
const git_tree_entry* entry,
void* payload )
276 std::vector<wxString>* prjs =
static_cast<std::vector<wxString>*
>( payload );
277 wxFileName root_fn( git_tree_entry_name( entry ) );
279 root_fn.SetPath( root );
281 if( git_tree_entry_type( entry ) == GIT_OBJECT_BLOB
282 && ( ( root_fn.GetExt() ==
"kicad_pro" ) || ( root_fn.GetExt() ==
"pro" ) ) )
284 prjs->push_back( root_fn.GetFullPath() );
291 std::sort( projDirs.begin(), projDirs.end(),
292 [](
const wxString& a,
const wxString& b )
294 int a_freq = a.Freq( wxFileName::GetPathSeparator() );
295 int b_freq = b.Freq( wxFileName::GetPathSeparator() );
297 if( a_freq == b_freq )
300 return a_freq < b_freq;
310 std::pair<std::set<wxString>, std::set<wxString>> modified_files;
313 return modified_files;
315 git_reference* head =
nullptr;
316 git_reference* remote_head =
nullptr;
318 if( git_repository_head( &head,
m_repo ) != GIT_OK )
320 wxLogTrace(
traceGit,
"Failed to get modified HEAD" );
321 return modified_files;
326 if( git_branch_upstream( &remote_head, head ) != GIT_OK )
331 wxLogTrace(
traceGit,
"Failed to get modified remote HEAD" );
332 return modified_files;
337 const git_oid* head_oid = git_reference_target( head );
338 const git_oid* remote_oid = git_reference_target( remote_head );
340 if( !head_oid || !remote_oid )
341 return modified_files;
344 [
this](
const git_oid* aOid ) -> git_tree*
346 git_commit* commit =
nullptr;
348 if( git_commit_lookup( &commit,
m_repo, aOid ) != GIT_OK )
350 wxLogTrace(
traceGit,
"Failed to lookup commit for diff: %s",
356 git_tree* tree =
nullptr;
358 if( git_commit_tree( &tree, commit ) != GIT_OK )
360 wxLogTrace(
traceGit,
"Failed to get commit tree for diff: %s",
368 git_tree* head_tree = load_tree( head_oid );
371 git_tree* remote_tree = load_tree( remote_oid );
374 if( !head_tree || !remote_tree )
375 return modified_files;
384 if( git_merge_base( &base_oid,
m_repo, head_oid, remote_oid ) != GIT_OK )
386 wxLogTrace(
traceGit,
"No merge base between local and remote: %s",
388 return modified_files;
391 git_tree* base_tree = load_tree( &base_oid );
395 return modified_files;
398 [
this]( git_tree* aOldTree, git_tree* aNewTree, std::set<wxString>& aOut )
403 git_diff_options localOpts;
404 git_diff_init_options( &localOpts, GIT_DIFF_OPTIONS_VERSION );
406 git_diff* diff =
nullptr;
408 if( git_diff_tree_to_tree( &diff,
m_repo, aOldTree, aNewTree, &localOpts )
411 wxLogTrace(
traceGit,
"Failed to diff trees: %s",
417 [&aOut](
const git_diff_delta& aDelta )
419 if( aDelta.new_file.path )
420 aOut.insert( wxString::FromUTF8( aDelta.new_file.path ) );
422 if( aDelta.old_file.path )
423 aOut.insert( wxString::FromUTF8( aDelta.old_file.path ) );
426 git_diff_free( diff );
429 collect_paths( base_tree, head_tree, modified_files.first );
430 collect_paths( base_tree, remote_tree, modified_files.second );
436 std::set<wxString> actuallyDifferent;
437 collect_paths( head_tree, remote_tree, actuallyDifferent );
439 auto filterToDifferent = [&]( std::set<wxString>& aSet )
441 for(
auto it = aSet.begin(); it != aSet.end(); )
442 it = actuallyDifferent.count( *it ) ? std::next( it ) : aSet.erase( it );
445 filterToDifferent( modified_files.first );
446 filterToDifferent( modified_files.second );
448 return modified_files;
457 git_reference* head =
nullptr;
458 git_reference* remote_head =
nullptr;
460 if( git_repository_head( &head,
m_repo ) != GIT_OK )
468 if( git_branch_upstream( &remote_head, head ) != GIT_OK )
476 const git_oid* head_oid = git_reference_target( head );
477 const git_oid* remote_oid = git_reference_target( remote_head );
478 git_revwalk* walker =
nullptr;
480 if( git_revwalk_new( &walker,
m_repo ) != GIT_OK )
488 if( !head_oid || git_revwalk_push( walker, head_oid ) != GIT_OK )
494 if( remote_oid && git_revwalk_hide( walker, remote_oid ) != GIT_OK )
503 if( git_revwalk_next( &oid, walker ) != GIT_OK )
521 [
this](
const char* aName ) ->
bool
523 git_remote* remote =
nullptr;
525 if( git_remote_lookup( &remote,
m_repo, aName ) != GIT_OK )
530 const char* fetch_url = git_remote_url( remote );
531 const char* push_url = git_remote_pushurl( remote );
535 push_url = fetch_url;
537 return fetch_url && push_url;
542 if( checkRemote( preferred.c_str() ) )
545 if( preferred !=
"origin" && checkRemote(
"origin" ) )
548 git_strarray remotes = {
nullptr, 0 };
550 if( git_remote_list( &remotes,
m_repo ) != GIT_OK )
552 wxLogTrace(
traceGit,
"Failed to enumerate remotes for haspushpull" );
558 for(
size_t ii = 0; ii < remotes.count; ++ii )
560 if( checkRemote( remotes.strings[ii] ) )
572 if( remoteName.IsEmpty() )
573 remoteName = wxS(
"origin" );
581 wxCHECK(
m_repo, wxEmptyString );
584 git_reference* head =
nullptr;
585 git_reference* upstream =
nullptr;
587 if( git_repository_head( &head,
m_repo ) != GIT_OK )
595 if( git_branch_upstream( &upstream, head ) != GIT_OK )
598 git_strarray remotes = {
nullptr, 0 };
600 if( git_remote_list( &remotes,
m_repo ) == GIT_OK )
605 if( remotes.count == 1 )
607 retval = remotes.strings[0];
611 for(
size_t ii = 0; ii < remotes.count; ++ii )
613 if( strcmp( remotes.strings[ii],
"origin" ) == 0 )
615 retval = remotes.strings[ii];
621 git_strarray_dispose( &remotes );
630 git_remote* remote =
nullptr;
632 if( git_remote_lookup( &remote,
m_repo,
"origin" ) == GIT_OK )
634 retval = git_remote_name( remote );
635 git_remote_free( remote );
639 wxLogTrace(
traceGit,
"Failed to get remote name from default remote: %s",
648 git_buf remote_name = GIT_BUF_INIT_CONST(
nullptr, 0 );
650 if( git_branch_remote_name( &remote_name,
m_repo, git_reference_name( upstream ) ) == GIT_OK )
652 retval = remote_name.ptr;
653 git_buf_dispose( &remote_name );
658 "Failed to get remote name from upstream branch: %s",
680 return wxEmptyString;
684 if(
const char* workdir = git_repository_workdir(
m_repo ) )
685 return wxString::FromUTF8( workdir );
687 if(
const char*
path = git_repository_path(
m_repo ) )
688 return wxString::FromUTF8(
path );
690 return wxEmptyString;
698 wxFileName keyFile( wxGetHomeDir(), wxEmptyString );
699 keyFile.AppendDir(
".ssh" );
700 keyFile.SetFullName(
"id_rsa" );
702 if( keyFile.FileExists() )
705 keyFile.SetFullName(
"id_dsa" );
707 if( keyFile.FileExists() )
710 keyFile.SetFullName(
"id_ecdsa" );
712 if( keyFile.FileExists() )
715 keyFile.SetFullName(
"id_ed25519" );
717 if( keyFile.FileExists() )
721 wxFileName sshConfig( wxGetHomeDir(), wxEmptyString );
722 sshConfig.AppendDir(
".ssh" );
723 sshConfig.SetFullName(
"config" );
725 if( sshConfig.FileExists() )
727 wxTextFile configFile( sshConfig.GetFullPath() );
732 for( wxString line = configFile.GetFirstLine(); !configFile.Eof(); line = configFile.GetNextLine() )
734 line.Trim(
false ).Trim(
true );
736 if( line.StartsWith(
"Host " ) )
741 if( line.StartsWith(
"Host" ) && line.Contains(
m_hostname ) )
744 if( match && line.StartsWith(
"IdentityFile" ) )
746 wxString keyPath = line.AfterFirst(
' ' ).Trim(
false ).Trim(
true );
749 if( keyPath.StartsWith(
"~" ) )
750 keyPath.Replace(
"~", wxGetHomeDir(),
false );
753 if( wxFileName::FileExists( keyPath ) )
771 git_remote* remote =
nullptr;
777 if( git_remote_lookup( &remote,
m_repo, remote_name.ToStdString().c_str() ) == GIT_OK )
779 const char* url = git_remote_url( remote );
784 git_remote_free( remote );
796 if( remote.IsEmpty() )
799 if( remote.StartsWith(
"https://" ) || remote.StartsWith(
"http://" ) )
803 else if( remote.StartsWith(
"ssh://" ) || remote.StartsWith(
"git@" ) || remote.StartsWith(
"git+ssh://" )
804 || remote.EndsWith(
".git" ) )
825 size_t atPos = uri.find(
'@' );
827 if( atPos != wxString::npos )
829 size_t protoEnd = uri.find(
"//" );
831 if( protoEnd != wxString::npos )
833 wxString credentials = uri.Mid( protoEnd + 2, atPos - protoEnd - 2 );
834 size_t colonPos = credentials.find(
':' );
836 if( colonPos != wxString::npos )
839 m_password = credentials.Mid( colonPos + 1, credentials.Length() - colonPos - 1 );
855 size_t colonPos =
m_remote.find(
':' );
857 if( colonPos != wxString::npos )
863 size_t hostStart =
m_remote.find(
"://" ) + 2;
864 size_t hostEnd =
m_remote.find(
'/', hostStart );
867 if( hostEnd != wxString::npos )
868 host =
m_remote.Mid( hostStart, hostEnd - hostStart );
872 atPos = host.find(
'@' );
874 if( atPos != wxString::npos )
897 if( sshKey.IsEmpty() )
899 wxLogTrace(
traceGit,
"Finished testing all possible ssh keys" );
901 return GIT_PASSTHROUGH;
904 wxString sshPubKey = sshKey +
".pub";
907 wxLogTrace(
traceGit,
"Testing %s\n", sshKey );
909 if( git_credential_ssh_key_new( aOut, aUsername.mbc_str(), sshPubKey.mbc_str(), sshKey.mbc_str(),
910 password.mbc_str() ) != GIT_OK )
912 wxLogTrace(
traceGit,
"Failed to create SSH key credential for %s: %s",
914 return GIT_PASSTHROUGH;
925 git_credential_userpass_plaintext_new( aOut, aUsername.mbc_str(), password.mbc_str() );
936 if( git_credential_ssh_key_from_agent( aOut, aUsername.mbc_str() ) != GIT_OK )
938 wxLogTrace(
traceGit,
"Failed to create SSH agent credential for %s: %s",
940 return GIT_PASSTHROUGH;
948 const git_oid* aOID,
unsigned int aIsMerge,
void* aPayload )
951 git_oid_cpy( (git_oid*) aPayload, aOID );
957extern "C" void clone_progress_cb(
const char* aStr,
size_t aLen,
size_t aTotal,
void* aPayload )
961 wxString progressMessage( aStr );
966extern "C" int progress_cb(
const char* str,
int len,
void* aPayload )
972 wxLogTrace(
traceGit,
"Progress CB cancelled" );
976 wxString progressMessage( str, len );
987 wxString progressMessage = wxString::Format(
_(
"Received %u of %u objects" ),
988 aStats->received_objects,
989 aStats->total_objects );
992 wxLogTrace(
traceGit,
"Transfer progress cancelled" );
996 parent->
UpdateProgress( aStats->received_objects, aStats->total_objects, progressMessage );
1002extern "C" int update_cb(
const char* aRefname,
const git_oid* aFirst,
const git_oid* aSecond,
1005 constexpr int cstring_len = 8;
1006 char a_str[cstring_len + 1];
1007 char b_str[cstring_len + 1];
1012 git_oid_tostr( b_str, cstring_len, aSecond );
1014#if ( LIBGIT2_VER_MAJOR >= 1 ) || ( LIBGIT2_VER_MINOR >= 99 )
1015 if( !git_oid_is_zero( aFirst ) )
1017 if( !git_oid_iszero( aFirst ) )
1020 git_oid_tostr( a_str, cstring_len, aFirst );
1021 status = wxString::Format(
_(
"* [updated] %s..%s %s" ), a_str, b_str, aRefname );
1025 status = wxString::Format(
_(
"* [new] %s %s" ), b_str, aRefname );
1037 long long progress = 100;
1042 progress = ( aCurrent * 100ll ) / aTotal;
1045 wxString progressMessage = wxString::Format(
_(
"Writing objects: %lld%% (%u/%u), %zu bytes" ),
1046 progress, aCurrent, aTotal, aBytes );
1056 wxString status( aStatus );
1058 if( !status.IsEmpty() )
1060 wxString statusMessage = wxString::Format(
_(
"* [rejected] %s (%s)" ), aRefname, aStatus );
1065 wxString statusMessage = wxString::Format(
_(
"[updated] %s" ), aRefname );
1073extern "C" int credentials_cb( git_cred** aOut,
const char* aUrl,
const char* aUsername,
1074 unsigned int aAllowedTypes,
void* aPayload )
1079 wxLogTrace(
traceGit,
"Credentials callback for %s, testing %d", aUrl, aAllowedTypes );
1083 wxLogTrace(
traceGit,
"Local repository, no credentials needed" );
1084 return GIT_PASSTHROUGH;
1087 if( aAllowedTypes & GIT_CREDENTIAL_USERNAME
1088 && !( parent->
TestedTypes() & GIT_CREDENTIAL_USERNAME ) )
1090 wxString username = parent->
GetUsername().Trim().Trim(
false );
1091 wxLogTrace(
traceGit,
"Username credential for %s at %s with allowed type %d",
1092 username, aUrl, aAllowedTypes );
1094 if( git_credential_username_new( aOut, username.ToStdString().c_str() ) != GIT_OK )
1096 wxLogTrace(
traceGit,
"Failed to create username credential for %s: %s",
1101 wxLogTrace(
traceGit,
"Created username credential for %s", username );
1107 && ( aAllowedTypes & GIT_CREDENTIAL_USERPASS_PLAINTEXT )
1108 && !( parent->
TestedTypes() & GIT_CREDENTIAL_USERPASS_PLAINTEXT )
1112 wxLogTrace(
traceGit,
"Plaintext authentication for %s at %s with allowed type %d",
1117 && ( aAllowedTypes & GIT_CREDENTIAL_SSH_KEY )
1118 && !( parent->
TestedTypes() & GIT_CREDENTIAL_SSH_KEY ) )
1125 if(
result == GIT_PASSTHROUGH )
1128 git_error_set_str( GIT_ERROR_NET,
_(
"Unable to authenticate" ).mbc_str() );
1140 return GIT_PASSTHROUGH;
1143 git_error_set_str( GIT_ERROR_NET,
_(
"Unable to authenticate" ).mbc_str() );
1162 git_object* obj =
nullptr;
1163 std::string refStr( aRef.ToUTF8() );
1165 if( git_revparse_single( &obj, aRepo, refStr.c_str() ) != 0 )
1167 wxLogTrace(
traceGit,
"git_revparse_single failed for ref '%s': %s",
1173 git_tree* tree =
nullptr;
1175 if( git_object_peel(
reinterpret_cast<git_object**
>( &tree ), obj, GIT_OBJECT_TREE ) != 0 )
1177 wxLogTrace(
traceGit,
"git_object_peel to tree failed for '%s': %s",
1187 const std::function<
void(
const git_diff_delta& )>& aCallback )
1192 std::size_t numDeltas = git_diff_num_deltas( aDiff );
1194 for( std::size_t ii = 0; ii < numDeltas; ++ii )
1196 const git_diff_delta*
delta = git_diff_get_delta( aDiff, ii );
1201 aCallback( *
delta );
virtual void UpdateProgress(int aCurrent, int aTotal, const wxString &aMessage)
std::mutex m_gitActionMutex
std::vector< wxString > GetBranchNames() const
void updateConnectionType()
GIT_CONN_TYPE GetConnType() const
static wxString GetLastGitError()
wxString GetCurrentBranchName() const
wxString GetGitRootDirectory() const
void SetSSHKey(const wxString &aSSHKey)
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
bool HasPushAndPullRemote() const
void UpdateCurrentBranchInfo()
bool HasLocalCommits() const
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.
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.
git_tree * ResolveRefToTree(git_repository *aRepo, const wxString &aRef)
Resolve a string ref (branch name, short OID, full OID, tag) to its tree.
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.
void CollectDiffDeltas(git_diff *aDiff, const std::function< void(const git_diff_delta &)> &aCallback)
Walk every delta in a computed diff, invoking aCallback once per delta.
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_object, decltype([](git_object *aObject) { git_object_free(aObject); })> GitObjectPtr
A unique pointer for git_object 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.
wxString result
Test unit parsing edge cases and error handling.
wxLogTrace helper definitions.