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
31#include <qa_utils/file_utils.h>
33
34#include <git2.h>
35#include <git2/sys/repository.h>
36
37#include <wx/app.h>
38#include <wx/file.h>
39#include <wx/filename.h>
40#include <wx/log.h>
41#include <wx/string.h>
42#include <wx/stdpaths.h>
43
44#include <chrono>
45#include <algorithm>
46#include <fstream>
47#include <future>
48#include <iterator>
49#include <memory>
50#include <set>
51#include <stdexcept>
52#include <thread>
53
54#ifndef __WINDOWS__
55#include <unistd.h>
56#endif
57
58
59namespace
60{
61
62// Throwaway keys, generated for these tests and used nowhere else.
63
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";
72
73
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";
81
82
83const char* c_opensshTruncated = "-----BEGIN OPENSSH PRIVATE KEY-----\n"
84 "b3BlbnNzaC1r\n"
85 "-----END OPENSSH PRIVATE KEY-----\n";
86
87
88const char* c_pemEncrypted = "-----BEGIN EC PRIVATE KEY-----\n"
89 "Proc-Type: 4,ENCRYPTED\n"
90 "DEK-Info: AES-128-CBC,10297828DB5031A5C22276D18E077B61\n"
91 "\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";
101
102
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";
113
114
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";
127
128
129// Initialize a git repository at the given path with one initial commit on "main"
130git_repository* makeRepoWithCommit( const wxString& aRepoPath, const wxString& aFileName,
131 const std::string& aFileContents )
132{
134 KI_TEST::WriteGitFile( aRepoPath, aFileName, aFileContents );
135 KI_TEST::CommitGitFile( repo.get(), aFileName, "initial" );
136 return repo.release();
137}
138
139
140// Stage aFileName from the working tree and commit it on top of HEAD.
141void commitWorkingFile( git_repository* aRepo, const wxString& aFileName, const char* aMessage )
142{
143 KI_TEST::CommitGitFile( aRepo, aFileName, aMessage );
144}
145
146
147std::string readWorkingFile( const wxFileName& aFile )
148{
149 std::ifstream in( aFile.GetFullPath().utf8_string() );
150 return std::string( ( std::istreambuf_iterator<char>( in ) ), std::istreambuf_iterator<char>() );
151}
152
153
154void writeFile( const wxString& aRepoPath, const wxString& aFileName, const char* aContent )
155{
156 KI_TEST::WriteGitFile( aRepoPath, aFileName, aContent );
157}
158
159
160// Query completions use the application's event queue, just as they do for a dialog.
161bool waitForQuery( const std::function<bool()>& aFinished )
162{
163 const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds( 5 );
164
165 while( !aFinished() && std::chrono::steady_clock::now() < deadline )
166 {
167 wxTheApp->ProcessPendingEvents();
168 std::this_thread::sleep_for( std::chrono::milliseconds( 1 ) );
169 }
170
171 return aFinished();
172}
173
174
175// Like wxWeakRef, every copy and destruction of this callback capture must stay on the UI
176// thread. A shared pointee alone would miss copies made and destroyed while posting an event.
177struct UI_CALLBACK_CAPTURE
178{
179 explicit UI_CALLBACK_CAPTURE( std::shared_ptr<std::atomic<bool>> aWrongThread ) :
180 wrongThread( std::move( aWrongThread ) ),
181 uiThread( std::this_thread::get_id() )
182 {
183 }
184
185 UI_CALLBACK_CAPTURE( const UI_CALLBACK_CAPTURE& aOther ) :
186 wrongThread( aOther.wrongThread ),
187 uiThread( aOther.uiThread )
188 {
189 if( std::this_thread::get_id() != uiThread )
190 wrongThread->store( true );
191 }
192
193 UI_CALLBACK_CAPTURE( UI_CALLBACK_CAPTURE&& ) = default;
194
195 ~UI_CALLBACK_CAPTURE()
196 {
197 if( wrongThread && std::this_thread::get_id() != uiThread )
198 wrongThread->store( true );
199 }
200
201 std::shared_ptr<std::atomic<bool>> wrongThread;
202 std::thread::id uiThread;
203};
204
205} // namespace
206
207
208BOOST_FIXTURE_TEST_SUITE( KiGitCommon, KI_TEST::GIT_TEST_FIXTURE )
209
210
211// git_repository_path() returns a path ending in /.git/, which broke the popup-menu test for
212// whether a project lives at the repository root.
213BOOST_AUTO_TEST_CASE( GitRootDirectoryReturnsWorkdir )
214{
215 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "data\n" );
216 KIGIT::GitRepositoryPtr repoPtr( repo );
217
218 KIGIT_COMMON common( repo );
219 wxString root = common.GetGitRootDirectory();
220
221 BOOST_CHECK( !root.IsEmpty() );
222 BOOST_CHECK_MESSAGE( !root.Contains( wxS( "/.git" ) ) && !root.Contains( wxS( "\\.git" ) ),
223 "GetGitRootDirectory should return the working directory, got: "
224 + root.ToStdString() );
225
226 // Should be the repo path (with trailing separator).
227 wxFileName rootFn;
228 rootFn.AssignDir( root );
229 rootFn.MakeAbsolute();
230 wxFileName tmpFn;
231 tmpFn.AssignDir( TempPath() );
232 tmpFn.MakeAbsolute();
233 BOOST_CHECK_EQUAL( rootFn.GetFullPath().ToStdString(), tmpFn.GetFullPath().ToStdString() );
234}
235
236
237// The remote need not be called origin.
238BOOST_AUTO_TEST_CASE( HasPushAndPullRemoteAcceptsNonOriginName )
239{
240 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "data\n" );
241 KIGIT::GitRepositoryPtr repoPtr( repo );
242
243 // No remotes configured.
244 KIGIT_COMMON common( repo );
246
247 // Add a remote with a non-default name.
248 git_remote* remote = nullptr;
249 BOOST_REQUIRE_EQUAL( git_remote_create( &remote, repo, "github",
250 "[email protected]:example/repo.git" ),
251 GIT_OK );
252 KIGIT::GitRemotePtr remotePtr( remote );
253
255}
256
257
258BOOST_AUTO_TEST_CASE( HasPushAndPullRemoteFindsOrigin )
259{
260 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "data\n" );
261 KIGIT::GitRepositoryPtr repoPtr( repo );
262
263 git_remote* remote = nullptr;
264 BOOST_REQUIRE_EQUAL( git_remote_create( &remote, repo, "origin",
265 "[email protected]:repo.git" ),
266 GIT_OK );
267 KIGIT::GitRemotePtr remotePtr( remote );
268
269 KIGIT_COMMON common( repo );
271}
272
273
274// Walking unbounded history with no upstream OID dumped the whole root-commit tree into the
275// modified set, falsely flagging every file as ahead of the remote.
276BOOST_AUTO_TEST_CASE( GetDifferentFilesEmptyWithoutUpstream )
277{
278 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "data\n" );
279 KIGIT::GitRepositoryPtr repoPtr( repo );
280
281 KIGIT_COMMON common( repo );
282 auto [local, remote] = common.GetDifferentFiles();
283
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() ) );
290}
291
292
293// With disjoint histories the walk ran past the upstream to the local root commit, and a root
294// commit has no parent, so its whole tree landed in the AHEAD set.
295BOOST_AUTO_TEST_CASE( GetDifferentFilesHandlesUnrelatedHistories )
296{
297 // Local root commit with two files. The bug previously listed both as AHEAD.
298 git_repository* repo = makeRepoWithCommit( TempPath(), "untouched.txt", "stable\n" );
299 KIGIT::GitRepositoryPtr repoPtr( repo );
300
301 // Add a second file to local HEAD so we have more than the bare initial tree.
302 {
303 wxFileName extra( TempPath(), wxS( "second.txt" ) );
304 std::ofstream f( extra.GetFullPath().utf8_string() );
305 f << "more\n";
306 }
307
308 git_index* index = nullptr;
309 BOOST_REQUIRE_EQUAL( git_repository_index( &index, repo ), GIT_OK );
310 KIGIT::GitIndexPtr indexPtr( index );
311 BOOST_REQUIRE_EQUAL( git_index_add_bypath( index, "second.txt" ), GIT_OK );
312 BOOST_REQUIRE_EQUAL( git_index_write( index ), GIT_OK );
313
314 git_oid newTreeOid;
315 BOOST_REQUIRE_EQUAL( git_index_write_tree( &newTreeOid, index ), GIT_OK );
316
317 git_tree* newTree = nullptr;
318 BOOST_REQUIRE_EQUAL( git_tree_lookup( &newTree, repo, &newTreeOid ), GIT_OK );
319 KIGIT::GitTreePtr newTreePtr( newTree );
320
321 git_reference* head = nullptr;
322 BOOST_REQUIRE_EQUAL( git_repository_head( &head, repo ), GIT_OK );
323 KIGIT::GitReferencePtr headPtr( head );
324
325 git_commit* parentCommit = nullptr;
327 git_commit_lookup( &parentCommit, repo, git_reference_target( head ) ), GIT_OK );
328 KIGIT::GitCommitPtr parentCommitPtr( parentCommit );
329
330 git_signature* sig = nullptr;
331 BOOST_REQUIRE_EQUAL( git_signature_now( &sig, "QA Test", "[email protected]" ), GIT_OK );
332 KIGIT::GitSignaturePtr sigPtr( sig );
333
334 const git_commit* parents[1] = { parentCommit };
335 git_oid secondCommitOid;
336 BOOST_REQUIRE_EQUAL( git_commit_create( &secondCommitOid, repo, "HEAD", sig, sig, nullptr,
337 "second", newTree, 1, parents ),
338 GIT_OK );
339
340 // Build a disjoint upstream history with its own root commit and a different file.
341 git_treebuilder* tb = nullptr;
342 BOOST_REQUIRE_EQUAL( git_treebuilder_new( &tb, repo, nullptr ), GIT_OK );
343
344 git_oid blobOid;
345 const std::string upstreamData = "upstream\n";
346 BOOST_REQUIRE_EQUAL( git_blob_create_from_buffer( &blobOid, repo, upstreamData.data(),
347 upstreamData.size() ),
348 GIT_OK );
349 BOOST_REQUIRE_EQUAL( git_treebuilder_insert( nullptr, tb, "remote_only.txt", &blobOid,
350 GIT_FILEMODE_BLOB ),
351 GIT_OK );
352
353 git_oid remoteTreeOid;
354 BOOST_REQUIRE_EQUAL( git_treebuilder_write( &remoteTreeOid, tb ), GIT_OK );
355 git_treebuilder_free( tb );
356
357 git_tree* remoteTree = nullptr;
358 BOOST_REQUIRE_EQUAL( git_tree_lookup( &remoteTree, repo, &remoteTreeOid ), GIT_OK );
359 KIGIT::GitTreePtr remoteTreePtr( remoteTree );
360
361 git_oid remoteCommitOid;
362 BOOST_REQUIRE_EQUAL( git_commit_create( &remoteCommitOid, repo, nullptr, sig, sig, nullptr,
363 "upstream root", remoteTree, 0, nullptr ),
364 GIT_OK );
365
366 git_reference* remoteRef = nullptr;
367 BOOST_REQUIRE_EQUAL( git_reference_create( &remoteRef, repo, "refs/remotes/origin/main",
368 &remoteCommitOid, true, nullptr ),
369 GIT_OK );
370 KIGIT::GitReferencePtr remoteRefPtr( remoteRef );
371
372 git_config* cfg = nullptr;
373 BOOST_REQUIRE_EQUAL( git_repository_config( &cfg, repo ), GIT_OK );
374 KIGIT::GitConfigPtr cfgPtr( cfg );
375 BOOST_REQUIRE_EQUAL( git_config_set_string( cfg, "branch.main.remote", "origin" ),
376 GIT_OK );
377 BOOST_REQUIRE_EQUAL( git_config_set_string( cfg, "branch.main.merge", "refs/heads/main" ),
378 GIT_OK );
379
380 KIGIT_COMMON common( repo );
381 auto [local, remote] = common.GetDifferentFiles();
382
383 // With unrelated histories there is no merge base, so the implementation cannot
384 // attribute changes to either side. Pre-fix, the revwalk reached both root commits
385 // and dumped both trees in full; the new merge-base approach short-circuits and
386 // reports nothing on either side in this ambiguous state.
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" );
391}
392
393
394// Files present unchanged in both trees must not show up as ahead.
395BOOST_AUTO_TEST_CASE( GetDifferentFilesReportsOnlyTouchedFilesWhenAhead )
396{
397 // Initial commit becomes the shared base. Two files: only one will be modified later.
398 git_repository* repo = makeRepoWithCommit( TempPath(), "untouched.txt", "stable\n" );
399 KIGIT::GitRepositoryPtr repoPtr( repo );
400
401 {
402 wxFileName extra( TempPath(), wxS( "touched.txt" ) );
403 std::ofstream f( extra.GetFullPath().utf8_string() );
404 f << "v1\n";
405 }
406
407 git_index* index = nullptr;
408 BOOST_REQUIRE_EQUAL( git_repository_index( &index, repo ), GIT_OK );
409 KIGIT::GitIndexPtr indexPtr( index );
410 BOOST_REQUIRE_EQUAL( git_index_add_bypath( index, "touched.txt" ), GIT_OK );
411 BOOST_REQUIRE_EQUAL( git_index_write( index ), GIT_OK );
412
413 git_oid treeOid;
414 BOOST_REQUIRE_EQUAL( git_index_write_tree( &treeOid, index ), GIT_OK );
415
416 git_tree* tree = nullptr;
417 BOOST_REQUIRE_EQUAL( git_tree_lookup( &tree, repo, &treeOid ), GIT_OK );
418 KIGIT::GitTreePtr treePtr( tree );
419
420 git_reference* head = nullptr;
421 BOOST_REQUIRE_EQUAL( git_repository_head( &head, repo ), GIT_OK );
422 KIGIT::GitReferencePtr headPtr( head );
423
424 git_commit* parent = nullptr;
425 BOOST_REQUIRE_EQUAL( git_commit_lookup( &parent, repo, git_reference_target( head ) ),
426 GIT_OK );
427 KIGIT::GitCommitPtr parentPtr( parent );
428
429 git_signature* sig = nullptr;
430 BOOST_REQUIRE_EQUAL( git_signature_now( &sig, "QA Test", "[email protected]" ), GIT_OK );
431 KIGIT::GitSignaturePtr sigPtr( sig );
432
433 const git_commit* parents[1] = { parent };
434 git_oid base_oid;
435 BOOST_REQUIRE_EQUAL( git_commit_create( &base_oid, repo, "HEAD", sig, sig, nullptr,
436 "add touched", tree, 1, parents ),
437 GIT_OK );
438
439 // Mark this commit as the upstream tip and configure a remote so libgit2 accepts the
440 // branch.main.remote = origin pointer below.
441 git_remote* origin = nullptr;
442 BOOST_REQUIRE_EQUAL( git_remote_create( &origin, repo, "origin",
443 "[email protected]:repo.git" ),
444 GIT_OK );
445 KIGIT::GitRemotePtr originPtr( origin );
446
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 ),
450 GIT_OK );
451 KIGIT::GitReferencePtr upstreamRefPtr( upstream_ref );
452
453 git_config* cfg = nullptr;
454 BOOST_REQUIRE_EQUAL( git_repository_config( &cfg, repo ), GIT_OK );
455 KIGIT::GitConfigPtr cfgPtr( cfg );
456 BOOST_REQUIRE_EQUAL( git_config_set_string( cfg, "branch.main.remote", "origin" ),
457 GIT_OK );
458 BOOST_REQUIRE_EQUAL( git_config_set_string( cfg, "branch.main.merge", "refs/heads/main" ),
459 GIT_OK );
460
461 // Local-only commit modifying just touched.txt.
462 {
463 wxFileName extra( TempPath(), wxS( "touched.txt" ) );
464 std::ofstream f( extra.GetFullPath().utf8_string() );
465 f << "v2\n";
466 }
467
468 BOOST_REQUIRE_EQUAL( git_index_add_bypath( index, "touched.txt" ), GIT_OK );
469 BOOST_REQUIRE_EQUAL( git_index_write( index ), GIT_OK );
470
471 git_oid newTreeOid;
472 BOOST_REQUIRE_EQUAL( git_index_write_tree( &newTreeOid, index ), GIT_OK );
473
474 git_tree* newTree = nullptr;
475 BOOST_REQUIRE_EQUAL( git_tree_lookup( &newTree, repo, &newTreeOid ), GIT_OK );
476 KIGIT::GitTreePtr newTreePtr( newTree );
477
478 git_commit* baseCommit = nullptr;
479 BOOST_REQUIRE_EQUAL( git_commit_lookup( &baseCommit, repo, &base_oid ), GIT_OK );
480 KIGIT::GitCommitPtr baseCommitPtr( baseCommit );
481
482 const git_commit* aheadParents[1] = { baseCommit };
483 git_oid aheadOid;
484 BOOST_REQUIRE_EQUAL( git_commit_create( &aheadOid, repo, "HEAD", sig, sig, nullptr,
485 "modify touched", newTree, 1, aheadParents ),
486 GIT_OK );
487
488 KIGIT_COMMON common( repo );
489 auto [local, remote] = common.GetDifferentFiles();
490
491 BOOST_CHECK( remote.empty() );
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" );
497}
498
499
500BOOST_AUTO_TEST_CASE( PrivateKeyEncryptionOpenSSHFormat )
501{
502 BOOST_CHECK_MESSAGE( KIGIT::IsPrivateKeyEncrypted( c_opensshEncrypted ),
503 "A passphrase-protected OpenSSH key must be reported as encrypted" );
504
505 BOOST_CHECK_MESSAGE( !KIGIT::IsPrivateKeyEncrypted( c_opensshPlain ),
506 "An OpenSSH key with cipher \"none\" must be reported as unencrypted" );
507
508 BOOST_CHECK_MESSAGE( !KIGIT::IsPrivateKeyEncrypted( c_opensshTruncated ),
509 "An OpenSSH key with an unreadable body must not be reported as encrypted" );
510}
511
512
513BOOST_AUTO_TEST_CASE( PrivateKeyEncryptionClassicPEMFormat )
514{
515 BOOST_CHECK_MESSAGE( KIGIT::IsPrivateKeyEncrypted( c_pemEncrypted ),
516 "A PEM key with Proc-Type: 4,ENCRYPTED must be reported as encrypted" );
517
518 BOOST_CHECK_MESSAGE( !KIGIT::IsPrivateKeyEncrypted( c_pemPlain ),
519 "A PEM key with no Proc-Type must be reported as unencrypted" );
520}
521
522
523BOOST_AUTO_TEST_CASE( PrivateKeyEncryptionPKCS8Format )
524{
525 BOOST_CHECK_MESSAGE( KIGIT::IsPrivateKeyEncrypted( c_pkcs8Encrypted ),
526 "A PKCS#8 key headed ENCRYPTED PRIVATE KEY must be reported as encrypted" );
527}
528
529
530BOOST_AUTO_TEST_CASE( PrivateKeyEncryptionRejectsNonKeys )
531{
532 BOOST_CHECK( !KIGIT::IsPrivateKeyEncrypted( wxEmptyString ) );
533
534 BOOST_CHECK_MESSAGE( !KIGIT::IsPrivateKeyEncrypted( "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKB0 t\n" ),
535 "A public key is not an encrypted private key" );
536}
537
538
539// A file changed only on the remote tip, with the local branch still at the shared base, must
540// land in the BEHIND (remote) set and not in the AHEAD (local) set.
541BOOST_AUTO_TEST_CASE( GetDifferentFilesReportsRemoteOnlyFilesWhenBehind )
542{
543 git_repository* repo = makeRepoWithCommit( TempPath(), "untouched.txt", "stable\n" );
544 KIGIT::GitRepositoryPtr repoPtr( repo );
545
546 git_reference* head = nullptr;
547 BOOST_REQUIRE_EQUAL( git_repository_head( &head, repo ), GIT_OK );
548 KIGIT::GitReferencePtr headPtr( head );
549
550 git_oid baseOid = *git_reference_target( head );
551
552 git_commit* baseCommit = nullptr;
553 BOOST_REQUIRE_EQUAL( git_commit_lookup( &baseCommit, repo, &baseOid ), GIT_OK );
554 KIGIT::GitCommitPtr baseCommitPtr( baseCommit );
555
556 // Build a tree that adds remote_only.txt on top of the base, then commit it without moving
557 // HEAD so the local branch stays behind.
558 {
559 wxFileName extra( TempPath(), wxS( "remote_only.txt" ) );
560 std::ofstream f( extra.GetFullPath().utf8_string() );
561 f << "from remote\n";
562 }
563
564 git_index* index = nullptr;
565 BOOST_REQUIRE_EQUAL( git_repository_index( &index, repo ), GIT_OK );
566 KIGIT::GitIndexPtr indexPtr( index );
567 BOOST_REQUIRE_EQUAL( git_index_add_bypath( index, "remote_only.txt" ), GIT_OK );
568 BOOST_REQUIRE_EQUAL( git_index_write( index ), GIT_OK );
569
570 git_oid treeOid;
571 BOOST_REQUIRE_EQUAL( git_index_write_tree( &treeOid, index ), GIT_OK );
572
573 git_tree* tree = nullptr;
574 BOOST_REQUIRE_EQUAL( git_tree_lookup( &tree, repo, &treeOid ), GIT_OK );
575 KIGIT::GitTreePtr treePtr( tree );
576
577 git_signature* sig = nullptr;
578 BOOST_REQUIRE_EQUAL( git_signature_now( &sig, "QA Test", "[email protected]" ), GIT_OK );
579 KIGIT::GitSignaturePtr sigPtr( sig );
580
581 const git_commit* parents[1] = { baseCommit };
582 git_oid remoteOid;
584 git_commit_create( &remoteOid, repo, nullptr, sig, sig, nullptr, "add remote_only", tree, 1, parents ),
585 GIT_OK );
586
587 git_remote* origin = nullptr;
588 BOOST_REQUIRE_EQUAL( git_remote_create( &origin, repo, "origin", "[email protected]:repo.git" ), GIT_OK );
589 KIGIT::GitRemotePtr originPtr( origin );
590
591 git_reference* upstream_ref = nullptr;
593 git_reference_create( &upstream_ref, repo, "refs/remotes/origin/main", &remoteOid, true, nullptr ),
594 GIT_OK );
595 KIGIT::GitReferencePtr upstreamRefPtr( upstream_ref );
596
597 git_config* cfg = nullptr;
598 BOOST_REQUIRE_EQUAL( git_repository_config( &cfg, repo ), GIT_OK );
599 KIGIT::GitConfigPtr cfgPtr( cfg );
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 );
602
603 KIGIT_COMMON common( repo );
604 auto [local, remote] = common.GetDifferentFiles();
605
606 BOOST_CHECK( local.empty() );
607 BOOST_CHECK_MESSAGE( remote.find( wxS( "remote_only.txt" ) ) != remote.end(),
608 "remote_only.txt should be reported as BEHIND" );
609}
610
611
612// The commit panels and the advanced window register refresh listeners so a write from any
613// one of them updates the others. A notify must fire every registered listener.
614BOOST_AUTO_TEST_CASE( RefreshListenersFireOnNotify )
615{
616 KIGIT_COMMON common( nullptr );
617
618 int a = 0;
619 int b = 0;
620 int ownerA = 0;
621 int ownerB = 0;
622
623 common.AddRefreshListener( &ownerA,
624 [&]()
625 {
626 a++;
627 } );
628 common.AddRefreshListener( &ownerB,
629 [&]()
630 {
631 b++;
632 } );
633
634 common.NotifyRefreshListeners();
635 BOOST_CHECK_EQUAL( a, 1 );
636 BOOST_CHECK_EQUAL( b, 1 );
637
638 common.NotifyRefreshListeners();
639 BOOST_CHECK_EQUAL( a, 2 );
640 BOOST_CHECK_EQUAL( b, 2 );
641}
642
643
644// A removed listener, such as one whose panel was destroyed, must never fire again.
645BOOST_AUTO_TEST_CASE( RemovedRefreshListenerDoesNotFire )
646{
647 KIGIT_COMMON common( nullptr );
648
649 int a = 0;
650 int b = 0;
651 int ownerA = 0;
652 int ownerB = 0;
653
654 common.AddRefreshListener( &ownerA,
655 [&]()
656 {
657 a++;
658 } );
659 common.AddRefreshListener( &ownerB,
660 [&]()
661 {
662 b++;
663 } );
664 common.RemoveRefreshListener( &ownerA );
665
666 common.NotifyRefreshListeners();
667 BOOST_CHECK_EQUAL( a, 0 );
668 BOOST_CHECK_EQUAL( b, 1 );
669}
670
671
672// A panel torn down while responding to a refresh unregisters from inside its own callback, and
673// the remaining listeners must still run.
674BOOST_AUTO_TEST_CASE( RefreshListenerCanUnregisterDuringNotify )
675{
676 KIGIT_COMMON common( nullptr );
677
678 int a = 0;
679 int b = 0;
680 int ownerA = 0;
681 int ownerB = 0;
682
683 common.AddRefreshListener( &ownerA,
684 [&]()
685 {
686 a++;
687 common.RemoveRefreshListener( &ownerA );
688 } );
689 common.AddRefreshListener( &ownerB,
690 [&]()
691 {
692 b++;
693 } );
694
695 common.NotifyRefreshListeners();
696 BOOST_CHECK_EQUAL( a, 1 );
697 BOOST_CHECK_EQUAL( b, 1 );
698
699 common.NotifyRefreshListeners();
700 BOOST_CHECK_EQUAL( a, 1 );
701 BOOST_CHECK_EQUAL( b, 2 );
702}
703
704
705// A listener removed by an earlier listener in the same round must not run.
706BOOST_AUTO_TEST_CASE( RefreshListenerRemovedByAnotherDoesNotFire )
707{
708 KIGIT_COMMON common( nullptr );
709
710 int a = 0;
711 int b = 0;
712 int ownerA = 0;
713 int ownerB = 0;
714
715 common.AddRefreshListener( &ownerA,
716 [&]()
717 {
718 a++;
719 common.RemoveRefreshListener( &ownerB );
720 } );
721 common.AddRefreshListener( &ownerB,
722 [&]()
723 {
724 b++;
725 } );
726
727 common.NotifyRefreshListeners();
728 BOOST_CHECK_EQUAL( a, 1 );
729 BOOST_CHECK_EQUAL( b, 0 );
730}
731
732
733// The History page reads the log through GetRecentCommits. With no repository it must return
734// an empty list rather than crash.
735BOOST_AUTO_TEST_CASE( GetRecentCommitsEmptyWithoutRepo )
736{
737 KIGIT_COMMON common( nullptr );
738 BOOST_CHECK( common.GetRecentCommits( 10 ).empty() );
739}
740
741
742BOOST_AUTO_TEST_CASE( GetRecentCommitsHonorsAnAlreadyCancelledOperation )
743{
744 KIGIT::GitRepositoryPtr repo( makeRepoWithCommit( TempPath(), "file.txt", "one\n" ) );
745 KIGIT_COMMON common( repo.get() );
746 auto lock = common.LockAction();
747
748 BOOST_REQUIRE_EQUAL( common.GetRecentCommits( 10 ).size(), 1u );
749
751 token->store( true );
752
753 {
754 KIGIT_COMMON::OPERATION_GUARD operation( &common, token );
755 BOOST_CHECK( common.GetRecentCommits( 10 ).empty() );
756 }
757
758 // Cancellation belongs to that operation. A later history read must still work.
759 BOOST_CHECK_EQUAL( common.GetRecentCommits( 10 ).size(), 1u );
760}
761
762
763// GetRecentCommits returns commits newest-first with their summary, author and short hash,
764// and honors the requested maximum.
765BOOST_AUTO_TEST_CASE( GetRecentCommitsReturnsNewestFirst )
766{
767 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
768 KIGIT::GitRepositoryPtr repoPtr( repo );
769
770 // Add a second commit on top of the first.
771 {
772 wxFileName fn( TempPath(), wxS( "file.txt" ) );
773 std::ofstream f( fn.GetFullPath().utf8_string() );
774 f << "v2\n";
775 }
776
777 git_index* index = nullptr;
778 BOOST_REQUIRE_EQUAL( git_repository_index( &index, repo ), GIT_OK );
779 KIGIT::GitIndexPtr indexPtr( index );
780 BOOST_REQUIRE_EQUAL( git_index_add_bypath( index, "file.txt" ), GIT_OK );
781 BOOST_REQUIRE_EQUAL( git_index_write( index ), GIT_OK );
782
783 git_oid treeOid;
784 BOOST_REQUIRE_EQUAL( git_index_write_tree( &treeOid, index ), GIT_OK );
785
786 git_tree* tree = nullptr;
787 BOOST_REQUIRE_EQUAL( git_tree_lookup( &tree, repo, &treeOid ), GIT_OK );
788 KIGIT::GitTreePtr treePtr( tree );
789
790 git_reference* head = nullptr;
791 BOOST_REQUIRE_EQUAL( git_repository_head( &head, repo ), GIT_OK );
792 KIGIT::GitReferencePtr headPtr( head );
793
794 git_commit* parent = nullptr;
795 BOOST_REQUIRE_EQUAL( git_commit_lookup( &parent, repo, git_reference_target( head ) ), GIT_OK );
796 KIGIT::GitCommitPtr parentPtr( parent );
797
798 git_signature* sig = nullptr;
799 BOOST_REQUIRE_EQUAL( git_signature_now( &sig, "QA Test", "[email protected]" ), GIT_OK );
800 KIGIT::GitSignaturePtr sigPtr( sig );
801
802 const git_commit* parents[1] = { parent };
803 git_oid secondOid;
804 BOOST_REQUIRE_EQUAL( git_commit_create( &secondOid, repo, "HEAD", sig, sig, nullptr, "second", tree, 1, parents ),
805 GIT_OK );
806
807 KIGIT_COMMON common( repo );
808
809 std::vector<KIGIT_COMMIT_INFO> commits = common.GetRecentCommits( 10 );
810 BOOST_REQUIRE_EQUAL( commits.size(), 2u );
811 BOOST_CHECK_EQUAL( commits[0].summary.ToStdString(), "second" );
812 BOOST_CHECK_EQUAL( commits[1].summary.ToStdString(), "initial" );
813 BOOST_CHECK_EQUAL( commits[0].author.ToStdString(), "QA Test" );
814
815 // The hash is abbreviated to 12 characters. A 7-char abbreviation was ambiguous in large
816 // repositories, which made a later revparse of the hash fail.
817 BOOST_CHECK_EQUAL( commits[0].hash.length(), 12u );
818
819 // The maximum is honored.
820 BOOST_CHECK_EQUAL( common.GetRecentCommits( 1 ).size(), 1u );
821 BOOST_CHECK_EQUAL( common.GetRecentCommits( 0 ).size(), 0u );
822}
823
824
825// Git does not check the address itself, so this is only here to catch a typo before every
826// commit carries it.
827BOOST_AUTO_TEST_CASE( IdentityEmailMustLookLikeAnAddress )
828{
829 wxString error;
830
831 BOOST_CHECK( KIGIT::ValidateIdentity( wxT( "A Name" ), wxT( "[email protected]" ), error ) );
832 BOOST_CHECK( KIGIT::ValidateIdentity( wxT( "A Name" ), wxT( "[email protected]" ), error ) );
833
834 BOOST_CHECK( !KIGIT::ValidateIdentity( wxEmptyString, wxT( "[email protected]" ), error ) );
835 BOOST_CHECK( !KIGIT::ValidateIdentity( wxT( " " ), wxT( "[email protected]" ), error ) );
836
837 for( const wxString& bad : { wxT( "a@" ), wxT( "@" ), wxT( "@@" ), wxT( "a@b" ), wxT( "@b.c" ), wxT( "a [email protected]" ),
838 wxT( "[email protected]@d.e" ), wxT( "[email protected]" ), wxT( "a@b." ) } )
839 {
840 BOOST_CHECK_MESSAGE( !KIGIT::ValidateIdentity( wxT( "A Name" ), bad, error ), bad );
841 }
842}
843
844// A project can be open before it is ever put under version control, so a session with no
845// repository is a normal state and not a programming error.
846BOOST_AUTO_TEST_CASE( SessionWithoutARepositoryAnswersQuietly )
847{
848 KIGIT_COMMON common( nullptr );
849
850 BOOST_CHECK( common.GetCurrentBranchName().IsEmpty() );
851 BOOST_CHECK( common.GetProjectDirs().empty() );
853 BOOST_CHECK( common.GetRemotename().IsEmpty() );
854
855 BOOST_CHECK_NO_THROW( common.UpdateCurrentBranchInfo() );
856}
857
858// A branch name the user typed is checked before git sees it, so the refusal names the
859// mistake instead of quoting libgit2.
860BOOST_AUTO_TEST_CASE( BranchNameErrorNamesTheMistake )
861{
862 BOOST_CHECK( KIGIT::BranchNameError( wxS( "feature/new-thing" ) ).IsEmpty() );
863 BOOST_CHECK( KIGIT::BranchNameError( wxS( "fix_25074" ) ).IsEmpty() );
864
865 BOOST_CHECK( !KIGIT::BranchNameError( wxEmptyString ).IsEmpty() );
866 BOOST_CHECK( KIGIT::BranchNameError( wxS( "two words" ) ).Contains( wxS( "spaces" ) ) );
867 BOOST_CHECK( KIGIT::BranchNameError( wxS( "-lead" ) ).Contains( wxS( "dash" ) ) );
868 BOOST_CHECK( KIGIT::BranchNameError( wxS( "a..b" ) ).Contains( wxS( "dots" ) ) );
869 BOOST_CHECK( KIGIT::BranchNameError( wxS( "trailing/" ) ).Contains( wxS( "slash" ) ) );
870
871 // Caught by git itself rather than by a named rule.
872 BOOST_CHECK( !KIGIT::BranchNameError( wxS( "with~tilde" ) ).IsEmpty() );
873 BOOST_CHECK( !KIGIT::BranchNameError( wxS( "with:colon" ) ).IsEmpty() );
874 BOOST_CHECK( !KIGIT::BranchNameError( wxS( "ends.lock" ) ).IsEmpty() );
875}
876
877
878// The check belongs to branch creation, not to the one dialog that prompts for a name.
879BOOST_AUTO_TEST_CASE( CreateBranchRefusesAnInvalidName )
880{
881 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
882 KIGIT::GitRepositoryPtr repoPtr( repo );
883
884 KIGIT_COMMON common( repo );
885
886 BOOST_CHECK( !common.CreateBranch( wxS( "two words" ) ) );
887 BOOST_CHECK( !common.CreateBranch( wxS( "a..b" ) ) );
888 BOOST_CHECK( common.CreateBranch( wxS( "feature/ok" ) ) );
889}
890
891// Git stores commit text as opaque bytes. A repository written on Windows can hold ISO 8859-1,
892// and decoding it strictly as UTF-8 would leave the whole row blank.
893BOOST_AUTO_TEST_CASE( CommitTextThatIsNotUtf8StillReads )
894{
895 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
896 KIGIT::GitRepositoryPtr repoPtr( repo );
897
898 git_reference* head = nullptr;
899 BOOST_REQUIRE_EQUAL( git_repository_head( &head, repo ), GIT_OK );
900 KIGIT::GitReferencePtr headPtr( head );
901
902 git_commit* parent = nullptr;
903 BOOST_REQUIRE_EQUAL( git_commit_lookup( &parent, repo, git_reference_target( head ) ), GIT_OK );
904 KIGIT::GitCommitPtr parentPtr( parent );
905
906 git_tree* tree = nullptr;
907 BOOST_REQUIRE_EQUAL( git_commit_tree( &tree, parent ), GIT_OK );
908 KIGIT::GitTreePtr treePtr( tree );
909
910 // "Jorg" and "Cafe fix" with the accented letters as single Latin-1 bytes.
911 const char* latin1Author = "J\xf6rg";
912 const char* latin1Summary = "Caf\xe9 fix";
913
914 git_signature* sig = nullptr;
915 BOOST_REQUIRE_EQUAL( git_signature_now( &sig, latin1Author, "[email protected]" ), GIT_OK );
916 KIGIT::GitSignaturePtr sigPtr( sig );
917
918 const git_commit* parents[1] = { parent };
919 git_oid oid;
920 BOOST_REQUIRE_EQUAL( git_commit_create( &oid, repo, "HEAD", sig, sig, nullptr, latin1Summary, tree, 1, parents ),
921 GIT_OK );
922
923 KIGIT_COMMON common( repo );
924
925 std::vector<KIGIT_COMMIT_INFO> commits = common.GetRecentCommits( 1 );
926 BOOST_REQUIRE_EQUAL( commits.size(), 1u );
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() );
929
930 KIGIT_COMMIT_DETAIL detail = common.GetCommitDetail( commits[0].hash );
931 BOOST_REQUIRE( detail.found );
932 BOOST_CHECK( !detail.message.IsEmpty() );
933 BOOST_CHECK( !detail.author.IsEmpty() );
934}
935
936// The History page can narrow the log to one branch or to a search text. With no branch named
937// the walk covers every branch, so a commit on a side branch shows up too.
938BOOST_AUTO_TEST_CASE( GetRecentCommitsFiltersByBranchAndSearch )
939{
940 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
941 KIGIT::GitRepositoryPtr repoPtr( repo );
942
943 // A side branch with one commit of its own, then back on main for one more.
944 git_oid headOid;
945 BOOST_REQUIRE_EQUAL( git_reference_name_to_id( &headOid, repo, "HEAD" ), GIT_OK );
946
947 git_commit* headCommit = nullptr;
948 BOOST_REQUIRE_EQUAL( git_commit_lookup( &headCommit, repo, &headOid ), GIT_OK );
949 KIGIT::GitCommitPtr headPtr( headCommit );
950
951 git_reference* side = nullptr;
952 BOOST_REQUIRE_EQUAL( git_branch_create( &side, repo, "side", headCommit, 0 ), GIT_OK );
953 KIGIT::GitReferencePtr sidePtr( side );
954
955 BOOST_REQUIRE_EQUAL( git_repository_set_head( repo, "refs/heads/side" ), GIT_OK );
956 writeFile( TempPath(), "side.txt", "side\n" );
957 commitWorkingFile( repo, "side.txt", "side work" );
958
959 BOOST_REQUIRE_EQUAL( git_repository_set_head( repo, "refs/heads/main" ), GIT_OK );
960 writeFile( TempPath(), "file.txt", "v2\n" );
961 commitWorkingFile( repo, "file.txt", "main work" );
962
963 KIGIT_COMMON common( repo );
964
965 auto summaries = []( const std::vector<KIGIT_COMMIT_INFO>& aCommits )
966 {
967 std::vector<std::string> out;
968
969 for( const KIGIT_COMMIT_INFO& c : aCommits )
970 out.push_back( c.summary.ToStdString() );
971
972 return out;
973 };
974
975 // Every branch: the side commit is there along with main's.
976 std::vector<std::string> all = summaries( common.GetRecentCommits( 10 ) );
977 BOOST_REQUIRE_EQUAL( all.size(), 3u );
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() );
980
981 // One branch: only what it reaches.
983 onMain.branch = wxS( "main" );
984 std::vector<std::string> mainOnly = summaries( common.GetRecentCommits( 10, onMain ) );
985 BOOST_REQUIRE_EQUAL( mainOnly.size(), 2u );
986 BOOST_CHECK_EQUAL( mainOnly[0], "main work" );
987 BOOST_CHECK_EQUAL( mainOnly[1], "initial" );
988
989 // Search matches the summary, case insensitively, and the hash.
991 search.search = wxS( "SIDE" );
992 std::vector<KIGIT_COMMIT_INFO> found = common.GetRecentCommits( 10, search );
993 BOOST_REQUIRE_EQUAL( found.size(), 1u );
994 BOOST_CHECK_EQUAL( found[0].summary.ToStdString(), "side work" );
995
996 search.search = found[0].hash.Left( 6 );
997 BOOST_REQUIRE_EQUAL( common.GetRecentCommits( 10, search ).size(), 1u );
998
999 search.search = wxS( "nothing like this" );
1000 BOOST_CHECK( common.GetRecentCommits( 10, search ).empty() );
1001}
1002
1003
1004// Each commit carries the names of the refs that point at it, so the list can show where HEAD,
1005// the branches and the tags are.
1006BOOST_AUTO_TEST_CASE( GetRecentCommitsNamesRefsOnEachCommit )
1007{
1008 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
1009 KIGIT::GitRepositoryPtr repoPtr( repo );
1010
1011 git_oid firstOid;
1012 BOOST_REQUIRE_EQUAL( git_reference_name_to_id( &firstOid, repo, "HEAD" ), GIT_OK );
1013
1014 git_object* first = nullptr;
1015 BOOST_REQUIRE_EQUAL( git_object_lookup( &first, repo, &firstOid, GIT_OBJECT_COMMIT ), GIT_OK );
1016 KIGIT::GitObjectPtr firstPtr( first );
1017
1018 git_oid tagOid;
1019 BOOST_REQUIRE_EQUAL( git_tag_create_lightweight( &tagOid, repo, "v1.0", first, 0 ), GIT_OK );
1020
1021 writeFile( TempPath(), "file.txt", "v2\n" );
1022 commitWorkingFile( repo, "file.txt", "second" );
1023
1024 KIGIT_COMMON common( repo );
1025
1026 std::vector<KIGIT_COMMIT_INFO> commits = common.GetRecentCommits( 10 );
1027 BOOST_REQUIRE_EQUAL( commits.size(), 2u );
1028
1029 // The newest commit is HEAD and main. The first one carries only the tag.
1030 BOOST_REQUIRE_EQUAL( commits[0].refs.size(), 2u );
1031 BOOST_CHECK_EQUAL( commits[0].refs[0].ToStdString(), "HEAD" );
1032 BOOST_CHECK_EQUAL( commits[0].refs[1].ToStdString(), "main" );
1033
1034 BOOST_REQUIRE_EQUAL( commits[1].refs.size(), 1u );
1035 BOOST_CHECK_EQUAL( commits[1].refs[0].ToStdString(), "v1.0" );
1036}
1037
1038
1039// The detail pane shows one commit with its message, parent and per-file line counts.
1040// A project just put under version control has no commits. There is nothing to branch from, so
1041// naming a branch means pointing HEAD at it and letting the first commit create it.
1042BOOST_AUTO_TEST_CASE( StartUnbornBranchNamesTheBranchTheFirstCommitWillCreate )
1043{
1044 git_repository* repo = nullptr;
1045 git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
1046 opts.flags = GIT_REPOSITORY_INIT_MKPATH;
1047 opts.initial_head = "main";
1048
1049 BOOST_REQUIRE_EQUAL( git_repository_init_ext( &repo, TempPath().utf8_string().c_str(), &opts ), GIT_OK );
1050 KIGIT::GitRepositoryPtr repoPtr( repo );
1051
1052 KIGIT_COMMON common( repo );
1053
1054 BOOST_REQUIRE( common.IsHeadUnborn() );
1055 BOOST_CHECK( common.GetBranches().empty() );
1056 BOOST_CHECK( !common.HasLocalCommits() );
1057 BOOST_CHECK( !common.UpstreamStatus() );
1058 BOOST_CHECK( !common.GetUnmergedCommitCount( "main" ) );
1059 BOOST_CHECK( !common.GetBranchDetail( "main" ).divergence );
1060
1061 // There is no commit to start a branch at, which is why the method below exists.
1062 BOOST_CHECK( !common.CreateBranch( wxS( "rev-a" ) ) );
1063
1064 // A name git itself would refuse must be refused here too, or the first commit creates a
1065 // branch no git tool can name.
1066 BOOST_CHECK( !common.StartUnbornBranch( wxS( "-leading" ) ) );
1067 BOOST_CHECK( !common.StartUnbornBranch( wxS( "has space" ) ) );
1068 BOOST_CHECK( !common.StartUnbornBranch( wxS( "bad..name" ) ) );
1069
1070 BOOST_REQUIRE( common.StartUnbornBranch( wxS( "rev-a" ) ) );
1071
1072 git_reference* head = nullptr;
1073 BOOST_REQUIRE_EQUAL( git_reference_lookup( &head, repo, "HEAD" ), GIT_OK );
1074 KIGIT::GitReferencePtr headPtr( head );
1075
1076 BOOST_CHECK_EQUAL( std::string( git_reference_symbolic_target( head ) ), "refs/heads/rev-a" );
1077 BOOST_CHECK( common.IsHeadUnborn() );
1078 BOOST_CHECK( common.GetBranches().empty() );
1079}
1080
1081
1082// The History page shows a commit's whole message in its detail pane, summary line and body.
1083BOOST_AUTO_TEST_CASE( GetCommitDetailCarriesTheWholeMessage )
1084{
1085 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
1086 KIGIT::GitRepositoryPtr repoPtr( repo );
1087
1088 writeFile( TempPath(), "file.txt", "v2\n" );
1089 commitWorkingFile( repo, "file.txt", "Route the power rails\n\nBody line one.\nBody line two.\n" );
1090
1091 KIGIT_COMMON common( repo );
1092 KIGIT_COMMIT_DETAIL detail = common.GetCommitDetail( common.GetRecentCommits( 1 )[0].hash );
1093
1094 BOOST_REQUIRE( detail.found );
1095 BOOST_CHECK_EQUAL( detail.message.ToStdString(), "Route the power rails\n\nBody line one.\nBody line two." );
1096}
1097
1098
1099BOOST_AUTO_TEST_CASE( GetCommitDetailCountsLinesPerFile )
1100{
1101 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "one\ntwo\nthree\n" );
1102 KIGIT::GitRepositoryPtr repoPtr( repo );
1103
1104 // Change one line of the existing file and add a new file in the same commit.
1105 writeFile( TempPath(), "file.txt", "one\n2\nthree\n" );
1106 writeFile( TempPath(), "new.txt", "a\nb\n" );
1107
1108 {
1109 git_index* index = nullptr;
1110 BOOST_REQUIRE_EQUAL( git_repository_index( &index, repo ), GIT_OK );
1111 KIGIT::GitIndexPtr indexPtr( index );
1112 BOOST_REQUIRE_EQUAL( git_index_add_bypath( index, "new.txt" ), GIT_OK );
1113 BOOST_REQUIRE_EQUAL( git_index_write( index ), GIT_OK );
1114 }
1115
1116 commitWorkingFile( repo, "file.txt", "edit and add\n\nWith a body line." );
1117
1118 KIGIT_COMMON common( repo );
1119
1120 std::vector<KIGIT_COMMIT_INFO> commits = common.GetRecentCommits( 10 );
1121 BOOST_REQUIRE_EQUAL( commits.size(), 2u );
1122
1123 KIGIT_COMMIT_DETAIL detail = common.GetCommitDetail( commits[0].hash );
1124 BOOST_REQUIRE( detail.found );
1125 BOOST_CHECK_EQUAL( detail.shortHash.ToStdString(), commits[0].hash.ToStdString() );
1126 BOOST_CHECK_EQUAL( detail.hash.length(), 40u );
1127 BOOST_CHECK_EQUAL( detail.message.ToStdString(), "edit and add\n\nWith a body line." );
1128 BOOST_CHECK_EQUAL( detail.author.ToStdString(), "QA Test" );
1129 BOOST_REQUIRE_EQUAL( detail.parents.size(), 1u );
1130 BOOST_CHECK_EQUAL( detail.parents[0].ToStdString(), commits[1].hash.ToStdString() );
1131 BOOST_CHECK_EQUAL( detail.refs.size(), 2u );
1132
1133 BOOST_REQUIRE_EQUAL( detail.files.size(), 2u );
1134 BOOST_CHECK_EQUAL( detail.files[0].path.ToStdString(), "file.txt" );
1136 BOOST_CHECK_EQUAL( detail.files[0].added, 1 );
1137 BOOST_CHECK_EQUAL( detail.files[0].removed, 1 );
1138 BOOST_CHECK_EQUAL( detail.files[1].path.ToStdString(), "new.txt" );
1140 BOOST_CHECK_EQUAL( detail.files[1].added, 2 );
1141 BOOST_CHECK_EQUAL( detail.files[1].removed, 0 );
1142 BOOST_CHECK_EQUAL( detail.added, 3 );
1143 BOOST_CHECK_EQUAL( detail.removed, 1 );
1144
1145 // A root commit has no parent, so everything it holds counts as added.
1146 KIGIT_COMMIT_DETAIL root = common.GetCommitDetail( commits[1].hash );
1147 BOOST_REQUIRE( root.found );
1148 BOOST_CHECK( root.parents.empty() );
1149 BOOST_REQUIRE_EQUAL( root.files.size(), 1u );
1151 BOOST_CHECK_EQUAL( root.files[0].added, 3 );
1152
1153 BOOST_CHECK( !common.GetCommitDetail( wxS( "0000000000ab" ) ).found );
1154}
1155
1156
1157// The changes list leaves out other projects in the repository, and KiCad's own lock and
1158// backup files. The line counts sit beside that list, so they must cover the same files.
1159BOOST_AUTO_TEST_CASE( WorkingTreeLineStatsCountOnlyTheFilesGiven )
1160{
1161 git_repository* repo = makeRepoWithCommit( TempPath(), "readme.txt", "root\n" );
1162 KIGIT::GitRepositoryPtr repoPtr( repo );
1163
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" );
1168
1169 {
1170 git_index* index = nullptr;
1171 BOOST_REQUIRE_EQUAL( git_repository_index( &index, repo ), GIT_OK );
1172 KIGIT::GitIndexPtr indexPtr( index );
1173 BOOST_REQUIRE_EQUAL( git_index_add_bypath( index, "mine/board.txt" ), GIT_OK );
1174 BOOST_REQUIRE_EQUAL( git_index_add_bypath( index, "theirs/board.txt" ), GIT_OK );
1175 BOOST_REQUIRE_EQUAL( git_index_write( index ), GIT_OK );
1176 }
1177
1178 commitWorkingFile( repo, wxS( "readme.txt" ), "two projects\n" );
1179
1180 // One line added in this project, three in the other, and an untracked lock file.
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" );
1184
1185 KIGIT_COMMON common( repo );
1186
1187 std::pair<int, int> mine = common.GetWorkingTreeLineStats( { wxS( "mine/board.txt" ) } );
1188 BOOST_CHECK_EQUAL( mine.first, 1 );
1189 BOOST_CHECK_EQUAL( mine.second, 0 );
1190
1191 // A path that did not change contributes nothing, and an unlisted one is not counted.
1192 std::pair<int, int> none = common.GetWorkingTreeLineStats( { wxS( "readme.txt" ) } );
1193 BOOST_CHECK_EQUAL( none.first, 0 );
1194 BOOST_CHECK_EQUAL( none.second, 0 );
1195}
1196
1197
1198#ifndef __WINDOWS__
1199// A project can be opened through a symlink pointing straight at its folder inside the
1200// repository. Stepping the visible path up one component then lands outside the repository
1201// altogether, so the folder has to be worked out from the resolved paths instead.
1202BOOST_AUTO_TEST_CASE( SymlinkedProjectFolderStillFindsItsPrefix )
1203{
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" );
1207
1208 wxString deepLink = TempPath() + wxS( "/deep" );
1209 BOOST_REQUIRE_EQUAL( symlink( ( repoRoot + wxS( "/proj" ) ).utf8_string().c_str(), deepLink.utf8_string().c_str() ),
1210 0 );
1211
1212 wxString rootLink = TempPath() + wxS( "/top" );
1213 BOOST_REQUIRE_EQUAL( symlink( repoRoot.utf8_string().c_str(), rootLink.utf8_string().c_str() ), 0 );
1214
1215 const wxString canonicalWorkDir = repoRoot + wxS( "/" );
1216
1217 // Pointed at the project folder there is no visible path for the repository root, so the
1218 // resolved one is used.
1219 wxString deepProject = deepLink + wxS( "/" );
1220 wxString deepWorkDir = KIGIT::PROJECT_GIT_UTILS::ComputeSymlinkPreservingWorkDir( deepProject, canonicalWorkDir );
1221 BOOST_CHECK_EQUAL( deepWorkDir.ToStdString(), canonicalWorkDir.ToStdString() );
1222 std::optional<wxString> deepPrefix = KIGIT::PROJECT_GIT_UTILS::ProjectPrefix( deepWorkDir, deepProject );
1223 BOOST_REQUIRE( deepPrefix );
1224 BOOST_CHECK_EQUAL( deepPrefix->ToStdString(), "proj/" );
1225 BOOST_CHECK( KIGIT::PROJECT_GIT_UTILS::IsWithinProjectPath( repoRoot + wxS( "/proj/p.kicad_pro" ), deepProject ) );
1226 BOOST_CHECK( !KIGIT::PROJECT_GIT_UTILS::IsWithinProjectPath( repoRoot + wxS( "/elsewhere/x.txt" ), deepProject ) );
1227
1228 // Pointed at the repository root the visible path still stands for it, so it is kept.
1229 wxString topProject = rootLink + wxS( "/proj/" );
1230 wxString topWorkDir = KIGIT::PROJECT_GIT_UTILS::ComputeSymlinkPreservingWorkDir( topProject, canonicalWorkDir );
1231 BOOST_CHECK_EQUAL( topWorkDir.ToStdString(), ( rootLink + wxS( "/" ) ).ToStdString() );
1232 std::optional<wxString> topPrefix = KIGIT::PROJECT_GIT_UTILS::ProjectPrefix( topWorkDir, topProject );
1233 BOOST_REQUIRE( topPrefix );
1234 BOOST_CHECK_EQUAL( topPrefix->ToStdString(), "proj/" );
1235}
1236#endif
1237
1238
1239// WriteBlobToFile materializes a file's committed content, used to feed the diff viewer.
1240BOOST_AUTO_TEST_CASE( WriteBlobToFileExtractsCommittedContent )
1241{
1242 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "hello world\n" );
1243 KIGIT::GitRepositoryPtr repoPtr( repo );
1244
1245 wxString dest = TempPath() + wxFileName::GetPathSeparator() + wxS( "extracted.txt" );
1246
1247 BOOST_CHECK( KIGIT::PROJECT_GIT_UTILS::WriteBlobToFile( repo, wxS( "HEAD" ), wxS( "file.txt" ), dest ) );
1248
1249 wxFile in( dest );
1250 wxString content;
1251 BOOST_REQUIRE( in.IsOpened() );
1252 in.ReadAll( &content );
1253 BOOST_CHECK_EQUAL( content.ToStdString(), "hello world\n" );
1254
1255 // A path that is not in the tree returns false and leaves the destination alone.
1256 BOOST_CHECK( !KIGIT::PROJECT_GIT_UTILS::WriteBlobToFile( repo, wxS( "HEAD" ), wxS( "missing.txt" ), dest ) );
1257 BOOST_CHECK_EQUAL( readWorkingFile( wxFileName( dest ) ), "hello world\n" );
1258}
1259
1260
1261BOOST_AUTO_TEST_CASE( DiffWorkspaceKeepsExtractionsSeparateAndRemovesThem )
1262{
1263 KIGIT::GitRepositoryPtr repo( makeRepoWithCommit( TempPath(), "board.kicad_pcb", "board version\n" ) );
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" );
1268
1269 wxString root;
1270 wxString otherRoot;
1271 wxString boardFile;
1272 wxString schematicFile;
1273
1274 {
1275 GIT_DIFF_WORKSPACE workspace;
1276 BOOST_REQUIRE( workspace.IsOk() );
1277 root = workspace.Root();
1278 boardFile = workspace.Materialize( repo.get(), "HEAD", "board.kicad_pcb", KICAD_DIFF::DOC_KIND::PCB );
1279 BOOST_REQUIRE( !boardFile.IsEmpty() );
1280 BOOST_CHECK_EQUAL( readWorkingFile( wxFileName( boardFile ) ), "board version\n" );
1281
1282 {
1283 GIT_DIFF_WORKSPACE other;
1284 BOOST_REQUIRE( other.IsOk() );
1285 otherRoot = other.Root();
1286 BOOST_CHECK( root != otherRoot );
1287 wxString otherBoard = other.Materialize( repo.get(), "HEAD", "board.kicad_pcb", KICAD_DIFF::DOC_KIND::PCB );
1288 BOOST_REQUIRE( !otherBoard.IsEmpty() );
1289 BOOST_CHECK( boardFile != otherBoard );
1290 BOOST_CHECK_EQUAL( readWorkingFile( wxFileName( otherBoard ) ), "board version\n" );
1291 }
1292
1293 BOOST_CHECK( !wxFileName::DirExists( otherRoot ) );
1294 BOOST_CHECK_EQUAL( readWorkingFile( wxFileName( boardFile ) ), "board version\n" );
1295 BOOST_CHECK( workspace.Materialize( repo.get(), "missing-ref", "board.kicad_pcb", KICAD_DIFF::DOC_KIND::PCB )
1296 .IsEmpty() );
1297
1298 // A schematic extraction includes the child sheets beside its root sheet.
1299 schematicFile =
1300 workspace.Materialize( repo.get(), "HEAD", "schematics/root.kicad_sch", KICAD_DIFF::DOC_KIND::SCH );
1301 BOOST_REQUIRE( !schematicFile.IsEmpty() );
1302 BOOST_CHECK_EQUAL( readWorkingFile( wxFileName( schematicFile ) ), "root sheet\n" );
1303 wxFileName childFile( wxFileName( schematicFile ).GetPath(), wxS( "child.kicad_sch" ) );
1304 BOOST_CHECK_EQUAL( readWorkingFile( childFile ), "child sheet\n" );
1305 }
1306
1307 BOOST_CHECK( !wxFileName::DirExists( root ) );
1308 BOOST_CHECK( !wxFileName::FileExists( boardFile ) );
1309 BOOST_CHECK( !wxFileName::FileExists( schematicFile ) );
1310}
1311
1312
1313// GetTags resolves each tag to the commit it points at.
1314BOOST_AUTO_TEST_CASE( GetTagsResolvesTagToCommit )
1315{
1316 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
1317 KIGIT::GitRepositoryPtr repoPtr( repo );
1318
1319 KIGIT_COMMON emptyCommon( nullptr );
1320 BOOST_CHECK( emptyCommon.GetTags().empty() );
1321
1322 KIGIT_COMMON common( repo );
1323 BOOST_CHECK( common.GetTags().empty() );
1324
1325 git_object* target = nullptr;
1326 BOOST_REQUIRE_EQUAL( git_revparse_single( &target, repo, "HEAD" ), GIT_OK );
1327 KIGIT::GitObjectPtr targetPtr( target );
1328
1329 git_oid tagOid;
1330 BOOST_REQUIRE_EQUAL( git_tag_create_lightweight( &tagOid, repo, "v1.0", target, 0 ), GIT_OK );
1331
1332 std::vector<KIGIT_TAG_INFO> tags = common.GetTags();
1333 BOOST_REQUIRE_EQUAL( tags.size(), 1u );
1334 BOOST_CHECK_EQUAL( tags[0].name.ToStdString(), "v1.0" );
1335 BOOST_CHECK_EQUAL( tags[0].summary.ToStdString(), "initial" );
1336 BOOST_CHECK_EQUAL( tags[0].hash, common.GetRecentCommits( 1 )[0].hash );
1337}
1338
1339
1340// GetStashes lists the repository's stashes.
1341BOOST_AUTO_TEST_CASE( GetStashesListsStash )
1342{
1343 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
1344 KIGIT::GitRepositoryPtr repoPtr( repo );
1345
1346 KIGIT_COMMON common( repo );
1347 BOOST_CHECK( common.GetStashes().empty() );
1348
1349 // Dirty the working tree so there is something to stash.
1350 {
1351 wxFileName fn( TempPath(), wxS( "file.txt" ) );
1352 std::ofstream f( fn.GetFullPath().utf8_string() );
1353 f << "v2\n";
1354 }
1355
1356 git_signature* sig = nullptr;
1357 BOOST_REQUIRE_EQUAL( git_signature_now( &sig, "QA Test", "[email protected]" ), GIT_OK );
1358 KIGIT::GitSignaturePtr sigPtr( sig );
1359
1360 git_oid stashOid;
1361 BOOST_REQUIRE_EQUAL( git_stash_save( &stashOid, repo, sig, "work in progress", GIT_STASH_DEFAULT ), GIT_OK );
1362
1363 std::vector<KIGIT_STASH_INFO> stashes = common.GetStashes();
1364 BOOST_REQUIRE_EQUAL( stashes.size(), 1u );
1365 BOOST_CHECK_EQUAL( stashes[0].index, 0u );
1366 BOOST_CHECK( stashes[0].message.Contains( "work in progress" ) );
1367}
1368
1369
1370// A new stash takes index 0 and pushes the rest down, so anything holding an index from an
1371// earlier listing would act on the wrong stash. The id stays with its own stash.
1372BOOST_AUTO_TEST_CASE( StashIdStaysWithItsStashWhenAnotherIsAdded )
1373{
1374 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
1375 KIGIT::GitRepositoryPtr repoPtr( repo );
1376
1377 KIGIT_COMMON common( repo );
1378
1379 auto dirtyAndStash = [&]( const char* aContent, const wxString& aMessage )
1380 {
1381 wxFileName fn( TempPath(), wxS( "file.txt" ) );
1382
1383 {
1384 std::ofstream f( fn.GetFullPath().utf8_string() );
1385 f << aContent;
1386 }
1387
1388 BOOST_REQUIRE( common.StashSave( aMessage ) );
1389 };
1390
1391 dirtyAndStash( "v2\n", wxS( "first" ) );
1392
1393 std::vector<KIGIT_STASH_INFO> before = common.GetStashes();
1394 BOOST_REQUIRE_EQUAL( before.size(), 1u );
1395
1396 wxString firstId = before[0].id;
1397 BOOST_REQUIRE( !firstId.IsEmpty() );
1398
1399 dirtyAndStash( "v3\n", wxS( "second" ) );
1400
1401 std::vector<KIGIT_STASH_INFO> after = common.GetStashes();
1402 BOOST_REQUIRE_EQUAL( after.size(), 2u );
1403
1404 // Index 0 is now a different stash than it was.
1405 BOOST_CHECK( after[0].id != firstId );
1406
1407 // The first stash is still there, one place further down, under the same id.
1408 BOOST_CHECK_EQUAL( after[1].id.ToStdString(), firstId.ToStdString() );
1409}
1410
1411
1412// StashSave puts the committed content back, and the stash restores the edits afterwards.
1413BOOST_AUTO_TEST_CASE( StashSaveRoundTrip )
1414{
1415 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
1416 KIGIT::GitRepositoryPtr repoPtr( repo );
1417
1418 KIGIT_COMMON common( repo );
1419
1420 writeFile( TempPath(), "file.txt", "v2\n" );
1421 BOOST_REQUIRE( common.StashSave( wxT( "set aside for pull" ) ) );
1422
1423 wxFileName fn( TempPath(), wxS( "file.txt" ) );
1424 BOOST_CHECK_EQUAL( readWorkingFile( fn ), "v1\n" );
1425
1426 std::vector<KIGIT_STASH_INFO> stashes = common.GetStashes();
1427 BOOST_REQUIRE_EQUAL( stashes.size(), 1u );
1428 BOOST_CHECK( stashes[0].message.Contains( "set aside for pull" ) );
1429
1430 BOOST_CHECK( common.StashPop( 0 ) );
1431 BOOST_CHECK_EQUAL( readWorkingFile( fn ), "v2\n" );
1432 BOOST_CHECK( common.GetStashes().empty() );
1433}
1434
1435
1436// CreateBranch and DeleteBranch add and remove a local branch.
1437BOOST_AUTO_TEST_CASE( CreateAndDeleteBranch )
1438{
1439 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
1440 KIGIT::GitRepositoryPtr repoPtr( repo );
1441
1442 KIGIT_COMMON common( repo );
1443
1444 std::vector<KIGIT_BRANCH_INFO> branches = common.GetBranches();
1445 BOOST_REQUIRE_EQUAL( branches.size(), 1u );
1446 wxString original = branches[0].name;
1447
1448 BOOST_CHECK( common.CreateBranch( "feature" ) );
1449 branches = common.GetBranches();
1450 BOOST_REQUIRE_EQUAL( branches.size(), 2u );
1451 BOOST_CHECK( std::any_of( branches.begin(), branches.end(),
1452 []( const KIGIT_BRANCH_INFO& aBranch )
1453 {
1454 return aBranch.name == wxS( "feature" ) && !aBranch.isRemote;
1455 } ) );
1456
1457 BOOST_CHECK( common.DeleteBranch( "feature" ) );
1458 branches = common.GetBranches();
1459 BOOST_REQUIRE_EQUAL( branches.size(), 1u );
1460 BOOST_CHECK_EQUAL( branches[0].name, original );
1461}
1462
1463
1464// CreateTag and DeleteTag add and remove a lightweight tag.
1465BOOST_AUTO_TEST_CASE( CreateAndDeleteTag )
1466{
1467 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
1468 KIGIT::GitRepositoryPtr repoPtr( repo );
1469
1470 KIGIT_COMMON common( repo );
1471
1472 BOOST_CHECK( common.CreateTag( "v1.0" ) );
1473
1474 std::vector<KIGIT_TAG_INFO> tags = common.GetTags();
1475 BOOST_REQUIRE_EQUAL( tags.size(), 1u );
1476 BOOST_CHECK_EQUAL( tags[0].name.ToStdString(), "v1.0" );
1477
1478 BOOST_CHECK( common.DeleteTag( "v1.0" ) );
1479 BOOST_CHECK( common.GetTags().empty() );
1480}
1481
1482
1483// SetUserConfig writes user.name and user.email to the repository config.
1484BOOST_AUTO_TEST_CASE( SetUserConfigPersists )
1485{
1486 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
1487 KIGIT::GitRepositoryPtr repoPtr( repo );
1488
1489 KIGIT_COMMON common( repo );
1490
1491 BOOST_CHECK( common.SetUserConfig( "New Name", "[email protected]" ) );
1492
1493 git_config* config = nullptr;
1494 BOOST_REQUIRE_EQUAL( git_repository_config( &config, repo ), GIT_OK );
1495 KIGIT::GitConfigPtr configPtr( config );
1496
1497 git_config_entry* nameEntry = nullptr;
1498 BOOST_REQUIRE_EQUAL( git_config_get_entry( &nameEntry, config, "user.name" ), GIT_OK );
1499 BOOST_CHECK_EQUAL( wxString::FromUTF8( nameEntry->value ).ToStdString(), "New Name" );
1500 git_config_entry_free( nameEntry );
1501
1502 git_config_entry* emailEntry = nullptr;
1503 BOOST_REQUIRE_EQUAL( git_config_get_entry( &emailEntry, config, "user.email" ), GIT_OK );
1504 BOOST_CHECK_EQUAL( wxString::FromUTF8( emailEntry->value ).ToStdString(), "[email protected]" );
1505 git_config_entry_free( emailEntry );
1506}
1507
1508
1509BOOST_AUTO_TEST_CASE( GitIdentityUsesRepositoryValuesBeforeCustomDefaults )
1510{
1512 KI_TEST::InitGitRepository( TempPath(), "main", "Repository Author", "[email protected]" );
1513 KIGIT_COMMON common( repo.get() );
1514 auto lock = common.LockAction();
1515
1516 git_config* config = nullptr;
1517 BOOST_REQUIRE_EQUAL( git_repository_config( &config, repo.get() ), GIT_OK );
1518 KIGIT::GitConfigPtr configPtr( config );
1519
1520 // Keep the user's global Git identity out of this test.
1521 git_config* local = nullptr;
1522 BOOST_REQUIRE_EQUAL( git_config_open_level( &local, config, GIT_CONFIG_LEVEL_LOCAL ), GIT_OK );
1523 KIGIT::GitConfigPtr localPtr( local );
1524 BOOST_REQUIRE_EQUAL( git_repository_set_config( repo.get(), local ), GIT_OK );
1525
1526 COMMON_SETTINGS::GIT preferences{};
1527 preferences.useDefaultAuthor = false;
1528 preferences.authorName = wxS( "Custom Author" );
1529 preferences.authorEmail = wxS( "[email protected]" );
1530
1531 KIGIT::GIT_IDENTITY identity = KIGIT::ReadGitIdentity( common );
1532 BOOST_CHECK( identity.configured );
1533 GitUserConfig resolved = KIGIT::ResolveGitIdentity( identity, preferences, wxS( "OS User" ) );
1534 BOOST_CHECK_EQUAL( resolved.authorName, wxS( "Repository Author" ) );
1535 BOOST_CHECK_EQUAL( resolved.authorEmail, wxS( "[email protected]" ) );
1536
1537 BOOST_REQUIRE_EQUAL( git_config_delete_entry( local, "user.email" ), GIT_OK );
1538 identity = KIGIT::ReadGitIdentity( common );
1539 BOOST_CHECK( !identity.configured );
1540 resolved = KIGIT::ResolveGitIdentity( identity, preferences, wxS( "OS User" ) );
1541 BOOST_CHECK_EQUAL( resolved.authorName, wxS( "Repository Author" ) );
1542 BOOST_CHECK_EQUAL( resolved.authorEmail, wxS( "[email protected]" ) );
1543
1544 BOOST_REQUIRE_EQUAL( git_config_delete_entry( local, "user.name" ), GIT_OK );
1545 identity = KIGIT::ReadGitIdentity( common );
1546 BOOST_CHECK( !identity.configured );
1547 resolved = KIGIT::ResolveGitIdentity( identity, preferences, wxS( "OS User" ) );
1548 BOOST_CHECK_EQUAL( resolved.authorName, wxS( "Custom Author" ) );
1549 BOOST_CHECK_EQUAL( resolved.authorEmail, wxS( "[email protected]" ) );
1550
1551 // Default mode must not revive custom values left in Preferences.
1552 preferences.useDefaultAuthor = true;
1553 resolved = KIGIT::ResolveGitIdentity( identity, preferences, wxS( "OS User" ) );
1554 BOOST_CHECK_EQUAL( resolved.authorName, wxS( "OS User" ) );
1555 BOOST_CHECK( resolved.authorEmail.IsEmpty() );
1556
1557 preferences.useDefaultAuthor = false;
1558 preferences.authorName.clear();
1559 resolved = KIGIT::ResolveGitIdentity( identity, preferences, wxS( "OS User" ) );
1560 BOOST_CHECK_EQUAL( resolved.authorName, wxS( "OS User" ) );
1561 BOOST_CHECK_EQUAL( resolved.authorEmail, wxS( "[email protected]" ) );
1562}
1563
1564
1565// StashDrop removes a stash from the list.
1566BOOST_AUTO_TEST_CASE( StashDropRemovesStash )
1567{
1568 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
1569 KIGIT::GitRepositoryPtr repoPtr( repo );
1570
1571 KIGIT_COMMON common( repo );
1572
1573 writeFile( TempPath(), "file.txt", "v2\n" );
1574 BOOST_REQUIRE( common.StashSave( wxS( "work in progress" ) ) );
1575
1576 BOOST_REQUIRE_EQUAL( common.GetStashes().size(), 1u );
1577 BOOST_CHECK( common.StashDrop( 0 ) );
1578 BOOST_CHECK( common.GetStashes().empty() );
1579}
1580
1581
1582// Apply leaves the stash in the list. Pop removes it.
1583BOOST_AUTO_TEST_CASE( StashApplyKeepsAndStashPopRemoves )
1584{
1585 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
1586 KIGIT::GitRepositoryPtr repoPtr( repo );
1587
1588 KIGIT_COMMON common( repo );
1589
1590 writeFile( TempPath(), "file.txt", "v2\n" );
1591 BOOST_REQUIRE( common.StashSave( wxS( "wip" ) ) );
1592 BOOST_REQUIRE_EQUAL( common.GetStashes().size(), 1u );
1593
1594 // Apply keeps the stash in the list.
1595 BOOST_CHECK( common.StashApply( 0 ) );
1596 BOOST_CHECK_EQUAL( common.GetStashes().size(), 1u );
1597
1598 // Applying needs a clean tree, so put the applied change away before popping.
1599 wxString error;
1600 BOOST_REQUIRE( common.DiscardChanges( { wxS( "file.txt" ) }, error ) );
1601
1602 // Pop removes it.
1603 BOOST_CHECK( common.StashPop( 0 ) );
1604 BOOST_CHECK( common.GetStashes().empty() );
1605}
1606
1607
1608// libgit2 reports a conflicting apply as success, so pop would drop the only copy of the work.
1609BOOST_AUTO_TEST_CASE( StashPopKeepsStashWhenItConflicts )
1610{
1611 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
1612 KIGIT::GitRepositoryPtr repoPtr( repo );
1613 KIGIT_COMMON common( repo );
1614
1615 wxFileName fn( TempPath(), wxS( "file.txt" ) );
1616
1617 writeFile( TempPath(), "file.txt", "stashed\n" );
1618 BOOST_REQUIRE( common.StashSave( wxS( "wip" ) ) );
1619 BOOST_REQUIRE_EQUAL( common.GetStashes().size(), 1u );
1620
1621 // A different change to the same line, committed, so the stash no longer applies cleanly.
1622 writeFile( TempPath(), "file.txt", "committed\n" );
1623 commitWorkingFile( repo, wxS( "file.txt" ), "conflicting change" );
1624
1625 BOOST_CHECK( !common.StashPop( 0 ) );
1626 BOOST_CHECK_EQUAL( common.GetStashes().size(), 1u );
1627 BOOST_CHECK_EQUAL( readWorkingFile( fn ), "committed\n" );
1628
1629 git_index* index = nullptr;
1630 BOOST_REQUIRE_EQUAL( git_repository_index( &index, repo ), GIT_OK );
1631 KIGIT::GitIndexPtr indexPtr( index );
1632 BOOST_CHECK( !git_index_has_conflicts( index ) );
1633}
1634
1635
1636// Whether the project rebases when getting changes is read with git's own bool parser, so every
1637// spelling git accepts counts.
1638BOOST_AUTO_TEST_CASE( PullRebaseConfigIsReadAsGitReadsIt )
1639{
1640 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
1641 KIGIT::GitRepositoryPtr repoPtr( repo );
1642
1643 KIGIT_COMMON common( repo );
1644
1645 // Unset means merge, as git does.
1647
1648 auto setRebase = [repo]( const char* aValue )
1649 {
1650 git_config* cfg = nullptr;
1651 BOOST_REQUIRE_EQUAL( git_repository_config( &cfg, repo ), GIT_OK );
1652 KIGIT::GitConfigPtr cfgPtr( cfg );
1653 BOOST_REQUIRE_EQUAL( git_config_set_string( cfg, "pull.rebase", aValue ), GIT_OK );
1654 };
1655
1656 for( const char* yes : { "true", "yes", "on", "1" } )
1657 {
1658 setRebase( yes );
1659 BOOST_CHECK_MESSAGE( common.IsPullRebaseConfigured(), std::string( "pull.rebase=" ) + yes );
1660 }
1661
1662 for( const char* no : { "false", "no", "off", "0" } )
1663 {
1664 setRebase( no );
1665 BOOST_CHECK_MESSAGE( !common.IsPullRebaseConfigured(), std::string( "pull.rebase=" ) + no );
1666 }
1667}
1668
1669
1670// A detached HEAD is on no branch, so it has no upstream. That missing upstream must not read
1671// as unpushed work, while a branch without one still does.
1672BOOST_AUTO_TEST_CASE( DetachedHeadHasNoLocalCommits )
1673{
1674 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
1675 KIGIT::GitRepositoryPtr repoPtr( repo );
1676
1677 KIGIT_COMMON common( repo );
1678 BOOST_REQUIRE( common.CreateTag( "v1.0" ) );
1679
1680 // On a branch with no upstream the commit really is unpublished.
1681 BOOST_REQUIRE( !common.IsHeadDetached() );
1682 BOOST_CHECK( common.HasLocalCommits() );
1683 std::optional<KIGIT_UPSTREAM_STATUS> status = common.UpstreamStatus();
1684 BOOST_REQUIRE( status );
1685 BOOST_CHECK( !status->hasUpstream );
1686
1687 BOOST_REQUIRE( common.CheckoutTag( "v1.0" ) );
1688 BOOST_REQUIRE( common.IsHeadDetached() );
1689 BOOST_CHECK( !common.HasLocalCommits() );
1690 BOOST_CHECK( !common.UpstreamStatus() );
1691
1692 std::optional<size_t> count = common.GetUnmergedCommitCount( "main" );
1693 BOOST_REQUIRE( count );
1694 BOOST_CHECK_EQUAL( *count, 0u );
1695}
1696
1697
1698// CheckoutTag puts the working tree at the tagged commit with HEAD detached.
1699BOOST_AUTO_TEST_CASE( CheckoutTagDetachesHead )
1700{
1701 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
1702 KIGIT::GitRepositoryPtr repoPtr( repo );
1703
1704 KIGIT_COMMON common( repo );
1705 BOOST_REQUIRE( common.CreateTag( "v1.0" ) );
1706
1707 git_oid tagged;
1708 BOOST_REQUIRE_EQUAL( git_reference_name_to_id( &tagged, repo, "HEAD" ), GIT_OK );
1709
1710 // A later commit, so the checkout has to move both the tree and HEAD back.
1711 writeFile( TempPath(), "file.txt", "v2\n" );
1712 commitWorkingFile( repo, "file.txt", "second" );
1713
1714 BOOST_CHECK( !common.IsHeadDetached() );
1715 BOOST_CHECK( common.CheckoutTag( "v1.0" ) );
1716 BOOST_CHECK( common.IsHeadDetached() );
1717
1718 git_oid headNow;
1719 BOOST_REQUIRE_EQUAL( git_reference_name_to_id( &headNow, repo, "HEAD" ), GIT_OK );
1720 BOOST_CHECK( git_oid_equal( &headNow, &tagged ) );
1721 BOOST_CHECK_EQUAL( readWorkingFile( wxFileName( TempPath(), "file.txt" ) ), "v1\n" );
1722}
1723
1724
1725BOOST_AUTO_TEST_CASE( UnmergedCommitCountIsWhatHeadDoesNotContain )
1726{
1727 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
1728 KIGIT::GitRepositoryPtr repoPtr( repo );
1729 KIGIT_COMMON common( repo );
1730
1731 BOOST_REQUIRE( common.CreateBranch( "feature" ) );
1732 std::optional<size_t> count = common.GetUnmergedCommitCount( "feature" );
1733 BOOST_REQUIRE( count );
1734 BOOST_CHECK_EQUAL( *count, 0u );
1735
1736 BOOST_REQUIRE_EQUAL( git_repository_set_head( repo, "refs/heads/feature" ), GIT_OK );
1737
1738 wxFileName fn( TempPath(), wxS( "file.txt" ) );
1739 {
1740 std::ofstream f( fn.GetFullPath().utf8_string() );
1741 f << "v2\n";
1742 }
1743
1744 commitWorkingFile( repo, wxS( "file.txt" ), "only on feature" );
1745 BOOST_REQUIRE_EQUAL( git_repository_set_head( repo, "refs/heads/main" ), GIT_OK );
1746
1747 count = common.GetUnmergedCommitCount( "feature" );
1748 BOOST_REQUIRE( count );
1749 BOOST_CHECK_EQUAL( *count, 1u );
1750 count = common.GetUnmergedCommitCount( "main" );
1751 BOOST_REQUIRE( count );
1752 BOOST_CHECK_EQUAL( *count, 0u );
1753 BOOST_CHECK( !common.GetUnmergedCommitCount( "no-such-branch" ) );
1754}
1755
1756
1757// Remotes round-trip without returning embedded user credentials.
1758BOOST_AUTO_TEST_CASE( RemoteRoundTripAndUserinfoStripped )
1759{
1760 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
1761 KIGIT::GitRepositoryPtr repoPtr( repo );
1762
1763 KIGIT_COMMON common( repo );
1764 BOOST_CHECK( common.GetRemotes().empty() );
1765
1766 BOOST_CHECK( common.AddRemote( "origin", "https://example.com/x.git" ) );
1767
1768 std::vector<std::pair<wxString, wxString>> remotes = common.GetRemotes();
1769 BOOST_REQUIRE_EQUAL( remotes.size(), 1u );
1770 BOOST_CHECK_EQUAL( remotes[0].first.ToStdString(), "origin" );
1771 BOOST_CHECK_EQUAL( remotes[0].second.ToStdString(), "https://example.com/x.git" );
1772
1773 BOOST_CHECK( common.SetRemoteUrl( "origin", "https://u:[email protected]/y.git" ) );
1774
1775 // The password must not surface through GetRemotes.
1776 BOOST_CHECK_EQUAL( common.GetRemotes()[0].second.ToStdString(), "https://example.com/y.git" );
1777
1778 BOOST_CHECK( common.RemoveRemote( "origin" ) );
1779 BOOST_CHECK( common.GetRemotes().empty() );
1780}
1781
1782
1783// Branches created at the same HEAD share a timestamp. A std::map keyed on it would drop one.
1784BOOST_AUTO_TEST_CASE( GetBranchesKeepsBranchesSharingACommitTime )
1785{
1786 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
1787 KIGIT::GitRepositoryPtr repoPtr( repo );
1788
1789 KIGIT_COMMON common( repo );
1790 BOOST_REQUIRE( common.CreateBranch( "alpha" ) );
1791 BOOST_REQUIRE( common.CreateBranch( "beta" ) );
1792
1793 std::vector<KIGIT_BRANCH_INFO> branches = common.GetBranches();
1794 BOOST_REQUIRE_EQUAL( branches.size(), 3u );
1795 std::set<wxString> names;
1796
1797 for( const KIGIT_BRANCH_INFO& branch : branches )
1798 {
1799 names.insert( branch.name );
1800 BOOST_CHECK( !branch.isRemote );
1801 BOOST_CHECK( branch.when == branches.front().when );
1802 }
1803
1804 BOOST_CHECK_EQUAL( names.count( wxS( "main" ) ), 1u );
1805 BOOST_CHECK_EQUAL( names.count( wxS( "alpha" ) ), 1u );
1806 BOOST_CHECK_EQUAL( names.count( wxS( "beta" ) ), 1u );
1807}
1808
1809
1810// Branch listings carry the tip commit and identify the current branch.
1811BOOST_AUTO_TEST_CASE( GetBranchesCarriesTipAndCurrent )
1812{
1813 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
1814 KIGIT::GitRepositoryPtr repoPtr( repo );
1815
1816 KIGIT_COMMON common( repo );
1817 BOOST_REQUIRE( common.CreateBranch( "older" ) );
1818
1819 writeFile( TempPath(), "file.txt", "v2\n" );
1820 const char* message = "second\n\nA full commit message.\n";
1821 commitWorkingFile( repo, "file.txt", message );
1822
1823 std::vector<KIGIT_COMMIT_INFO> commits = common.GetRecentCommits( 10 );
1824 BOOST_REQUIRE_EQUAL( commits.size(), 2u );
1825
1826 auto hashOf = [&]( const char* aSummary )
1827 {
1828 for( const KIGIT_COMMIT_INFO& c : commits )
1829 {
1830 if( c.summary == aSummary )
1831 return c.hash.ToStdString();
1832 }
1833
1834 return std::string();
1835 };
1836
1837 std::vector<KIGIT_BRANCH_INFO> branches = common.GetBranches();
1838 BOOST_REQUIRE_EQUAL( branches.size(), 2u );
1839
1840 for( const KIGIT_BRANCH_INFO& branch : branches )
1841 {
1842 BOOST_CHECK( !branch.isRemote );
1843 BOOST_CHECK( branch.when.IsValid() );
1844
1845 if( branch.name == wxS( "main" ) )
1846 {
1847 BOOST_CHECK( branch.isCurrent );
1848 BOOST_CHECK_EQUAL( branch.tipHash.ToStdString(), hashOf( "second" ) );
1849 BOOST_CHECK_EQUAL( branch.message.ToStdString(), message );
1850 }
1851 else
1852 {
1853 BOOST_CHECK_EQUAL( branch.name.ToStdString(), "older" );
1854 BOOST_CHECK( !branch.isCurrent );
1855 BOOST_CHECK_EQUAL( branch.tipHash.ToStdString(), hashOf( "initial" ) );
1856 BOOST_CHECK_EQUAL( branch.message.ToStdString(), "initial" );
1857 }
1858 }
1859}
1860
1861
1862BOOST_AUTO_TEST_CASE( GetBranchesDistinguishesRemoteNamesAndSkipsSymbolicAliases )
1863{
1864 KIGIT::GitRepositoryPtr repo( makeRepoWithCommit( TempPath(), "file.txt", "v1\n" ) );
1865 KIGIT_COMMON common( repo.get() );
1866 BOOST_REQUIRE( common.CreateBranch( "origin/topic" ) );
1867
1868 git_oid headOid;
1869 BOOST_REQUIRE_EQUAL( git_reference_name_to_id( &headOid, repo.get(), "HEAD" ), GIT_OK );
1870 git_reference* remote = nullptr;
1871 BOOST_REQUIRE_EQUAL( git_reference_create( &remote, repo.get(), "refs/remotes/origin/topic", &headOid, 0, nullptr ),
1872 GIT_OK );
1873 KIGIT::GitReferencePtr remotePtr( remote );
1874
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 ),
1878 GIT_OK );
1879 KIGIT::GitReferencePtr aliasPtr( alias );
1880
1881 std::vector<KIGIT_BRANCH_INFO> branches = common.GetBranches();
1882 BOOST_REQUIRE_EQUAL( branches.size(), 3u );
1883 unsigned int localCount = 0;
1884 unsigned int remoteCount = 0;
1885
1886 for( const KIGIT_BRANCH_INFO& branch : branches )
1887 {
1888 if( branch.name == wxS( "origin/topic" ) )
1889 {
1890 if( branch.isRemote )
1891 ++remoteCount;
1892 else
1893 ++localCount;
1894
1895 BOOST_CHECK( !branch.isCurrent );
1896 }
1897 else
1898 {
1899 BOOST_CHECK_EQUAL( branch.name.ToStdString(), "main" );
1900 BOOST_CHECK( branch.isCurrent );
1901 BOOST_CHECK( !branch.isRemote );
1902 }
1903 }
1904
1905 BOOST_CHECK_EQUAL( localCount, 1u );
1906 BOOST_CHECK_EQUAL( remoteCount, 1u );
1907
1908 BOOST_REQUIRE_EQUAL( git_repository_set_head_detached( repo.get(), &headOid ), GIT_OK );
1909 branches = common.GetBranches();
1910 BOOST_REQUIRE_EQUAL( branches.size(), 3u );
1911 BOOST_CHECK( std::none_of( branches.begin(), branches.end(),
1912 []( const KIGIT_BRANCH_INFO& aBranch )
1913 {
1914 return aBranch.isCurrent;
1915 } ) );
1916}
1917
1918
1919BOOST_AUTO_TEST_CASE( SortBranchesForDisplayGroupsBranchesAndBreaksTimeTies )
1920{
1921 const wxDateTime older( 1, wxDateTime::Jan, 2020 );
1922 const wxDateTime newer( 2, wxDateTime::Jan, 2020 );
1923
1924 auto branch = []( const wxString& aName, const wxDateTime& aWhen, bool aRemote = false, bool aCurrent = false )
1925 {
1927 info.name = aName;
1928 info.when = aWhen;
1929 info.isRemote = aRemote;
1930 info.isCurrent = aCurrent;
1931 return info;
1932 };
1933
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 )
1939 };
1940
1942
1943 const std::vector<wxString> expected = { "main", "z-new", "a-tie", "b-tie",
1944 "a-undated", "origin/z-new", "origin/a-old", "origin/unknown" };
1945
1946 BOOST_REQUIRE_EQUAL( branches.size(), expected.size() );
1947
1948 for( size_t i = 0; i < expected.size(); ++i )
1949 BOOST_CHECK_EQUAL( branches[i].name.ToStdString(), expected[i].ToStdString() );
1950}
1951
1952
1953// The detail pane shows a branch's tip commit and how far it is from the branch it tracks.
1954BOOST_AUTO_TEST_CASE( GetBranchDetailReportsTipAndUpstream )
1955{
1956 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
1957 KIGIT::GitRepositoryPtr repoPtr( repo );
1958
1959 KIGIT_COMMON common( repo );
1960 BOOST_REQUIRE( common.CreateBranch( "base" ) );
1961
1962 writeFile( TempPath(), "file.txt", "v2\n" );
1963 commitWorkingFile( repo, "file.txt", "second\n\nWith a body." );
1964
1965 // Track the local branch, which git allows, so no remote is needed to see ahead and behind.
1966 git_reference* main = nullptr;
1967 BOOST_REQUIRE_EQUAL( git_branch_lookup( &main, repo, "main", GIT_BRANCH_LOCAL ), GIT_OK );
1968 KIGIT::GitReferencePtr mainPtr( main );
1969 BOOST_REQUIRE_EQUAL( git_branch_set_upstream( main, "base" ), GIT_OK );
1970
1971 KIGIT_BRANCH_DETAIL detail = common.GetBranchDetail( wxS( "main" ) );
1972 BOOST_REQUIRE( detail.found );
1973 BOOST_CHECK_EQUAL( detail.name.ToStdString(), "main" );
1974 BOOST_CHECK_EQUAL( detail.tipHash.ToStdString(), common.GetRecentCommits( 1 )[0].hash.ToStdString() );
1975 BOOST_CHECK_EQUAL( detail.author.ToStdString(), "QA Test" );
1976 BOOST_CHECK_EQUAL( detail.email.ToStdString(), "[email protected]" );
1977 BOOST_CHECK_EQUAL( detail.message.ToStdString(), "second\n\nWith a body." );
1978 BOOST_CHECK_EQUAL( detail.upstream.ToStdString(), "base" );
1979 BOOST_REQUIRE( detail.divergence );
1980 BOOST_CHECK_EQUAL( detail.divergence->ahead, 1u );
1981 BOOST_CHECK_EQUAL( detail.divergence->behind, 0u );
1982
1983 KIGIT_BRANCH_DETAIL base = common.GetBranchDetail( wxS( "base" ) );
1984 BOOST_REQUIRE( base.found );
1985 BOOST_CHECK( base.upstream.IsEmpty() );
1986 BOOST_CHECK( !base.divergence );
1987 BOOST_CHECK_EQUAL( base.message.ToStdString(), "initial" );
1988
1989 BOOST_CHECK( !common.GetBranchDetail( wxS( "nowhere" ) ).found );
1990}
1991
1992
1993BOOST_AUTO_TEST_CASE( BranchComparisonsUseTheSelectedBranchAndItsOwnUpstream )
1994{
1995 KIGIT::GitRepositoryPtr repo( makeRepoWithCommit( TempPath(), "file.txt", "initial\n" ) );
1996 KIGIT_COMMON common( repo.get() );
1997 BOOST_REQUIRE( common.CreateBranch( "selected" ) );
1998 BOOST_REQUIRE( common.CreateBranch( "main-upstream" ) );
1999
2000 auto commitOn = [&]( const char* aBranch, const char* aMessage )
2001 {
2002 wxString ref = wxS( "refs/heads/" ) + wxString::FromUTF8( aBranch );
2003 BOOST_REQUIRE_EQUAL( git_repository_set_head( repo.get(), ref.utf8_str() ), GIT_OK );
2004 writeFile( TempPath(), "file.txt", aMessage );
2005 commitWorkingFile( repo.get(), "file.txt", aMessage );
2006 };
2007
2008 commitOn( "selected", "selected one" );
2009 BOOST_REQUIRE( common.CreateBranch( "selected-upstream" ) );
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" );
2015 BOOST_REQUIRE_EQUAL( git_repository_set_head( repo.get(), "refs/heads/main" ), GIT_OK );
2016
2017 auto setUpstream = [&]( const char* aBranch, const char* aUpstream )
2018 {
2019 git_reference* ref = nullptr;
2020 BOOST_REQUIRE_EQUAL( git_branch_lookup( &ref, repo.get(), aBranch, GIT_BRANCH_LOCAL ), GIT_OK );
2021 KIGIT::GitReferencePtr refPtr( ref );
2022 BOOST_REQUIRE_EQUAL( git_branch_set_upstream( ref, aUpstream ), GIT_OK );
2023 };
2024
2025 setUpstream( "main", "main-upstream" );
2026 setUpstream( "selected", "selected-upstream" );
2027
2028 // The selected branch and its upstream each have one new commit.
2029 KIGIT_BRANCH_DETAIL selected = common.GetBranchDetail( "selected" );
2030 BOOST_REQUIRE( selected.found );
2031 BOOST_CHECK_EQUAL( selected.upstream.ToStdString(), "selected-upstream" );
2032 BOOST_REQUIRE( selected.divergence );
2033 BOOST_CHECK_EQUAL( selected.divergence->ahead, 1u );
2034 BOOST_CHECK_EQUAL( selected.divergence->behind, 1u );
2035
2036 // Both selected branch commits are missing from HEAD.
2037 std::optional<size_t> count = common.GetUnmergedCommitCount( "selected" );
2038 BOOST_REQUIRE( count );
2039 BOOST_CHECK_EQUAL( *count, 2u );
2040
2041 KIGIT_BRANCH_DETAIL main = common.GetBranchDetail( "main" );
2042 BOOST_REQUIRE( main.divergence );
2043 BOOST_CHECK_EQUAL( main.divergence->ahead, 0u );
2044 BOOST_CHECK_EQUAL( main.divergence->behind, 3u );
2045 BOOST_CHECK( !common.HasLocalCommits() );
2046 std::optional<KIGIT_UPSTREAM_STATUS> status = common.UpstreamStatus();
2047 BOOST_REQUIRE( status );
2048 BOOST_CHECK_EQUAL( status->divergence.behind, 3u );
2049
2050 commitOn( "main", "local main one" );
2051 main = common.GetBranchDetail( "main" );
2052 BOOST_REQUIRE( main.divergence );
2053 BOOST_CHECK_EQUAL( main.divergence->ahead, 1u );
2054 BOOST_CHECK_EQUAL( main.divergence->behind, 3u );
2055 BOOST_CHECK( common.HasLocalCommits() );
2056 status = common.UpstreamStatus();
2057 BOOST_REQUIRE( status );
2058 BOOST_CHECK_EQUAL( status->divergence.ahead, 1u );
2059 BOOST_CHECK_EQUAL( status->divergence.behind, 3u );
2060}
2061
2062
2063BOOST_AUTO_TEST_CASE( BranchComparisonsStayUnknownWhenReferencesCannotBeRead )
2064{
2065 KIGIT_COMMON unavailable( nullptr );
2066 BOOST_CHECK( !unavailable.HasLocalCommits() );
2067 BOOST_CHECK( !unavailable.UpstreamStatus() );
2068 BOOST_CHECK( !unavailable.GetUnmergedCommitCount( "main" ) );
2069 BOOST_CHECK( !unavailable.GetBranchDetail( "main" ).divergence );
2070
2071 KIGIT::GitRepositoryPtr repo( makeRepoWithCommit( TempPath(), "file.txt", "initial\n" ) );
2072 KIGIT_COMMON common( repo.get() );
2073
2074 // The reference exists, but its commit is missing.
2075 writeFile( TempPath(), ".git/refs/heads/broken", "1111111111111111111111111111111111111111\n" );
2076 git_config* config = nullptr;
2077 BOOST_REQUIRE_EQUAL( git_repository_config( &config, repo.get() ), GIT_OK );
2078 KIGIT::GitConfigPtr configPtr( config );
2079 BOOST_REQUIRE_EQUAL( git_config_set_string( config, "branch.main.remote", "." ), GIT_OK );
2080 BOOST_REQUIRE_EQUAL( git_config_set_string( config, "branch.main.merge", "refs/heads/broken" ), GIT_OK );
2081
2082 KIGIT_BRANCH_DETAIL detail = common.GetBranchDetail( "main" );
2083 BOOST_REQUIRE( detail.found );
2084 BOOST_CHECK_EQUAL( detail.upstream.ToStdString(), "broken" );
2085 BOOST_CHECK( !detail.divergence );
2086 BOOST_CHECK( !common.HasLocalCommits() );
2087 BOOST_CHECK( !common.UpstreamStatus() );
2088 BOOST_CHECK( !common.GetUnmergedCommitCount( "broken" ) );
2089}
2090
2091
2092// New Branch in the detail pane starts the branch where the selected one is, not at HEAD.
2093BOOST_AUTO_TEST_CASE( CreateBranchStartsAtTheGivenBranch )
2094{
2095 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
2096 KIGIT::GitRepositoryPtr repoPtr( repo );
2097
2098 KIGIT_COMMON common( repo );
2099 BOOST_REQUIRE( common.CreateBranch( "base" ) );
2100
2101 writeFile( TempPath(), "file.txt", "v2\n" );
2102 commitWorkingFile( repo, "file.txt", "second" );
2103
2104 BOOST_REQUIRE( common.CreateBranch( "feature", "base" ) );
2105
2106 KIGIT_BRANCH_DETAIL feature = common.GetBranchDetail( wxS( "feature" ) );
2107 KIGIT_BRANCH_DETAIL base = common.GetBranchDetail( wxS( "base" ) );
2108 BOOST_REQUIRE( feature.found && base.found );
2109 BOOST_CHECK_EQUAL( feature.tipHash.ToStdString(), base.tipHash.ToStdString() );
2110
2111 BOOST_CHECK( !common.CreateBranch( "orphan", "nowhere" ) );
2112}
2113
2114
2115// The Remotes page shows when the repository last fetched. Git leaves that in FETCH_HEAD, which
2116// a repository that never fetched does not have.
2117BOOST_AUTO_TEST_CASE( GetLastFetchTimeReadsFetchHead )
2118{
2119 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
2120 KIGIT::GitRepositoryPtr repoPtr( repo );
2121
2122 KIGIT_COMMON common( repo );
2123 BOOST_CHECK( !common.GetLastFetchTime().IsValid() );
2124
2125 wxFileName fetchHead( wxString::FromUTF8( git_repository_path( repo ) ), wxS( "FETCH_HEAD" ) );
2126
2127 {
2128 std::ofstream f( fetchHead.GetFullPath().utf8_string() );
2129 f << "0000000000000000000000000000000000000000\t\tbranch 'main' of https://example.com/x.git\n";
2130 }
2131
2132 // The time is the file's own, so set it to something that is not now.
2133 wxDateTime stamp( 1, wxDateTime::Jan, 2020, 12, 0, 0 );
2134 BOOST_REQUIRE( fetchHead.SetTimes( nullptr, &stamp, nullptr ) );
2135
2136 wxDateTime when = common.GetLastFetchTime();
2137 BOOST_REQUIRE( when.IsValid() );
2138 BOOST_CHECK( when.IsEqualTo( stamp ) );
2139}
2140
2141
2142// The Changes page sums the lines the working tree adds and removes, a new file counting as
2143// all added.
2144BOOST_AUTO_TEST_CASE( GetWorkingTreeLineStatsCountsEditsAndNewFiles )
2145{
2146 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "one\ntwo\nthree\n" );
2147 KIGIT::GitRepositoryPtr repoPtr( repo );
2148
2149 KIGIT_COMMON common( repo );
2150
2151 std::pair<int, int> clean = common.GetWorkingTreeLineStats( { wxS( "file.txt" ) } );
2152 BOOST_CHECK_EQUAL( clean.first, 0 );
2153 BOOST_CHECK_EQUAL( clean.second, 0 );
2154
2155 writeFile( TempPath(), "file.txt", "one\n2\nthree\n" );
2156 writeFile( TempPath(), "new.txt", "a\nb\n" );
2157
2158 std::pair<int, int> dirty = common.GetWorkingTreeLineStats( { wxS( "file.txt" ), wxS( "new.txt" ) } );
2159 BOOST_CHECK_EQUAL( dirty.first, 3 );
2160 BOOST_CHECK_EQUAL( dirty.second, 1 );
2161}
2162
2163
2164// The detail pane shows what a stash holds: the branch and commit it was taken on, its message
2165// and the files it changes, a new file included.
2166BOOST_AUTO_TEST_CASE( GetStashDetailNamesBranchCommitAndFiles )
2167{
2168 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
2169 KIGIT::GitRepositoryPtr repoPtr( repo );
2170
2171 KIGIT_COMMON common( repo );
2172
2173 std::vector<KIGIT_COMMIT_INFO> commits = common.GetRecentCommits( 1 );
2174 BOOST_REQUIRE_EQUAL( commits.size(), 1u );
2175
2176 writeFile( TempPath(), "file.txt", "v2\n" );
2177 writeFile( TempPath(), "new.txt", "n\n" );
2178
2179 {
2180 git_index* index = nullptr;
2181 BOOST_REQUIRE_EQUAL( git_repository_index( &index, repo ), GIT_OK );
2182 KIGIT::GitIndexPtr indexPtr( index );
2183 BOOST_REQUIRE_EQUAL( git_index_add_bypath( index, "new.txt" ), GIT_OK );
2184 BOOST_REQUIRE_EQUAL( git_index_write( index ), GIT_OK );
2185 }
2186
2187 BOOST_REQUIRE( common.StashSave( wxS( "connector work" ) ) );
2188
2189 std::vector<KIGIT_STASH_INFO> stashes = common.GetStashes();
2190 BOOST_REQUIRE_EQUAL( stashes.size(), 1u );
2191
2192 KIGIT_STASH_DETAIL detail = common.GetStashDetail( stashes[0].id );
2193 BOOST_REQUIRE( detail.found );
2194 BOOST_CHECK_EQUAL( detail.message.ToStdString(), "connector work" );
2195 BOOST_CHECK_EQUAL( detail.branch.ToStdString(), "main" );
2196 BOOST_CHECK_EQUAL( detail.baseHash.ToStdString(), commits[0].hash.ToStdString() );
2197 BOOST_CHECK( detail.when.IsValid() );
2198
2199 std::vector<std::string> files;
2200
2201 for( const wxString& f : detail.files )
2202 files.push_back( f.ToStdString() );
2203
2204 std::sort( files.begin(), files.end() );
2205 BOOST_REQUIRE_EQUAL( files.size(), 2u );
2206 BOOST_CHECK_EQUAL( files[0], "file.txt" );
2207 BOOST_CHECK_EQUAL( files[1], "new.txt" );
2208
2209 BOOST_CHECK( !common.GetStashDetail( wxS( "0000000000000000000000000000000000000000" ) ).found );
2210}
2211
2212
2213// The History page can be narrowed to one file or folder. A commit stays listed when the tree
2214// entry at that path differs from its parent's, so a folder catches changes anywhere under it.
2215BOOST_AUTO_TEST_CASE( GetRecentCommitsFiltersByPath )
2216{
2217 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
2218 KIGIT::GitRepositoryPtr repoPtr( repo );
2219
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" );
2227
2228 KIGIT_COMMON common( repo );
2230
2231 filter.path = wxS( "file.txt" );
2232 std::vector<KIGIT_COMMIT_INFO> commits = common.GetRecentCommits( 50, filter );
2233 BOOST_REQUIRE_EQUAL( commits.size(), 2 );
2234 BOOST_CHECK_EQUAL( commits[0].summary, wxS( "edit file" ) );
2235 BOOST_CHECK_EQUAL( commits[1].summary, wxS( "initial" ) );
2236
2237 filter.path = wxS( "sub" );
2238 commits = common.GetRecentCommits( 50, filter );
2239 BOOST_REQUIRE_EQUAL( commits.size(), 2 );
2240 BOOST_CHECK_EQUAL( commits[0].summary, wxS( "add b" ) );
2241 BOOST_CHECK_EQUAL( commits[1].summary, wxS( "add a" ) );
2242
2243 filter.path = wxS( "sub/a.txt" );
2244 BOOST_CHECK_EQUAL( common.GetRecentCommits( 50, filter ).size(), 1 );
2245
2246 filter.path = wxS( "nowhere.txt" );
2247 BOOST_CHECK( common.GetRecentCommits( 50, filter ).empty() );
2248
2249 // The search still applies on top of the path.
2250 filter.path = wxS( "sub" );
2251 filter.search = wxS( "add b" );
2252 BOOST_CHECK_EQUAL( common.GetRecentCommits( 50, filter ).size(), 1 );
2253}
2254
2255
2256// The Tags page shows a tag's message, author and date. An annotated tag has its own, a
2257// lightweight tag borrows the commit's.
2258// libgit2 runs no filter program, so a file git routes through LFS has to be recognised before
2259// an operation writes the placeholder text over it.
2260BOOST_AUTO_TEST_CASE( ProjectUsesGitLfsFindsAFilteredFile )
2261{
2263
2264
2265 git_repository* repo = makeRepoWithCommit( TempPath(), "board.kicad_pcb", "(kicad_pcb)\n" );
2266 KIGIT::GitRepositoryPtr repoPtr( repo );
2267 KIGIT_COMMON common( repo, TempPath() );
2268
2269 BOOST_CHECK( !PROJECT_GIT_UTILS::ProjectUsesGitLfs( common ) );
2270
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" );
2275
2276 BOOST_CHECK( PROJECT_GIT_UTILS::PathUsesGitLfs( repo, wxS( "case.step" ) ) );
2277 BOOST_CHECK( !PROJECT_GIT_UTILS::PathUsesGitLfs( repo, wxS( "board.kicad_pcb" ) ) );
2278
2279 wxString example;
2280 BOOST_CHECK( PROJECT_GIT_UTILS::ProjectUsesGitLfs( common, &example ) );
2281 BOOST_CHECK_EQUAL( example.ToStdString(), "case.step" );
2282
2283 // A project folder that holds none of the filtered files is left alone.
2284 const wxString elsewhere = TempPath() + wxS( "/elsewhere" );
2285 BOOST_REQUIRE( wxFileName::Mkdir( elsewhere ) );
2286 KIGIT_COMMON otherProject( repo, elsewhere );
2287 BOOST_CHECK( !PROJECT_GIT_UTILS::ProjectUsesGitLfs( otherProject ) );
2288
2289 std::optional<wxString> prefix = PROJECT_GIT_UTILS::ProjectPrefix( wxS( "/repo" ), wxS( "/repo/proj" ) );
2290 BOOST_REQUIRE( prefix );
2291 BOOST_CHECK_EQUAL( prefix->ToStdString(), "proj/" );
2292 prefix = PROJECT_GIT_UTILS::ProjectPrefix( wxS( "/repo" ), wxS( "/repo" ) );
2293 BOOST_REQUIRE( prefix );
2294 BOOST_CHECK( prefix->IsEmpty() );
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 ) );
2298
2299#ifdef __WINDOWS__
2300 // The working directory comes from libgit2 and the project path from KiCad, so the two
2301 // can disagree about separators and about case. Windows does not care about either.
2302 prefix = PROJECT_GIT_UTILS::ProjectPrefix( wxS( "C:\\repo" ), wxS( "C:/repo/proj" ) );
2303 BOOST_REQUIRE( prefix );
2304 BOOST_CHECK_EQUAL( prefix->ToStdString(), "proj/" );
2305 prefix = PROJECT_GIT_UTILS::ProjectPrefix( wxS( "c:\\Repo\\" ), wxS( "C:\\repo\\Proj" ) );
2306 BOOST_REQUIRE( prefix );
2307 BOOST_CHECK_EQUAL( prefix->ToStdString(), "Proj/" );
2308 BOOST_CHECK( !PROJECT_GIT_UTILS::ProjectPrefix( wxS( "C:\\repo" ), wxS( "D:\\repo\\proj" ) ) );
2309#endif
2310}
2311
2312
2313BOOST_AUTO_TEST_CASE( ProjectUsesGitLfsStaysWithinTheSessionProject )
2314{
2316
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" );
2324
2325 KIGIT_COMMON common( repo.get(), TempPath() + wxS( "/projects/board" ) );
2326 wxString example;
2327 BOOST_CHECK( !PROJECT_GIT_UTILS::ProjectUsesGitLfs( common, &example ) );
2328 BOOST_CHECK( PROJECT_GIT_UTILS::LfsRefusal( &common ).IsEmpty() );
2329
2330 KIGIT_COMMON unknownProject( repo.get() );
2331 BOOST_CHECK( !PROJECT_GIT_UTILS::LfsRefusal( &unknownProject ).IsEmpty() );
2332
2333 writeFile( TempPath(), "projects/board/models/case.step", "project enclosure\n" );
2334 commitWorkingFile( repo.get(), "projects/board/models/case.step", "add project enclosure" );
2335
2336 BOOST_CHECK( PROJECT_GIT_UTILS::ProjectUsesGitLfs( common, &example ) );
2337 BOOST_CHECK_EQUAL( example.ToStdString(), "projects/board/models/case.step" );
2338 BOOST_CHECK( PROJECT_GIT_UTILS::LfsRefusal( &common ).Contains( example ) );
2339}
2340
2341
2342// An amend or a rebase leaves a commit's author time behind its committer time. The History
2343// page dates a commit by the committer time, so the Tags page has to agree.
2344BOOST_AUTO_TEST_CASE( GetTagDetailDatesALightweightTagByCommitterTime )
2345{
2346 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
2347 KIGIT::GitRepositoryPtr repoPtr( repo );
2348
2349 git_signature* authored = nullptr;
2350 BOOST_REQUIRE_EQUAL( git_signature_new( &authored, "QA Test", "[email protected]", 1000, 0 ), GIT_OK );
2351 KIGIT::GitSignaturePtr authoredPtr( authored );
2352
2353 git_signature* committed = nullptr;
2354 BOOST_REQUIRE_EQUAL( git_signature_new( &committed, "QA Test", "[email protected]", 2000, 0 ), GIT_OK );
2355 KIGIT::GitSignaturePtr committedPtr( committed );
2356
2357 git_oid headOid;
2358 BOOST_REQUIRE_EQUAL( git_reference_name_to_id( &headOid, repo, "HEAD" ), GIT_OK );
2359
2360 git_commit* parent = nullptr;
2361 BOOST_REQUIRE_EQUAL( git_commit_lookup( &parent, repo, &headOid ), GIT_OK );
2362 KIGIT::GitCommitPtr parentPtr( parent );
2363
2364 git_tree* tree = nullptr;
2365 BOOST_REQUIRE_EQUAL( git_commit_tree( &tree, parent ), GIT_OK );
2366 KIGIT::GitTreePtr tree_ptr( tree );
2367
2368 const git_commit* parents[1] = { parent };
2369 git_oid commitOid;
2371 git_commit_create( &commitOid, repo, "HEAD", authored, committed, nullptr, "rebased", tree, 1, parents ),
2372 GIT_OK );
2373
2374 git_object* target = nullptr;
2375 BOOST_REQUIRE_EQUAL( git_revparse_single( &target, repo, "HEAD" ), GIT_OK );
2376 KIGIT::GitObjectPtr targetPtr( target );
2377
2378 git_oid tagOid;
2379 BOOST_REQUIRE_EQUAL( git_tag_create_lightweight( &tagOid, repo, "rebased", target, 0 ), GIT_OK );
2380
2381 KIGIT_COMMON common( repo );
2382 KIGIT_TAG_DETAIL detail = common.GetTagDetail( wxS( "rebased" ) );
2383
2384 BOOST_REQUIRE( detail.found );
2385 BOOST_CHECK_EQUAL( detail.when.GetTicks(), 2000 );
2386 BOOST_CHECK_EQUAL( common.GetRecentCommits( 1 )[0].when.GetTicks(), detail.when.GetTicks() );
2387}
2388
2389
2390BOOST_AUTO_TEST_CASE( GetTagDetailReadsAnnotatedAndLightweightTags )
2391{
2392 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
2393 KIGIT::GitRepositoryPtr repoPtr( repo );
2394
2395 KIGIT_COMMON common( repo );
2396 wxString head = common.GetRecentCommits( 1 )[0].hash;
2397
2398 // A different tagger from the commit author, so the detail must come from the tag object.
2399 BOOST_REQUIRE( common.SetUserConfig( wxS( "Tag Maker" ), wxS( "[email protected]" ) ) );
2400 BOOST_REQUIRE( common.CreateTag( "v1.0", "Release v1.0\n\n- Add mounting holes" ) );
2401 BOOST_REQUIRE( common.CreateTag( "beta-1" ) );
2402
2403 git_object* tagObject = nullptr;
2404 BOOST_REQUIRE_EQUAL( git_revparse_single( &tagObject, repo, "refs/tags/v1.0" ), GIT_OK );
2405 KIGIT::GitObjectPtr tagObjectPtr( tagObject );
2406 BOOST_CHECK_EQUAL( git_object_type( tagObject ), GIT_OBJECT_TAG );
2407
2408 KIGIT_TAG_DETAIL annotated = common.GetTagDetail( wxS( "v1.0" ) );
2409 BOOST_REQUIRE( annotated.found );
2410 BOOST_CHECK_EQUAL( annotated.name.ToStdString(), "v1.0" );
2411 BOOST_CHECK_EQUAL( annotated.hash.ToStdString(), head.ToStdString() );
2412 BOOST_CHECK_EQUAL( annotated.message.ToStdString(), "Release v1.0\n\n- Add mounting holes" );
2413 BOOST_CHECK_EQUAL( annotated.author.ToStdString(), "Tag Maker" );
2414 BOOST_CHECK_EQUAL( annotated.email.ToStdString(), "[email protected]" );
2415 BOOST_CHECK( annotated.when.IsValid() );
2416
2417 KIGIT_TAG_DETAIL lightweight = common.GetTagDetail( wxS( "beta-1" ) );
2418 BOOST_REQUIRE( lightweight.found );
2419 BOOST_CHECK_EQUAL( lightweight.name.ToStdString(), "beta-1" );
2420 BOOST_CHECK_EQUAL( lightweight.hash.ToStdString(), head.ToStdString() );
2421 BOOST_CHECK_EQUAL( lightweight.message.ToStdString(), "initial" );
2422 BOOST_CHECK_EQUAL( lightweight.author.ToStdString(), "QA Test" );
2423 BOOST_CHECK( lightweight.when.IsValid() );
2424
2425 BOOST_CHECK( !common.GetTagDetail( wxS( "nowhere" ) ).found );
2426}
2427
2428
2429// The Tags page lists newest first. A tag on a tree has no commit and so no date, and goes last.
2430BOOST_AUTO_TEST_CASE( GetTagsListsNewestFirstAndKeepsTagsWithoutCommits )
2431{
2432 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
2433 KIGIT::GitRepositoryPtr repoPtr( repo );
2434
2435 KIGIT_COMMON common( repo );
2436 BOOST_REQUIRE( common.CreateTag( "v1.0", "Release" ) );
2437 BOOST_REQUIRE( common.CreateTag( "beta-1" ) );
2438
2439 // An old tag made long ago, and a tag on a tree.
2440 git_object* commitObject = nullptr;
2441 BOOST_REQUIRE_EQUAL( git_revparse_single( &commitObject, repo, "HEAD" ), GIT_OK );
2442 KIGIT::GitObjectPtr commitObjectPtr( commitObject );
2443
2444 git_signature* oldSig = nullptr;
2445 BOOST_REQUIRE_EQUAL( git_signature_new( &oldSig, "QA Test", "[email protected]", 1000, 0 ), GIT_OK );
2446 KIGIT::GitSignaturePtr oldSigPtr( oldSig );
2447
2448 git_oid oid;
2449 BOOST_REQUIRE_EQUAL( git_tag_create( &oid, repo, "old", commitObject, oldSig, "old tag", 0 ), GIT_OK );
2450
2451 git_object* treeObject = nullptr;
2452 BOOST_REQUIRE_EQUAL( git_revparse_single( &treeObject, repo, "HEAD^{tree}" ), GIT_OK );
2453 KIGIT::GitObjectPtr treeObjectPtr( treeObject );
2454 BOOST_REQUIRE_EQUAL( git_tag_create_lightweight( &oid, repo, "tree-tag", treeObject, 0 ), GIT_OK );
2455
2456 // The two tags made just now share a second, so their order between them is not fixed.
2457 std::vector<KIGIT_TAG_INFO> tags = common.GetTags();
2458 BOOST_REQUIRE_EQUAL( tags.size(), 4 );
2459 std::set<wxString> newest{ tags[0].name, tags[1].name };
2460 std::set<wxString> madeNow{ wxS( "v1.0" ), wxS( "beta-1" ) };
2461 BOOST_CHECK( newest == madeNow );
2462 BOOST_CHECK_EQUAL( tags[2].name.ToStdString(), "old" );
2463 BOOST_CHECK_EQUAL( tags[3].name.ToStdString(), "tree-tag" );
2464 BOOST_CHECK( tags[3].hash.IsEmpty() && !tags[3].when.IsValid() );
2465}
2466
2467
2468// The docked panel's Recent messages dropdown offers earlier messages to start from. Newest
2469// first, a message that was used twice listed once, and only what HEAD can reach.
2470BOOST_AUTO_TEST_CASE( GetRecentCommitMessagesListsDistinctMessagesNewestFirst )
2471{
2472 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
2473 KIGIT::GitRepositoryPtr repoPtr( repo );
2474
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" );
2483
2484 KIGIT_COMMON common( repo );
2485 std::vector<wxString> messages = common.GetRecentCommitMessages( 10 );
2486
2487 // The blank message is not offered.
2488 BOOST_REQUIRE_EQUAL( messages.size(), 3 );
2489 BOOST_CHECK_EQUAL( messages[0], wxS( "Update BOM\n\nSecond pass." ) );
2490 BOOST_CHECK_EQUAL( messages[1], wxS( "Route power" ) );
2491 BOOST_CHECK_EQUAL( messages[2], wxS( "initial" ) );
2492
2493 BOOST_CHECK_EQUAL( common.GetRecentCommitMessages( 1 ).size(), 1 );
2494 BOOST_CHECK( KIGIT_COMMON( nullptr ).GetRecentCommitMessages( 10 ).empty() );
2495
2496 // A repository with no commit yet has nothing to offer either.
2497 KI_TEST::SCOPED_TEMP_DIR empty( wxS( "kicad-qa-git-empty" ) );
2498 git_repository* unborn = nullptr;
2499 BOOST_REQUIRE_EQUAL( git_repository_init( &unborn, empty.PathStr().utf8_string().c_str(), 0 ), GIT_OK );
2500 KIGIT::GitRepositoryPtr unbornPtr( unborn );
2501 BOOST_CHECK( KIGIT_COMMON( unborn ).GetRecentCommitMessages( 10 ).empty() );
2502}
2503
2504
2505// The docked panel composes the message from a summary line, a description box and a sign off
2506// tick, and takes an amended commit's message apart again to refill those boxes.
2507BOOST_AUTO_TEST_CASE( ComposeAndSplitCommitMessage )
2508{
2509 wxString signOff = wxS( "QA Test <[email protected]>" );
2510
2511 BOOST_CHECK_EQUAL( KIGIT::ComposeCommitMessage( wxS( " Fix the thing " ), wxEmptyString, wxEmptyString ),
2512 wxS( "Fix the thing" ) );
2513 BOOST_CHECK_EQUAL( KIGIT::ComposeCommitMessage( wxS( "Fix" ), wxS( "Why it broke.\n" ), wxEmptyString ),
2514 wxS( "Fix\n\nWhy it broke." ) );
2515 BOOST_CHECK_EQUAL( KIGIT::ComposeCommitMessage( wxS( "Fix" ), wxEmptyString, signOff ),
2516 wxS( "Fix\n\nSigned-off-by: QA Test <[email protected]>" ) );
2517 BOOST_CHECK_EQUAL( KIGIT::ComposeCommitMessage( wxS( "Fix" ), wxS( "Why." ), signOff ),
2518 wxS( "Fix\n\nWhy.\n\nSigned-off-by: QA Test <[email protected]>" ) );
2519
2520 // An amended message already carries its sign off, even with a trailer after it, and
2521 // someone else's sign off is not ours.
2522 wxString signedOff = wxS( "Why.\n\nSigned-off-by: QA Test <[email protected]>" );
2523 wxString coauthor = wxS( "\nCo-authored-by: X <[email protected]>" );
2524 wxString other = wxS( "Signed-off-by: Other <[email protected]>" );
2525
2526 BOOST_CHECK_EQUAL( KIGIT::ComposeCommitMessage( wxS( "Fix" ), signedOff, signOff ), wxS( "Fix\n\n" ) + signedOff );
2527 BOOST_CHECK_EQUAL( KIGIT::ComposeCommitMessage( wxS( "Fix" ), signedOff + wxS( "\n" ), signOff ),
2528 wxS( "Fix\n\n" ) + signedOff );
2529 BOOST_CHECK_EQUAL( KIGIT::ComposeCommitMessage( wxS( "Fix" ), signedOff + coauthor, signOff ),
2530 wxS( "Fix\n\n" ) + signedOff + coauthor );
2531 BOOST_CHECK_EQUAL( KIGIT::ComposeCommitMessage( wxS( "Fix" ), other, signOff ),
2532 wxS( "Fix\n\n" ) + other + wxS( "\n\nSigned-off-by: QA Test <[email protected]>" ) );
2533
2534 wxString summary, description;
2535
2536 KIGIT::SplitCommitMessage( wxS( "Fix\n\nWhy it broke.\nAnd how.\n" ), summary, description );
2537 BOOST_CHECK_EQUAL( summary, wxS( "Fix" ) );
2538 BOOST_CHECK_EQUAL( description, wxS( "Why it broke.\nAnd how." ) );
2539
2540 KIGIT::SplitCommitMessage( wxS( "Just a line\n" ), summary, description );
2541 BOOST_CHECK_EQUAL( summary, wxS( "Just a line" ) );
2542 BOOST_CHECK( description.IsEmpty() );
2543
2544 // A two line message with no blank line keeps its second line as the description.
2545 KIGIT::SplitCommitMessage( wxS( "First\nSecond" ), summary, description );
2546 BOOST_CHECK_EQUAL( summary, wxS( "First" ) );
2547 BOOST_CHECK_EQUAL( description, wxS( "Second" ) );
2548
2549 // A message read back from a Windows checkout can carry carriage returns.
2550 KIGIT::SplitCommitMessage( wxS( "Fix\r\n\r\nWhy.\r\n" ), summary, description );
2551 BOOST_CHECK_EQUAL( summary, wxS( "Fix" ) );
2552 BOOST_CHECK_EQUAL( description, wxS( "Why." ) );
2553}
2554
2555
2556// Committing to a new branch creates it at HEAD and switches before the commit. The switch
2557// must leave the uncommitted work in place, since the two branches share the same tree.
2558BOOST_AUTO_TEST_CASE( SwitchToBranchAtHeadKeepsUncommittedChanges )
2559{
2560 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
2561 KIGIT::GitRepositoryPtr repoPtr( repo );
2562
2563 writeFile( TempPath(), "file.txt", "v2\n" );
2564
2565 KIGIT_COMMON common( repo );
2566 BOOST_REQUIRE( common.CreateBranch( "topic" ) );
2567
2568 GIT_BRANCH_HANDLER handler( &common );
2569 BOOST_REQUIRE_MESSAGE( handler.SwitchToBranch( wxS( "topic" ) ) == BranchResult::Success,
2570 handler.GetErrorString() );
2571
2572 BOOST_CHECK_EQUAL( common.GetCurrentBranchName(), wxS( "topic" ) );
2573 BOOST_CHECK_EQUAL( readWorkingFile( wxFileName( TempPath(), "file.txt" ) ), "v2\n" );
2575}
2576
2577
2578// The stored connection string must not carry the secret. A scp-style git@host is left intact.
2579BOOST_AUTO_TEST_CASE( SetRemoteStripsUrlUserinfo )
2580{
2581 KIGIT_COMMON common( nullptr );
2582
2583 common.SetRemote( wxS( "https://user:[email protected]/x/y.git" ) );
2584 BOOST_CHECK_EQUAL( common.GetRemote().ToStdString(), "https://gitlab.com/x/y.git" );
2585
2586 common.SetRemote( wxS( "[email protected]:x/y.git" ) );
2587 BOOST_CHECK_EQUAL( common.GetRemote().ToStdString(), "[email protected]:x/y.git" );
2588}
2589
2590
2591// A password may contain '@'. Splitting on the first one would leave part of the secret.
2592BOOST_AUTO_TEST_CASE( SetRemoteStripsUserinfoContainingAtSign )
2593{
2594 KIGIT_COMMON common( nullptr );
2595
2596 common.SetRemote( wxS( "https://alice:pa@[email protected]/x/y.git" ) );
2597 BOOST_CHECK_EQUAL( common.GetRemote().ToStdString(), "https://gitlab.com/x/y.git" );
2598
2599 // The credentials must also be split at the right place.
2600 BOOST_CHECK_EQUAL( common.GetUsername().ToStdString(), "alice" );
2601 BOOST_CHECK_EQUAL( common.GetPassword().ToStdString(), "pa@ss" );
2602
2603 // An '@' that belongs to the path is not userinfo.
2604 common.SetRemote( wxS( "https://gitlab.com/x/[email protected]" ) );
2605 BOOST_CHECK_EQUAL( common.GetRemote().ToStdString(), "https://gitlab.com/x/[email protected]" );
2606}
2607
2608
2609BOOST_AUTO_TEST_CASE( ParseRemoteURLPreservesSSHAddress )
2610{
2611 struct TEST_CASE
2612 {
2613 const char* url;
2614 const char* username;
2615 const char* hostname;
2616 };
2617
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" },
2621 { "git+ssh://[email protected]:2222/srv/repo", "runner", "example.com" },
2622 { "[email protected]:team/repo.git", "git", "example.com" },
2623 { "[email protected]:team/repo", "runner", "example.com" },
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" },
2627 };
2628
2629 for( const TEST_CASE& test : cases )
2630 {
2632 {
2633 KIGIT_COMMON::REMOTE_URL parsed = KIGIT_COMMON::ParseRemoteURL( wxString::FromUTF8( test.url ) );
2634
2635 BOOST_CHECK_EQUAL( parsed.url.ToStdString(), test.url );
2636 BOOST_CHECK_EQUAL( parsed.username.ToStdString(), test.username );
2637 BOOST_CHECK_EQUAL( parsed.hostname.ToStdString(), test.hostname );
2638 BOOST_CHECK( parsed.password.IsEmpty() );
2640 }
2641 }
2642}
2643
2644
2645BOOST_AUTO_TEST_CASE( ParseRemoteURLKeepsPortWhenExtractingCredentials )
2646{
2647 struct TEST_CASE
2648 {
2649 const char* input;
2650 const char* address;
2651 const char* username;
2652 const char* password;
2654 };
2655
2656 const TEST_CASE cases[] = {
2657 { "https://alice:pa@[email protected]:8443/team/[email protected]", "https://example.com:8443/team/[email protected]",
2659 { "http://alice:[email protected]:8080/team/repo", "http://example.com:8080/team/repo", "alice", "secret",
2661 { "https://example.com:8443/team/[email protected]", "https://example.com:8443/team/[email protected]", "", "",
2663 { "ssh://runner:pa@[email protected]:29022/srv/repo", "ssh://[email protected]:29022/srv/repo", "runner", "pa@ss",
2665 };
2666
2667 for( const TEST_CASE& test : cases )
2668 {
2669 BOOST_TEST_CONTEXT( test.input )
2670 {
2671 KIGIT_COMMON::REMOTE_URL parsed = KIGIT_COMMON::ParseRemoteURL( wxString::FromUTF8( test.input ) );
2672
2673 BOOST_CHECK_EQUAL( parsed.url.ToStdString(), test.address );
2674 BOOST_CHECK_EQUAL( parsed.username.ToStdString(), test.username );
2675 BOOST_CHECK_EQUAL( parsed.password.ToStdString(), test.password );
2676 BOOST_CHECK_EQUAL( parsed.hostname.ToStdString(), "example.com" );
2677 BOOST_CHECK( parsed.connType == test.connType );
2678 }
2679 }
2680}
2681
2682
2683BOOST_AUTO_TEST_CASE( ParseRemoteURLKeepsLocalPaths )
2684{
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",
2691 };
2692
2693 for( const char* path : paths )
2694 {
2696 {
2697 KIGIT_COMMON::REMOTE_URL parsed = KIGIT_COMMON::ParseRemoteURL( wxString::FromUTF8( path ) );
2698
2699 BOOST_CHECK_EQUAL( parsed.url.ToStdString(), path );
2700 BOOST_CHECK( parsed.username.IsEmpty() );
2701 BOOST_CHECK( parsed.password.IsEmpty() );
2702 BOOST_CHECK( parsed.hostname.IsEmpty() );
2704 }
2705 }
2706}
2707
2708
2709BOOST_AUTO_TEST_CASE( SetRemotePreservesSSHAddressAndAcceptsLocalReplacement )
2710{
2711 KIGIT_COMMON common( nullptr );
2712 const wxString sshURL = wxS( "ssh://[email protected]:29022/srv/git/repos/l3-auth-30.git" );
2713
2714 common.SetUsername( wxS( "previous-user" ) );
2715 common.SetRemote( sshURL );
2716
2717 BOOST_CHECK_EQUAL( common.GetRemote(), sshURL );
2718 BOOST_CHECK_EQUAL( common.GetUsername(), wxS( "runner" ) );
2720
2721 common.SetRemote( wxS( "/srv/git/repos/local.git" ) );
2722
2723 BOOST_CHECK_EQUAL( common.GetRemote(), wxS( "/srv/git/repos/local.git" ) );
2725}
2726
2727
2728// Called after editing a remote, so a later push authenticates against the new server.
2729BOOST_AUTO_TEST_CASE( UpdateCurrentBranchInfoRefreshesChangedRemoteUrl )
2730{
2731 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
2732 KIGIT::GitRepositoryPtr repoPtr( repo );
2733
2734 KIGIT_COMMON common( repo );
2735 BOOST_REQUIRE( common.AddRemote( "origin", "https://example.com/old.git" ) );
2736
2737 git_config* cfg = nullptr;
2738 BOOST_REQUIRE_EQUAL( git_repository_config( &cfg, repo ), GIT_OK );
2739 KIGIT::GitConfigPtr cfgPtr( cfg );
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 );
2742
2743 common.UpdateCurrentBranchInfo();
2744 BOOST_CHECK_EQUAL( common.GetRemote().ToStdString(), "https://example.com/old.git" );
2745
2746 BOOST_REQUIRE( common.SetRemoteUrl( "origin", "https://example.com/new.git" ) );
2747 common.UpdateCurrentBranchInfo();
2748 BOOST_CHECK_EQUAL( common.GetRemote().ToStdString(), "https://example.com/new.git" );
2749}
2750
2751
2752// An empty result means the end of the list, so an empty entry would hide every key behind it.
2753BOOST_AUTO_TEST_CASE( SetSSHKeyIgnoresEmptyKey )
2754{
2755 // Only keys with both files on disk are offered
2756 KI_TEST::WriteGitFile( TempPath(), wxS( "keys/kicad" ), c_opensshPlain );
2757 KI_TEST::WriteGitFile( TempPath(), wxS( "keys/kicad.pub" ), "ssh-ed25519 AAAA qa\n" );
2758
2759 const wxString key = wxFileName( TempPath() + wxS( "/keys/kicad" ) ).GetFullPath();
2760 KIGIT_COMMON common( nullptr );
2761
2762 common.SetSSHKey( key );
2763 common.SetSSHKey( wxEmptyString );
2764 common.ResetNextKey();
2765
2766 BOOST_CHECK_EQUAL( common.GetNextPublicKey(), key );
2767}
2768
2769
2770// libgit2 reports forward slashes on every platform while the project path is native. On Windows
2771// the raw prefix test rejected every file and the commit panel stayed empty. A backslash is an
2772// ordinary file name character elsewhere, so only Windows folds the separators.
2773BOOST_AUTO_TEST_CASE( IsWithinProjectPathHandlesMixedSeparators )
2774{
2776
2777#ifdef __WINDOWS__
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" ) ) );
2784#endif
2785
2786 // A sibling directory sharing the prefix as plain text is not inside the project.
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" ) ) );
2789}
2790
2791
2792// The pull gate refuses over uncommitted tracked changes, but untracked files must not block
2793// getting changes, since a merge leaves them alone.
2794BOOST_AUTO_TEST_CASE( HasUncommittedTrackedChangesIgnoresUntracked )
2795{
2796 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "v1\n" );
2797 KIGIT::GitRepositoryPtr repoPtr( repo );
2798
2799 KIGIT_COMMON common( repo );
2801
2802 // An untracked file alone does not count.
2803 {
2804 wxFileName fn( TempPath(), wxS( "notes.txt" ) );
2805 std::ofstream f( fn.GetFullPath().utf8_string() );
2806 f << "scratch\n";
2807 }
2808
2810
2811 // A modified tracked file does.
2812 {
2813 wxFileName fn( TempPath(), wxS( "file.txt" ) );
2814 std::ofstream f( fn.GetFullPath().utf8_string() );
2815 f << "v2\n";
2816 }
2817
2819}
2820
2821
2822BOOST_AUTO_TEST_CASE( WorktreeRefusalPrioritizesUnsavedDocumentAndLibraryEditors )
2823{
2824 GIT_WORKTREE_STATUS status;
2825 status.checked = true;
2826 status.hasTrackedChanges = true;
2827 status.lfsMessage = wxS( "Git LFS prevents this rewrite." );
2828 status.outsideProject = { wxS( "other/board.kicad_pcb" ) };
2829
2830 const std::vector<wxString> editors = { wxS( "Schematic Editor" ), wxS( "Footprint Editor" ) };
2831 wxString message = status.RefusalMessage( editors );
2832
2833 BOOST_CHECK( message.Contains( "Schematic Editor" ) );
2834 BOOST_CHECK( message.Contains( "Footprint Editor" ) );
2835 BOOST_CHECK( message.Contains( "unsaved changes" ) );
2836 BOOST_CHECK( message.Contains( "Save or discard" ) );
2837 BOOST_CHECK( !message.Contains( "Git LFS" ) );
2838 BOOST_CHECK( !message.Contains( "other/board.kicad_pcb" ) );
2839
2840 wxString stashMessage = status.RefusalMessage( editors, true );
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" ) );
2846
2847 // A failed disk check must still explain the unsaved work the user can resolve first.
2848 status.checked = false;
2849 BOOST_CHECK_EQUAL( status.RefusalMessage( editors ).ToStdString(), message.ToStdString() );
2850}
2851
2852
2853BOOST_AUTO_TEST_CASE( WorktreeStatusRefusesUnavailableAndFailedReads )
2854{
2855 KIGIT_COMMON unavailable( nullptr );
2856 auto unavailableLock = unavailable.LockAction();
2857
2858 BOOST_CHECK( !GIT_WORKTREE_STATUS{}.RefusalMessage( {} ).IsEmpty() );
2859
2860 GIT_WORKTREE_STATUS missing = ReadGitWorktreeStatus( unavailable, wxEmptyString );
2861 BOOST_CHECK( !missing.checked );
2862 BOOST_CHECK( missing.RefusalMessage( {} ).Contains( "Could not check" ) );
2863 BOOST_CHECK( !missing.RefusalMessage( {} ).Contains( "Stash Changes" ) );
2864 BOOST_CHECK( !missing.RefusalMessage( {}, true ).IsEmpty() );
2865
2866 // A repository without a working tree makes libgit2's status read fail.
2867 git_repository* rawRepo = nullptr;
2868 BOOST_REQUIRE_EQUAL( git_repository_init( &rawRepo, TempPath().utf8_str(), 1 ), GIT_OK );
2869 KIGIT::GitRepositoryPtr repo( rawRepo );
2870 KIGIT_COMMON common( repo.get() );
2871 auto lock = common.LockAction();
2872
2873 GIT_WORKTREE_STATUS failed = ReadGitWorktreeStatus( common, TempPath() );
2874 BOOST_CHECK( !failed.checked );
2875 BOOST_CHECK( failed.RefusalMessage( {} ).Contains( "Could not check" ) );
2876}
2877
2878
2879BOOST_AUTO_TEST_CASE( WorktreeStatusAllowsCleanAndUntrackedFiles )
2880{
2881 KIGIT::GitRepositoryPtr repo( makeRepoWithCommit( TempPath(), "board.kicad_pcb", "original\n" ) );
2882 KIGIT_COMMON common( repo.get(), TempPath() );
2883 auto lock = common.LockAction();
2884
2885 GIT_WORKTREE_STATUS clean = ReadGitWorktreeStatus( common, TempPath() );
2886 BOOST_CHECK( clean.checked );
2888 BOOST_CHECK( clean.RefusalMessage( {} ).IsEmpty() );
2889
2890 writeFile( TempPath(), "notes.txt", "untracked notes\n" );
2891
2892 GIT_WORKTREE_STATUS untracked = ReadGitWorktreeStatus( common, TempPath() );
2893 BOOST_CHECK( untracked.checked );
2894 BOOST_CHECK( !untracked.hasTrackedChanges );
2895 BOOST_CHECK( untracked.outsideProject.empty() );
2896 BOOST_CHECK( untracked.RefusalMessage( {} ).IsEmpty() );
2897}
2898
2899
2900BOOST_AUTO_TEST_CASE( WorktreeStatusExplainsTrackedChangesInsideAndOutsideTheProject )
2901{
2902 KIGIT::GitRepositoryPtr repo( makeRepoWithCommit( TempPath(), "README.txt", "repository\n" ) );
2903
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" );
2911
2912 KIGIT_COMMON common( repo.get(), projectPath );
2913 auto lock = common.LockAction();
2914 writeFile( TempPath(), "project/board.kicad_pcb", "edited project board\n" );
2915
2916 GIT_WORKTREE_STATUS inside = ReadGitWorktreeStatus( common, projectPath );
2917 BOOST_CHECK( inside.checked );
2919 BOOST_CHECK( inside.outsideProject.empty() );
2920 BOOST_CHECK( inside.RefusalMessage( {} ).Contains( "Stash Changes" ) );
2921 BOOST_CHECK( inside.RefusalMessage( {}, true ).IsEmpty() );
2922
2923 writeFile( TempPath(), "project2/board.kicad_pcb", "edited sibling board\n" );
2924 writeFile( TempPath(), "project2/notes.txt", "untracked sibling notes\n" );
2925
2926 GIT_WORKTREE_STATUS outside = ReadGitWorktreeStatus( common, projectPath );
2927 BOOST_CHECK( outside.checked );
2928 BOOST_CHECK( outside.hasTrackedChanges );
2929 BOOST_REQUIRE_EQUAL( outside.outsideProject.size(), 1 );
2930 BOOST_CHECK_EQUAL( outside.outsideProject.front().ToStdString(), "project2/board.kicad_pcb" );
2931
2932 wxString message = outside.RefusalMessage( {} );
2933 BOOST_CHECK( message.Contains( "project2/board.kicad_pcb" ) );
2934 BOOST_CHECK( !message.Contains( "project/board.kicad_pcb" ) );
2935 BOOST_CHECK( !message.Contains( "notes.txt" ) );
2936 BOOST_CHECK( outside.RefusalMessage( {}, true ).IsEmpty() );
2937}
2938
2939
2940BOOST_AUTO_TEST_CASE( WorktreeStatusDistinguishesGitLfsFromUncommittedChanges )
2941{
2942 KIGIT::GitRepositoryPtr repo( makeRepoWithCommit( TempPath(), "board.kicad_pcb", "original\n" ) );
2943
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" );
2948
2949 KIGIT_COMMON common( repo.get(), TempPath() );
2950 auto lock = common.LockAction();
2951
2952 GIT_WORKTREE_STATUS clean = ReadGitWorktreeStatus( common, TempPath() );
2953 BOOST_CHECK( clean.checked );
2955 BOOST_CHECK( clean.RefusalMessage( {} ).Contains( "Git LFS" ) );
2956 BOOST_CHECK( clean.RefusalMessage( {} ).Contains( "case.step" ) );
2957
2958 writeFile( TempPath(), "board.kicad_pcb", "edited board\n" );
2959
2960 GIT_WORKTREE_STATUS dirty = ReadGitWorktreeStatus( common, TempPath() );
2961 BOOST_CHECK( dirty.checked );
2963 BOOST_CHECK( dirty.RefusalMessage( {} ).Contains( "Git LFS" ) );
2964 BOOST_CHECK( !dirty.RefusalMessage( {} ).Contains( "Stash Changes" ) );
2965 BOOST_CHECK( dirty.RefusalMessage( {}, true ).Contains( "Git LFS" ) );
2966}
2967
2968
2969// Discarding a conflicted file puts back what HEAD holds and clears the conflict, so it settles
2970// the file rather than only throwing away an edit. The panel says so, and asks first.
2971BOOST_AUTO_TEST_CASE( DiscardingAConflictedPathSettlesIt )
2972{
2973 git_repository* repo = makeRepoWithCommit( TempPath(), "file.txt", "one\ntwo\nthree\n" );
2974 KIGIT::GitRepositoryPtr repoPtr( repo );
2975
2976 KIGIT_COMMON common( repo );
2977 BOOST_CHECK( !common.IsPathConflicted( wxS( "file.txt" ) ) );
2978 BOOST_CHECK( !common.IsPathConflicted( wxS( "nothing.txt" ) ) );
2979
2980 git_oid blobOid;
2981 BOOST_REQUIRE_EQUAL( git_blob_create_from_buffer( &blobOid, repo, "other\n", 6 ), GIT_OK );
2982
2983 {
2984 git_index* index = nullptr;
2985 BOOST_REQUIRE_EQUAL( git_repository_index( &index, repo ), GIT_OK );
2986 KIGIT::GitIndexPtr indexPtr( index );
2987
2988 git_index_entry stage{};
2989 stage.path = "file.txt";
2990 stage.mode = GIT_FILEMODE_BLOB;
2991 git_oid_cpy( &stage.id, &blobOid );
2992
2993 BOOST_REQUIRE_EQUAL( git_index_conflict_add( index, &stage, &stage, &stage ), GIT_OK );
2994 BOOST_REQUIRE_EQUAL( git_index_write( index ), GIT_OK );
2995 }
2996
2997 BOOST_CHECK( common.IsPathConflicted( wxS( "file.txt" ) ) );
2998
2999 wxString error;
3000 BOOST_REQUIRE_MESSAGE( common.DiscardChanges( { wxS( "file.txt" ) }, error ), error.ToStdString() );
3001
3002 BOOST_CHECK_MESSAGE( !common.IsPathConflicted( wxS( "file.txt" ) ),
3003 "discarding left the file still needing resolution" );
3004}
3005
3006
3007BOOST_AUTO_TEST_CASE( ForcePushRejectsAChangedConfirmedTarget )
3008{
3009 KIGIT::GitRepositoryPtr repo( makeRepoWithCommit( TempPath(), "file.txt", "one\n" ) );
3010 KIGIT_COMMON common( repo.get() );
3011
3012 git_oid head;
3013 BOOST_REQUIRE_EQUAL( git_reference_name_to_id( &head, repo.get(), "HEAD" ), GIT_OK );
3014 const wxString confirmedHead = wxString::FromUTF8( git_oid_tostr_s( &head ) );
3015 const wxString confirmedBranch = common.GetCurrentBranchName();
3016
3017 auto forcePushError = [&common]( const wxString& aBranch, const wxString& aHeadOid )
3018 {
3019 GIT_PUSH_HANDLER handler( &common );
3020 handler.SetExpectedTarget( aBranch, aHeadOid );
3021 BOOST_CHECK_EQUAL( static_cast<int>( handler.PerformPush( true ) ), static_cast<int>( PushResult::Error ) );
3022 return handler.GetErrorString();
3023 };
3024
3025 // A matching target gets past confirmation validation. With no remote configured it
3026 // stops at lookup, so none of these checks can start a network connection.
3027 const wxString noRemote = wxS( "There is no remote named 'origin'." );
3028 BOOST_CHECK( forcePushError( confirmedBranch, confirmedHead ).StartsWith( noRemote ) );
3029
3030 const wxString targetChanged = wxS( "The branch or its last commit changed while the push was waiting." );
3031
3032 // Switching branches must be rejected even when both branches name the same commit.
3033 BOOST_REQUIRE( common.CreateBranch( wxS( "other" ) ) );
3034 BOOST_REQUIRE_EQUAL( git_repository_set_head( repo.get(), "refs/heads/other" ), GIT_OK );
3035 BOOST_CHECK( forcePushError( confirmedBranch, confirmedHead ).Contains( targetChanged ) );
3036
3037 // Advancing the confirmed branch must also be rejected, with the branch name unchanged.
3038 BOOST_REQUIRE_EQUAL( git_repository_set_head( repo.get(), "refs/heads/main" ), GIT_OK );
3039 writeFile( TempPath(), wxS( "file.txt" ), "two\n" );
3040 commitWorkingFile( repo.get(), wxS( "file.txt" ), "second" );
3041 BOOST_CHECK( forcePushError( confirmedBranch, confirmedHead ).Contains( targetChanged ) );
3042
3043 BOOST_REQUIRE_EQUAL( git_reference_name_to_id( &head, repo.get(), "HEAD" ), GIT_OK );
3045 forcePushError( confirmedBranch, wxString::FromUTF8( git_oid_tostr_s( &head ) ) ).StartsWith( noRemote ) );
3046}
3047
3048
3049BOOST_AUTO_TEST_CASE( PushWaitsForQueryLockAndHonorsCancellation )
3050{
3051 KIGIT::GitRepositoryPtr repo( makeRepoWithCommit( TempPath(), "file.txt", "one\n" ) );
3052 KIGIT_COMMON common( repo.get() );
3053 GIT_PUSH_HANDLER handler( &common );
3054 handler.SetWaitForActionLock( true );
3055
3057 handler.SetCancelToken( token );
3058
3059 auto lock = common.LockAction();
3060 std::promise<void> entered;
3061 auto ready = entered.get_future();
3062 auto result = std::async( std::launch::async,
3063 [&handler, &entered]()
3064 {
3065 entered.set_value();
3066 return handler.PerformPush();
3067 } );
3068
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 );
3072 lock.unlock();
3073
3074 BOOST_CHECK( started );
3075 BOOST_CHECK( waited );
3076 BOOST_REQUIRE( result.wait_for( std::chrono::seconds( 5 ) ) == std::future_status::ready );
3077 BOOST_CHECK_EQUAL( static_cast<int>( result.get() ), static_cast<int>( PushResult::Error ) );
3079
3080 // No remote is configured. Reaching remote lookup would have added an error, so an empty
3081 // error list confirms cancellation stopped the operation before any network preparation.
3082 BOOST_CHECK( handler.GetErrorStrings().empty() );
3083 BOOST_CHECK_EQUAL( common.GetRecentCommits( 1 ).size(), 1u );
3084}
3085
3086
3087BOOST_AUTO_TEST_CASE( TransferCompletionCapturesStayOnUiThread )
3088{
3089 auto wrongThread = std::make_shared<std::atomic<bool>>( false );
3090 UI_CALLBACK_CAPTURE capture( wrongThread );
3091 bool completed = false;
3092
3093 {
3094 KIGIT_TRANSFER transfer;
3095 BOOST_REQUIRE( transfer.Start(
3096 "completion capture",
3097 []( const KIGIT_COMMON::CANCEL_TOKEN& )
3098 {
3099 },
3100 [capture, &completed]()
3101 {
3102 completed = true;
3103 } ) );
3104
3105 BOOST_REQUIRE( waitForQuery(
3106 [&transfer]()
3107 {
3108 return !transfer.IsBusy();
3109 } ) );
3110 }
3111
3112 BOOST_CHECK( completed );
3113 BOOST_CHECK( !wrongThread->load() );
3114
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>();
3120 completed = false;
3121
3122 BOOST_REQUIRE( transfer->Start(
3123 "destroyed completion owner",
3124 [entered, proceed]( const KIGIT_COMMON::CANCEL_TOKEN& )
3125 {
3126 entered->set_value();
3127 proceed.wait();
3128 },
3129 [capture, &completed]()
3130 {
3131 completed = true;
3132 } ) );
3133
3134 const bool started = ready.wait_for( std::chrono::seconds( 5 ) ) == std::future_status::ready;
3135 transfer.reset();
3136 release.set_value();
3137
3138 BOOST_CHECK( started );
3139 BOOST_REQUIRE_EQUAL( Backend()->OrphanRegistry().JoinAll( std::chrono::seconds( 5 ) ), 0u );
3140 wxTheApp->ProcessPendingEvents();
3141 BOOST_CHECK( !completed );
3142 BOOST_CHECK( !wrongThread->load() );
3143}
3144
3145
3146BOOST_AUTO_TEST_CASE( QuerySerializesAndKeepsOnlyTheLatestRequest )
3147{
3148 KIGIT::GitRepositoryPtr repo( makeRepoWithCommit( TempPath(), "file.txt", "one\n" ) );
3149 auto session = std::make_shared<KIGIT_COMMON>( repo.get() );
3150 KIGIT_QUERY query;
3151
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;
3159
3160 query.Run(
3161 session,
3162 [worked, workerThread, proceed, entered]( KIGIT_COMMON& )
3163 {
3164 *workerThread = std::this_thread::get_id();
3165 worked->fetch_or( 1 );
3166 entered->set_value();
3167 proceed.wait();
3168 },
3169 [&completed]()
3170 {
3171 completed |= 1;
3172 } );
3173
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() );
3177
3178 query.Run(
3179 session,
3180 [worked]( KIGIT_COMMON& )
3181 {
3182 worked->fetch_or( 2 );
3183 },
3184 [&completed]()
3185 {
3186 completed |= 2;
3187 } );
3188 query.Run(
3189 session,
3190 [worked]( KIGIT_COMMON& )
3191 {
3192 worked->fetch_or( 4 );
3193 },
3194 [&completed]()
3195 {
3196 completed |= 4;
3197 } );
3198 release.set_value();
3199
3200 BOOST_REQUIRE( waitForQuery(
3201 [&query]()
3202 {
3203 return !query.IsBusy();
3204 } ) );
3205 BOOST_CHECK_EQUAL( worked->load(), 5u );
3206 BOOST_CHECK_EQUAL( completed, 4u );
3207}
3208
3209
3210BOOST_AUTO_TEST_CASE( QueryCancelDropsPendingWorkAndQueuedCompletion )
3211{
3212 KIGIT::GitRepositoryPtr repo( makeRepoWithCommit( TempPath(), "file.txt", "one\n" ) );
3213 auto session = std::make_shared<KIGIT_COMMON>( repo.get() );
3214 KIGIT_QUERY query;
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;
3219
3220 query.Run(
3221 session,
3222 [worked, workerLifetime]( KIGIT_COMMON& )
3223 {
3224 worked->fetch_add( 1 );
3225 },
3226 [&completed]()
3227 {
3228 completed = true;
3229 } );
3230 workerLifetime.reset();
3231
3232 // The worker drops its capture after posting completion. Leave that event queued.
3233 const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds( 5 );
3234
3235 while( !workerWatch.expired() && std::chrono::steady_clock::now() < deadline )
3236 std::this_thread::sleep_for( std::chrono::milliseconds( 1 ) );
3237
3238 BOOST_REQUIRE( workerWatch.expired() );
3239 query.Run(
3240 session,
3241 [worked]( KIGIT_COMMON& )
3242 {
3243 worked->fetch_add( 1 );
3244 },
3245 [&completed]()
3246 {
3247 completed = true;
3248 } );
3249 query.Cancel();
3250
3251 BOOST_REQUIRE( waitForQuery(
3252 [&query]()
3253 {
3254 return !query.IsBusy();
3255 } ) );
3256 BOOST_CHECK_EQUAL( worked->load(), 1u );
3257 BOOST_CHECK( !completed );
3258}
3259
3260
3261BOOST_AUTO_TEST_CASE( QueryDestructionDoesNotWaitAndPinsTheSession )
3262{
3263 KIGIT::GitRepositoryPtr repo( makeRepoWithCommit( TempPath(), "file.txt", "one\n" ) );
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>();
3267
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;
3273
3274 query->Run(
3275 session,
3276 [proceed, entered]( KIGIT_COMMON& )
3277 {
3278 entered->set_value();
3279 proceed.wait();
3280 },
3281 [&completed]()
3282 {
3283 completed = true;
3284 } );
3285
3286 BOOST_REQUIRE( ready.wait_for( std::chrono::seconds( 5 ) ) == std::future_status::ready );
3287 session.reset();
3288 const auto start = std::chrono::steady_clock::now();
3289 query.reset();
3290 BOOST_CHECK( std::chrono::steady_clock::now() - start < std::chrono::seconds( 1 ) );
3291 BOOST_CHECK( !watched.expired() );
3292 release.set_value();
3293
3294 BOOST_REQUIRE( waitForQuery(
3295 [&watched]()
3296 {
3297 return watched.expired();
3298 } ) );
3299 wxTheApp->ProcessPendingEvents();
3300 BOOST_CHECK( !completed );
3301}
3302
3303
3304BOOST_AUTO_TEST_CASE( QuerySkipsUnavailableSessionsAndCompletesOnUi )
3305{
3306 KIGIT::GitRepositoryPtr repo( makeRepoWithCommit( TempPath(), "file.txt", "one\n" ) );
3307 KIGIT_QUERY query;
3308 auto worked = std::make_shared<std::atomic<bool>>( false );
3309 std::thread::id completionThread;
3310
3311 auto work = [worked]( KIGIT_COMMON& )
3312 {
3313 worked->store( true );
3314 };
3315 auto done = [&completionThread]()
3316 {
3317 completionThread = std::this_thread::get_id();
3318 };
3319
3320 query.Run( nullptr, work, done );
3321 BOOST_REQUIRE( waitForQuery(
3322 [&query]()
3323 {
3324 return !query.IsBusy();
3325 } ) );
3326 BOOST_CHECK( completionThread == std::this_thread::get_id() );
3327
3328 auto session = std::make_shared<KIGIT_COMMON>( nullptr );
3329 completionThread = {};
3330 query.Run( session, work, done );
3331 BOOST_REQUIRE( waitForQuery(
3332 [&query]()
3333 {
3334 return !query.IsBusy();
3335 } ) );
3336 BOOST_CHECK( completionThread == std::this_thread::get_id() );
3337
3338 session->SetRepo( repo.get() );
3339 session->SetShuttingDown( true );
3340 completionThread = {};
3341 query.Run( session, work, done );
3342 BOOST_REQUIRE( waitForQuery(
3343 [&query]()
3344 {
3345 return !query.IsBusy();
3346 } ) );
3347 BOOST_CHECK( completionThread == std::this_thread::get_id() );
3348 BOOST_CHECK( !worked->load() );
3349
3350 Backend()->OrphanRegistry().JoinAll( std::chrono::seconds( 1 ) );
3351 completionThread = {};
3352 query.Run( session, work, done );
3353 BOOST_CHECK( !query.IsBusy() );
3354 BOOST_CHECK( completionThread == std::this_thread::get_id() );
3355}
3356
3357
3358BOOST_AUTO_TEST_CASE( QueryExceptionStillCompletesAndCompletionCanDestroyOwner )
3359{
3360 KIGIT::GitRepositoryPtr repo( makeRepoWithCommit( TempPath(), "file.txt", "one\n" ) );
3361 auto session = std::make_shared<KIGIT_COMMON>( repo.get() );
3362 auto query = std::make_unique<KIGIT_QUERY>();
3363 wxLogNull noLog;
3364
3365 query->Run(
3366 session,
3367 []( KIGIT_COMMON& )
3368 {
3369 throw std::runtime_error( "query failed" );
3370 },
3371 [&query]()
3372 {
3373 query.reset();
3374 } );
3375
3376 BOOST_REQUIRE( waitForQuery(
3377 [&query]()
3378 {
3379 return !query;
3380 } ) );
3381 BOOST_CHECK( session->TryLockAction().owns_lock() );
3382 BOOST_CHECK_EQUAL( session->GetRecentCommits( 1 ).size(), 1u );
3383}
3384
3385
3386BOOST_AUTO_TEST_CASE( QueryExceptionStillStartsThePendingRequest )
3387{
3388 KIGIT::GitRepositoryPtr repo( makeRepoWithCommit( TempPath(), "file.txt", "one\n" ) );
3389 auto session = std::make_shared<KIGIT_COMMON>( repo.get() );
3390 KIGIT_QUERY query;
3391 wxLogNull noLog;
3392
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;
3399
3400 query.Run(
3401 session,
3402 [entered, proceed]( KIGIT_COMMON& )
3403 {
3404 entered->set_value();
3405 proceed.wait();
3406 throw 1;
3407 },
3408 [&completed]()
3409 {
3410 completed |= 1;
3411 } );
3412
3413 BOOST_REQUIRE( ready.wait_for( std::chrono::seconds( 5 ) ) == std::future_status::ready );
3414 query.Run(
3415 session,
3416 [worked]( KIGIT_COMMON& )
3417 {
3418 worked->store( true );
3419 },
3420 [&completed]()
3421 {
3422 completed |= 2;
3423 } );
3424 release.set_value();
3425
3426 BOOST_REQUIRE( waitForQuery(
3427 [&query]()
3428 {
3429 return !query.IsBusy();
3430 } ) );
3431 BOOST_CHECK( worked->load() );
3432 BOOST_CHECK_EQUAL( completed, 2u );
3433}
3434
3435
int index
const char * name
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.
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 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::string message
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()
std::string path
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.