35#include <git2/sys/repository.h>
39#include <wx/filename.h>
42#include <wx/stdpaths.h>
64const char* c_opensshEncrypted =
"-----BEGIN OPENSSH PRIVATE KEY-----\n"
65 "b3BlbnNzaC1rZXktdjEAAAAACmFlczI1Ni1jdHIAAAAGYmNyeXB0AAAAGAAAABAxS2G8mY\n"
66 "+85LKkVcFtJi1GAAAAGAAAAAEAAAAzAAAAC3NzaC1lZDI1NTE5AAAAIKB0oSTjiyvxC+qh\n"
67 "cJ2zEnPDzZWArHcl90v2Se1SkmoGAAAAkKKrIYipMtR+JUEsuoYSmnfD0b/ng57Fq2uj25\n"
68 "oHDRqrDyPKS0xqQJa9+IQvPeM+E3G1fAGvsOYTQXsLJzXuQDiLX3vceEuN9AmJkA9Naea4\n"
69 "qAPy8CTlBx/ktdd4YgnYXBETi6Ab1f/h7LAVlLJTOc9Ta9HilKzO8Vl5p3EUS1PWkfGGqe\n"
70 "QLo1/ryfJjC3ZZbA==\n"
71 "-----END OPENSSH PRIVATE KEY-----\n";
74const char* c_opensshPlain =
"-----BEGIN OPENSSH PRIVATE KEY-----\n"
75 "b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW\n"
76 "QyNTUxOQAAACD4Xd9qfxjO+vpBXO94Otoa0kg/p5H8kMYhN6eFe7v8MgAAAIjYZhWO2GYV\n"
77 "jgAAAAtzc2gtZWQyNTUxOQAAACD4Xd9qfxjO+vpBXO94Otoa0kg/p5H8kMYhN6eFe7v8Mg\n"
78 "AAAEBNlyz61Kiufy1a3WO6i2I7vB+7W2iQc8R81H0qrBheSfhd32p/GM76+kFc73g62hrS\n"
79 "SD+nkfyQxiE3p4V7u/wyAAAAAXQBAgME\n"
80 "-----END OPENSSH PRIVATE KEY-----\n";
83const char* c_opensshTruncated =
"-----BEGIN OPENSSH PRIVATE KEY-----\n"
85 "-----END OPENSSH PRIVATE KEY-----\n";
88const char* c_pemEncrypted =
"-----BEGIN EC PRIVATE KEY-----\n"
89 "Proc-Type: 4,ENCRYPTED\n"
90 "DEK-Info: AES-128-CBC,10297828DB5031A5C22276D18E077B61\n"
92 "rHfBqMZtByvcsRb7zgNtkSjhFgnRY3jpyyagjM8jxNoEDRaSSHNSugON80psxytE\n"
93 "HBTGShcJbDXOC7EER7QZOTjM/d+3DB+FEL4EwnJxgnVfWN/5p9hP3CoJLgeItkQV\n"
94 "POqt3mQ1V8AnJibunyn13YrczHM39IFruBB9JvNKKG+i/7yf4Jy4jCB9nicH7TC+\n"
95 "hQCu/mOMde02ziwULJiWIU3EALSYo6JhDJxTRIqUr7TDGXEAPcxpQEvMDz+HAgzY\n"
96 "9zTU6LC/CSbHQNjHYSvgDY4zRDFWxeHgNeQVfqrlOPGGE8NC5HCeo5i1r+rSVPn+\n"
97 "kzqXcGpwN682lM/oJV4ix3her0jf59RnAPKYwyWXfhUwYFtzFwroOoquFi6+HHVT\n"
98 "LlzJacbVVj/GTBOdHBSsu8cfiVLymHhEEzQCyIYY05DeHFe8GiSwDDdvMCPppX38\n"
99 "/R3PnNaEYwOEUK4o50nsWEfPHQw8kZaUkiUcX7uIyV4=\n"
100 "-----END EC PRIVATE KEY-----\n";
103const char* c_pemPlain =
"-----BEGIN EC PRIVATE KEY-----\n"
104 "MIIBaAIBAQQgGuajOowPzHaTJKkj1W+RQS0p87K8bEkZbgbw5pmVArmggfowgfcC\n"
105 "AQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAAAAAAAAAAAAAA////////////////\n"
106 "MFsEIP////8AAAABAAAAAAAAAAAAAAAA///////////////8BCBaxjXYqjqT57Pr\n"
107 "vVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSdNgiG5wSTamZ44ROdJreBn36QBEEE\n"
108 "axfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5RdiYwpZP40Li/hp/m47n60p8D54W\n"
109 "K84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA//////////+85vqtpxeehPO5ysL8\n"
110 "YyVRAgEBoUQDQgAELhPMgsc2xVYFBrKzqXq8CBRS8t9BBCnznM96FcZFv1MvdbIK\n"
111 "yn1nix57Bwq0VoyooErjaZ3SpXl47JlJpIsm4A==\n"
112 "-----END EC PRIVATE KEY-----\n";
115const char* c_pkcs8Encrypted =
"-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
116 "MIIBzzBJBgkqhkiG9w0BBQ0wPDAbBgkqhkiG9w0BBQwwDgQImaRUWsA/SFECAggA\n"
117 "MB0GCWCGSAFlAwQBAgQQiywEwYp7En3NzOemv0ek9QSCAYAIh56t3GB2r1S/B7Cy\n"
118 "UFS4qn1DvGDuhubEl6cw3n8Y1PnWj6ryq6O9Pw/DKdnKonzVN+PVaofXg26ZPLdR\n"
119 "q+J7fmcMPIEC0DqV/6T+oGqWp2iaiTZ11BBOoyBkd8ev59c/TxngOWlbJvvmGhEF\n"
120 "mOiCwXKoKjzU8kSVbmL+vFe1thZjrCaihL79JsZE6Ws5bd7yAn6grPBPI8sBexNi\n"
121 "fERG81T9Nu2mxCEd7zkNAoK0cZBnUwFvxDama18wMvXRYzEHETCUCWevoNFdcwo/\n"
122 "IkGgLciMxtSBBOXyWiVvAzTloSGHa5wafptTsgyTc1HKZBTR0ZTNvBX37aziggWE\n"
123 "u4fMHcHlAJwP4UXgCACtgcfktVhjohgNQdBm5RB95qEUdNAQDQSe4Ro27HfT0YyR\n"
124 "AHZYNXziRjZHsO6wqGarcxdZkH1Cy8Ca9FJr3uRWWr+8CWIjE9nDEkFbXUyQGOPq\n"
125 "sTAC7FpnQHVczHcaa45jXYaPmU/iNFnPkO2DzxWADe/1xIg=\n"
126 "-----END ENCRYPTED PRIVATE KEY-----\n";
130git_repository* makeRepoWithCommit(
const wxString& aRepoPath,
const wxString& aFileName,
131 const std::string& aFileContents )
136 return repo.release();
141void commitWorkingFile(
git_repository* aRepo,
const wxString& aFileName,
const char* aMessage )
147std::string readWorkingFile(
const wxFileName& aFile )
149 std::ifstream in( aFile.GetFullPath().utf8_string() );
150 return std::string( ( std::istreambuf_iterator<char>( in ) ), std::istreambuf_iterator<char>() );
154void writeFile(
const wxString& aRepoPath,
const wxString& aFileName,
const char* aContent )
161bool waitForQuery(
const std::function<
bool()>& aFinished )
163 const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds( 5 );
165 while( !aFinished() && std::chrono::steady_clock::now() < deadline )
167 wxTheApp->ProcessPendingEvents();
168 std::this_thread::sleep_for( std::chrono::milliseconds( 1 ) );
177struct UI_CALLBACK_CAPTURE
179 explicit UI_CALLBACK_CAPTURE( std::shared_ptr<std::atomic<bool>> aWrongThread ) :
180 wrongThread( std::
move( aWrongThread ) ),
181 uiThread( std::this_thread::get_id() )
185 UI_CALLBACK_CAPTURE(
const UI_CALLBACK_CAPTURE& aOther ) :
186 wrongThread( aOther.wrongThread ),
187 uiThread( aOther.uiThread )
189 if( std::this_thread::get_id() != uiThread )
190 wrongThread->store(
true );
193 UI_CALLBACK_CAPTURE( UI_CALLBACK_CAPTURE&& ) =
default;
195 ~UI_CALLBACK_CAPTURE()
197 if( wrongThread && std::this_thread::get_id() != uiThread )
198 wrongThread->store(
true );
201 std::shared_ptr<std::atomic<bool>> wrongThread;
202 std::thread::id uiThread;
215 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"data\n" );
222 BOOST_CHECK_MESSAGE( !root.Contains( wxS(
"/.git" ) ) && !root.Contains( wxS(
"\\.git" ) ),
223 "GetGitRootDirectory should return the working directory, got: "
224 + root.ToStdString() );
228 rootFn.AssignDir( root );
229 rootFn.MakeAbsolute();
231 tmpFn.AssignDir( TempPath() );
232 tmpFn.MakeAbsolute();
233 BOOST_CHECK_EQUAL( rootFn.GetFullPath().ToStdString(), tmpFn.GetFullPath().ToStdString() );
240 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"data\n" );
248 git_remote* remote =
nullptr;
260 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"data\n" );
263 git_remote* remote =
nullptr;
278 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"data\n" );
284 BOOST_CHECK_MESSAGE( local.empty(),
285 "Expected no AHEAD files when no upstream is configured, got "
286 + std::to_string( local.size() ) );
287 BOOST_CHECK_MESSAGE( remote.empty(),
288 "Expected no BEHIND files when no upstream is configured, got "
289 + std::to_string( remote.size() ) );
298 git_repository* repo = makeRepoWithCommit( TempPath(),
"untouched.txt",
"stable\n" );
303 wxFileName extra( TempPath(), wxS(
"second.txt" ) );
304 std::ofstream f( extra.GetFullPath().utf8_string() );
308 git_index*
index =
nullptr;
317 git_tree* newTree =
nullptr;
321 git_reference* head =
nullptr;
325 git_commit* parentCommit =
nullptr;
327 git_commit_lookup( &parentCommit, repo, git_reference_target( head ) ), GIT_OK );
330 git_signature* sig =
nullptr;
334 const git_commit* parents[1] = { parentCommit };
335 git_oid secondCommitOid;
337 "second", newTree, 1, parents ),
341 git_treebuilder* tb =
nullptr;
345 const std::string upstreamData =
"upstream\n";
347 upstreamData.size() ),
353 git_oid remoteTreeOid;
355 git_treebuilder_free( tb );
357 git_tree* remoteTree =
nullptr;
361 git_oid remoteCommitOid;
362 BOOST_REQUIRE_EQUAL( git_commit_create( &remoteCommitOid, repo,
nullptr, sig, sig,
nullptr,
363 "upstream root", remoteTree, 0,
nullptr ),
366 git_reference* remoteRef =
nullptr;
368 &remoteCommitOid,
true,
nullptr ),
372 git_config* cfg =
nullptr;
387 BOOST_CHECK_MESSAGE( local.empty(),
"no files should be reported as AHEAD when the local and remote "
388 "histories share no ancestor" );
389 BOOST_CHECK_MESSAGE( remote.empty(),
"no files should be reported as BEHIND when the local and remote "
390 "histories share no ancestor" );
398 git_repository* repo = makeRepoWithCommit( TempPath(),
"untouched.txt",
"stable\n" );
402 wxFileName extra( TempPath(), wxS(
"touched.txt" ) );
403 std::ofstream f( extra.GetFullPath().utf8_string() );
407 git_index*
index =
nullptr;
416 git_tree* tree =
nullptr;
420 git_reference* head =
nullptr;
424 git_commit* parent =
nullptr;
429 git_signature* sig =
nullptr;
433 const git_commit* parents[1] = { parent };
436 "add touched", tree, 1, parents ),
441 git_remote* origin =
nullptr;
447 git_reference* upstream_ref =
nullptr;
448 BOOST_REQUIRE_EQUAL( git_reference_create( &upstream_ref, repo,
"refs/remotes/origin/main",
449 &base_oid,
true,
nullptr ),
453 git_config* cfg =
nullptr;
463 wxFileName extra( TempPath(), wxS(
"touched.txt" ) );
464 std::ofstream f( extra.GetFullPath().utf8_string() );
474 git_tree* newTree =
nullptr;
478 git_commit* baseCommit =
nullptr;
482 const git_commit* aheadParents[1] = { baseCommit };
485 "modify touched", newTree, 1, aheadParents ),
492 BOOST_CHECK_MESSAGE( local.find( wxS(
"touched.txt" ) ) != local.end(),
493 "touched.txt should be reported as AHEAD" );
494 BOOST_CHECK_MESSAGE( local.find( wxS(
"untouched.txt" ) ) == local.end(),
495 "untouched.txt should NOT be reported as AHEAD - this was the "
496 "regression in issue 21576" );
503 "A passphrase-protected OpenSSH key must be reported as encrypted" );
506 "An OpenSSH key with cipher \"none\" must be reported as unencrypted" );
509 "An OpenSSH key with an unreadable body must not be reported as encrypted" );
516 "A PEM key with Proc-Type: 4,ENCRYPTED must be reported as encrypted" );
519 "A PEM key with no Proc-Type must be reported as unencrypted" );
526 "A PKCS#8 key headed ENCRYPTED PRIVATE KEY must be reported as encrypted" );
535 "A public key is not an encrypted private key" );
543 git_repository* repo = makeRepoWithCommit( TempPath(),
"untouched.txt",
"stable\n" );
546 git_reference* head =
nullptr;
550 git_oid baseOid = *git_reference_target( head );
552 git_commit* baseCommit =
nullptr;
559 wxFileName extra( TempPath(), wxS(
"remote_only.txt" ) );
560 std::ofstream f( extra.GetFullPath().utf8_string() );
561 f <<
"from remote\n";
564 git_index*
index =
nullptr;
573 git_tree* tree =
nullptr;
577 git_signature* sig =
nullptr;
581 const git_commit* parents[1] = { baseCommit };
584 git_commit_create( &remoteOid, repo,
nullptr, sig, sig,
nullptr,
"add remote_only", tree, 1, parents ),
587 git_remote* origin =
nullptr;
591 git_reference* upstream_ref =
nullptr;
593 git_reference_create( &upstream_ref, repo,
"refs/remotes/origin/main", &remoteOid,
true,
nullptr ),
597 git_config* cfg =
nullptr;
600 BOOST_REQUIRE_EQUAL( git_config_set_string( cfg,
"branch.main.remote",
"origin" ), GIT_OK );
601 BOOST_REQUIRE_EQUAL( git_config_set_string( cfg,
"branch.main.merge",
"refs/heads/main" ), GIT_OK );
607 BOOST_CHECK_MESSAGE( remote.find( wxS(
"remote_only.txt" ) ) != remote.end(),
608 "remote_only.txt should be reported as BEHIND" );
751 token->store(
true );
767 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
772 wxFileName fn( TempPath(), wxS(
"file.txt" ) );
773 std::ofstream f( fn.GetFullPath().utf8_string() );
777 git_index*
index =
nullptr;
786 git_tree* tree =
nullptr;
790 git_reference* head =
nullptr;
794 git_commit* parent =
nullptr;
795 BOOST_REQUIRE_EQUAL( git_commit_lookup( &parent, repo, git_reference_target( head ) ), GIT_OK );
798 git_signature* sig =
nullptr;
802 const git_commit* parents[1] = { parent };
804 BOOST_REQUIRE_EQUAL( git_commit_create( &secondOid, repo,
"HEAD", sig, sig,
nullptr,
"second", tree, 1, parents ),
837 for(
const wxString& bad : { wxT(
"a@" ), wxT(
"@" ), wxT(
"@@" ), wxT(
"a@b" ), wxT(
"@b.c" ), wxT(
"a [email protected]" ),
881 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
895 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
898 git_reference* head =
nullptr;
902 git_commit* parent =
nullptr;
903 BOOST_REQUIRE_EQUAL( git_commit_lookup( &parent, repo, git_reference_target( head ) ), GIT_OK );
906 git_tree* tree =
nullptr;
911 const char* latin1Author =
"J\xf6rg";
912 const char* latin1Summary =
"Caf\xe9 fix";
914 git_signature* sig =
nullptr;
918 const git_commit* parents[1] = { parent };
920 BOOST_REQUIRE_EQUAL( git_commit_create( &oid, repo,
"HEAD", sig, sig,
nullptr, latin1Summary, tree, 1, parents ),
927 BOOST_CHECK_EQUAL( commits[0].summary.ToStdString(), wxString::FromUTF8(
"Caf\xc3\xa9 fix" ).ToStdString() );
928 BOOST_CHECK_EQUAL( commits[0].author.ToStdString(), wxString::FromUTF8(
"J\xc3\xb6rg" ).ToStdString() );
940 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
947 git_commit* headCommit =
nullptr;
951 git_reference* side =
nullptr;
956 writeFile( TempPath(),
"side.txt",
"side\n" );
957 commitWorkingFile( repo,
"side.txt",
"side work" );
960 writeFile( TempPath(),
"file.txt",
"v2\n" );
961 commitWorkingFile( repo,
"file.txt",
"main work" );
965 auto summaries = [](
const std::vector<KIGIT_COMMIT_INFO>& aCommits )
967 std::vector<std::string> out;
970 out.push_back( c.summary.ToStdString() );
978 BOOST_CHECK( std::find( all.begin(), all.end(),
"side work" ) != all.end() );
979 BOOST_CHECK( std::find( all.begin(), all.end(),
"main work" ) != all.end() );
983 onMain.
branch = wxS(
"main" );
984 std::vector<std::string> mainOnly = summaries( common.
GetRecentCommits( 10, onMain ) );
991 search.
search = wxS(
"SIDE" );
992 std::vector<KIGIT_COMMIT_INFO> found = common.
GetRecentCommits( 10, search );
996 search.
search = found[0].hash.Left( 6 );
999 search.
search = wxS(
"nothing like this" );
1008 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
1014 git_object* first =
nullptr;
1015 BOOST_REQUIRE_EQUAL( git_object_lookup( &first, repo, &firstOid, GIT_OBJECT_COMMIT ), GIT_OK );
1019 BOOST_REQUIRE_EQUAL( git_tag_create_lightweight( &tagOid, repo,
"v1.0", first, 0 ), GIT_OK );
1021 writeFile( TempPath(),
"file.txt",
"v2\n" );
1022 commitWorkingFile( repo,
"file.txt",
"second" );
1045 git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
1046 opts.flags = GIT_REPOSITORY_INIT_MKPATH;
1047 opts.initial_head =
"main";
1049 BOOST_REQUIRE_EQUAL( git_repository_init_ext( &repo, TempPath().utf8_string().c_str(), &opts ), GIT_OK );
1072 git_reference* head =
nullptr;
1076 BOOST_CHECK_EQUAL( std::string( git_reference_symbolic_target( head ) ),
"refs/heads/rev-a" );
1085 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
1088 writeFile( TempPath(),
"file.txt",
"v2\n" );
1089 commitWorkingFile( repo,
"file.txt",
"Route the power rails\n\nBody line one.\nBody line two.\n" );
1101 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"one\ntwo\nthree\n" );
1105 writeFile( TempPath(),
"file.txt",
"one\n2\nthree\n" );
1106 writeFile( TempPath(),
"new.txt",
"a\nb\n" );
1109 git_index*
index =
nullptr;
1116 commitWorkingFile( repo,
"file.txt",
"edit and add\n\nWith a body line." );
1161 git_repository* repo = makeRepoWithCommit( TempPath(),
"readme.txt",
"root\n" );
1164 BOOST_REQUIRE( wxFileName::Mkdir( TempPath() + wxS(
"/mine" ) ) );
1165 BOOST_REQUIRE( wxFileName::Mkdir( TempPath() + wxS(
"/theirs" ) ) );
1166 writeFile( TempPath(), wxS(
"mine/board.txt" ),
"one\ntwo\n" );
1167 writeFile( TempPath(), wxS(
"theirs/board.txt" ),
"one\ntwo\n" );
1170 git_index*
index =
nullptr;
1178 commitWorkingFile( repo, wxS(
"readme.txt" ),
"two projects\n" );
1181 writeFile( TempPath(), wxS(
"mine/board.txt" ),
"one\ntwo\nthree\n" );
1182 writeFile( TempPath(), wxS(
"theirs/board.txt" ),
"one\ntwo\na\nb\nc\n" );
1183 writeFile( TempPath(), wxS(
"mine/~board.lck" ),
"locked\n" );
1204 wxString repoRoot = TempPath() + wxS(
"/real/repo" );
1205 BOOST_REQUIRE( wxFileName::Mkdir( repoRoot + wxS(
"/proj" ), wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
1206 writeFile( repoRoot, wxS(
"proj/p.kicad_pro" ),
"{}\n" );
1208 wxString deepLink = TempPath() + wxS(
"/deep" );
1209 BOOST_REQUIRE_EQUAL( symlink( ( repoRoot + wxS(
"/proj" ) ).utf8_string().c_str(), deepLink.utf8_string().c_str() ),
1212 wxString rootLink = TempPath() + wxS(
"/top" );
1213 BOOST_REQUIRE_EQUAL( symlink( repoRoot.utf8_string().c_str(), rootLink.utf8_string().c_str() ), 0 );
1215 const wxString canonicalWorkDir = repoRoot + wxS(
"/" );
1219 wxString deepProject = deepLink + wxS(
"/" );
1229 wxString topProject = rootLink + wxS(
"/proj/" );
1242 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"hello world\n" );
1245 wxString dest = TempPath() + wxFileName::GetPathSeparator() + wxS(
"extracted.txt" );
1252 in.ReadAll( &content );
1264 writeFile( TempPath(),
"schematics/root.kicad_sch",
"root sheet\n" );
1265 commitWorkingFile( repo.get(),
"schematics/root.kicad_sch",
"add root sheet" );
1266 writeFile( TempPath(),
"schematics/child.kicad_sch",
"child sheet\n" );
1267 commitWorkingFile( repo.get(),
"schematics/child.kicad_sch",
"add child sheet" );
1272 wxString schematicFile;
1277 root = workspace.
Root();
1280 BOOST_CHECK_EQUAL( readWorkingFile( wxFileName( boardFile ) ),
"board version\n" );
1285 otherRoot = other.
Root();
1290 BOOST_CHECK_EQUAL( readWorkingFile( wxFileName( otherBoard ) ),
"board version\n" );
1293 BOOST_CHECK( !wxFileName::DirExists( otherRoot ) );
1294 BOOST_CHECK_EQUAL( readWorkingFile( wxFileName( boardFile ) ),
"board version\n" );
1302 BOOST_CHECK_EQUAL( readWorkingFile( wxFileName( schematicFile ) ),
"root sheet\n" );
1303 wxFileName childFile( wxFileName( schematicFile ).GetPath(), wxS(
"child.kicad_sch" ) );
1308 BOOST_CHECK( !wxFileName::FileExists( boardFile ) );
1309 BOOST_CHECK( !wxFileName::FileExists( schematicFile ) );
1316 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
1325 git_object* target =
nullptr;
1330 BOOST_REQUIRE_EQUAL( git_tag_create_lightweight( &tagOid, repo,
"v1.0", target, 0 ), GIT_OK );
1332 std::vector<KIGIT_TAG_INFO> tags = common.
GetTags();
1343 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
1351 wxFileName fn( TempPath(), wxS(
"file.txt" ) );
1352 std::ofstream f( fn.GetFullPath().utf8_string() );
1356 git_signature* sig =
nullptr;
1361 BOOST_REQUIRE_EQUAL( git_stash_save( &stashOid, repo, sig,
"work in progress", GIT_STASH_DEFAULT ), GIT_OK );
1363 std::vector<KIGIT_STASH_INFO> stashes = common.
GetStashes();
1374 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
1379 auto dirtyAndStash = [&](
const char* aContent,
const wxString& aMessage )
1381 wxFileName fn( TempPath(), wxS(
"file.txt" ) );
1384 std::ofstream f( fn.GetFullPath().utf8_string() );
1391 dirtyAndStash(
"v2\n", wxS(
"first" ) );
1393 std::vector<KIGIT_STASH_INFO> before = common.
GetStashes();
1396 wxString firstId = before[0].id;
1399 dirtyAndStash(
"v3\n", wxS(
"second" ) );
1401 std::vector<KIGIT_STASH_INFO> after = common.
GetStashes();
1415 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
1420 writeFile( TempPath(),
"file.txt",
"v2\n" );
1423 wxFileName fn( TempPath(), wxS(
"file.txt" ) );
1426 std::vector<KIGIT_STASH_INFO> stashes = common.
GetStashes();
1439 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
1444 std::vector<KIGIT_BRANCH_INFO> branches = common.
GetBranches();
1446 wxString original = branches[0].name;
1451 BOOST_CHECK( std::any_of( branches.begin(), branches.end(),
1454 return aBranch.name == wxS(
"feature" ) && !aBranch.isRemote;
1467 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
1474 std::vector<KIGIT_TAG_INFO> tags = common.
GetTags();
1486 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
1493 git_config*
config =
nullptr;
1497 git_config_entry* nameEntry =
nullptr;
1499 BOOST_CHECK_EQUAL( wxString::FromUTF8( nameEntry->value ).ToStdString(),
"New Name" );
1500 git_config_entry_free( nameEntry );
1502 git_config_entry* emailEntry =
nullptr;
1505 git_config_entry_free( emailEntry );
1516 git_config*
config =
nullptr;
1521 git_config* local =
nullptr;
1528 preferences.authorName = wxS(
"Custom Author" );
1552 preferences.useDefaultAuthor =
true;
1557 preferences.useDefaultAuthor =
false;
1558 preferences.authorName.clear();
1568 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
1573 writeFile( TempPath(),
"file.txt",
"v2\n" );
1585 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
1590 writeFile( TempPath(),
"file.txt",
"v2\n" );
1611 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
1615 wxFileName fn( TempPath(), wxS(
"file.txt" ) );
1617 writeFile( TempPath(),
"file.txt",
"stashed\n" );
1622 writeFile( TempPath(),
"file.txt",
"committed\n" );
1623 commitWorkingFile( repo, wxS(
"file.txt" ),
"conflicting change" );
1629 git_index*
index =
nullptr;
1640 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
1648 auto setRebase = [repo](
const char* aValue )
1650 git_config* cfg =
nullptr;
1656 for(
const char* yes : {
"true",
"yes",
"on",
"1" } )
1662 for(
const char* no : {
"false",
"no",
"off",
"0" } )
1674 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
1683 std::optional<KIGIT_UPSTREAM_STATUS> status = common.
UpstreamStatus();
1701 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
1711 writeFile( TempPath(),
"file.txt",
"v2\n" );
1712 commitWorkingFile( repo,
"file.txt",
"second" );
1720 BOOST_CHECK( git_oid_equal( &headNow, &tagged ) );
1721 BOOST_CHECK_EQUAL( readWorkingFile( wxFileName( TempPath(),
"file.txt" ) ),
"v1\n" );
1727 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
1738 wxFileName fn( TempPath(), wxS(
"file.txt" ) );
1740 std::ofstream f( fn.GetFullPath().utf8_string() );
1744 commitWorkingFile( repo, wxS(
"file.txt" ),
"only on feature" );
1760 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
1768 std::vector<std::pair<wxString, wxString>> remotes = common.
GetRemotes();
1771 BOOST_CHECK_EQUAL( remotes[0].second.ToStdString(),
"https://example.com/x.git" );
1786 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
1793 std::vector<KIGIT_BRANCH_INFO> branches = common.
GetBranches();
1795 std::set<wxString> names;
1799 names.insert( branch.name );
1801 BOOST_CHECK( branch.when == branches.front().when );
1813 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
1819 writeFile( TempPath(),
"file.txt",
"v2\n" );
1820 const char*
message =
"second\n\nA full commit message.\n";
1821 commitWorkingFile( repo,
"file.txt",
message );
1826 auto hashOf = [&](
const char* aSummary )
1830 if( c.summary == aSummary )
1831 return c.hash.ToStdString();
1834 return std::string();
1837 std::vector<KIGIT_BRANCH_INFO> branches = common.
GetBranches();
1845 if( branch.name == wxS(
"main" ) )
1870 git_reference* remote =
nullptr;
1871 BOOST_REQUIRE_EQUAL( git_reference_create( &remote, repo.get(),
"refs/remotes/origin/topic", &headOid, 0,
nullptr ),
1875 git_reference* alias =
nullptr;
1876 BOOST_REQUIRE_EQUAL( git_reference_symbolic_create( &alias, repo.get(),
"refs/remotes/origin/HEAD",
1877 "refs/remotes/origin/topic", 0,
nullptr ),
1881 std::vector<KIGIT_BRANCH_INFO> branches = common.
GetBranches();
1883 unsigned int localCount = 0;
1884 unsigned int remoteCount = 0;
1888 if( branch.name == wxS(
"origin/topic" ) )
1890 if( branch.isRemote )
1911 BOOST_CHECK( std::none_of( branches.begin(), branches.end(),
1914 return aBranch.isCurrent;
1921 const wxDateTime older( 1, wxDateTime::Jan, 2020 );
1922 const wxDateTime newer( 2, wxDateTime::Jan, 2020 );
1924 auto branch = [](
const wxString& aName,
const wxDateTime& aWhen,
bool aRemote =
false,
bool aCurrent = false )
1929 info.isRemote = aRemote;
1930 info.isCurrent = aCurrent;
1934 std::vector<KIGIT_BRANCH_INFO> branches = {
1935 branch(
"origin/unknown", wxDateTime(),
true ), branch(
"b-tie", older ),
1936 branch(
"origin/z-new", newer,
true ), branch(
"a-undated", wxDateTime() ),
1937 branch(
"origin/a-old", older,
true ), branch(
"z-new", newer ),
1938 branch(
"main", wxDateTime(),
false,
true ), branch(
"a-tie", older )
1943 const std::vector<wxString>
expected = {
"main",
"z-new",
"a-tie",
"b-tie",
1944 "a-undated",
"origin/z-new",
"origin/a-old",
"origin/unknown" };
1948 for(
size_t i = 0; i <
expected.size(); ++i )
1956 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
1962 writeFile( TempPath(),
"file.txt",
"v2\n" );
1963 commitWorkingFile( repo,
"file.txt",
"second\n\nWith a body." );
1966 git_reference*
main =
nullptr;
2000 auto commitOn = [&](
const char* aBranch,
const char* aMessage )
2002 wxString ref = wxS(
"refs/heads/" ) + wxString::FromUTF8( aBranch );
2004 writeFile( TempPath(),
"file.txt", aMessage );
2005 commitWorkingFile( repo.get(),
"file.txt", aMessage );
2008 commitOn(
"selected",
"selected one" );
2010 commitOn(
"selected",
"selected two" );
2011 commitOn(
"selected-upstream",
"selected upstream one" );
2012 commitOn(
"main-upstream",
"main upstream one" );
2013 commitOn(
"main-upstream",
"main upstream two" );
2014 commitOn(
"main-upstream",
"main upstream three" );
2017 auto setUpstream = [&](
const char* aBranch,
const char* aUpstream )
2019 git_reference* ref =
nullptr;
2020 BOOST_REQUIRE_EQUAL( git_branch_lookup( &ref, repo.get(), aBranch, GIT_BRANCH_LOCAL ), GIT_OK );
2025 setUpstream(
"main",
"main-upstream" );
2026 setUpstream(
"selected",
"selected-upstream" );
2046 std::optional<KIGIT_UPSTREAM_STATUS> status = common.
UpstreamStatus();
2050 commitOn(
"main",
"local main one" );
2075 writeFile( TempPath(),
".git/refs/heads/broken",
"1111111111111111111111111111111111111111\n" );
2076 git_config*
config =
nullptr;
2095 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
2101 writeFile( TempPath(),
"file.txt",
"v2\n" );
2102 commitWorkingFile( repo,
"file.txt",
"second" );
2119 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
2125 wxFileName fetchHead( wxString::FromUTF8( git_repository_path( repo ) ), wxS(
"FETCH_HEAD" ) );
2128 std::ofstream f( fetchHead.GetFullPath().utf8_string() );
2129 f <<
"0000000000000000000000000000000000000000\t\tbranch 'main' of https://example.com/x.git\n";
2133 wxDateTime stamp( 1, wxDateTime::Jan, 2020, 12, 0, 0 );
2134 BOOST_REQUIRE( fetchHead.SetTimes(
nullptr, &stamp,
nullptr ) );
2146 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"one\ntwo\nthree\n" );
2155 writeFile( TempPath(),
"file.txt",
"one\n2\nthree\n" );
2156 writeFile( TempPath(),
"new.txt",
"a\nb\n" );
2168 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
2176 writeFile( TempPath(),
"file.txt",
"v2\n" );
2177 writeFile( TempPath(),
"new.txt",
"n\n" );
2180 git_index*
index =
nullptr;
2189 std::vector<KIGIT_STASH_INFO> stashes = common.
GetStashes();
2199 std::vector<std::string> files;
2201 for(
const wxString& f :
detail.files )
2202 files.push_back( f.ToStdString() );
2204 std::sort( files.begin(), files.end() );
2217 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
2220 wxFileName::Mkdir( TempPath() + wxS(
"/sub" ) );
2221 writeFile( TempPath(),
"sub/a.txt",
"a\n" );
2222 commitWorkingFile( repo,
"sub/a.txt",
"add a" );
2223 writeFile( TempPath(),
"file.txt",
"v2\n" );
2224 commitWorkingFile( repo,
"file.txt",
"edit file" );
2225 writeFile( TempPath(),
"sub/b.txt",
"b\n" );
2226 commitWorkingFile( repo,
"sub/b.txt",
"add b" );
2231 filter.path = wxS(
"file.txt" );
2237 filter.path = wxS(
"sub" );
2243 filter.path = wxS(
"sub/a.txt" );
2246 filter.path = wxS(
"nowhere.txt" );
2250 filter.path = wxS(
"sub" );
2251 filter.search = wxS(
"add b" );
2265 git_repository* repo = makeRepoWithCommit( TempPath(),
"board.kicad_pcb",
"(kicad_pcb)\n" );
2269 BOOST_CHECK( !PROJECT_GIT_UTILS::ProjectUsesGitLfs( common ) );
2271 writeFile( TempPath(),
".gitattributes",
"*.step filter=lfs diff=lfs merge=lfs -text\n" );
2272 commitWorkingFile( repo,
".gitattributes",
"route step files through lfs" );
2273 writeFile( TempPath(),
"case.step",
"ISO-10303-21;\n" );
2274 commitWorkingFile( repo,
"case.step",
"add the enclosure" );
2276 BOOST_CHECK( PROJECT_GIT_UTILS::PathUsesGitLfs( repo, wxS(
"case.step" ) ) );
2277 BOOST_CHECK( !PROJECT_GIT_UTILS::PathUsesGitLfs( repo, wxS(
"board.kicad_pcb" ) ) );
2280 BOOST_CHECK( PROJECT_GIT_UTILS::ProjectUsesGitLfs( common, &example ) );
2284 const wxString elsewhere = TempPath() + wxS(
"/elsewhere" );
2287 BOOST_CHECK( !PROJECT_GIT_UTILS::ProjectUsesGitLfs( otherProject ) );
2289 std::optional<wxString> prefix = PROJECT_GIT_UTILS::ProjectPrefix( wxS(
"/repo" ), wxS(
"/repo/proj" ) );
2292 prefix = PROJECT_GIT_UTILS::ProjectPrefix( wxS(
"/repo" ), wxS(
"/repo" ) );
2295 BOOST_CHECK( !PROJECT_GIT_UTILS::ProjectPrefix( wxS(
"/repo" ), wxS(
"/other/proj" ) ) );
2296 BOOST_CHECK( !PROJECT_GIT_UTILS::ProjectPrefix( wxS(
"/repo" ), wxS(
"/repo2/proj" ) ) );
2297 BOOST_CHECK( !PROJECT_GIT_UTILS::ProjectPrefix( wxS(
"/repo" ), wxEmptyString ) );
2302 prefix = PROJECT_GIT_UTILS::ProjectPrefix( wxS(
"C:\\repo" ), wxS(
"C:/repo/proj" ) );
2305 prefix = PROJECT_GIT_UTILS::ProjectPrefix( wxS(
"c:\\Repo\\" ), wxS(
"C:\\repo\\Proj" ) );
2308 BOOST_CHECK( !PROJECT_GIT_UTILS::ProjectPrefix( wxS(
"C:\\repo" ), wxS(
"D:\\repo\\proj" ) ) );
2318 writeFile( TempPath(),
".gitattributes",
"*.step filter=lfs diff=lfs merge=lfs -text\n" );
2319 commitWorkingFile( repo.get(),
".gitattributes",
"use lfs" );
2320 writeFile( TempPath(),
"projects/board/board.kicad_pcb",
"(kicad_pcb)\n" );
2321 commitWorkingFile( repo.get(),
"projects/board/board.kicad_pcb",
"add project" );
2322 writeFile( TempPath(),
"projects/board2/case.step",
"sibling enclosure\n" );
2323 commitWorkingFile( repo.get(),
"projects/board2/case.step",
"add sibling enclosure" );
2325 KIGIT_COMMON common( repo.get(), TempPath() + wxS(
"/projects/board" ) );
2327 BOOST_CHECK( !PROJECT_GIT_UTILS::ProjectUsesGitLfs( common, &example ) );
2328 BOOST_CHECK( PROJECT_GIT_UTILS::LfsRefusal( &common ).IsEmpty() );
2331 BOOST_CHECK( !PROJECT_GIT_UTILS::LfsRefusal( &unknownProject ).IsEmpty() );
2333 writeFile( TempPath(),
"projects/board/models/case.step",
"project enclosure\n" );
2334 commitWorkingFile( repo.get(),
"projects/board/models/case.step",
"add project enclosure" );
2336 BOOST_CHECK( PROJECT_GIT_UTILS::ProjectUsesGitLfs( common, &example ) );
2338 BOOST_CHECK( PROJECT_GIT_UTILS::LfsRefusal( &common ).Contains( example ) );
2346 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
2349 git_signature* authored =
nullptr;
2353 git_signature* committed =
nullptr;
2360 git_commit* parent =
nullptr;
2364 git_tree* tree =
nullptr;
2368 const git_commit* parents[1] = { parent };
2371 git_commit_create( &commitOid, repo,
"HEAD", authored, committed,
nullptr,
"rebased", tree, 1, parents ),
2374 git_object* target =
nullptr;
2379 BOOST_REQUIRE_EQUAL( git_tag_create_lightweight( &tagOid, repo,
"rebased", target, 0 ), GIT_OK );
2392 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
2403 git_object* tagObject =
nullptr;
2432 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
2440 git_object* commitObject =
nullptr;
2444 git_signature* oldSig =
nullptr;
2449 BOOST_REQUIRE_EQUAL( git_tag_create( &oid, repo,
"old", commitObject, oldSig,
"old tag", 0 ), GIT_OK );
2451 git_object* treeObject =
nullptr;
2454 BOOST_REQUIRE_EQUAL( git_tag_create_lightweight( &oid, repo,
"tree-tag", treeObject, 0 ), GIT_OK );
2457 std::vector<KIGIT_TAG_INFO> tags = common.
GetTags();
2459 std::set<wxString> newest{ tags[0].name, tags[1].name };
2460 std::set<wxString> madeNow{ wxS(
"v1.0" ), wxS(
"beta-1" ) };
2464 BOOST_CHECK( tags[3].hash.IsEmpty() && !tags[3].when.IsValid() );
2472 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
2475 writeFile( TempPath(),
"file.txt",
"v2\n" );
2476 commitWorkingFile( repo,
"file.txt",
"Update BOM\n\nSecond pass.\n" );
2477 writeFile( TempPath(),
"file.txt",
"v3\n" );
2478 commitWorkingFile( repo,
"file.txt",
"Route power\n" );
2479 writeFile( TempPath(),
"file.txt",
"v4\n" );
2480 commitWorkingFile( repo,
"file.txt",
"Update BOM\n\nSecond pass.\n" );
2481 writeFile( TempPath(),
"file.txt",
"v5\n" );
2482 commitWorkingFile( repo,
"file.txt",
" \n\n" );
2512 wxS(
"Fix the thing" ) );
2514 wxS(
"Fix\n\nWhy it broke." ) );
2522 wxString signedOff = wxS(
"Why.\n\nSigned-off-by: QA Test <[email protected]>" );
2528 wxS(
"Fix\n\n" ) + signedOff );
2530 wxS(
"Fix\n\n" ) + signedOff + coauthor );
2532 wxS(
"Fix\n\n" ) + other + wxS(
"\n\nSigned-off-by: QA Test <[email protected]>" ) );
2534 wxString summary, description;
2560 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
2563 writeFile( TempPath(),
"file.txt",
"v2\n" );
2573 BOOST_CHECK_EQUAL( readWorkingFile( wxFileName( TempPath(),
"file.txt" ) ),
"v2\n" );
2614 const char* username;
2615 const char* hostname;
2618 const TEST_CASE cases[] = {
2619 {
"ssh://[email protected]:29022/srv/git/repos/l3-auth-30.git",
"runner",
"192.168.1.55" },
2620 {
"ssh://192.168.1.55:29022/srv/git/repos/l3-auth-30.git",
"",
"192.168.1.55" },
2624 {
"example.com:team/repo",
"",
"example.com" },
2625 {
"ssh://runner@[2001:db8::1]:2222/srv/repo",
"runner",
"2001:db8::1" },
2626 {
"runner@[2001:db8::1]:team/repo",
"runner",
"2001:db8::1" },
2629 for(
const TEST_CASE&
test : cases )
2650 const char* address;
2651 const char* username;
2652 const char* password;
2656 const TEST_CASE cases[] = {
2659 {
"http://alice:[email protected]:8080/team/repo",
"http://example.com:8080/team/repo",
"alice",
"secret",
2667 for(
const TEST_CASE&
test : cases )
2685 const char* paths[] = {
2686 "/srv/git/repos/project.git",
"../repos/project.git",
2687 "./repos/project:backup.git",
"C:/repos/project.git",
2688 "C:\\repos\\project.git",
"C:project.git",
2689 "\\\\server\\repos\\project.git",
"file:///srv/git/repos/project.git",
2690 "file:///C:/repos/project.git",
2693 for(
const char*
path : paths )
2712 const wxString sshURL = wxS(
"ssh://[email protected]:29022/srv/git/repos/l3-auth-30.git" );
2721 common.
SetRemote( wxS(
"/srv/git/repos/local.git" ) );
2731 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
2737 git_config* cfg =
nullptr;
2740 BOOST_REQUIRE_EQUAL( git_config_set_string( cfg,
"branch.main.remote",
"origin" ), GIT_OK );
2741 BOOST_REQUIRE_EQUAL( git_config_set_string( cfg,
"branch.main.merge",
"refs/heads/main" ), GIT_OK );
2759 const wxString key = wxFileName( TempPath() + wxS(
"/keys/kicad" ) ).GetFullPath();
2779 PROJECT_GIT_UTILS::IsWithinProjectPath( wxS(
"C:/repo/proj/board.kicad_pcb" ), wxS(
"C:\\repo\\proj" ) ) );
2780 BOOST_CHECK( PROJECT_GIT_UTILS::IsWithinProjectPath( wxS(
"C:/repo/proj/sub/sheet.kicad_sch" ),
2781 wxS(
"C:\\repo\\proj\\" ) ) );
2782 BOOST_CHECK( !PROJECT_GIT_UTILS::IsWithinProjectPath( wxS(
"C:/repo/other/board.kicad_pcb" ),
2783 wxS(
"C:\\repo\\proj" ) ) );
2787 BOOST_CHECK( !PROJECT_GIT_UTILS::IsWithinProjectPath( wxS(
"/repo/project2/file" ), wxS(
"/repo/project" ) ) );
2788 BOOST_CHECK( PROJECT_GIT_UTILS::IsWithinProjectPath( wxS(
"/repo/project/file" ), wxS(
"/repo/project" ) ) );
2796 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"v1\n" );
2804 wxFileName fn( TempPath(), wxS(
"notes.txt" ) );
2805 std::ofstream f( fn.GetFullPath().utf8_string() );
2813 wxFileName fn( TempPath(), wxS(
"file.txt" ) );
2814 std::ofstream f( fn.GetFullPath().utf8_string() );
2827 status.
lfsMessage = wxS(
"Git LFS prevents this rewrite." );
2830 const std::vector<wxString> editors = { wxS(
"Schematic Editor" ), wxS(
"Footprint Editor" ) };
2841 BOOST_CHECK( stashMessage.Contains(
"Schematic Editor" ) );
2842 BOOST_CHECK( stashMessage.Contains(
"Footprint Editor" ) );
2843 BOOST_CHECK( stashMessage.Contains(
"unsaved changes" ) );
2844 BOOST_CHECK( stashMessage.Contains(
"Save or discard" ) );
2845 BOOST_CHECK( stashMessage.Contains(
"before stashing" ) );
2856 auto unavailableLock = unavailable.
LockAction();
2890 writeFile( TempPath(),
"notes.txt",
"untracked notes\n" );
2904 const wxString projectPath = TempPath() + wxS(
"/project" );
2905 BOOST_REQUIRE( wxFileName::Mkdir( projectPath, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
2906 BOOST_REQUIRE( wxFileName::Mkdir( TempPath() + wxS(
"/project2" ), wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
2907 writeFile( TempPath(),
"project/board.kicad_pcb",
"project board\n" );
2908 commitWorkingFile( repo.get(),
"project/board.kicad_pcb",
"add project" );
2909 writeFile( TempPath(),
"project2/board.kicad_pcb",
"sibling board\n" );
2910 commitWorkingFile( repo.get(),
"project2/board.kicad_pcb",
"add sibling" );
2914 writeFile( TempPath(),
"project/board.kicad_pcb",
"edited project board\n" );
2923 writeFile( TempPath(),
"project2/board.kicad_pcb",
"edited sibling board\n" );
2924 writeFile( TempPath(),
"project2/notes.txt",
"untracked sibling notes\n" );
2944 writeFile( TempPath(),
".gitattributes",
"*.step filter=lfs diff=lfs merge=lfs -text\n" );
2945 commitWorkingFile( repo.get(),
".gitattributes",
"route step files through lfs" );
2946 writeFile( TempPath(),
"case.step",
"ISO-10303-21;\n" );
2947 commitWorkingFile( repo.get(),
"case.step",
"add the enclosure" );
2958 writeFile( TempPath(),
"board.kicad_pcb",
"edited board\n" );
2973 git_repository* repo = makeRepoWithCommit( TempPath(),
"file.txt",
"one\ntwo\nthree\n" );
2981 BOOST_REQUIRE_EQUAL( git_blob_create_from_buffer( &blobOid, repo,
"other\n", 6 ), GIT_OK );
2984 git_index*
index =
nullptr;
2988 git_index_entry stage{};
2989 stage.path =
"file.txt";
2990 stage.mode = GIT_FILEMODE_BLOB;
2991 git_oid_cpy( &stage.id, &blobOid );
3003 "discarding left the file still needing resolution" );
3014 const wxString confirmedHead = wxString::FromUTF8( git_oid_tostr_s( &head ) );
3017 auto forcePushError = [&common](
const wxString& aBranch,
const wxString& aHeadOid )
3027 const wxString noRemote = wxS(
"There is no remote named 'origin'." );
3028 BOOST_CHECK( forcePushError( confirmedBranch, confirmedHead ).StartsWith( noRemote ) );
3030 const wxString targetChanged = wxS(
"The branch or its last commit changed while the push was waiting." );
3035 BOOST_CHECK( forcePushError( confirmedBranch, confirmedHead ).Contains( targetChanged ) );
3039 writeFile( TempPath(), wxS(
"file.txt" ),
"two\n" );
3040 commitWorkingFile( repo.get(), wxS(
"file.txt" ),
"second" );
3041 BOOST_CHECK( forcePushError( confirmedBranch, confirmedHead ).Contains( targetChanged ) );
3045 forcePushError( confirmedBranch, wxString::FromUTF8( git_oid_tostr_s( &head ) ) ).StartsWith( noRemote ) );
3060 std::promise<void> entered;
3061 auto ready = entered.get_future();
3062 auto result = std::async( std::launch::async,
3063 [&handler, &entered]()
3065 entered.set_value();
3069 const bool started = ready.wait_for( std::chrono::seconds( 5 ) ) == std::future_status::ready;
3070 const bool waited =
result.wait_for( std::chrono::milliseconds( 50 ) ) == std::future_status::timeout;
3071 token->store(
true );
3076 BOOST_REQUIRE(
result.wait_for( std::chrono::seconds( 5 ) ) == std::future_status::ready );
3089 auto wrongThread = std::make_shared<std::atomic<bool>>( false );
3090 UI_CALLBACK_CAPTURE capture( wrongThread );
3091 bool completed =
false;
3096 "completion capture",
3100 [capture, &completed]()
3108 return !transfer.
IsBusy();
3115 auto entered = std::make_shared<std::promise<void>>();
3116 std::promise<void> release;
3117 auto ready = entered->get_future();
3118 auto proceed = release.get_future().share();
3119 auto transfer = std::make_unique<KIGIT_TRANSFER>();
3123 "destroyed completion owner",
3126 entered->set_value();
3129 [capture, &completed]()
3134 const bool started = ready.wait_for( std::chrono::seconds( 5 ) ) == std::future_status::ready;
3136 release.set_value();
3139 BOOST_REQUIRE_EQUAL( Backend()->OrphanRegistry().JoinAll( std::chrono::seconds( 5 ) ), 0u );
3140 wxTheApp->ProcessPendingEvents();
3149 auto session = std::make_shared<KIGIT_COMMON>( repo.get() );
3152 auto entered = std::make_shared<std::promise<void>>();
3153 std::promise<void> release;
3154 auto ready = entered->get_future();
3155 auto proceed = release.get_future().share();
3156 auto worked = std::make_shared<std::atomic<unsigned>>( 0 );
3157 auto workerThread = std::make_shared<std::thread::id>();
3158 unsigned completed = 0;
3162 [worked, workerThread, proceed, entered](
KIGIT_COMMON& )
3164 *workerThread = std::this_thread::get_id();
3165 worked->fetch_or( 1 );
3166 entered->set_value();
3174 BOOST_REQUIRE( ready.wait_for( std::chrono::seconds( 5 ) ) == std::future_status::ready );
3175 BOOST_CHECK( *workerThread != std::this_thread::get_id() );
3176 BOOST_CHECK( !session->TryLockAction().owns_lock() );
3182 worked->fetch_or( 2 );
3192 worked->fetch_or( 4 );
3198 release.set_value();
3213 auto session = std::make_shared<KIGIT_COMMON>( repo.get() );
3215 bool completed =
false;
3216 auto worked = std::make_shared<std::atomic<unsigned>>( 0 );
3217 std::shared_ptr<int> workerLifetime = std::make_shared<int>( 0 );
3218 std::weak_ptr<int> workerWatch = workerLifetime;
3224 worked->fetch_add( 1 );
3230 workerLifetime.reset();
3233 const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds( 5 );
3235 while( !workerWatch.expired() && std::chrono::steady_clock::now() < deadline )
3236 std::this_thread::sleep_for( std::chrono::milliseconds( 1 ) );
3243 worked->fetch_add( 1 );
3264 auto session = std::make_shared<KIGIT_COMMON>( repo.get() );
3265 std::weak_ptr<KIGIT_COMMON> watched = session;
3266 auto query = std::make_unique<KIGIT_QUERY>();
3268 auto entered = std::make_shared<std::promise<void>>();
3269 std::promise<void> release;
3270 auto ready = entered->get_future();
3271 auto proceed = release.get_future().share();
3272 bool completed =
false;
3278 entered->set_value();
3286 BOOST_REQUIRE( ready.wait_for( std::chrono::seconds( 5 ) ) == std::future_status::ready );
3288 const auto start = std::chrono::steady_clock::now();
3290 BOOST_CHECK( std::chrono::steady_clock::now() - start < std::chrono::seconds( 1 ) );
3292 release.set_value();
3297 return watched.expired();
3299 wxTheApp->ProcessPendingEvents();
3308 auto worked = std::make_shared<std::atomic<bool>>( false );
3309 std::thread::id completionThread;
3313 worked->store(
true );
3315 auto done = [&completionThread]()
3317 completionThread = std::this_thread::get_id();
3320 query.
Run(
nullptr, work, done );
3326 BOOST_CHECK( completionThread == std::this_thread::get_id() );
3328 auto session = std::make_shared<KIGIT_COMMON>(
nullptr );
3329 completionThread = {};
3330 query.
Run( session, work, done );
3336 BOOST_CHECK( completionThread == std::this_thread::get_id() );
3338 session->SetRepo( repo.get() );
3339 session->SetShuttingDown(
true );
3340 completionThread = {};
3341 query.
Run( session, work, done );
3347 BOOST_CHECK( completionThread == std::this_thread::get_id() );
3350 Backend()->OrphanRegistry().JoinAll( std::chrono::seconds( 1 ) );
3351 completionThread = {};
3352 query.
Run( session, work, done );
3354 BOOST_CHECK( completionThread == std::this_thread::get_id() );
3361 auto session = std::make_shared<KIGIT_COMMON>( repo.get() );
3362 auto query = std::make_unique<KIGIT_QUERY>();
3369 throw std::runtime_error(
"query failed" );
3381 BOOST_CHECK( session->TryLockAction().owns_lock() );
3389 auto session = std::make_shared<KIGIT_COMMON>( repo.get() );
3393 auto entered = std::make_shared<std::promise<void>>();
3394 std::promise<void> release;
3395 auto ready = entered->get_future();
3396 auto proceed = release.get_future().share();
3397 auto worked = std::make_shared<std::atomic<bool>>( false );
3398 unsigned completed = 0;
3404 entered->set_value();
3413 BOOST_REQUIRE( ready.wait_for( std::chrono::seconds( 5 ) ) == std::future_status::ready );
3418 worked->store(
true );
3424 release.set_value();
BranchResult SwitchToBranch(const wxString &aBranchName)
Switch to the specified branch.
A private scratch directory for extracting committed file versions to diff, removed when this object ...
bool IsOk() const
True when the scratch directory was created.
const wxString & Root() const
The scratch directory, for callers that lay out their own files (e.g. footprint libraries).
wxString Materialize(git_repository *aRepo, const wxString &aRef, const wxString &aRelPath, KICAD_DIFF::DOC_KIND aKind)
Extract aRelPath at aRef into a uniquely named slot and return the path to load, or an empty string w...
void SetExpectedTarget(const wxString &aBranch, const wxString &aHeadOid)
Pin the branch and full HEAD commit ID confirmed by the caller, so a queued push cannot publish a dif...
PushResult PerformPush(bool aForce=false)
Utility class with helper functions for project level git operations.
static bool WriteBlobToFile(git_repository *aRepo, const wxString &aRef, const wxString &aPath, const wxString &aDestPath)
Write a file as it exists at a git ref to a path on disk, so a committed version can be diffed.
static wxString ComputeSymlinkPreservingWorkDir(const wxString &aUserProjectPath, const wxString &aCanonicalWorkDir)
Compute a working directory path that preserves symlinks from the user's project path.
static bool IsWithinProjectPath(const wxString &aAbsPath, const wxString &aProjectPath)
Test whether an absolute file path lives inside the current project directory.
static std::optional< wxString > ProjectPrefix(const wxString &aWorkDir, const wxString &aProjectPath)
The project's folder inside the repository, ending in a slash.
Publishes a token for as long as its operation runs.
The project's git session.
std::pair< int, int > GetWorkingTreeLineStats(const std::set< wxString > &aPaths) const
void ResetNextKey()
Start the next attempt from the first key, with the key that last connected to the current remote mov...
std::shared_ptr< std::atomic< bool > > CANCEL_TOKEN
One token per operation.
GIT_CONN_TYPE GetConnType() const
std::vector< KIGIT_COMMIT_INFO > GetRecentCommits(size_t aMax=50, const KIGIT_HISTORY_FILTER &aFilter={}) const
bool HasUncommittedTrackedChanges(bool *aReadOk=nullptr) const
bool StashPop(size_t aIndex)
wxString GetRemote() const
wxString GetCurrentBranchName() const
wxString GetGitRootDirectory() const
KIGIT_TAG_DETAIL GetTagDetail(const wxString &aName) const
void SetSSHKey(const wxString &aSSHKey)
std::unique_lock< std::mutex > LockAction()
bool IsPullRebaseConfigured() const
True when the repository is set to rebase rather than merge when getting changes.
KIGIT_COMMIT_DETAIL GetCommitDetail(const wxString &aHash) const
bool CheckoutTag(const wxString &aName)
void SetUsername(const wxString &aUsername)
bool DeleteTag(const wxString &aName)
std::vector< wxString > GetProjectDirs()
Return a vector of project files in the repository.
void NotifyRefreshListeners()
std::vector< wxString > GetRecentCommitMessages(size_t aMax) const
bool RemoveRemote(const wxString &aName)
std::optional< size_t > GetUnmergedCommitCount(const wxString &aBranch) const
Commits in aBranch that HEAD does not contain. No value when either cannot be read.
bool CreateBranch(const wxString &aName, const wxString &aFrom=wxEmptyString)
wxString GetNextPublicKey()
By value, so the caller never holds a pointer into the vector.
bool AddRemote(const wxString &aName, const wxString &aUrl)
static CANCEL_TOKEN NewCancelToken()
void AddRefreshListener(void *aOwner, std::function< void()> aCallback)
std::vector< KIGIT_STASH_INFO > GetStashes() const
bool StartUnbornBranch(const wxString &aName)
Point HEAD at a branch that does not exist yet, so the first commit creates it.
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 IsHeadUnborn() const
True while the repository has no commits, so HEAD names a branch that does not exist yet.
KIGIT_BRANCH_DETAIL GetBranchDetail(const wxString &aBranch) const
bool HasPushAndPullRemote() const
bool StashApply(size_t aIndex)
bool DiscardChanges(const std::vector< wxString > &aPaths, wxString &aError)
Put the given working directory relative paths back to their committed state.
wxString GetUsername() const
bool SetUserConfig(const wxString &aName, const wxString &aEmail)
std::optional< KIGIT_UPSTREAM_STATUS > UpstreamStatus() const
The current branch against its upstream.
KIGIT_STASH_DETAIL GetStashDetail(const wxString &aId) const
std::vector< std::pair< wxString, wxString > > GetRemotes() const
Returns every remote as a (name, URL) pair.
bool CreateTag(const wxString &aName, const wxString &aMessage=wxEmptyString)
void UpdateCurrentBranchInfo()
void RemoveRefreshListener(void *aOwner)
bool StashDrop(size_t aIndex)
bool HasLocalCommits() const
True when HEAD has commits its upstream does not, or has no upstream yet.
bool StashSave(const wxString &aMessage)
Set the uncommitted tracked changes aside as a new stash, as git stash push would.
wxDateTime GetLastFetchTime() const
static REMOTE_URL ParseRemoteURL(const wxString &aURL)
wxString GetPassword()
The secret for the remote, read without ever prompting, so it is safe on a transfer worker.
std::vector< KIGIT_TAG_INFO > GetTags() const
std::vector< KIGIT_BRANCH_INFO > GetBranches() const
Local and remote branches with their tip commits.
bool DeleteBranch(const wxString &aName)
bool IsPathConflicted(const wxString &aPath) const
True while aPath is one of the files a merge or rebase stopped on.
wxString GetRemotename() const
bool IsHeadDetached() const
True when HEAD points at a commit rather than a branch, as after checking out a tag.
void SetRemote(const wxString &aRemote)
bool SetRemoteUrl(const wxString &aName, const wxString &aUrl)
const std::vector< wxString > & GetErrorStrings() const
wxString GetErrorString()
Read repository state without blocking the UI.
bool IsBusy() const
Remains true until the running worker returns, including after Cancel().
void Cancel()
Suppress the running completion and discard pending work. Does not wait for a read.
void Run(std::shared_ptr< KIGIT_COMMON > aSession, std::function< void(KIGIT_COMMON &)> aWork, std::function< void()> aDone)
Run aWork under the session's action lock on a worker, then aDone on the UI thread.
void SetCancelToken(const KIGIT_COMMON::CANCEL_TOKEN &aToken)
Callbacks read this token rather than the session, so one operation's cancel cannot reach another.
bool IsThisOperationCancelled() const
void SetWaitForActionLock(bool aWait)
Worker transfers may wait for a background read to finish.
bool IsBusy() const
True while a transfer started here is still running.
bool Start(const std::string &aLabel, const std::function< void(const KIGIT_COMMON::CANCEL_TOKEN &)> &aWork, const std::function< void()> &aDone)
Run aWork on a background thread, then aDone on the UI thread.
Give each test a backend and a temporary directory.
static bool empty(const wxTextEntryBase *aCtrl)
static std::string ToStdString(const wxString &aStr)
GIT_WORKTREE_STATUS ReadGitWorktreeStatus(KIGIT_COMMON &aCommon, const wxString &aProjectPath)
Read tracked changes and Git LFS using a captured project path.
std::unique_ptr< git_tree, decltype([](git_tree *aTree) { git_tree_free(aTree); })> GitTreePtr
A unique pointer for git_tree objects with automatic cleanup.
void SortBranchesForDisplay(std::vector< KIGIT_BRANCH_INFO > &aBranches)
Put the current branch first, then local and remote branches with the newest tips first.
bool IsPrivateKeyEncrypted(const wxString &aKeyText)
Report whether an SSH private key is protected by a passphrase.
void SplitCommitMessage(const wxString &aMessage, wxString &aSummary, wxString &aDescription)
Split a commit message into its first line and the description after the first blank line.
wxString BranchNameError(const wxString &aName)
Empty when aName is a branch name git would accept, otherwise why it would not, in the words to show ...
std::unique_ptr< git_repository, decltype([](git_repository *aRepo) { git_repository_free(aRepo); })> GitRepositoryPtr
A unique pointer for git_repository objects with automatic cleanup.
GitUserConfig ResolveGitIdentity(const GIT_IDENTITY &aIdentity, const COMMON_SETTINGS::GIT &aPreferences, const wxString &aUserName)
Fill missing values from custom preferences, then use the OS user name if needed.
std::unique_ptr< git_commit, decltype([](git_commit *aCommit) { git_commit_free(aCommit); })> GitCommitPtr
A unique pointer for git_commit objects with automatic cleanup.
GIT_IDENTITY ReadGitIdentity(KIGIT_COMMON &aCommon)
Read on a worker while holding the session's action lock.
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.
wxString ComposeCommitMessage(const wxString &aSummary, const wxString &aDescription, const wxString &aSignOff)
Join a summary line, an optional description and an optional sign-off into one commit message.
bool ValidateIdentity(const wxString &aName, const wxString &aEmail, wxString &aError)
Check a name and email for recording commits. False leaves the reason in aError.
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_object, decltype([](git_object *aObject) { git_object_free(aObject); })> GitObjectPtr
A unique pointer for git_object 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.
KIGIT::GitRepositoryPtr InitGitRepository(const wxString &aPath, const char *aBranch="main", const char *aAuthorName="QA Test", const char *aAuthorEmail="
[email protected]")
Pin the branch and author so repository setup does not depend on the user's Git config.
void WriteGitFile(const wxString &aRepoPath, const wxString &aRelativePath, const std::string &aContents)
Write test data without staging it. Create parent directories when needed.
git_oid CommitGitFile(git_repository *aRepo, const wxString &aPath, const wxString &aMessage)
Stage one file and commit the index. Other staged changes are kept.
Repository state used before an operation that requires a clean working tree.
std::vector< wxString > outsideProject
wxString RefusalMessage(const std::vector< wxString > &aUnsavedEditors, bool aAllowTrackedChanges=false) const
Empty when rewriting is allowed.
Repository values and whether Git can use them to record a commit.
std::optional< KIGIT_AHEAD_BEHIND > divergence
Comparison with upstream, if it could be read.
wxString upstream
The branch this one tracks, or empty.
std::vector< wxString > parents
Short hashes.
std::vector< KIGIT_COMMIT_FILE > files
A transport address with any embedded credentials extracted.
wxString url
Keeps the SSH username, scheme and port, but never a password.
wxString search
Keep only commits whose summary, author or hash contains this.
wxString branch
One branch, local or remote, or empty for every branch and HEAD.
wxString message
The tag's own message, or the commit's for a lightweight tag.
wxString email
Their email.
wxString name
The tag name.
wxString hash
Short hash of the tagged commit.
wxDateTime when
When the tag was made, or the commit for a lightweight tag.
wxString author
Who made the tag, or the commit for a lightweight tag.
bool found
False when no tag of that name points at a commit.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
BOOST_REQUIRE_EQUAL(rows.size(), 1)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_REQUIRE_MESSAGE(dc.CacheTableInfo(table, { "part_id", "val" }), dc.GetLastError())
BOOST_REQUIRE(res.columns()==1)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(GitRootDirectoryReturnsWorkdir)
VECTOR3I expected(15, 30, 45)
BOOST_TEST_CONTEXT("Test Clearance")
wxString result
Test unit parsing edge cases and error handling.