KiCad PCB EDA Suite
Loading...
Searching...
No Matches
local_history.h
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
20#pragma once
21
22#include <kicommon.h>
25
26#include <atomic>
27#include <future>
28#include <memory>
29#include <vector>
30#include <set>
31#include <map>
32#include <functional>
33#include <string>
34#include <wx/arrstr.h>
35#include <wx/datetime.h>
36#include <wx/string.h>
37#include <wx/window.h>
38
40class PROJECT;
41
48
49
57{
58 wxString relativePath;
59 std::string content;
60 wxString sourcePath;
61 bool prettify = false;
62 KICAD_FORMAT::FORMAT_MODE formatMode = KICAD_FORMAT::FORMAT_MODE::NORMAL;
63 bool removed = false;
64};
65
67{
68 wxString hash;
69 wxDateTime date;
70 wxString summary;
71 wxString message;
72 int filesChanged = 0;
73 wxArrayString changedFiles;
74};
75
81{
82public:
85
87 bool Init( const wxString& aProjectPath );
88
90 bool CommitSnapshot( const std::vector<wxString>& aFiles, const wxString& aTitle );
91
97 bool CommitFullProjectSnapshot( const wxString& aProjectPath, const wxString& aTitle );
98
109 void RegisterSaver(
110 const void* aSaverObject,
111 const std::function<void( const wxString&, std::vector<HISTORY_FILE_DATA>& )>& aSaver,
112 const std::weak_ptr<void>& aLifetime = {} );
113
116 void RegisterProjectSaver( PROJECT* aProject );
117
120 void UnregisterSaver( const void* aSaverObject );
121
123 void ClearAllSavers();
124
128 bool RunRegisteredSaversAndCommit( const wxString& aProjectPath, const wxString& aTitle,
129 const wxString& aTagFileType = wxEmptyString );
130
143 bool RunRegisteredSaversAsAutosaveFiles( const wxString& aProjectPath );
144
155 std::vector<std::pair<wxString, wxString>> FindStaleAutosaveFiles( const wxString& aProjectPath,
156 const std::vector<wxString>& aExtensions ) const;
157
170 static std::vector<std::pair<wxString, wxString>>
171 CollectAutosaveFilePairs( const wxString& aAutosaveRoot, const wxString& aProjectPath,
172 BACKUP_LOCATION aLocation );
173
181 void RemoveAutosaveFiles( const wxString& aProjectPath ) const;
182
189 void RemoveAutosaveFiles( const wxString& aProjectPath,
190 const std::vector<wxString>& aSourcePaths ) const;
191
193 void NoteFileChange( const wxString& aFile );
194
196 bool CommitPending();
197
199 bool HistoryExists( const wxString& aProjectPath );
200
202 bool TagSave( const wxString& aProjectPath, const wxString& aFileType );
203
206 bool CommitDuplicateOfLastSave( const wxString& aProjectPath, const wxString& aFileType,
207 const wxString& aMessage );
208
211 bool EnforceSizeLimit( const wxString& aProjectPath, size_t aMaxBytes, PROGRESS_REPORTER* aReporter = nullptr );
212
214 bool HeadNewerThanLastSave( const wxString& aProjectPath );
215
217 wxString GetHeadHash( const wxString& aProjectPath );
218
222 bool RestoreCommit( const wxString& aProjectPath, const wxString& aHash, wxWindow* aParent = nullptr,
223 bool aConfirm = true );
224
226 std::vector<LOCAL_HISTORY_SNAPSHOT_INFO> GetSnapshots( const wxString& aProjectPath );
227
233 bool ExtractAllFilesAtCommit( const wxString& aProjectPath, const wxString& aHash, const wxString& aDestDir,
234 const std::vector<wxString>& aExtensions = {} );
235
240 wxString TreeFingerprint( const wxString& aProjectPath, const wxString& aHash, const wxString& aExtension );
241
243 void ShowRestoreDialog( const wxString& aProjectPath, wxWindow* aParent );
244
248 void WaitForPendingSave();
249
251 bool IsCompacting() const;
252
255 void EnforceSizeLimitInBackground( const wxString& aProjectPath, size_t aMaxBytes );
256
257private:
258 std::vector<LOCAL_HISTORY_SNAPSHOT_INFO> LoadSnapshots( const wxString& aProjectPath );
259
261 bool enforceSizeLimit( const wxString& aProjectPath, const wxString& aHistoryPath, size_t aMaxBytes,
262 PROGRESS_REPORTER* aReporter );
263
265 SNAPSHOT_COMMIT_RESULT commitInBackground( const wxString& aProjectPath, const wxString& aTitle,
266 const std::vector<HISTORY_FILE_DATA>& aFileData,
267 bool aIsManualSave );
268
271 void scheduleCompaction( const wxString& aProjectPath, size_t aLooseLimit );
272
274 void pruneExpiredSavers();
275
276 // A registered saver plus the optional liveness token of the document it serializes. Tracking
277 // is opt-in because a live token at registration is what distinguishes a tracked saver from an
278 // untracked one (a default-constructed weak_ptr is indistinguishable from an expired one).
280 {
281 std::function<void( const wxString&, std::vector<HISTORY_FILE_DATA>& )> saver;
282 std::weak_ptr<void> lifetime;
283 bool tracked = false;
284 };
285
286 std::set<wxString> m_pendingFiles;
287 std::map<const void*, SAVER_ENTRY> m_savers;
288
289 std::atomic<bool> m_saveInProgress{ false };
290 std::future<bool> m_pendingFuture;
291
292 std::atomic<bool> m_cancelCompaction{ false };
293 std::future<void> m_compactFuture;
294};
295
std::vector< LOCAL_HISTORY_SNAPSHOT_INFO > LoadSnapshots(const wxString &aProjectPath)
void EnforceSizeLimitInBackground(const wxString &aProjectPath, size_t aMaxBytes)
Run EnforceSizeLimit without a reporter on a background thread, after any running compaction.
bool EnforceSizeLimit(const wxString &aProjectPath, size_t aMaxBytes, PROGRESS_REPORTER *aReporter=nullptr)
Enforce total size limit by rebuilding trimmed history keeping newest commits whose cumulative unique...
bool TagSave(const wxString &aProjectPath, const wxString &aFileType)
Tag a manual save in the local history repository.
static std::vector< std::pair< wxString, wxString > > CollectAutosaveFilePairs(const wxString &aAutosaveRoot, const wxString &aProjectPath, BACKUP_LOCATION aLocation)
Enumerate every (autosave, source) pair found under aAutosaveRoot for the project at aProjectPath,...
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.
std::vector< std::pair< wxString, wxString > > FindStaleAutosaveFiles(const wxString &aProjectPath, const std::vector< wxString > &aExtensions) const
Enumerate autosave files newer than their corresponding source files for the project at aProjectPath,...
wxString GetHeadHash(const wxString &aProjectPath)
Return the current head commit hash.
std::future< void > m_compactFuture
void RegisterProjectSaver(PROJECT *aProject)
Register aProject's own files (project, rules and library tables) for history snapshots.
SNAPSHOT_COMMIT_RESULT commitInBackground(const wxString &aProjectPath, const wxString &aTitle, const std::vector< HISTORY_FILE_DATA > &aFileData, bool aIsManualSave)
Execute file writes and git commit on a background thread.
void ShowRestoreDialog(const wxString &aProjectPath, wxWindow *aParent)
Show a dialog allowing the user to choose a snapshot to restore.
std::map< const void *, SAVER_ENTRY > m_savers
bool HeadNewerThanLastSave(const wxString &aProjectPath)
Return true if the autosave data is newer than the last manual save.
std::set< wxString > m_pendingFiles
bool CommitDuplicateOfLastSave(const wxString &aProjectPath, const wxString &aFileType, const wxString &aMessage)
Create a new commit duplicating the tree pointed to by Last_Save_<fileType> and move the Last_Save_<f...
void WaitForPendingSave()
Block until any pending background save completes.
bool RestoreCommit(const wxString &aProjectPath, const wxString &aHash, wxWindow *aParent=nullptr, bool aConfirm=true)
Restore the project files to the state recorded by the given commit hash.
void scheduleCompaction(const wxString &aProjectPath, size_t aLooseLimit)
Start packing loose history objects on a background thread once they exceed aLooseLimit bytes,...
bool IsCompacting() const
True while a background compaction started by autosave is still running.
bool Init(const wxString &aProjectPath)
Initialize the local history repository for the given project path.
void ClearAllSavers()
Clear all registered savers.
bool CommitSnapshot(const std::vector< wxString > &aFiles, const wxString &aTitle)
Commit the given files to the local history repository.
std::atomic< bool > m_saveInProgress
void NoteFileChange(const wxString &aFile)
Record that a file has been modified and should be included in the next snapshot.
bool enforceSizeLimit(const wxString &aProjectPath, const wxString &aHistoryPath, size_t aMaxBytes, PROGRESS_REPORTER *aReporter)
EnforceSizeLimit with the history directory already resolved, so it can run off the UI thread.
bool CommitPending()
Commit any pending modified files to the history repository.
bool HistoryExists(const wxString &aProjectPath)
Return true if history exists for the project.
bool RunRegisteredSaversAsAutosaveFiles(const wxString &aProjectPath)
Run all registered savers and write their output to autosave files instead of committing to the local...
bool CommitFullProjectSnapshot(const wxString &aProjectPath, const wxString &aTitle)
Commit a snapshot of the entire project directory (excluding the .history directory and ignored trans...
std::vector< LOCAL_HISTORY_SNAPSHOT_INFO > GetSnapshots(const wxString &aProjectPath)
Snapshots (commits) for the project, newest first.
wxString TreeFingerprint(const wxString &aProjectPath, const wxString &aHash, const wxString &aExtension)
Fingerprint of all files ending in aExtension recorded by commit aHash (sorted path:blob pairs).
bool ExtractAllFilesAtCommit(const wxString &aProjectPath, const wxString &aHash, const wxString &aDestDir, const std::vector< wxString > &aExtensions={})
Write files recorded at aHash into aDestDir, recreating the project's relative folder structure.
std::atomic< bool > m_cancelCompaction
std::future< bool > m_pendingFuture
void RegisterSaver(const void *aSaverObject, const std::function< void(const wxString &, std::vector< HISTORY_FILE_DATA > &)> &aSaver, const std::weak_ptr< void > &aLifetime={})
Register a saver callback invoked during autosave history commits.
void UnregisterSaver(const void *aSaverObject)
Unregister a previously registered saver callback.
void pruneExpiredSavers()
Drop tracked savers whose owning document has been freed, before any saver runs.
void RemoveAutosaveFiles(const wxString &aProjectPath) const
Remove every autosave file under the project at aProjectPath regardless of which source it shadowed.
A progress reporter interface for use in multi-threaded environments.
Container for project specific data.
Definition project.h:63
BACKUP_LOCATION
#define KICOMMON_API
Definition kicommon.h:27
SNAPSHOT_COMMIT_RESULT
Data produced by a registered saver on the UI thread, consumed by either the background local-history...
wxString sourcePath
For file-copy savers (small files like .kicad_pro)
bool removed
The file no longer exists and leaves the snapshot.
std::string content
Serialized content (mutually exclusive with sourcePath)
KICAD_FORMAT::FORMAT_MODE formatMode
wxString relativePath
Destination path relative to the project root.
std::function< void(const wxString &, std::vector< HISTORY_FILE_DATA > &)> saver
bool tracked
std::weak_ptr< void > lifetime