49 wxLogTrace(
traceGit,
"GIT_PULL_HANDLER::PerformFetch() - No repository found" );
53 std::unique_lock<std::mutex> lock(
GetCommon()->m_gitActionMutex, std::try_to_lock );
55 if( !aSkipLock && !lock.owns_lock() )
57 wxLogTrace(
traceGit,
"GIT_PULL_HANDLER::PerformFetch() - Could not lock mutex" );
62 git_remote* remote =
nullptr;
64 if( git_remote_lookup( &remote,
GetRepo(),
"origin" ) != 0 )
66 wxLogTrace(
traceGit,
"GIT_PULL_HANDLER::PerformFetch() - Failed to lookup remote 'origin'" );
67 AddErrorString( wxString::Format(
_(
"Could not lookup remote '%s'" ),
"origin" ) );
73 git_remote_callbacks remoteCallbacks;
74 git_remote_init_callbacks( &remoteCallbacks, GIT_REMOTE_CALLBACKS_VERSION );
78 remoteCallbacks.payload =
this;
83 if( git_remote_connect( remote, GIT_DIRECTION_FETCH, &remoteCallbacks,
nullptr,
nullptr ) )
86 wxLogTrace(
traceGit,
"GIT_PULL_HANDLER::PerformFetch() - Failed to connect to remote: %s", errorMsg );
87 AddErrorString( wxString::Format(
_(
"Could not connect to remote '%s': %s" ),
"origin", errorMsg ) );
91 git_fetch_options fetchOptions;
92 git_fetch_init_options( &fetchOptions, GIT_FETCH_OPTIONS_VERSION );
93 fetchOptions.callbacks = remoteCallbacks;
95 if( git_remote_fetch( remote,
nullptr, &fetchOptions,
nullptr ) )
98 wxLogTrace(
traceGit,
"GIT_PULL_HANDLER::PerformFetch() - Failed to fetch from remote: %s", errorMsg );
99 AddErrorString( wxString::Format(
_(
"Could not fetch data from remote '%s': %s" ),
"origin", errorMsg ) );
103 wxLogTrace(
traceGit,
"GIT_PULL_HANDLER::PerformFetch() - Fetch completed successfully" );
111 std::unique_lock<std::mutex> lock(
GetCommon()->m_gitActionMutex, std::try_to_lock );
113 if( !lock.owns_lock() )
115 wxLogTrace(
traceGit,
"GIT_PULL_HANDLER::PerformPull() - Could not lock mutex" );
116 return PullResult::Error;
120 return PullResult::Error;
122 git_oid pull_merge_oid = {};
128 return PullResult::Error;
131 git_annotated_commit* fetchhead_commit;
133 if( git_annotated_commit_lookup( &fetchhead_commit,
GetRepo(), &pull_merge_oid ) )
136 return PullResult::Error;
140 const git_annotated_commit* merge_commits[] = { fetchhead_commit };
141 git_merge_analysis_t merge_analysis;
142 git_merge_preference_t merge_preference = GIT_MERGE_PREFERENCE_NONE;
144 if( git_merge_analysis( &merge_analysis, &merge_preference,
GetRepo(), merge_commits, 1 ) )
147 return PullResult::Error;
150 if( merge_analysis & GIT_MERGE_ANALYSIS_UNBORN )
153 return PullResult::MergeFailed;
157 if( merge_analysis & GIT_MERGE_ANALYSIS_UP_TO_DATE )
159 wxLogTrace(
traceGit,
"GIT_PULL_HANDLER::PerformPull() - Repository is up to date" );
160 git_repository_state_cleanup(
GetRepo() );
161 return PullResult::UpToDate;
165 if( merge_analysis & GIT_MERGE_ANALYSIS_FASTFORWARD )
167 wxLogTrace(
traceGit,
"GIT_PULL_HANDLER::PerformPull() - Fast-forward merge" );
171 if( merge_analysis & GIT_MERGE_ANALYSIS_NORMAL )
173 wxLogTrace(
traceGit,
"GIT_PULL_HANDLER::PerformPull() - Normal merge" );
176 git_config*
config =
nullptr;
180 wxLogTrace(
traceGit,
"GIT_PULL_HANDLER::PerformPull() - Failed to get repository config" );
182 return PullResult::Error;
188 int rebase_value = 0;
189 int ret = git_config_get_bool( &rebase_value,
config,
"pull.rebase" );
192 if( ret == GIT_OK && rebase_value )
194 wxLogTrace(
traceGit,
"GIT_PULL_HANDLER::PerformPull() - Using rebase based on config" );
198 wxLogTrace(
traceGit,
"GIT_PULL_HANDLER::PerformPull() - Using merge based on config" );
202 wxLogTrace(
traceGit,
"GIT_PULL_HANDLER::PerformPull() - Merge needs resolution" );
208const std::vector<std::pair<std::string, std::vector<CommitDetails>>>&
217 if( aMessage.empty() )
220 size_t firstLineEnd = aMessage.find_first_of(
'\n' );
222 if( firstLineEnd != std::string::npos )
223 return aMessage.substr( 0, firstLineEnd );
232 time_t time =
static_cast<time_t
>( aTime.time );
235 localtime_s( &timeInfo, &time );
237 gmtime_r( &time, &timeInfo );
239 strftime( dateBuffer,
sizeof( dateBuffer ),
"%Y-%b-%d %H:%M:%S", &timeInfo );
246 git_reference* rawRef =
nullptr;
249 if( git_repository_head( &rawRef,
GetRepo() ) )
252 return PullResult::Error;
257 git_oid updatedRefOid;
258 const char* currentBranchName = git_reference_name( rawRef );
259 wxString remoteBranchName = wxString::Format(
"refs/remotes/origin/%s",
260 currentBranchName + strlen(
"refs/heads/" ) );
263 if( git_reference_name_to_id( &updatedRefOid,
GetRepo(), remoteBranchName.c_str() ) != GIT_OK )
265 AddErrorString( wxString::Format(
_(
"Could not get reference OID for reference '%s'" ),
266 remoteBranchName ) );
267 return PullResult::Error;
271 git_commit* targetCommit =
nullptr;
273 if( git_commit_lookup( &targetCommit,
GetRepo(), &updatedRefOid ) != GIT_OK )
276 return PullResult::Error;
282 git_tree* targetTree =
nullptr;
284 if( git_commit_tree( &targetTree, targetCommit ) != GIT_OK )
286 git_commit_free( targetCommit );
288 return PullResult::Error;
294 git_checkout_options checkoutOptions;
295 git_checkout_init_options( &checkoutOptions, GIT_CHECKOUT_OPTIONS_VERSION );
296 auto notify_cb = []( git_checkout_notify_t why,
const char*
path,
const git_diff_file* baseline,
297 const git_diff_file* target,
const git_diff_file* workdir,
void* payload ) ->
int
301 case GIT_CHECKOUT_NOTIFY_CONFLICT:
304 case GIT_CHECKOUT_NOTIFY_DIRTY:
307 case GIT_CHECKOUT_NOTIFY_UPDATED:
310 case GIT_CHECKOUT_NOTIFY_UNTRACKED:
313 case GIT_CHECKOUT_NOTIFY_IGNORED:
323 checkoutOptions.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_ALLOW_CONFLICTS;
324 checkoutOptions.notify_flags = GIT_CHECKOUT_NOTIFY_ALL;
325 checkoutOptions.notify_cb = notify_cb;
327 if( git_checkout_tree(
GetRepo(),
reinterpret_cast<git_object*
>( targetTree ), &checkoutOptions ) != GIT_OK )
330 return PullResult::Error;
333 git_reference* updatedRef =
nullptr;
336 if (git_reference_set_target(&updatedRef, rawRef, &updatedRefOid,
nullptr) != GIT_OK)
338 AddErrorString( wxString::Format(
_(
"Failed to update reference '%s' to point to '%s'" ), currentBranchName,
339 git_oid_tostr_s( &updatedRefOid ) ) );
340 return PullResult::Error;
346 if( git_repository_state_cleanup(
GetRepo() ) != GIT_OK )
348 AddErrorString(
_(
"Failed to clean up repository state after fast-forward." ) );
349 return PullResult::Error;
352 git_revwalk* revWalker =
nullptr;
355 if( git_revwalk_new( &revWalker,
GetRepo() ) != GIT_OK )
358 return PullResult::Error;
362 git_revwalk_sorting( revWalker, GIT_SORT_TIME );
364 if( git_revwalk_push_glob( revWalker, currentBranchName ) != GIT_OK )
367 return PullResult::Error;
370 std::pair<std::string, std::vector<CommitDetails>>& branchCommits =
m_fetchResults.emplace_back();
371 branchCommits.first = currentBranchName;
375 while( git_revwalk_next( &commitOid, revWalker ) == GIT_OK )
377 git_commit* commit =
nullptr;
379 if( git_commit_lookup( &commit,
GetRepo(), &commitOid ) )
382 git_oid_tostr_s( &commitOid ) ) );
383 return PullResult::Error;
389 details.
m_sha = git_oid_tostr_s( &commitOid );
391 details.
m_author = git_commit_author( commit )->name;
394 branchCommits.second.push_back( details );
397 return PullResult::FastForward;
402 size_t aMergeHeadsCount )
404 git_merge_options merge_opts;
405 git_merge_options_init( &merge_opts, GIT_MERGE_OPTIONS_VERSION );
407 git_checkout_options checkout_opts;
408 git_checkout_init_options( &checkout_opts, GIT_CHECKOUT_OPTIONS_VERSION );
410 checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
412 if( git_merge(
GetRepo(), aMergeHeads, aMergeHeadsCount, &merge_opts, &checkout_opts ) )
415 return PullResult::Error;
419 git_index* index =
nullptr;
421 if( git_repository_index( &index,
GetRepo() ) )
424 return PullResult::Error;
430 git_index_conflict_iterator* conflicts =
nullptr;
432 if( git_index_conflict_iterator_new( &conflicts, index ) )
435 return PullResult::Error;
440 const git_index_entry* ancestor =
nullptr;
441 const git_index_entry* our =
nullptr;
442 const git_index_entry* their =
nullptr;
443 std::vector<ConflictData> conflict_data;
445 while( git_index_conflict_next( &ancestor, &our, &their, conflicts ) == 0 )
448 if( ancestor && our && their )
451 conflict_datum.
filename = our->path;
452 conflict_datum.
our_oid = our->id;
460 conflict_data.push_back( conflict_datum );
463 else if( !ancestor && our && their )
466 conflict_datum.
filename = our->path;
467 conflict_datum.
our_oid = our->id;
475 conflict_data.push_back( conflict_datum );
478 else if( their && !our )
481 git_index_add( index, their );
484 else if( our && !their )
487 git_index_add( index, our );
491 wxLogError( wxS(
"Unexpected conflict state" ) );
495 if( conflict_data.empty() )
497 git_index_conflict_cleanup( index );
498 git_index_write( index );
501 return conflict_data.empty() ? PullResult::Success : PullResult::MergeFailed;
508 git_reference* head_ref =
nullptr;
510 if( git_repository_head( &head_ref,
GetRepo() ) )
513 wxLogTrace(
traceGit,
"GIT_PULL_HANDLER::handleRebase() - Failed to get HEAD: %s", errorMsg );
514 return PullResult::Error;
520 git_rebase* rebase =
nullptr;
521 git_rebase_options rebase_opts = GIT_REBASE_OPTIONS_INIT;
522 rebase_opts.checkout_options.checkout_strategy = GIT_CHECKOUT_SAFE;
524 if( git_rebase_init( &rebase,
GetRepo(),
nullptr,
nullptr, aMergeHeads[0], &rebase_opts ) )
527 wxLogTrace(
traceGit,
"GIT_PULL_HANDLER::handleRebase() - Failed to initialize rebase: %s", errorMsg );
528 return PullResult::Error;
532 git_rebase_operation* operation =
nullptr;
534 while( git_rebase_next( &operation, rebase ) != GIT_ITEROVER )
537 git_index* index =
nullptr;
538 if( git_repository_index( &index,
GetRepo() ) )
540 wxLogTrace(
traceGit,
"GIT_PULL_HANDLER::handleRebase() - Failed to get index: %s",
542 return PullResult::Error;
546 if( git_index_has_conflicts( index ) )
549 git_rebase_abort( rebase );
551 return PullResult::MergeFailed;
555 git_signature* committer =
nullptr;
557 if( git_signature_default( &committer,
GetRepo() ) )
559 wxLogTrace(
traceGit,
"GIT_PULL_HANDLER::handleRebase() - Failed to create signature: %s",
561 return PullResult::Error;
566 if( git_rebase_commit( &commit_id, rebase,
nullptr, committer,
nullptr,
nullptr ) != GIT_OK )
569 wxLogTrace(
traceGit,
"GIT_PULL_HANDLER::handleRebase() - Failed to commit operation: %s", errorMsg );
570 git_rebase_abort( rebase );
571 return PullResult::Error;
576 if( git_rebase_finish( rebase,
nullptr ) )
578 wxLogTrace(
traceGit,
"GIT_PULL_HANDLER::handleRebase() - Failed to finish rebase: %s",
580 return PullResult::Error;
583 wxLogTrace(
traceGit,
"GIT_PULL_HANDLER::handleRebase() - Rebase completed successfully" );
584 git_repository_state_cleanup(
GetRepo() );
585 return PullResult::Success;
bool PerformFetch(bool aSkipLock=false)
std::string getFirstLineFromCommitMessage(const std::string &aMessage)
PullResult handleRebase(const git_annotated_commit **aMergeHeads, size_t aMergeHeadsCount)
PullResult handleFastForward()
PullResult handleMerge(const git_annotated_commit **aMergeHeads, size_t aMergeHeadsCount)
const std::vector< std::pair< std::string, std::vector< CommitDetails > > > & GetFetchResults() const
GIT_PULL_HANDLER(KIGIT_COMMON *aCommon)
void UpdateProgress(int aCurrent, int aTotal, const wxString &aMessage) override
std::string getFormattedCommitDate(const git_time &aTime)
std::vector< std::pair< std::string, std::vector< CommitDetails > > > m_fetchResults
static wxString GetLastGitError()
void AddErrorString(const wxString aErrorString)
git_repository * GetRepo() const
Get a pointer to the git repository.
unsigned & TestedTypes()
Return the connection types that have been tested for authentication.
KIGIT_COMMON * GetCommon() const
Get the common object.
void ResetNextKey()
Reset the next public key to test.
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 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)
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_annotated_commit, decltype([](git_annotated_commit *aCommit) { git_annotated_commit_free(aCommit) GitAnnotatedCommitPtr
A unique pointer for git_annotated_commit objects with automatic cleanup.
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_index, decltype([](git_index *aIndex) { git_index_free(aIndex) GitIndexPtr
A unique pointer for git_index objects with automatic cleanup.
std::unique_ptr< git_signature, decltype([](git_signature *aSignature) { git_signature_free(aSignature) GitSignaturePtr
A unique pointer for git_signature objects with automatic cleanup.
std::unique_ptr< git_rebase, decltype([](git_rebase *aRebase) { git_rebase_free(aRebase) GitRebasePtr
A unique pointer for git_rebase objects with automatic cleanup.
std::unique_ptr< git_config, decltype([](git_config *aConfig) { git_config_free(aConfig) GitConfigPtr
A unique pointer for git_config 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_index_conflict_iterator, decltype([](git_index_conflict_iterator *aIterator) { git_index_conflict_iterator_free(aIterator) GitIndexConflictIteratorPtr
A unique pointer for git_index_conflict_iterator 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_remote, decltype([](git_remote *aRemote) { git_remote_free(aRemote) GitRemotePtr
A unique pointer for git_remote objects with automatic cleanup.
git_time_t their_commit_time
git_time_t our_commit_time
wxLogTrace helper definitions.