KiCad PCB EDA Suite
Loading...
Searching...
No Matches
git_pull_handler.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/gpl-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include "git_pull_handler.h"
26
27#include <wx/log.h>
28
29#include <iostream>
30#include <time.h>
31
33{}
34
35
37{}
38
39
41{
42 // Fetch updates from remote repository
43 git_remote* remote = nullptr;
44
45 if( git_remote_lookup( &remote, m_repo, "origin" ) != 0 )
46 {
47 AddErrorString( wxString::Format( _( "Could not lookup remote '%s'" ), "origin" ) );
48 return false;
49 }
50
51 git_remote_callbacks remoteCallbacks;
52 git_remote_init_callbacks( &remoteCallbacks, GIT_REMOTE_CALLBACKS_VERSION );
53 remoteCallbacks.sideband_progress = progress_cb;
54 remoteCallbacks.transfer_progress = transfer_progress_cb;
55 remoteCallbacks.credentials = credentials_cb;
56 remoteCallbacks.payload = this;
57
58 m_testedTypes = 0;
60
61 if( git_remote_connect( remote, GIT_DIRECTION_FETCH, &remoteCallbacks, nullptr, nullptr ) )
62 {
63 git_remote_free( remote );
64 AddErrorString( wxString::Format( _( "Could not connect to remote '%s': %s" ), "origin",
65 git_error_last()->message ) );
66 return false;
67 }
68
69 git_fetch_options fetchOptions;
70 git_fetch_init_options( &fetchOptions, GIT_FETCH_OPTIONS_VERSION );
71 fetchOptions.callbacks = remoteCallbacks;
72
73 if( git_remote_fetch( remote, nullptr, &fetchOptions, nullptr ) )
74 {
75 git_remote_free( remote );
76 AddErrorString( wxString::Format( _( "Could not fetch data from remote '%s': %s" ),
77 "origin", git_error_last()->message ) );
78 return false;
79 }
80
81 git_remote_free( remote );
82
83 return true;
84}
85
86
88{
89 PullResult result = PullResult::Success;
90
91 if( !PerformFetch() )
92 return PullResult::Error;
93
94 git_oid pull_merge_oid = {};
95
96 if( git_repository_fetchhead_foreach( m_repo, fetchhead_foreach_cb, &pull_merge_oid ) )
97 {
98 AddErrorString( _( "Could not read 'FETCH_HEAD'" ) );
99 return PullResult::Error;
100 }
101
102 git_annotated_commit* fetchhead_commit;
103
104 if( git_annotated_commit_lookup( &fetchhead_commit, m_repo, &pull_merge_oid ) )
105 {
106 AddErrorString( _( "Could not lookup commit" ) );
107 return PullResult::Error;
108 }
109
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;
113
114 if( git_merge_analysis( &merge_analysis, &merge_preference, m_repo, merge_commits, 1 ) )
115 {
116 AddErrorString( _( "Could not analyze merge" ) );
117 git_annotated_commit_free( fetchhead_commit );
118 return PullResult::Error;
119 }
120
121 if( !( merge_analysis & GIT_MERGE_ANALYSIS_NORMAL ) )
122 git_annotated_commit_free( fetchhead_commit );
123
124 if( merge_analysis & GIT_MERGE_ANALYSIS_UNBORN )
125 {
126 AddErrorString( _( "Invalid HEAD. Cannot merge." ) );
127 return PullResult::MergeFailed;
128 }
129
130 // Nothing to do if the repository is up to date
131 if( merge_analysis & GIT_MERGE_ANALYSIS_UP_TO_DATE )
132 {
133 git_repository_state_cleanup( m_repo );
134 return PullResult::UpToDate;
135 }
136
137 // Fast-forward is easy, just update the local reference
138 if( merge_analysis & GIT_MERGE_ANALYSIS_FASTFORWARD )
139 {
140 return handleFastForward();
141 }
142
143 if( merge_analysis & GIT_MERGE_ANALYSIS_NORMAL )
144 {
145 PullResult ret = handleMerge( merge_commits, 1 );
146 git_annotated_commit_free( fetchhead_commit );
147 return ret;
148 }
149
150 //TODO: handle merges when they need to be resolved
151
152 return result;
153}
154
155const std::vector<std::pair<std::string, std::vector<CommitDetails>>>&
157{
158 return m_fetchResults;
159}
160
161
162std::string GIT_PULL_HANDLER::getFirstLineFromCommitMessage( const std::string& aMessage )
163{
164 size_t firstLineEnd = aMessage.find_first_of( '\n' );
165
166 if( firstLineEnd != std::string::npos )
167 return aMessage.substr( 0, firstLineEnd );
168
169 return aMessage;
170}
171
172
173std::string GIT_PULL_HANDLER::getFormattedCommitDate( const git_time& aTime )
174{
175 char dateBuffer[64];
176 time_t time = static_cast<time_t>( aTime.time );
177 strftime( dateBuffer, sizeof( dateBuffer ), "%Y-%b-%d %H:%M:%S", gmtime( &time ) );
178 return dateBuffer;
179}
180
181
183{
184 // Update local references with fetched data
185 git_reference* updatedRef = nullptr;
186
187 if( git_repository_head( &updatedRef, m_repo ) )
188 {
189 AddErrorString( _( "Could not get repository head" ) );
190 return PullResult::Error;
191 }
192
193 const char* updatedRefName = git_reference_name( updatedRef );
194 git_reference_free( updatedRef );
195
196 git_oid updatedRefOid;
197
198 if( git_reference_name_to_id( &updatedRefOid, m_repo, updatedRefName ) )
199 {
200 AddErrorString( wxString::Format( _( "Could not get reference OID for reference '%s'" ),
201 updatedRefName ) );
202 return PullResult::Error;
203 }
204
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 ) )
209 {
210 AddErrorString( _( "Failed to perform checkout operation." ) );
211 return PullResult::Error;
212 }
213
214 // Collect commit details for updated references
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 );
219
220 git_oid commitOid;
221
222 while( git_revwalk_next( &commitOid, revWalker ) == 0 )
223 {
224 git_commit* commit = nullptr;
225
226 if( git_commit_lookup( &commit, m_repo, &commitOid ) )
227 {
228 AddErrorString( wxString::Format( _( "Could not lookup commit '{}'" ),
229 git_oid_tostr_s( &commitOid ) ) );
230 git_revwalk_free( revWalker );
231 return PullResult::Error;
232 }
233
234 CommitDetails details;
235 details.m_sha = git_oid_tostr_s( &commitOid );
236 details.m_firstLine = getFirstLineFromCommitMessage( git_commit_message( commit ) );
237 details.m_author = git_commit_author( commit )->name;
238 details.m_date = getFormattedCommitDate( git_commit_author( commit )->when );
239
240 std::pair<std::string, std::vector<CommitDetails>>& branchCommits =
241 m_fetchResults.emplace_back();
242 branchCommits.first = updatedRefName;
243 branchCommits.second.push_back( details );
244
245 //TODO: log these to the parent
246 git_commit_free( commit );
247 }
248
249 git_revwalk_free( revWalker );
250
251 git_repository_state_cleanup( m_repo );
252 return PullResult::FastForward;
253}
254
255
256PullResult GIT_PULL_HANDLER::handleMerge( const git_annotated_commit** aMergeHeads,
257 size_t aMergeHeadsCount )
258{
259 git_merge_options merge_opts;
260 git_merge_options_init( &merge_opts, GIT_MERGE_OPTIONS_VERSION );
261
262 git_checkout_options checkout_opts;
263 git_checkout_init_options( &checkout_opts, GIT_CHECKOUT_OPTIONS_VERSION );
264
265 checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
266
267 if( git_merge( m_repo, aMergeHeads, aMergeHeadsCount, &merge_opts, &checkout_opts ) )
268 {
269 AddErrorString( _( "Could not merge commits" ) );
270 return PullResult::Error;
271 }
272
273 // Get the repository index
274 git_index* index = nullptr;
275
276 if( git_repository_index( &index, m_repo ) )
277 {
278 AddErrorString( _( "Could not get repository index" ) );
279 return PullResult::Error;
280 }
281
282 // Check for conflicts
283 git_index_conflict_iterator* conflicts = nullptr;
284
285 if( git_index_conflict_iterator_new( &conflicts, index ) )
286 {
287 AddErrorString( _( "Could not get conflict iterator" ) );
288 return PullResult::Error;
289 }
290
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;
295
296 while( git_index_conflict_next( &ancestor, &our, &their, conflicts ) == 0 )
297 {
298 // Case 3: Both files have changed
299 if( ancestor && our && their )
300 {
301 ConflictData conflict_datum;
302 conflict_datum.filename = our->path;
303 conflict_datum.our_oid = our->id;
304 conflict_datum.their_oid = their->id;
305 conflict_datum.our_commit_time = our->mtime.seconds;
306 conflict_datum.their_commit_time = their->mtime.seconds;
307 conflict_datum.our_status = _( "Changed" );
308 conflict_datum.their_status = _( "Changed" );
309 conflict_datum.use_ours = true;
310
311 conflict_data.push_back( conflict_datum );
312 }
313 // Case 4: File added in both ours and theirs
314 else if( !ancestor && our && their )
315 {
316 ConflictData conflict_datum;
317 conflict_datum.filename = our->path;
318 conflict_datum.our_oid = our->id;
319 conflict_datum.their_oid = their->id;
320 conflict_datum.our_commit_time = our->mtime.seconds;
321 conflict_datum.their_commit_time = their->mtime.seconds;
322 conflict_datum.our_status = _( "Added" );
323 conflict_datum.their_status = _( "Added" );
324 conflict_datum.use_ours = true;
325
326 conflict_data.push_back( conflict_datum );
327 }
328 // Case 1: Remote file has changed or been added, local file has not
329 else if( their && !our )
330 {
331 // Accept their changes
332 git_index_add( index, their );
333 }
334 // Case 2: Local file has changed or been added, remote file has not
335 else if( our && !their )
336 {
337 // Accept our changes
338 git_index_add( index, our );
339 }
340 else
341 {
342 wxLogError( wxS( "Unexpected conflict state" ) );
343 }
344 }
345
346 if( conflict_data.empty() )
347 {
348 git_index_conflict_cleanup( index );
349 git_index_write( index );
350 }
351
352 git_index_conflict_iterator_free( conflicts );
353 git_index_free( index );
354
355 return conflict_data.empty() ? PullResult::Success : PullResult::MergeFailed;
356}
357
358
359void GIT_PULL_HANDLER::UpdateProgress( int aCurrent, int aTotal, const wxString& aMessage )
360{
361 ReportProgress( aCurrent, aTotal, aMessage );
362}
void ReportProgress(int aCurrent, int aTotal, const wxString &aMessage)
Definition: git_progress.h:45
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
PullResult PerformPull()
git_repository * m_repo
unsigned m_testedTypes
void AddErrorString(const wxString aErrorString)
#define _(s)
PullResult
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)
std::string m_sha
std::string m_date
std::string m_firstLine
std::string m_author
std::string their_status
std::string our_status
std::string filename
git_time_t their_commit_time
git_time_t our_commit_time