KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_kigit_common.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, see <https://www.gnu.org/licenses/>.
18 */
19
21
24
25#include <git2.h>
26
27#include <wx/filename.h>
28#include <wx/string.h>
29#include <wx/stdpaths.h>
30
31#include <chrono>
32#include <fstream>
33#include <memory>
34
35
36namespace
37{
38
39struct GitInitGuard
40{
41 GitInitGuard() { git_libgit2_init(); }
42 ~GitInitGuard() { git_libgit2_shutdown(); }
43};
44
45
46struct ScopedTempDir
47{
48 wxString path;
49
51 {
52 auto now = std::chrono::steady_clock::now().time_since_epoch();
53 long long ticks = std::chrono::duration_cast<std::chrono::nanoseconds>( now ).count();
54
55 wxString base = wxFileName::GetTempDir();
56 wxFileName fn;
57 fn.AssignDir( base );
58 fn.AppendDir( wxString::Format( "kicad-qa-git-%lld-%p", ticks, this ) );
59 wxFileName::Mkdir( fn.GetPath(), wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
60 path = fn.GetPath();
61 }
62
64 {
65 if( !path.IsEmpty() )
66 wxFileName::Rmdir( path, wxPATH_RMDIR_RECURSIVE );
67 }
68};
69
70
71// Initialize a git repository at the given path with one initial commit on "main"
72git_repository* makeRepoWithCommit( const wxString& aRepoPath, const wxString& aFileName,
73 const std::string& aFileContents )
74{
75 git_repository* repo = nullptr;
76 git_repository_init_options init_opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
77 init_opts.flags = GIT_REPOSITORY_INIT_MKPATH;
78 init_opts.initial_head = "main";
79
80 BOOST_REQUIRE_EQUAL( git_repository_init_ext( &repo, aRepoPath.utf8_string().c_str(),
81 &init_opts ),
82 GIT_OK );
83
84 // Write a file
85 wxFileName filePath( aRepoPath, aFileName );
86 {
87 std::ofstream f( filePath.GetFullPath().utf8_string() );
88 f << aFileContents;
89 }
90
91 // Configure user
92 git_config* cfg = nullptr;
93 BOOST_REQUIRE_EQUAL( git_repository_config( &cfg, repo ), GIT_OK );
94 KIGIT::GitConfigPtr cfgPtr( cfg );
95 git_config_set_string( cfg, "user.name", "QA Test" );
96 git_config_set_string( cfg, "user.email", "[email protected]" );
97
98 // Stage and commit
99 git_index* index = nullptr;
100 BOOST_REQUIRE_EQUAL( git_repository_index( &index, repo ), GIT_OK );
101 KIGIT::GitIndexPtr indexPtr( index );
102 BOOST_REQUIRE_EQUAL( git_index_add_bypath( index, aFileName.utf8_string().c_str() ), GIT_OK );
103 BOOST_REQUIRE_EQUAL( git_index_write( index ), GIT_OK );
104
105 git_oid tree_oid;
106 BOOST_REQUIRE_EQUAL( git_index_write_tree( &tree_oid, index ), GIT_OK );
107
108 git_tree* tree = nullptr;
109 BOOST_REQUIRE_EQUAL( git_tree_lookup( &tree, repo, &tree_oid ), GIT_OK );
110 KIGIT::GitTreePtr treePtr( tree );
111
112 git_signature* sig = nullptr;
113 BOOST_REQUIRE_EQUAL( git_signature_now( &sig, "QA Test", "[email protected]" ), GIT_OK );
114 KIGIT::GitSignaturePtr sigPtr( sig );
115
116 git_oid commit_oid;
117 BOOST_REQUIRE_EQUAL( git_commit_create( &commit_oid, repo, "HEAD", sig, sig, nullptr,
118 "initial", tree, 0, nullptr ),
119 GIT_OK );
120
121 return repo;
122}
123
124} // namespace
125
126
127BOOST_AUTO_TEST_SUITE( KiGitCommon )
128
129
130// GetGitRootDirectory must return the working directory (project root) and not the
131// internal .git folder. Older code returned git_repository_path() which produced a path
132// ending in /.git/, breaking the version-control popup-menu detection logic for whether a
133// project lives at the repository root.
134BOOST_AUTO_TEST_CASE( GitRootDirectoryReturnsWorkdir )
135{
136 GitInitGuard libgit;
137 ScopedTempDir tmp;
138
139 git_repository* repo = makeRepoWithCommit( tmp.path, "file.txt", "data\n" );
140 KIGIT::GitRepositoryPtr repoPtr( repo );
141
142 KIGIT_COMMON common( repo );
143 wxString root = common.GetGitRootDirectory();
144
145 BOOST_CHECK( !root.IsEmpty() );
146 BOOST_CHECK_MESSAGE( !root.Contains( wxS( "/.git" ) ) && !root.Contains( wxS( "\\.git" ) ),
147 "GetGitRootDirectory should return the working directory, got: "
148 + root.ToStdString() );
149
150 // Should be the repo path (with trailing separator).
151 wxFileName rootFn;
152 rootFn.AssignDir( root );
153 wxFileName tmpFn;
154 tmpFn.AssignDir( tmp.path );
155 BOOST_CHECK_EQUAL( rootFn.GetFullPath().ToStdString(), tmpFn.GetFullPath().ToStdString() );
156}
157
158
159// HasPushAndPullRemote must report true for repositories whose remote uses a name other
160// than "origin", because libgit2 allows arbitrary remote names and many users rename or
161// add additional remotes.
162BOOST_AUTO_TEST_CASE( HasPushAndPullRemoteAcceptsNonOriginName )
163{
164 GitInitGuard libgit;
165 ScopedTempDir tmp;
166
167 git_repository* repo = makeRepoWithCommit( tmp.path, "file.txt", "data\n" );
168 KIGIT::GitRepositoryPtr repoPtr( repo );
169
170 // No remotes configured.
171 KIGIT_COMMON common( repo );
172 BOOST_CHECK( !common.HasPushAndPullRemote() );
173
174 // Add a remote with a non-default name.
175 git_remote* remote = nullptr;
176 BOOST_REQUIRE_EQUAL( git_remote_create( &remote, repo, "github",
177 "[email protected]:example/repo.git" ),
178 GIT_OK );
179 KIGIT::GitRemotePtr remotePtr( remote );
180
181 BOOST_CHECK( common.HasPushAndPullRemote() );
182}
183
184
185BOOST_AUTO_TEST_CASE( HasPushAndPullRemoteFindsOrigin )
186{
187 GitInitGuard libgit;
188 ScopedTempDir tmp;
189
190 git_repository* repo = makeRepoWithCommit( tmp.path, "file.txt", "data\n" );
191 KIGIT::GitRepositoryPtr repoPtr( repo );
192
193 git_remote* remote = nullptr;
194 BOOST_REQUIRE_EQUAL( git_remote_create( &remote, repo, "origin",
195 "[email protected]:repo.git" ),
196 GIT_OK );
197 KIGIT::GitRemotePtr remotePtr( remote );
198
199 KIGIT_COMMON common( repo );
200 BOOST_CHECK( common.HasPushAndPullRemote() );
201}
202
203
204// GetDifferentFiles previously walked unbounded history when no upstream OID was available
205// and dumped every file in the root commit's tree into the modified set, falsely flagging
206// every file as ahead of the remote. With no upstream configured, both sets must be empty.
207BOOST_AUTO_TEST_CASE( GetDifferentFilesEmptyWithoutUpstream )
208{
209 GitInitGuard libgit;
210 ScopedTempDir tmp;
211
212 git_repository* repo = makeRepoWithCommit( tmp.path, "file.txt", "data\n" );
213 KIGIT::GitRepositoryPtr repoPtr( repo );
214
215 KIGIT_COMMON common( repo );
216 auto [local, remote] = common.GetDifferentFiles();
217
218 BOOST_CHECK_MESSAGE( local.empty(),
219 "Expected no AHEAD files when no upstream is configured, got "
220 + std::to_string( local.size() ) );
221 BOOST_CHECK_MESSAGE( remote.empty(),
222 "Expected no BEHIND files when no upstream is configured, got "
223 + std::to_string( remote.size() ) );
224}
225
226
227// Regression test for the root-commit dump that this commit fixes. Set up an upstream
228// tracking ref whose history is disjoint from the local HEAD's history (separate root
229// commits, no shared ancestors). Pre-fix, get_modified_files() walked the local history
230// past the unrelated upstream and reached the local root commit. Because the root commit
231// has no parent, the code dumped *every* file in its tree into the AHEAD set, including
232// files like "untouched.txt" that were neither created nor modified relative to the
233// upstream's view. The fix bails out before we can include these phantom changes.
234BOOST_AUTO_TEST_CASE( GetDifferentFilesHandlesUnrelatedHistories )
235{
236 GitInitGuard libgit;
237 ScopedTempDir tmp;
238
239 // Local root commit with two files. The bug previously listed both as AHEAD.
240 git_repository* repo = makeRepoWithCommit( tmp.path, "untouched.txt", "stable\n" );
241 KIGIT::GitRepositoryPtr repoPtr( repo );
242
243 // Add a second file to local HEAD so we have more than the bare initial tree.
244 {
245 wxFileName extra( tmp.path, wxS( "second.txt" ) );
246 std::ofstream f( extra.GetFullPath().utf8_string() );
247 f << "more\n";
248 }
249
250 git_index* index = nullptr;
251 BOOST_REQUIRE_EQUAL( git_repository_index( &index, repo ), GIT_OK );
252 KIGIT::GitIndexPtr indexPtr( index );
253 BOOST_REQUIRE_EQUAL( git_index_add_bypath( index, "second.txt" ), GIT_OK );
254 BOOST_REQUIRE_EQUAL( git_index_write( index ), GIT_OK );
255
256 git_oid newTreeOid;
257 BOOST_REQUIRE_EQUAL( git_index_write_tree( &newTreeOid, index ), GIT_OK );
258
259 git_tree* newTree = nullptr;
260 BOOST_REQUIRE_EQUAL( git_tree_lookup( &newTree, repo, &newTreeOid ), GIT_OK );
261 KIGIT::GitTreePtr newTreePtr( newTree );
262
263 git_reference* head = nullptr;
264 BOOST_REQUIRE_EQUAL( git_repository_head( &head, repo ), GIT_OK );
265 KIGIT::GitReferencePtr headPtr( head );
266
267 git_commit* parentCommit = nullptr;
268 BOOST_REQUIRE_EQUAL(
269 git_commit_lookup( &parentCommit, repo, git_reference_target( head ) ), GIT_OK );
270 KIGIT::GitCommitPtr parentCommitPtr( parentCommit );
271
272 git_signature* sig = nullptr;
273 BOOST_REQUIRE_EQUAL( git_signature_now( &sig, "QA Test", "[email protected]" ), GIT_OK );
274 KIGIT::GitSignaturePtr sigPtr( sig );
275
276 const git_commit* parents[1] = { parentCommit };
277 git_oid secondCommitOid;
278 BOOST_REQUIRE_EQUAL( git_commit_create( &secondCommitOid, repo, "HEAD", sig, sig, nullptr,
279 "second", newTree, 1, parents ),
280 GIT_OK );
281
282 // Build a disjoint upstream history with its own root commit and a different file.
283 git_treebuilder* tb = nullptr;
284 BOOST_REQUIRE_EQUAL( git_treebuilder_new( &tb, repo, nullptr ), GIT_OK );
285
286 git_oid blobOid;
287 const std::string upstreamData = "upstream\n";
288 BOOST_REQUIRE_EQUAL( git_blob_create_from_buffer( &blobOid, repo, upstreamData.data(),
289 upstreamData.size() ),
290 GIT_OK );
291 BOOST_REQUIRE_EQUAL( git_treebuilder_insert( nullptr, tb, "remote_only.txt", &blobOid,
292 GIT_FILEMODE_BLOB ),
293 GIT_OK );
294
295 git_oid remoteTreeOid;
296 BOOST_REQUIRE_EQUAL( git_treebuilder_write( &remoteTreeOid, tb ), GIT_OK );
297 git_treebuilder_free( tb );
298
299 git_tree* remoteTree = nullptr;
300 BOOST_REQUIRE_EQUAL( git_tree_lookup( &remoteTree, repo, &remoteTreeOid ), GIT_OK );
301 KIGIT::GitTreePtr remoteTreePtr( remoteTree );
302
303 git_oid remoteCommitOid;
304 BOOST_REQUIRE_EQUAL( git_commit_create( &remoteCommitOid, repo, nullptr, sig, sig, nullptr,
305 "upstream root", remoteTree, 0, nullptr ),
306 GIT_OK );
307
308 git_reference* remoteRef = nullptr;
309 BOOST_REQUIRE_EQUAL( git_reference_create( &remoteRef, repo, "refs/remotes/origin/main",
310 &remoteCommitOid, true, nullptr ),
311 GIT_OK );
312 KIGIT::GitReferencePtr remoteRefPtr( remoteRef );
313
314 git_config* cfg = nullptr;
315 BOOST_REQUIRE_EQUAL( git_repository_config( &cfg, repo ), GIT_OK );
316 KIGIT::GitConfigPtr cfgPtr( cfg );
317 BOOST_REQUIRE_EQUAL( git_config_set_string( cfg, "branch.main.remote", "origin" ),
318 GIT_OK );
319 BOOST_REQUIRE_EQUAL( git_config_set_string( cfg, "branch.main.merge", "refs/heads/main" ),
320 GIT_OK );
321
322 KIGIT_COMMON common( repo );
323 auto [local, remote] = common.GetDifferentFiles();
324
325 // With unrelated histories there is no merge base, so the implementation cannot
326 // attribute changes to either side. Pre-fix, the revwalk reached both root commits
327 // and dumped both trees in full; the new merge-base approach short-circuits and
328 // reports no AHEAD/BEHIND files in this ambiguous state. In particular, the
329 // never-touched-locally "untouched.txt" must not appear in the AHEAD set.
330 BOOST_CHECK_MESSAGE( local.find( wxS( "untouched.txt" ) ) == local.end(),
331 "untouched.txt should not be reported as AHEAD when the local and "
332 "remote histories share no ancestor" );
333 BOOST_CHECK_MESSAGE( remote.find( wxS( "untouched.txt" ) ) == remote.end(),
334 "untouched.txt should not be reported as BEHIND when the local "
335 "and remote histories share no ancestor" );
336}
337
338
339// When local has commits ahead of a real upstream, only files that actually changed in those
340// local commits should be reported as AHEAD. Files that exist in both trees unchanged must
341// not show up as ahead. This is the common-case behaviour the issue reproducer exercises.
342BOOST_AUTO_TEST_CASE( GetDifferentFilesReportsOnlyTouchedFilesWhenAhead )
343{
344 GitInitGuard libgit;
345 ScopedTempDir tmp;
346
347 // Initial commit becomes the shared base. Two files: only one will be modified later.
348 git_repository* repo = makeRepoWithCommit( tmp.path, "untouched.txt", "stable\n" );
349 KIGIT::GitRepositoryPtr repoPtr( repo );
350
351 {
352 wxFileName extra( tmp.path, wxS( "touched.txt" ) );
353 std::ofstream f( extra.GetFullPath().utf8_string() );
354 f << "v1\n";
355 }
356
357 git_index* index = nullptr;
358 BOOST_REQUIRE_EQUAL( git_repository_index( &index, repo ), GIT_OK );
359 KIGIT::GitIndexPtr indexPtr( index );
360 BOOST_REQUIRE_EQUAL( git_index_add_bypath( index, "touched.txt" ), GIT_OK );
361 BOOST_REQUIRE_EQUAL( git_index_write( index ), GIT_OK );
362
363 git_oid treeOid;
364 BOOST_REQUIRE_EQUAL( git_index_write_tree( &treeOid, index ), GIT_OK );
365
366 git_tree* tree = nullptr;
367 BOOST_REQUIRE_EQUAL( git_tree_lookup( &tree, repo, &treeOid ), GIT_OK );
368 KIGIT::GitTreePtr treePtr( tree );
369
370 git_reference* head = nullptr;
371 BOOST_REQUIRE_EQUAL( git_repository_head( &head, repo ), GIT_OK );
372 KIGIT::GitReferencePtr headPtr( head );
373
374 git_commit* parent = nullptr;
375 BOOST_REQUIRE_EQUAL( git_commit_lookup( &parent, repo, git_reference_target( head ) ),
376 GIT_OK );
377 KIGIT::GitCommitPtr parentPtr( parent );
378
379 git_signature* sig = nullptr;
380 BOOST_REQUIRE_EQUAL( git_signature_now( &sig, "QA Test", "[email protected]" ), GIT_OK );
381 KIGIT::GitSignaturePtr sigPtr( sig );
382
383 const git_commit* parents[1] = { parent };
384 git_oid base_oid;
385 BOOST_REQUIRE_EQUAL( git_commit_create( &base_oid, repo, "HEAD", sig, sig, nullptr,
386 "add touched", tree, 1, parents ),
387 GIT_OK );
388
389 // Mark this commit as the upstream tip and configure a remote so libgit2 accepts the
390 // branch.main.remote = origin pointer below.
391 git_remote* origin = nullptr;
392 BOOST_REQUIRE_EQUAL( git_remote_create( &origin, repo, "origin",
393 "[email protected]:repo.git" ),
394 GIT_OK );
395 KIGIT::GitRemotePtr originPtr( origin );
396
397 git_reference* upstream_ref = nullptr;
398 BOOST_REQUIRE_EQUAL( git_reference_create( &upstream_ref, repo, "refs/remotes/origin/main",
399 &base_oid, true, nullptr ),
400 GIT_OK );
401 KIGIT::GitReferencePtr upstreamRefPtr( upstream_ref );
402
403 git_config* cfg = nullptr;
404 BOOST_REQUIRE_EQUAL( git_repository_config( &cfg, repo ), GIT_OK );
405 KIGIT::GitConfigPtr cfgPtr( cfg );
406 BOOST_REQUIRE_EQUAL( git_config_set_string( cfg, "branch.main.remote", "origin" ),
407 GIT_OK );
408 BOOST_REQUIRE_EQUAL( git_config_set_string( cfg, "branch.main.merge", "refs/heads/main" ),
409 GIT_OK );
410
411 // Local-only commit modifying just touched.txt.
412 {
413 wxFileName extra( tmp.path, wxS( "touched.txt" ) );
414 std::ofstream f( extra.GetFullPath().utf8_string() );
415 f << "v2\n";
416 }
417
418 BOOST_REQUIRE_EQUAL( git_index_add_bypath( index, "touched.txt" ), GIT_OK );
419 BOOST_REQUIRE_EQUAL( git_index_write( index ), GIT_OK );
420
421 git_oid newTreeOid;
422 BOOST_REQUIRE_EQUAL( git_index_write_tree( &newTreeOid, index ), GIT_OK );
423
424 git_tree* newTree = nullptr;
425 BOOST_REQUIRE_EQUAL( git_tree_lookup( &newTree, repo, &newTreeOid ), GIT_OK );
426 KIGIT::GitTreePtr newTreePtr( newTree );
427
428 git_commit* baseCommit = nullptr;
429 BOOST_REQUIRE_EQUAL( git_commit_lookup( &baseCommit, repo, &base_oid ), GIT_OK );
430 KIGIT::GitCommitPtr baseCommitPtr( baseCommit );
431
432 const git_commit* aheadParents[1] = { baseCommit };
433 git_oid aheadOid;
434 BOOST_REQUIRE_EQUAL( git_commit_create( &aheadOid, repo, "HEAD", sig, sig, nullptr,
435 "modify touched", newTree, 1, aheadParents ),
436 GIT_OK );
437
438 KIGIT_COMMON common( repo );
439 auto [local, remote] = common.GetDifferentFiles();
440
441 BOOST_CHECK( remote.empty() );
442 BOOST_CHECK_MESSAGE( local.find( wxS( "touched.txt" ) ) != local.end(),
443 "touched.txt should be reported as AHEAD" );
444 BOOST_CHECK_MESSAGE( local.find( wxS( "untouched.txt" ) ) == local.end(),
445 "untouched.txt should NOT be reported as AHEAD - this was the "
446 "regression in issue 21576" );
447}
448
449
int index
wxString GetGitRootDirectory() const
std::pair< std::set< wxString >, std::set< wxString > > GetDifferentFiles() const
Return a pair of sets of files that differ locally from the remote repository The first set is files ...
bool HasPushAndPullRemote() const
std::unique_ptr< git_tree, decltype([](git_tree *aTree) { git_tree_free(aTree); })> GitTreePtr
A unique pointer for git_tree objects with automatic cleanup.
std::unique_ptr< git_repository, decltype([](git_repository *aRepo) { git_repository_free(aRepo); })> GitRepositoryPtr
A unique pointer for git_repository objects with automatic cleanup.
std::unique_ptr< git_commit, decltype([](git_commit *aCommit) { git_commit_free(aCommit); })> GitCommitPtr
A unique pointer for git_commit objects with automatic cleanup.
std::unique_ptr< git_config, decltype([](git_config *aConfig) { git_config_free(aConfig); })> GitConfigPtr
A unique pointer for git_config objects with automatic cleanup.
std::unique_ptr< git_reference, decltype([](git_reference *aRef) { git_reference_free(aRef); })> GitReferencePtr
A unique pointer for git_reference objects with automatic cleanup.
std::unique_ptr< git_signature, decltype([](git_signature *aSignature) { git_signature_free(aSignature); })> GitSignaturePtr
A unique pointer for git_signature objects with automatic cleanup.
std::unique_ptr< git_index, decltype([](git_index *aIndex) { git_index_free(aIndex); })> GitIndexPtr
A unique pointer for git_index objects with automatic cleanup.
std::unique_ptr< git_remote, decltype([](git_remote *aRemote) { git_remote_free(aRemote); })> GitRemotePtr
A unique pointer for git_remote objects with automatic cleanup.
Scoped temporary directory used by the tests below.
ScopedTempDir(const wxString &aTag)
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(GitRootDirectoryReturnsWorkdir)
BOOST_CHECK_MESSAGE(totalMismatches==0, std::to_string(totalMismatches)+" board(s) with strategy disagreements")
BOOST_CHECK_EQUAL(result, "25.4")