27#include <wx/filename.h>
29#include <wx/stdpaths.h>
41 GitInitGuard() { git_libgit2_init(); }
42 ~GitInitGuard() { git_libgit2_shutdown(); }
52 auto now = std::chrono::steady_clock::now().time_since_epoch();
53 long long ticks = std::chrono::duration_cast<std::chrono::nanoseconds>( now ).count();
55 wxString base = wxFileName::GetTempDir();
58 fn.AppendDir( wxString::Format(
"kicad-qa-git-%lld-%p", ticks,
this ) );
59 wxFileName::Mkdir( fn.GetPath(), wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
66 wxFileName::Rmdir(
path, wxPATH_RMDIR_RECURSIVE );
72git_repository* makeRepoWithCommit(
const wxString& aRepoPath,
const wxString& aFileName,
73 const std::string& aFileContents )
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";
80 BOOST_REQUIRE_EQUAL( git_repository_init_ext( &repo, aRepoPath.utf8_string().c_str(),
85 wxFileName filePath( aRepoPath, aFileName );
87 std::ofstream f( filePath.GetFullPath().utf8_string() );
92 git_config* cfg =
nullptr;
93 BOOST_REQUIRE_EQUAL( git_repository_config( &cfg, repo ), GIT_OK );
95 git_config_set_string( cfg,
"user.name",
"QA Test" );
99 git_index*
index =
nullptr;
100 BOOST_REQUIRE_EQUAL( git_repository_index( &
index, repo ), GIT_OK );
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 );
106 BOOST_REQUIRE_EQUAL( git_index_write_tree( &tree_oid,
index ), GIT_OK );
108 git_tree* tree =
nullptr;
109 BOOST_REQUIRE_EQUAL( git_tree_lookup( &tree, repo, &tree_oid ), GIT_OK );
112 git_signature* sig =
nullptr;
113 BOOST_REQUIRE_EQUAL( git_signature_now( &sig,
"QA Test",
"[email protected]" ), GIT_OK );
117 BOOST_REQUIRE_EQUAL( git_commit_create( &commit_oid, repo,
"HEAD", sig, sig,
nullptr,
118 "initial", tree, 0,
nullptr ),
139 git_repository* repo = makeRepoWithCommit( tmp.
path,
"file.txt",
"data\n" );
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() );
152 rootFn.AssignDir( root );
154 tmpFn.AssignDir( tmp.
path );
155 BOOST_CHECK_EQUAL( rootFn.GetFullPath().ToStdString(), tmpFn.GetFullPath().ToStdString() );
167 git_repository* repo = makeRepoWithCommit( tmp.
path,
"file.txt",
"data\n" );
175 git_remote* remote =
nullptr;
176 BOOST_REQUIRE_EQUAL( git_remote_create( &remote, repo,
"github",
190 git_repository* repo = makeRepoWithCommit( tmp.
path,
"file.txt",
"data\n" );
193 git_remote* remote =
nullptr;
194 BOOST_REQUIRE_EQUAL( git_remote_create( &remote, repo,
"origin",
212 git_repository* repo = makeRepoWithCommit( tmp.
path,
"file.txt",
"data\n" );
219 "Expected no AHEAD files when no upstream is configured, got "
220 + std::to_string( local.size() ) );
222 "Expected no BEHIND files when no upstream is configured, got "
223 + std::to_string( remote.size() ) );
240 git_repository* repo = makeRepoWithCommit( tmp.
path,
"untouched.txt",
"stable\n" );
245 wxFileName extra( tmp.
path, wxS(
"second.txt" ) );
246 std::ofstream f( extra.GetFullPath().utf8_string() );
250 git_index*
index =
nullptr;
251 BOOST_REQUIRE_EQUAL( git_repository_index( &
index, repo ), GIT_OK );
253 BOOST_REQUIRE_EQUAL( git_index_add_bypath(
index,
"second.txt" ), GIT_OK );
254 BOOST_REQUIRE_EQUAL( git_index_write(
index ), GIT_OK );
257 BOOST_REQUIRE_EQUAL( git_index_write_tree( &newTreeOid,
index ), GIT_OK );
259 git_tree* newTree =
nullptr;
260 BOOST_REQUIRE_EQUAL( git_tree_lookup( &newTree, repo, &newTreeOid ), GIT_OK );
263 git_reference* head =
nullptr;
264 BOOST_REQUIRE_EQUAL( git_repository_head( &head, repo ), GIT_OK );
267 git_commit* parentCommit =
nullptr;
269 git_commit_lookup( &parentCommit, repo, git_reference_target( head ) ), GIT_OK );
272 git_signature* sig =
nullptr;
273 BOOST_REQUIRE_EQUAL( git_signature_now( &sig,
"QA Test",
"[email protected]" ), GIT_OK );
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 ),
283 git_treebuilder* tb =
nullptr;
284 BOOST_REQUIRE_EQUAL( git_treebuilder_new( &tb, repo,
nullptr ), GIT_OK );
287 const std::string upstreamData =
"upstream\n";
288 BOOST_REQUIRE_EQUAL( git_blob_create_from_buffer( &blobOid, repo, upstreamData.data(),
289 upstreamData.size() ),
291 BOOST_REQUIRE_EQUAL( git_treebuilder_insert(
nullptr, tb,
"remote_only.txt", &blobOid,
295 git_oid remoteTreeOid;
296 BOOST_REQUIRE_EQUAL( git_treebuilder_write( &remoteTreeOid, tb ), GIT_OK );
297 git_treebuilder_free( tb );
299 git_tree* remoteTree =
nullptr;
300 BOOST_REQUIRE_EQUAL( git_tree_lookup( &remoteTree, repo, &remoteTreeOid ), GIT_OK );
303 git_oid remoteCommitOid;
304 BOOST_REQUIRE_EQUAL( git_commit_create( &remoteCommitOid, repo,
nullptr, sig, sig,
nullptr,
305 "upstream root", remoteTree, 0,
nullptr ),
308 git_reference* remoteRef =
nullptr;
309 BOOST_REQUIRE_EQUAL( git_reference_create( &remoteRef, repo,
"refs/remotes/origin/main",
310 &remoteCommitOid,
true,
nullptr ),
314 git_config* cfg =
nullptr;
315 BOOST_REQUIRE_EQUAL( git_repository_config( &cfg, repo ), GIT_OK );
317 BOOST_REQUIRE_EQUAL( git_config_set_string( cfg,
"branch.main.remote",
"origin" ),
319 BOOST_REQUIRE_EQUAL( git_config_set_string( cfg,
"branch.main.merge",
"refs/heads/main" ),
331 "untouched.txt should not be reported as AHEAD when the local and "
332 "remote histories share no ancestor" );
334 "untouched.txt should not be reported as BEHIND when the local "
335 "and remote histories share no ancestor" );
348 git_repository* repo = makeRepoWithCommit( tmp.
path,
"untouched.txt",
"stable\n" );
352 wxFileName extra( tmp.
path, wxS(
"touched.txt" ) );
353 std::ofstream f( extra.GetFullPath().utf8_string() );
357 git_index*
index =
nullptr;
358 BOOST_REQUIRE_EQUAL( git_repository_index( &
index, repo ), GIT_OK );
360 BOOST_REQUIRE_EQUAL( git_index_add_bypath(
index,
"touched.txt" ), GIT_OK );
361 BOOST_REQUIRE_EQUAL( git_index_write(
index ), GIT_OK );
364 BOOST_REQUIRE_EQUAL( git_index_write_tree( &treeOid,
index ), GIT_OK );
366 git_tree* tree =
nullptr;
367 BOOST_REQUIRE_EQUAL( git_tree_lookup( &tree, repo, &treeOid ), GIT_OK );
370 git_reference* head =
nullptr;
371 BOOST_REQUIRE_EQUAL( git_repository_head( &head, repo ), GIT_OK );
374 git_commit* parent =
nullptr;
375 BOOST_REQUIRE_EQUAL( git_commit_lookup( &parent, repo, git_reference_target( head ) ),
379 git_signature* sig =
nullptr;
380 BOOST_REQUIRE_EQUAL( git_signature_now( &sig,
"QA Test",
"[email protected]" ), GIT_OK );
383 const git_commit* parents[1] = { parent };
385 BOOST_REQUIRE_EQUAL( git_commit_create( &base_oid, repo,
"HEAD", sig, sig,
nullptr,
386 "add touched", tree, 1, parents ),
391 git_remote* origin =
nullptr;
392 BOOST_REQUIRE_EQUAL( git_remote_create( &origin, repo,
"origin",
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 ),
403 git_config* cfg =
nullptr;
404 BOOST_REQUIRE_EQUAL( git_repository_config( &cfg, repo ), GIT_OK );
406 BOOST_REQUIRE_EQUAL( git_config_set_string( cfg,
"branch.main.remote",
"origin" ),
408 BOOST_REQUIRE_EQUAL( git_config_set_string( cfg,
"branch.main.merge",
"refs/heads/main" ),
413 wxFileName extra( tmp.
path, wxS(
"touched.txt" ) );
414 std::ofstream f( extra.GetFullPath().utf8_string() );
418 BOOST_REQUIRE_EQUAL( git_index_add_bypath(
index,
"touched.txt" ), GIT_OK );
419 BOOST_REQUIRE_EQUAL( git_index_write(
index ), GIT_OK );
422 BOOST_REQUIRE_EQUAL( git_index_write_tree( &newTreeOid,
index ), GIT_OK );
424 git_tree* newTree =
nullptr;
425 BOOST_REQUIRE_EQUAL( git_tree_lookup( &newTree, repo, &newTreeOid ), GIT_OK );
428 git_commit* baseCommit =
nullptr;
429 BOOST_REQUIRE_EQUAL( git_commit_lookup( &baseCommit, repo, &base_oid ), GIT_OK );
432 const git_commit* aheadParents[1] = { baseCommit };
434 BOOST_REQUIRE_EQUAL( git_commit_create( &aheadOid, repo,
"HEAD", sig, sig,
nullptr,
435 "modify touched", newTree, 1, aheadParents ),
441 BOOST_CHECK( remote.empty() );
443 "touched.txt should be reported as AHEAD" );
445 "untouched.txt should NOT be reported as AHEAD - this was the "
446 "regression in issue 21576" );
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")