43 git_remote* remote =
nullptr;
45 if( git_remote_lookup( &remote,
m_repo,
"origin" ) != 0 )
47 AddErrorString( wxString::Format(
_(
"Could not lookup remote '%s'" ),
"origin" ) );
51 git_remote_callbacks remoteCallbacks;
52 git_remote_init_callbacks( &remoteCallbacks, GIT_REMOTE_CALLBACKS_VERSION );
56 remoteCallbacks.payload =
this;
58 if( git_remote_connect( remote, GIT_DIRECTION_FETCH, &remoteCallbacks,
nullptr,
nullptr ) )
60 git_remote_free( remote );
61 AddErrorString( wxString::Format(
_(
"Could not connect to remote '%s': %s" ),
"origin",
62 git_error_last()->message ) );
66 git_fetch_options fetchOptions;
67 git_fetch_init_options( &fetchOptions, GIT_FETCH_OPTIONS_VERSION );
68 fetchOptions.callbacks = remoteCallbacks;
70 if( git_remote_fetch( remote,
nullptr, &fetchOptions,
nullptr ) )
72 git_remote_free( remote );
73 AddErrorString( wxString::Format(
_(
"Could not fetch data from remote '%s': %s" ),
74 "origin", git_error_last()->message ) );
78 git_remote_free( remote );
89 return PullResult::Error;
91 git_oid pull_merge_oid = {};
96 return PullResult::Error;
99 git_annotated_commit* fetchhead_commit;
101 if( git_annotated_commit_lookup( &fetchhead_commit,
m_repo, &pull_merge_oid ) )
104 return PullResult::Error;
107 const git_annotated_commit* merge_commits[] = { fetchhead_commit };
108 git_merge_analysis_t merge_analysis;
109 git_merge_preference_t merge_preference = GIT_MERGE_PREFERENCE_NONE;
111 if( git_merge_analysis( &merge_analysis, &merge_preference,
m_repo, merge_commits, 1 ) )
114 git_annotated_commit_free( fetchhead_commit );
115 return PullResult::Error;
118 if( !( merge_analysis & GIT_MERGE_ANALYSIS_NORMAL ) )
119 git_annotated_commit_free( fetchhead_commit );
121 if( merge_analysis & GIT_MERGE_ANALYSIS_UNBORN )
124 return PullResult::MergeFailed;
128 if( merge_analysis & GIT_MERGE_ANALYSIS_UP_TO_DATE )
130 git_repository_state_cleanup(
m_repo );
131 return PullResult::UpToDate;
135 if( merge_analysis & GIT_MERGE_ANALYSIS_FASTFORWARD )
140 if( merge_analysis & GIT_MERGE_ANALYSIS_NORMAL )
143 git_annotated_commit_free( fetchhead_commit );
152const std::vector<std::pair<std::string, std::vector<CommitDetails>>>&
161 size_t firstLineEnd = aMessage.find_first_of(
'\n' );
163 if( firstLineEnd != std::string::npos )
164 return aMessage.substr( 0, firstLineEnd );
173 time_t time =
static_cast<time_t
>( aTime.time );
174 strftime( dateBuffer,
sizeof( dateBuffer ),
"%Y-%b-%d %H:%M:%S", gmtime( &time ) );
182 git_reference* updatedRef =
nullptr;
184 if( git_repository_head( &updatedRef,
m_repo ) )
187 return PullResult::Error;
190 const char* updatedRefName = git_reference_name( updatedRef );
191 git_reference_free( updatedRef );
193 git_oid updatedRefOid;
195 if( git_reference_name_to_id( &updatedRefOid,
m_repo, updatedRefName ) )
197 AddErrorString( wxString::Format(
_(
"Could not get reference OID for reference '%s'" ),
199 return PullResult::Error;
202 git_checkout_options checkoutOptions;
203 git_checkout_init_options( &checkoutOptions, GIT_CHECKOUT_OPTIONS_VERSION );
204 checkoutOptions.checkout_strategy = GIT_CHECKOUT_SAFE;
205 if( git_checkout_head(
m_repo, &checkoutOptions ) )
208 return PullResult::Error;
212 git_revwalk* revWalker =
nullptr;
213 git_revwalk_new( &revWalker,
m_repo );
214 git_revwalk_sorting( revWalker, GIT_SORT_TIME );
215 git_revwalk_push_glob( revWalker, updatedRefName );
219 while( git_revwalk_next( &commitOid, revWalker ) == 0 )
221 git_commit* commit =
nullptr;
223 if( git_commit_lookup( &commit,
m_repo, &commitOid ) )
226 git_oid_tostr_s( &commitOid ) ) );
227 git_revwalk_free( revWalker );
228 return PullResult::Error;
232 details.
m_sha = git_oid_tostr_s( &commitOid );
234 details.
m_author = git_commit_author( commit )->name;
237 std::pair<std::string, std::vector<CommitDetails>>& branchCommits =
239 branchCommits.first = updatedRefName;
240 branchCommits.second.push_back( details );
243 git_commit_free( commit );
246 git_revwalk_free( revWalker );
248 git_repository_state_cleanup(
m_repo );
249 return PullResult::FastForward;
254 size_t aMergeHeadsCount )
256 git_merge_options merge_opts;
257 git_merge_options_init( &merge_opts, GIT_MERGE_OPTIONS_VERSION );
259 git_checkout_options checkout_opts;
260 git_checkout_init_options( &checkout_opts, GIT_CHECKOUT_OPTIONS_VERSION );
262 checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
264 if( git_merge(
m_repo, aMergeHeads, aMergeHeadsCount, &merge_opts, &checkout_opts ) )
267 return PullResult::Error;
271 git_index* index =
nullptr;
273 if( git_repository_index( &index,
m_repo ) )
276 return PullResult::Error;
280 git_index_conflict_iterator* conflicts =
nullptr;
282 if( git_index_conflict_iterator_new( &conflicts, index ) )
285 return PullResult::Error;
288 const git_index_entry* ancestor =
nullptr;
289 const git_index_entry* our =
nullptr;
290 const git_index_entry* their =
nullptr;
291 std::vector<ConflictData> conflict_data;
293 while( git_index_conflict_next( &ancestor, &our, &their, conflicts ) == 0 )
296 if( ancestor && our && their )
299 conflict_datum.
filename = our->path;
300 conflict_datum.
our_oid = our->id;
308 conflict_data.push_back( conflict_datum );
311 else if( !ancestor && our && their )
314 conflict_datum.
filename = our->path;
315 conflict_datum.
our_oid = our->id;
323 conflict_data.push_back( conflict_datum );
326 else if( their && !our )
329 git_index_add( index, their );
332 else if( our && !their )
335 git_index_add( index, our );
339 wxLogError( wxS(
"Unexpected conflict state" ) );
343 if( conflict_data.empty() )
345 git_index_conflict_cleanup( index );
346 git_index_write( index );
349 git_index_conflict_iterator_free( conflicts );
350 git_index_free( index );
352 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