31#include <wx/filename.h>
33#include <wx/stdpaths.h>
45 GitInitGuard() { git_libgit2_init(); }
46 ~GitInitGuard() { git_libgit2_shutdown(); }
56 auto now = std::chrono::steady_clock::now().time_since_epoch();
57 long long ticks = std::chrono::duration_cast<std::chrono::nanoseconds>( now ).count();
59 wxString base = wxFileName::GetTempDir();
62 fn.AppendDir( wxString::Format(
"kicad-qa-git-%lld-%p", ticks,
this ) );
63 wxFileName::Mkdir( fn.GetPath(), wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
70 wxFileName::Rmdir( path, wxPATH_RMDIR_RECURSIVE );
76git_repository* makeRepoWithCommit(
const wxString& aRepoPath,
const wxString& aFileName,
77 const std::string& aFileContents )
79 git_repository* repo =
nullptr;
80 git_repository_init_options init_opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
81 init_opts.flags = GIT_REPOSITORY_INIT_MKPATH;
82 init_opts.initial_head =
"main";
84 BOOST_REQUIRE_EQUAL( git_repository_init_ext( &repo, aRepoPath.utf8_string().c_str(),
89 wxFileName filePath( aRepoPath, aFileName );
91 std::ofstream f( filePath.GetFullPath().utf8_string() );
96 git_config* cfg =
nullptr;
97 BOOST_REQUIRE_EQUAL( git_repository_config( &cfg, repo ), GIT_OK );
99 git_config_set_string( cfg,
"user.name",
"QA Test" );
103 git_index*
index =
nullptr;
104 BOOST_REQUIRE_EQUAL( git_repository_index( &
index, repo ), GIT_OK );
106 BOOST_REQUIRE_EQUAL( git_index_add_bypath(
index, aFileName.utf8_string().c_str() ), GIT_OK );
107 BOOST_REQUIRE_EQUAL( git_index_write(
index ), GIT_OK );
110 BOOST_REQUIRE_EQUAL( git_index_write_tree( &tree_oid,
index ), GIT_OK );
112 git_tree* tree =
nullptr;
113 BOOST_REQUIRE_EQUAL( git_tree_lookup( &tree, repo, &tree_oid ), GIT_OK );
116 git_signature* sig =
nullptr;
117 BOOST_REQUIRE_EQUAL( git_signature_now( &sig,
"QA Test",
"[email protected]" ), GIT_OK );
121 BOOST_REQUIRE_EQUAL( git_commit_create( &commit_oid, repo,
"HEAD", sig, sig,
nullptr,
122 "initial", tree, 0,
nullptr ),
143 git_repository* repo = makeRepoWithCommit( tmp.path,
"file.txt",
"data\n" );
149 BOOST_CHECK( !root.IsEmpty() );
150 BOOST_CHECK_MESSAGE( !root.Contains( wxS(
"/.git" ) ) && !root.Contains( wxS(
"\\.git" ) ),
151 "GetGitRootDirectory should return the working directory, got: "
152 + root.ToStdString() );
156 rootFn.AssignDir( root );
158 tmpFn.AssignDir( tmp.path );
159 BOOST_CHECK_EQUAL( rootFn.GetFullPath().ToStdString(), tmpFn.GetFullPath().ToStdString() );
171 git_repository* repo = makeRepoWithCommit( tmp.path,
"file.txt",
"data\n" );
179 git_remote* remote =
nullptr;
180 BOOST_REQUIRE_EQUAL( git_remote_create( &remote, repo,
"github",
194 git_repository* repo = makeRepoWithCommit( tmp.path,
"file.txt",
"data\n" );
197 git_remote* remote =
nullptr;
198 BOOST_REQUIRE_EQUAL( git_remote_create( &remote, repo,
"origin",
216 git_repository* repo = makeRepoWithCommit( tmp.path,
"file.txt",
"data\n" );
223 "Expected no AHEAD files when no upstream is configured, got "
224 + std::to_string( local.size() ) );
226 "Expected no BEHIND files when no upstream is configured, got "
227 + std::to_string( remote.size() ) );
244 git_repository* repo = makeRepoWithCommit( tmp.path,
"untouched.txt",
"stable\n" );
249 wxFileName extra( tmp.path, wxS(
"second.txt" ) );
250 std::ofstream f( extra.GetFullPath().utf8_string() );
254 git_index*
index =
nullptr;
255 BOOST_REQUIRE_EQUAL( git_repository_index( &
index, repo ), GIT_OK );
257 BOOST_REQUIRE_EQUAL( git_index_add_bypath(
index,
"second.txt" ), GIT_OK );
258 BOOST_REQUIRE_EQUAL( git_index_write(
index ), GIT_OK );
261 BOOST_REQUIRE_EQUAL( git_index_write_tree( &newTreeOid,
index ), GIT_OK );
263 git_tree* newTree =
nullptr;
264 BOOST_REQUIRE_EQUAL( git_tree_lookup( &newTree, repo, &newTreeOid ), GIT_OK );
267 git_reference* head =
nullptr;
268 BOOST_REQUIRE_EQUAL( git_repository_head( &head, repo ), GIT_OK );
271 git_commit* parentCommit =
nullptr;
273 git_commit_lookup( &parentCommit, repo, git_reference_target( head ) ), GIT_OK );
276 git_signature* sig =
nullptr;
277 BOOST_REQUIRE_EQUAL( git_signature_now( &sig,
"QA Test",
"[email protected]" ), GIT_OK );
280 const git_commit* parents[1] = { parentCommit };
281 git_oid secondCommitOid;
282 BOOST_REQUIRE_EQUAL( git_commit_create( &secondCommitOid, repo,
"HEAD", sig, sig,
nullptr,
283 "second", newTree, 1, parents ),
287 git_treebuilder* tb =
nullptr;
288 BOOST_REQUIRE_EQUAL( git_treebuilder_new( &tb, repo,
nullptr ), GIT_OK );
291 const std::string upstreamData =
"upstream\n";
292 BOOST_REQUIRE_EQUAL( git_blob_create_from_buffer( &blobOid, repo, upstreamData.data(),
293 upstreamData.size() ),
295 BOOST_REQUIRE_EQUAL( git_treebuilder_insert(
nullptr, tb,
"remote_only.txt", &blobOid,
299 git_oid remoteTreeOid;
300 BOOST_REQUIRE_EQUAL( git_treebuilder_write( &remoteTreeOid, tb ), GIT_OK );
301 git_treebuilder_free( tb );
303 git_tree* remoteTree =
nullptr;
304 BOOST_REQUIRE_EQUAL( git_tree_lookup( &remoteTree, repo, &remoteTreeOid ), GIT_OK );
307 git_oid remoteCommitOid;
308 BOOST_REQUIRE_EQUAL( git_commit_create( &remoteCommitOid, repo,
nullptr, sig, sig,
nullptr,
309 "upstream root", remoteTree, 0,
nullptr ),
312 git_reference* remoteRef =
nullptr;
313 BOOST_REQUIRE_EQUAL( git_reference_create( &remoteRef, repo,
"refs/remotes/origin/main",
314 &remoteCommitOid,
true,
nullptr ),
318 git_config* cfg =
nullptr;
319 BOOST_REQUIRE_EQUAL( git_repository_config( &cfg, repo ), GIT_OK );
321 BOOST_REQUIRE_EQUAL( git_config_set_string( cfg,
"branch.main.remote",
"origin" ),
323 BOOST_REQUIRE_EQUAL( git_config_set_string( cfg,
"branch.main.merge",
"refs/heads/main" ),
335 "untouched.txt should not be reported as AHEAD when the local and "
336 "remote histories share no ancestor" );
338 "untouched.txt should not be reported as BEHIND when the local "
339 "and remote histories share no ancestor" );
352 git_repository* repo = makeRepoWithCommit( tmp.path,
"untouched.txt",
"stable\n" );
356 wxFileName extra( tmp.path, wxS(
"touched.txt" ) );
357 std::ofstream f( extra.GetFullPath().utf8_string() );
361 git_index*
index =
nullptr;
362 BOOST_REQUIRE_EQUAL( git_repository_index( &
index, repo ), GIT_OK );
364 BOOST_REQUIRE_EQUAL( git_index_add_bypath(
index,
"touched.txt" ), GIT_OK );
365 BOOST_REQUIRE_EQUAL( git_index_write(
index ), GIT_OK );
368 BOOST_REQUIRE_EQUAL( git_index_write_tree( &treeOid,
index ), GIT_OK );
370 git_tree* tree =
nullptr;
371 BOOST_REQUIRE_EQUAL( git_tree_lookup( &tree, repo, &treeOid ), GIT_OK );
374 git_reference* head =
nullptr;
375 BOOST_REQUIRE_EQUAL( git_repository_head( &head, repo ), GIT_OK );
378 git_commit* parent =
nullptr;
379 BOOST_REQUIRE_EQUAL( git_commit_lookup( &parent, repo, git_reference_target( head ) ),
383 git_signature* sig =
nullptr;
384 BOOST_REQUIRE_EQUAL( git_signature_now( &sig,
"QA Test",
"[email protected]" ), GIT_OK );
387 const git_commit* parents[1] = { parent };
389 BOOST_REQUIRE_EQUAL( git_commit_create( &base_oid, repo,
"HEAD", sig, sig,
nullptr,
390 "add touched", tree, 1, parents ),
395 git_remote* origin =
nullptr;
396 BOOST_REQUIRE_EQUAL( git_remote_create( &origin, repo,
"origin",
401 git_reference* upstream_ref =
nullptr;
402 BOOST_REQUIRE_EQUAL( git_reference_create( &upstream_ref, repo,
"refs/remotes/origin/main",
403 &base_oid,
true,
nullptr ),
407 git_config* cfg =
nullptr;
408 BOOST_REQUIRE_EQUAL( git_repository_config( &cfg, repo ), GIT_OK );
410 BOOST_REQUIRE_EQUAL( git_config_set_string( cfg,
"branch.main.remote",
"origin" ),
412 BOOST_REQUIRE_EQUAL( git_config_set_string( cfg,
"branch.main.merge",
"refs/heads/main" ),
417 wxFileName extra( tmp.path, wxS(
"touched.txt" ) );
418 std::ofstream f( extra.GetFullPath().utf8_string() );
422 BOOST_REQUIRE_EQUAL( git_index_add_bypath(
index,
"touched.txt" ), GIT_OK );
423 BOOST_REQUIRE_EQUAL( git_index_write(
index ), GIT_OK );
426 BOOST_REQUIRE_EQUAL( git_index_write_tree( &newTreeOid,
index ), GIT_OK );
428 git_tree* newTree =
nullptr;
429 BOOST_REQUIRE_EQUAL( git_tree_lookup( &newTree, repo, &newTreeOid ), GIT_OK );
432 git_commit* baseCommit =
nullptr;
433 BOOST_REQUIRE_EQUAL( git_commit_lookup( &baseCommit, repo, &base_oid ), GIT_OK );
436 const git_commit* aheadParents[1] = { baseCommit };
438 BOOST_REQUIRE_EQUAL( git_commit_create( &aheadOid, repo,
"HEAD", sig, sig,
nullptr,
439 "modify touched", newTree, 1, aheadParents ),
445 BOOST_CHECK( remote.empty() );
447 "touched.txt should be reported as AHEAD" );
449 "untouched.txt should NOT be reported as AHEAD - this was the "
450 "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.
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")