37#include <wx/filename.h>
38#include <wx/stdpaths.h>
47 LIBGIT2_SCOPE() { git_libgit2_init(); }
48 ~LIBGIT2_SCOPE() { git_libgit2_shutdown(); }
52struct SCOPED_BOOL_OVERRIDE
54 explicit SCOPED_BOOL_OVERRIDE(
bool& aFlag ) : m_flag( aFlag ), m_original( aFlag ) {}
55 ~SCOPED_BOOL_OVERRIDE() { m_flag = m_original; }
66 explicit SCOPED_TEMP_DIR(
const wxString& aPrefix )
68 wxString base = wxStandardPaths::Get().GetTempDir();
69 m_path = base + wxFileName::GetPathSeparator() + aPrefix
70 + wxString::Format( wxS(
"_%lu_%ld" ),
71 static_cast<unsigned long>( ::wxGetProcessId() ),
72 static_cast<long>( wxDateTime::UNow().GetTicks() ) );
73 wxFileName::Mkdir( m_path, 0777, wxPATH_MKDIR_FULL );
78 if( !m_path.IsEmpty() && wxDirExists( m_path ) )
79 wxFileName::Rmdir( m_path, wxPATH_RMDIR_RECURSIVE );
82 const wxString& Path()
const {
return m_path; }
88void writeTextFile(
const wxString& aPath,
const wxString& aContents )
90 wxFFile f( aPath, wxT(
"w" ) );
113 std::vector<HISTORY_FILE_DATA> fileData;
117 BOOST_CHECK_NO_THROW( board.
SaveToHistory( wxS(
"/tmp/anywhere" ), fileData ) );
118 BOOST_CHECK( fileData.empty() );
130 wxString tempDir = wxStandardPaths::Get().GetTempDir();
131 wxString projectPath = tempDir + wxFileName::GetPathSeparator() + wxS(
"pcb_autosave.kicad_pro" );
138 std::vector<HISTORY_FILE_DATA> fileData;
142 BOOST_CHECK( fileData.empty() );
159 LIBGIT2_SCOPE libgit;
162 SCOPED_BOOL_OVERRIDE restoreBackupFlag( backupEnabled );
163 backupEnabled =
true;
165 SCOPED_TEMP_DIR notAProject( wxS(
"kicad_qa_no_project" ) );
166 const wxString&
path = notAProject.Path();
170 wxString boardPath =
path + wxFileName::GetPathSeparator() + wxS(
"stray.kicad_pcb" );
171 writeTextFile( boardPath, wxS(
"(kicad_pcb (version 20240108))\n" ) );
175 BOOST_CHECK( !history.
Init(
path ) );
177 BOOST_CHECK( !history.
TagSave(
path, wxS(
"pcb" ) ) );
180 wxString historyDir =
path + wxFileName::GetPathSeparator() + wxS(
".history" );
181 BOOST_CHECK( !wxDirExists( historyDir ) );
194 LIBGIT2_SCOPE libgit;
197 SCOPED_BOOL_OVERRIDE restoreBackupFlag( backupEnabled );
198 backupEnabled =
true;
200 SCOPED_TEMP_DIR
project( wxS(
"kicad_qa_subdirs" ) );
203 writeTextFile(
path + wxFileName::GetPathSeparator() + wxS(
"subdirs.kicad_pro" ), wxS(
"{}\n" ) );
204 writeTextFile(
path + wxFileName::GetPathSeparator() + wxS(
"subdirs.kicad_pcb" ),
205 wxS(
"(kicad_pcb (version 20240108))\n" ) );
209 wxString subDir =
path + wxFileName::GetPathSeparator() + wxS(
"libs" );
210 BOOST_REQUIRE( wxFileName::Mkdir( subDir, 0777, wxPATH_MKDIR_FULL ) );
211 writeTextFile( subDir + wxFileName::GetPathSeparator() + wxS(
"fp.kicad_mod" ),
212 wxS(
"(footprint test)\n" ) );
218 wxString historyDir =
path + wxFileName::GetPathSeparator() + wxS(
".history" );
219 BOOST_CHECK( wxDirExists( historyDir ) );
220 BOOST_CHECK( !wxDirExists( subDir + wxFileName::GetPathSeparator() + wxS(
".history" ) ) );
226 writeTextFile( subDir + wxFileName::GetPathSeparator() + wxS(
"fp.kicad_mod" ),
227 wxS(
"(footprint test (modified))\n" ) );
235 BOOST_CHECK( headBefore != headAfter );
242 LIBGIT2_SCOPE libgit;
246 SCOPED_BOOL_OVERRIDE restoreBackupFlag( backupEnabled );
247 backupEnabled =
true;
249 SCOPED_TEMP_DIR tempProject( wxS(
"kicad_qa_issue24016" ) );
250 const wxString& projectPath = tempProject.Path();
253 projectPath + wxFileName::GetPathSeparator() + wxS(
"issue24016.kicad_pcb" );
254 writeTextFile( boardPath, wxS(
"(kicad_pcb (version 20240108))\n" ) );
258 wxString projectFile =
259 projectPath + wxFileName::GetPathSeparator() + wxS(
"issue24016.kicad_pro" );
260 writeTextFile( projectFile, wxS(
"{}\n" ) );
265 wxString headHash = history.
GetHeadHash( projectPath );
270 wxString backupsDir =
271 projectPath + wxFileName::GetPathSeparator() + wxS(
"issue24016-backups" );
272 BOOST_REQUIRE( wxFileName::Mkdir( backupsDir, 0777, wxPATH_MKDIR_FULL ) );
274 wxString zipPath = backupsDir + wxFileName::GetPathSeparator()
275 + wxS(
"issue24016-2026-04-22_120000.zip" );
276 writeTextFile( zipPath, wxS(
"pretend-zip-contents" ) );
278 writeTextFile( boardPath, wxS(
"(kicad_pcb (version 20240108) (dirty yes))\n" ) );
283 "zip backups directory must survive RestoreCommit" );
285 ".zip archive inside the backups directory must survive RestoreCommit" );
304 LIBGIT2_SCOPE libgit;
307 SCOPED_BOOL_OVERRIDE restoreBackupFlag( backupEnabled );
308 backupEnabled =
true;
310 SCOPED_TEMP_DIR tempProject( wxS(
"kicad_qa_nested_project" ) );
311 const wxString& projectPath = tempProject.Path();
314 wxString parentPro = projectPath + wxFileName::GetPathSeparator() + wxS(
"projectA.kicad_pro" );
315 wxString parentPcb = projectPath + wxFileName::GetPathSeparator() + wxS(
"projectA.kicad_pcb" );
316 writeTextFile( parentPro, wxS(
"{}\n" ) );
317 writeTextFile( parentPcb, wxS(
"(kicad_pcb (version 20240108))\n" ) );
322 wxString headHash = history.
GetHeadHash( projectPath );
328 wxString nestedDir = projectPath + wxFileName::GetPathSeparator() + wxS(
"projectB" );
329 BOOST_REQUIRE( wxFileName::Mkdir( nestedDir, 0777, wxPATH_MKDIR_FULL ) );
331 wxString nestedPro = nestedDir + wxFileName::GetPathSeparator() + wxS(
"projectB.kicad_pro" );
332 wxString nestedPcb = nestedDir + wxFileName::GetPathSeparator() + wxS(
"projectB.kicad_pcb" );
333 wxString nestedSch = nestedDir + wxFileName::GetPathSeparator() + wxS(
"projectB.kicad_sch" );
334 writeTextFile( nestedPro, wxS(
"{ \"nested\": true }\n" ) );
335 writeTextFile( nestedPcb, wxS(
"(kicad_pcb (version 20240108) (nested yes))\n" ) );
336 writeTextFile( nestedSch, wxS(
"(kicad_sch (version 20240108))\n" ) );
339 writeTextFile( parentPcb, wxS(
"(kicad_pcb (version 20240108) (dirty yes))\n" ) );
345 "nested project directory must survive RestoreCommit" );
347 "nested .kicad_pro must survive RestoreCommit" );
349 "nested .kicad_pcb must survive RestoreCommit" );
351 "nested .kicad_sch must survive RestoreCommit" );
354 BOOST_CHECK( wxFileExists( parentPro ) );
355 BOOST_CHECK( wxFileExists( parentPcb ) );
366 LIBGIT2_SCOPE libgit;
369 SCOPED_BOOL_OVERRIDE restoreBackupFlag( backupEnabled );
370 backupEnabled =
true;
372 SCOPED_TEMP_DIR tempProject( wxS(
"kicad_qa_retained_backup" ) );
373 const wxString& projectPath = tempProject.Path();
375 wxString boardPath = projectPath + wxFileName::GetPathSeparator() + wxS(
"rb.kicad_pcb" );
376 wxString projectFile = projectPath + wxFileName::GetPathSeparator() + wxS(
"rb.kicad_pro" );
377 writeTextFile( projectFile, wxS(
"{}\n" ) );
378 writeTextFile( boardPath, wxS(
"(kicad_pcb (version 20240108))\n" ) );
383 wxString headHash = history.
GetHeadHash( projectPath );
387 writeTextFile( boardPath, wxS(
"(kicad_pcb (version 20240108) (dirty yes))\n" ) );
393 wxString parentDir = wxFileName( projectPath ).GetPath();
394 wxString leafPrefix = wxFileName( projectPath ).GetFullName() + wxS(
"_restore_backup_" );
396 wxDir dir( parentDir );
399 wxString retainedBackup;
400 bool foundLegacyBackup =
false;
403 for(
bool cont = dir.GetFirst( &
name, wxEmptyString, wxDIR_DIRS ); cont;
404 cont = dir.GetNext( &
name ) )
406 if(
name.StartsWith( leafPrefix ) )
408 retainedBackup = parentDir + wxFileName::GetPathSeparator() +
name;
412 "retained backup directory name must not contain ':' "
413 "(Windows-illegal in path components)" );
415 else if(
name == wxFileName( projectPath ).GetFullName() + wxS(
"_restore_backup" ) )
417 foundLegacyBackup =
true;
422 !retainedBackup.IsEmpty(),
423 "RestoreCommit must retain a timestamped _restore_backup_<ts>/ sibling directory" );
426 "RestoreCommit must not leave the legacy non-timestamped _restore_backup/ behind" );
429 if( !retainedBackup.IsEmpty() && wxDirExists( retainedBackup ) )
430 wxFileName::Rmdir( retainedBackup, wxPATH_RMDIR_RECURSIVE );
439 LIBGIT2_SCOPE libgit;
442 SCOPED_BOOL_OVERRIDE restoreBackupFlag( backupEnabled );
443 backupEnabled =
true;
445 SCOPED_TEMP_DIR
project( wxS(
"kicad_qa_privacy" ) );
448 writeTextFile(
path + wxFileName::GetPathSeparator() + wxS(
"p.kicad_pro" ), wxS(
"{}\n" ) );
449 writeTextFile(
path + wxFileName::GetPathSeparator() + wxS(
"p.kicad_pcb" ),
450 wxS(
"(kicad_pcb (version 20240108))\n" ) );
451 writeTextFile(
path + wxFileName::GetPathSeparator() + wxS(
"p.kicad_sch" ),
452 wxS(
"(kicad_sch (version 20240108))\n" ) );
454 writeTextFile(
path + wxFileName::GetPathSeparator() + wxS(
"passwords.txt" ), wxS(
"secret\n" ) );
455 writeTextFile(
path + wxFileName::GetPathSeparator() + wxS(
"datasheet.pdf" ), wxS(
"fake pdf bytes\n" ) );
456 writeTextFile(
path + wxFileName::GetPathSeparator() + wxS(
"notes.md" ), wxS(
"personal notes\n" ) );
458 wxString subDir =
path + wxFileName::GetPathSeparator() + wxS(
"docs" );
459 BOOST_REQUIRE( wxFileName::Mkdir( subDir, 0777, wxPATH_MKDIR_FULL ) );
460 writeTextFile( subDir + wxFileName::GetPathSeparator() + wxS(
"manual.txt" ), wxS(
"irrelevant\n" ) );
465 wxString hist =
path + wxFileName::GetPathSeparator() + wxS(
".history" );
466 git_repository* repo =
nullptr;
467 BOOST_REQUIRE_EQUAL( git_repository_open( &repo, hist.mb_str().data() ), 0 );
470 BOOST_REQUIRE_EQUAL( git_reference_name_to_id( &head_oid, repo,
"HEAD" ), 0 );
472 git_commit* head =
nullptr;
473 BOOST_REQUIRE_EQUAL( git_commit_lookup( &head, repo, &head_oid ), 0 );
475 git_tree* tree =
nullptr;
476 BOOST_REQUIRE_EQUAL( git_commit_tree( &tree, head ), 0 );
478 std::vector<std::string> committedPaths;
480 tree, GIT_TREEWALK_PRE,
481 [](
const char* root,
const git_tree_entry* entry,
void* payload ) ->
int
483 auto* paths =
static_cast<std::vector<std::string>*
>( payload );
485 if( git_tree_entry_type( entry ) == GIT_OBJECT_BLOB )
486 paths->push_back( std::string( root ) + git_tree_entry_name( entry ) );
492 git_tree_free( tree );
493 git_commit_free( head );
494 git_repository_free( repo );
496 auto contains = [&](
const std::string& s )
498 return std::find( committedPaths.begin(), committedPaths.end(), s ) != committedPaths.end();
505 BOOST_CHECK_MESSAGE( !contains(
"passwords.txt" ),
"passwords.txt must NOT appear in history" );
506 BOOST_CHECK_MESSAGE( !contains(
"datasheet.pdf" ),
"datasheet.pdf must NOT appear in history" );
508 BOOST_CHECK_MESSAGE( !contains(
"docs/manual.txt" ),
"subdirectory user content must NOT appear in history" );
518 LIBGIT2_SCOPE libgit;
521 SCOPED_BOOL_OVERRIDE restoreBackupFlag( backupEnabled );
522 backupEnabled =
true;
524 SCOPED_TEMP_DIR
project( wxS(
"kicad_qa_first_idle_autosave" ) );
527 writeTextFile(
path + wxFileName::GetPathSeparator() + wxS(
"p.kicad_pro" ), wxS(
"{}\n" ) );
528 writeTextFile(
path + wxFileName::GetPathSeparator() + wxS(
"p.kicad_pcb" ),
529 wxS(
"(kicad_pcb (version 20240108))\n" ) );
534 std::string inMemoryContent =
"(kicad_pcb (version 20240108))\n";
536 auto saver = [&inMemoryContent](
const wxString&, std::vector<HISTORY_FILE_DATA>& aFileData )
540 entry.
content = inMemoryContent;
541 aFileData.push_back( std::move( entry ) );
550 wxString histDir =
path + wxFileName::GetPathSeparator() + wxS(
".history" );
551 BOOST_CHECK( wxDirExists( histDir ) );
554 BOOST_CHECK_MESSAGE( head.IsEmpty(),
"no untagged HEAD should exist after an idle first save" );
557 inMemoryContent =
"(kicad_pcb (version 20240108) (edited yes))\n";
563 BOOST_CHECK_MESSAGE( !head.IsEmpty(),
"in-memory edits diverging from disk must produce a commit" );
576 LIBGIT2_SCOPE libgit;
579 SCOPED_BOOL_OVERRIDE restoreBackupFlag( backupEnabled );
580 backupEnabled =
true;
582 SCOPED_TEMP_DIR
project( wxS(
"kicad_qa_first_manual_save" ) );
585 writeTextFile(
path + wxFileName::GetPathSeparator() + wxS(
"p.kicad_pro" ), wxS(
"{}\n" ) );
586 writeTextFile(
path + wxFileName::GetPathSeparator() + wxS(
"p.kicad_pcb" ),
587 wxS(
"(kicad_pcb (version 20240108))\n" ) );
591 std::string inMemoryContent =
"(kicad_pcb (version 20240108))\n";
593 auto saver = [&inMemoryContent](
const wxString&, std::vector<HISTORY_FILE_DATA>& aFileData )
597 entry.
content = inMemoryContent;
598 aFileData.push_back( std::move( entry ) );
607 BOOST_CHECK_MESSAGE( !head.IsEmpty(),
"manual save on a fresh project must commit even when staged matches disk" );
Information pertinent to a Pcbnew printed circuit board.
void SaveToHistory(const wxString &aProjectPath, std::vector< HISTORY_FILE_DATA > &aFileData)
Serialize board into HISTORY_FILE_DATA for non-blocking history commit.
void SetProject(PROJECT *aProject, bool aReferenceOnly=false)
Link a board to a given project.
const wxString & GetFileName() const
PROJECT * GetProject() const
Simple local history manager built on libgit2.
bool TagSave(const wxString &aProjectPath, const wxString &aFileType)
Tag a manual save in the local history repository.
bool RunRegisteredSaversAndCommit(const wxString &aProjectPath, const wxString &aTitle, const wxString &aTagFileType=wxEmptyString)
Run all registered savers and, if any staged changes differ from HEAD, create a commit.
wxString GetHeadHash(const wxString &aProjectPath)
Return the current head commit hash.
bool RestoreCommit(const wxString &aProjectPath, const wxString &aHash, wxWindow *aParent=nullptr)
Restore the project files to the state recorded by the given commit hash.
void WaitForPendingSave()
Block until any pending background save completes.
void RegisterSaver(const void *aSaverObject, const std::function< void(const wxString &, std::vector< HISTORY_FILE_DATA > &)> &aSaver)
Register a saver callback invoked during autosave history commits.
bool Init(const wxString &aProjectPath)
Initialize the local history repository for the given project path.
bool CommitFullProjectSnapshot(const wxString &aProjectPath, const wxString &aTitle)
Commit a snapshot of the entire project directory (excluding the .history directory and ignored trans...
void UnregisterSaver(const void *aSaverObject)
Unregister a previously registered saver callback.
virtual COMMON_SETTINGS * GetCommonSettings() const
virtual const wxString GetProjectPath() const
Return the full path of the project.
bool LoadProject(const wxString &aFullPath, bool aSetActive=true)
Load a project or sets up a new project with a specified path.
bool UnloadProject(PROJECT *aProject, bool aSave=true)
Save, unload and unregister the given PROJECT.
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
PGM_BASE & Pgm()
The global program "get" accessor.
bool enabled
Automatically back up the project when files are saved.
Data produced by a registered saver on the UI thread, consumed by either the background local-history...
std::string content
Serialized content (mutually exclusive with sourcePath)
wxString relativePath
Destination path relative to the project root.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(SaveToHistoryWithNullProjectDoesNotCrash)
Regression test for https://gitlab.com/kicad/code/kicad/-/issues/23737.
BOOST_CHECK_MESSAGE(totalMismatches==0, std::to_string(totalMismatches)+" board(s) with strategy disagreements")