42 git_remote* remote =
nullptr;
44 if( git_remote_lookup( &remote,
m_repo,
"origin" ) != 0 )
46 AddErrorString( wxString::Format(
_(
"Could not lookup remote '%s'" ),
"origin" ) );
50 git_remote_callbacks remoteCallbacks;
51 git_remote_init_callbacks( &remoteCallbacks, GIT_REMOTE_CALLBACKS_VERSION );
55 remoteCallbacks.payload =
this;
57 if( git_remote_connect( remote, GIT_DIRECTION_FETCH, &remoteCallbacks,
nullptr,
nullptr ) )
59 git_remote_free( remote );
60 AddErrorString( wxString::Format(
_(
"Could not connect to remote '%s': %s" ),
"origin",
61 git_error_last()->message ) );
65 git_fetch_options fetchOptions;
66 git_fetch_init_options( &fetchOptions, GIT_FETCH_OPTIONS_VERSION );
67 fetchOptions.callbacks = remoteCallbacks;
69 if( git_remote_fetch( remote,
nullptr, &fetchOptions,
nullptr ) )
71 git_remote_free( remote );
72 AddErrorString( wxString::Format(
_(
"Could not fetch data from remote '%s': %s" ),
73 "origin", git_error_last()->message ) );
77 git_remote_free( remote );
88 return PullResult::Error;
90 git_oid pull_merge_oid = {};
95 return PullResult::Error;
98 git_annotated_commit* fetchhead_commit;
100 if( git_annotated_commit_lookup( &fetchhead_commit,
m_repo, &pull_merge_oid ) )
103 return PullResult::Error;
106 const git_annotated_commit* merge_commits[] = { fetchhead_commit };
107 git_merge_analysis_t merge_analysis;
108 git_merge_preference_t merge_preference = GIT_MERGE_PREFERENCE_NONE;
110 if( git_merge_analysis( &merge_analysis, &merge_preference,
m_repo, merge_commits, 1 ) )
113 git_annotated_commit_free( fetchhead_commit );
114 return PullResult::Error;
117 if( !( merge_analysis & GIT_MERGE_ANALYSIS_NORMAL ) )
118 git_annotated_commit_free( fetchhead_commit );
120 if( merge_analysis & GIT_MERGE_ANALYSIS_UNBORN )
123 return PullResult::MergeFailed;
127 if( merge_analysis & GIT_MERGE_ANALYSIS_UP_TO_DATE )
129 git_repository_state_cleanup(
m_repo );
130 return PullResult::UpToDate;
134 if( merge_analysis & GIT_MERGE_ANALYSIS_FASTFORWARD )
139 if( merge_analysis & GIT_MERGE_ANALYSIS_NORMAL )
142 git_annotated_commit_free( fetchhead_commit );
157 size_t firstLineEnd = aMessage.find_first_of(
'\n' );
159 if( firstLineEnd != std::string::npos )
160 return aMessage.substr( 0, firstLineEnd );
168 time_t time =
static_cast<time_t
>( aTime.time );
169 strftime( dateBuffer,
sizeof( dateBuffer ),
"%Y-%b-%d %H:%M:%S", gmtime( &time ) );
177 git_reference* updatedRef =
nullptr;
179 if( git_repository_head( &updatedRef,
m_repo ) )
182 return PullResult::Error;
185 const char* updatedRefName = git_reference_name( updatedRef );
186 git_reference_free( updatedRef );
188 git_oid updatedRefOid;
189 if( git_reference_name_to_id( &updatedRefOid,
m_repo, updatedRefName ) )
191 AddErrorString( wxString::Format(
_(
"Could not get reference OID for reference '%s'" ), updatedRefName ) );
192 return PullResult::Error;
195 git_checkout_options checkoutOptions;
196 git_checkout_init_options( &checkoutOptions, GIT_CHECKOUT_OPTIONS_VERSION );
197 checkoutOptions.checkout_strategy = GIT_CHECKOUT_SAFE;
198 if( git_checkout_head(
m_repo, &checkoutOptions ) )
201 return PullResult::Error;
205 git_revwalk* revWalker =
nullptr;
206 git_revwalk_new( &revWalker,
m_repo );
207 git_revwalk_sorting( revWalker, GIT_SORT_TIME );
208 git_revwalk_push_glob( revWalker, updatedRefName );
211 while( git_revwalk_next( &commitOid, revWalker ) == 0 )
213 git_commit* commit =
nullptr;
215 if( git_commit_lookup( &commit,
m_repo, &commitOid ) )
217 AddErrorString( wxString::Format(
_(
"Could not lookup commit '{}'" ), git_oid_tostr_s( &commitOid ) ) );
218 git_revwalk_free( revWalker );
219 return PullResult::Error;
223 details.
m_sha = git_oid_tostr_s( &commitOid );
225 details.
m_author = git_commit_author( commit )->name;
228 std::pair<std::string, std::vector<CommitDetails>>& branchCommits =
230 branchCommits.first = updatedRefName;
231 branchCommits.second.push_back( details );
234 git_commit_free( commit );
237 git_revwalk_free( revWalker );
239 git_repository_state_cleanup(
m_repo );
240 return PullResult::FastForward;
245 size_t aMergeHeadsCount )
247 git_merge_options merge_opts;
248 git_merge_options_init( &merge_opts, GIT_MERGE_OPTIONS_VERSION );
250 git_checkout_options checkout_opts;
251 git_checkout_init_options( &checkout_opts, GIT_CHECKOUT_OPTIONS_VERSION );
253 checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
255 if( git_merge(
m_repo, aMergeHeads, aMergeHeadsCount, &merge_opts, &checkout_opts ) )
258 return PullResult::Error;
262 git_index* index =
nullptr;
263 if( git_repository_index( &index,
m_repo ) )
266 return PullResult::Error;
270 git_index_conflict_iterator* conflicts =
nullptr;
271 if( git_index_conflict_iterator_new( &conflicts, index ) )
274 return PullResult::Error;
277 const git_index_entry* ancestor =
nullptr;
278 const git_index_entry* our =
nullptr;
279 const git_index_entry* their =
nullptr;
280 std::vector<ConflictData> conflict_data;
282 while( git_index_conflict_next( &ancestor, &our, &their, conflicts ) == 0 )
285 if( ancestor && our && their )
288 conflict_datum.
filename = our->path;
289 conflict_datum.
our_oid = our->id;
297 conflict_data.push_back( conflict_datum );
300 else if( !ancestor && our && their )
303 conflict_datum.
filename = our->path;
304 conflict_datum.
our_oid = our->id;
312 conflict_data.push_back( conflict_datum );
315 else if( their && !our )
318 git_index_add( index, their );
321 else if( our && !their )
324 git_index_add( index, our );
328 wxLogError( wxS(
"Unexpected conflict state" ) );
332 if( conflict_data.empty() )
334 git_index_conflict_cleanup( index );
335 git_index_write( index );
338 git_index_conflict_iterator_free( conflicts );
339 git_index_free( index );
341 return conflict_data.empty() ? PullResult::Success : PullResult::MergeFailed;
void ReportProgress(int aCurrent, int aTotal, const wxString &aMessage)
GIT_PULL_HANDLER(git_repository *aRepo)
std::string getFirstLineFromCommitMessage(const std::string &aMessage)
PullResult handleFastForward()
PullResult handleMerge(const git_annotated_commit **aMergeHeads, size_t aMergeHeadsCount)
const std::vector< std::pair< std::string, std::vector< CommitDetails > > > & GetFetchResults() const
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
void AddErrorString(const wxString aErrorString)
int fetchhead_foreach_cb(const char *, const char *, const git_oid *aOID, unsigned int aIsMerge, 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)
int progress_cb(const char *str, int len, void *data)
git_time_t their_commit_time
git_time_t our_commit_time