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;
61 if( git_remote_connect( remote, GIT_DIRECTION_FETCH, &remoteCallbacks,
nullptr,
nullptr ) )
63 git_remote_free( remote );
64 AddErrorString( wxString::Format(
_(
"Could not connect to remote '%s': %s" ),
"origin",
65 git_error_last()->message ) );
69 git_fetch_options fetchOptions;
70 git_fetch_init_options( &fetchOptions, GIT_FETCH_OPTIONS_VERSION );
71 fetchOptions.callbacks = remoteCallbacks;
73 if( git_remote_fetch( remote,
nullptr, &fetchOptions,
nullptr ) )
75 git_remote_free( remote );
76 AddErrorString( wxString::Format(
_(
"Could not fetch data from remote '%s': %s" ),
77 "origin", git_error_last()->message ) );
81 git_remote_free( remote );
92 return PullResult::Error;
94 git_oid pull_merge_oid = {};
99 return PullResult::Error;
102 git_annotated_commit* fetchhead_commit;
104 if( git_annotated_commit_lookup( &fetchhead_commit,
m_repo, &pull_merge_oid ) )
107 return PullResult::Error;
110 const git_annotated_commit* merge_commits[] = { fetchhead_commit };
111 git_merge_analysis_t merge_analysis;
112 git_merge_preference_t merge_preference = GIT_MERGE_PREFERENCE_NONE;
114 if( git_merge_analysis( &merge_analysis, &merge_preference,
m_repo, merge_commits, 1 ) )
117 git_annotated_commit_free( fetchhead_commit );
118 return PullResult::Error;
121 if( !( merge_analysis & GIT_MERGE_ANALYSIS_NORMAL ) )
122 git_annotated_commit_free( fetchhead_commit );
124 if( merge_analysis & GIT_MERGE_ANALYSIS_UNBORN )
127 return PullResult::MergeFailed;
131 if( merge_analysis & GIT_MERGE_ANALYSIS_UP_TO_DATE )
133 git_repository_state_cleanup(
m_repo );
134 return PullResult::UpToDate;
138 if( merge_analysis & GIT_MERGE_ANALYSIS_FASTFORWARD )
143 if( merge_analysis & GIT_MERGE_ANALYSIS_NORMAL )
146 git_annotated_commit_free( fetchhead_commit );
155const std::vector<std::pair<std::string, std::vector<CommitDetails>>>&
164 size_t firstLineEnd = aMessage.find_first_of(
'\n' );
166 if( firstLineEnd != std::string::npos )
167 return aMessage.substr( 0, firstLineEnd );
176 time_t time =
static_cast<time_t
>( aTime.time );
177 strftime( dateBuffer,
sizeof( dateBuffer ),
"%Y-%b-%d %H:%M:%S", gmtime( &time ) );
185 git_reference* updatedRef =
nullptr;
187 if( git_repository_head( &updatedRef,
m_repo ) )
190 return PullResult::Error;
193 const char* updatedRefName = git_reference_name( updatedRef );
194 git_reference_free( updatedRef );
196 git_oid updatedRefOid;
198 if( git_reference_name_to_id( &updatedRefOid,
m_repo, updatedRefName ) )
200 AddErrorString( wxString::Format(
_(
"Could not get reference OID for reference '%s'" ),
202 return PullResult::Error;
205 git_checkout_options checkoutOptions;
206 git_checkout_init_options( &checkoutOptions, GIT_CHECKOUT_OPTIONS_VERSION );
207 checkoutOptions.checkout_strategy = GIT_CHECKOUT_SAFE;
208 if( git_checkout_head(
m_repo, &checkoutOptions ) )
211 return PullResult::Error;
215 git_revwalk* revWalker =
nullptr;
216 git_revwalk_new( &revWalker,
m_repo );
217 git_revwalk_sorting( revWalker, GIT_SORT_TIME );
218 git_revwalk_push_glob( revWalker, updatedRefName );
222 while( git_revwalk_next( &commitOid, revWalker ) == 0 )
224 git_commit* commit =
nullptr;
226 if( git_commit_lookup( &commit,
m_repo, &commitOid ) )
229 git_oid_tostr_s( &commitOid ) ) );
230 git_revwalk_free( revWalker );
231 return PullResult::Error;
235 details.
m_sha = git_oid_tostr_s( &commitOid );
237 details.
m_author = git_commit_author( commit )->name;
240 std::pair<std::string, std::vector<CommitDetails>>& branchCommits =
242 branchCommits.first = updatedRefName;
243 branchCommits.second.push_back( details );
246 git_commit_free( commit );
249 git_revwalk_free( revWalker );
251 git_repository_state_cleanup(
m_repo );
252 return PullResult::FastForward;
257 size_t aMergeHeadsCount )
259 git_merge_options merge_opts;
260 git_merge_options_init( &merge_opts, GIT_MERGE_OPTIONS_VERSION );
262 git_checkout_options checkout_opts;
263 git_checkout_init_options( &checkout_opts, GIT_CHECKOUT_OPTIONS_VERSION );
265 checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
267 if( git_merge(
m_repo, aMergeHeads, aMergeHeadsCount, &merge_opts, &checkout_opts ) )
270 return PullResult::Error;
274 git_index* index =
nullptr;
276 if( git_repository_index( &index,
m_repo ) )
279 return PullResult::Error;
283 git_index_conflict_iterator* conflicts =
nullptr;
285 if( git_index_conflict_iterator_new( &conflicts, index ) )
288 return PullResult::Error;
291 const git_index_entry* ancestor =
nullptr;
292 const git_index_entry* our =
nullptr;
293 const git_index_entry* their =
nullptr;
294 std::vector<ConflictData> conflict_data;
296 while( git_index_conflict_next( &ancestor, &our, &their, conflicts ) == 0 )
299 if( ancestor && our && their )
302 conflict_datum.
filename = our->path;
303 conflict_datum.
our_oid = our->id;
311 conflict_data.push_back( conflict_datum );
314 else if( !ancestor && our && their )
317 conflict_datum.
filename = our->path;
318 conflict_datum.
our_oid = our->id;
326 conflict_data.push_back( conflict_datum );
329 else if( their && !our )
332 git_index_add( index, their );
335 else if( our && !their )
338 git_index_add( index, our );
342 wxLogError( wxS(
"Unexpected conflict state" ) );
346 if( conflict_data.empty() )
348 git_index_conflict_cleanup( index );
349 git_index_write( index );
352 git_index_conflict_iterator_free( conflicts );
353 git_index_free( index );
355 return conflict_data.empty() ? PullResult::Success : PullResult::MergeFailed;
void ReportProgress(int aCurrent, int aTotal, const wxString &aMessage)
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
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
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