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" ).ToStdString() );
50 git_remote_callbacks remoteCallbacks = GIT_REMOTE_CALLBACKS_INIT;
54 remoteCallbacks.payload =
this;
56 if( git_remote_connect( remote, GIT_DIRECTION_FETCH, &remoteCallbacks,
nullptr,
nullptr ) )
58 git_remote_free( remote );
59 AddErrorString( wxString::Format(
_(
"Could not connect to remote '%s'" ),
"origin" ).ToStdString() );
63 git_fetch_options fetchOptions = GIT_FETCH_OPTIONS_INIT;
64 fetchOptions.callbacks = remoteCallbacks;
66 if( git_remote_fetch( remote,
nullptr, &fetchOptions,
nullptr ) )
68 git_remote_free( remote );
69 AddErrorString( wxString::Format(
_(
"Could not fetch data from remote '%s'" ),
"origin" ) );
73 git_remote_free( remote );
84 return PullResult::Error;
86 git_oid pull_merge_oid = {};
91 return PullResult::Error;
94 git_annotated_commit* fetchhead_commit;
96 if( git_annotated_commit_lookup( &fetchhead_commit,
m_repo, &pull_merge_oid ) )
99 return PullResult::Error;
102 const git_annotated_commit* merge_commits[] = { fetchhead_commit };
103 git_merge_analysis_t merge_analysis;
104 git_merge_preference_t merge_preference = GIT_MERGE_PREFERENCE_NONE;
106 if( git_merge_analysis( &merge_analysis, &merge_preference,
m_repo, merge_commits, 1 ) )
109 git_annotated_commit_free( fetchhead_commit );
110 return PullResult::Error;
113 if( !( merge_analysis & GIT_MERGE_ANALYSIS_NORMAL ) )
114 git_annotated_commit_free( fetchhead_commit );
116 if( merge_analysis & GIT_MERGE_ANALYSIS_UNBORN )
119 return PullResult::MergeFailed;
123 if( merge_analysis & GIT_MERGE_ANALYSIS_UP_TO_DATE )
125 git_repository_state_cleanup(
m_repo );
126 return PullResult::UpToDate;
130 if( merge_analysis & GIT_MERGE_ANALYSIS_FASTFORWARD )
135 if( merge_analysis & GIT_MERGE_ANALYSIS_NORMAL )
138 git_annotated_commit_free( fetchhead_commit );
153 size_t firstLineEnd = aMessage.find_first_of(
'\n' );
155 if( firstLineEnd != std::string::npos )
156 return aMessage.substr( 0, firstLineEnd );
164 time_t time =
static_cast<time_t
>( aTime.time );
165 strftime( dateBuffer,
sizeof( dateBuffer ),
"%Y-%b-%d %H:%M:%S", gmtime( &time ) );
173 git_reference* updatedRef =
nullptr;
175 if( git_repository_head( &updatedRef,
m_repo ) )
178 return PullResult::Error;
181 const char* updatedRefName = git_reference_name( updatedRef );
182 git_reference_free( updatedRef );
184 git_oid updatedRefOid;
185 if( git_reference_name_to_id( &updatedRefOid,
m_repo, updatedRefName ) )
187 AddErrorString( wxString::Format(
_(
"Could not get reference OID for reference '%s'" ), updatedRefName ) );
188 return PullResult::Error;
191 git_checkout_options checkoutOptions = GIT_CHECKOUT_OPTIONS_INIT;
192 checkoutOptions.checkout_strategy = GIT_CHECKOUT_SAFE;
193 if( git_checkout_head(
m_repo, &checkoutOptions ) )
196 return PullResult::Error;
200 git_revwalk* revWalker =
nullptr;
201 git_revwalk_new( &revWalker,
m_repo );
202 git_revwalk_sorting( revWalker, GIT_SORT_TIME );
203 git_revwalk_push_glob( revWalker, updatedRefName );
206 while( git_revwalk_next( &commitOid, revWalker ) == 0 )
208 git_commit* commit =
nullptr;
210 if( git_commit_lookup( &commit,
m_repo, &commitOid ) )
212 AddErrorString( wxString::Format(
_(
"Could not lookup commit '{}'" ), git_oid_tostr_s( &commitOid ) ) );
213 git_revwalk_free( revWalker );
214 return PullResult::Error;
218 details.
m_sha = git_oid_tostr_s( &commitOid );
220 details.
m_author = git_commit_author( commit )->name;
223 std::pair<std::string, std::vector<CommitDetails>>& branchCommits =
225 branchCommits.first = updatedRefName;
226 branchCommits.second.push_back( details );
229 git_commit_free( commit );
232 git_revwalk_free( revWalker );
234 git_repository_state_cleanup(
m_repo );
235 return PullResult::FastForward;
240 size_t aMergeHeadsCount )
242 git_merge_options merge_opts = GIT_MERGE_OPTIONS_INIT;
243 git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
245 checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
247 if( git_merge(
m_repo, aMergeHeads, aMergeHeadsCount, &merge_opts, &checkout_opts ) )
250 return PullResult::Error;
254 git_index* index =
nullptr;
255 if( git_repository_index( &index,
m_repo ) )
258 return PullResult::Error;
262 git_index_conflict_iterator* conflicts =
nullptr;
263 if( git_index_conflict_iterator_new( &conflicts, index ) )
266 return PullResult::Error;
269 const git_index_entry* ancestor =
nullptr;
270 const git_index_entry* our =
nullptr;
271 const git_index_entry* their =
nullptr;
272 std::vector<ConflictData> conflict_data;
274 while( git_index_conflict_next( &ancestor, &our, &their, conflicts ) == 0 )
277 if( ancestor && our && their )
280 conflict_datum.
filename = our->path;
281 conflict_datum.
our_oid = our->id;
289 conflict_data.push_back( conflict_datum );
292 else if( !ancestor && our && their )
295 conflict_datum.
filename = our->path;
296 conflict_datum.
our_oid = our->id;
304 conflict_data.push_back( conflict_datum );
307 else if( their && !our )
310 git_index_add( index, their );
313 else if( our && !their )
316 git_index_add( index, our );
320 wxLogError( wxS(
"Unexpected conflict state" ) );
324 if( conflict_data.empty() )
326 git_index_conflict_cleanup( index );
327 git_index_write( index );
330 git_index_conflict_iterator_free( conflicts );
331 git_index_free( index );
333 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