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
40
47
48
56{
57 wxString relativePath;
58 std::string content;
59 wxString sourcePath;
60 bool prettify = false;
61 KICAD_FORMAT::FORMAT_MODE formatMode = KICAD_FORMAT::FORMAT_MODE::NORMAL;
62};
63
65{
66 wxString hash;
67 wxDateTime date;
68 wxString summary;
69 wxString message;
70 int filesChanged = 0;
71 wxArrayString changedFiles;
72};
73
79{
80public:
83
85 bool Init( const wxString& aProjectPath );
86
88 bool CommitSnapshot( const std::vector<wxString>& aFiles, const wxString& aTitle );
89
95 bool CommitFullProjectSnapshot( const wxString& aProjectPath, const wxString& aTitle );
96
107 void RegisterSaver(
108 const void* aSaverObject,
109 const std::function<void( const wxString&, std::vector<HISTORY_FILE_DATA>& )>& aSaver,
110 const std::weak_ptr<void>& aLifetime = {} );
111
114 void UnregisterSaver( const void* aSaverObject );
115
117 void ClearAllSavers();
118
122 bool RunRegisteredSaversAndCommit( const wxString& aProjectPath, const wxString& aTitle,
123 const wxString& aTagFileType = wxEmptyString );
124
137 bool RunRegisteredSaversAsAutosaveFiles( const wxString& aProjectPath );
138
149 std::vector<std::pair<wxString, wxString>> FindStaleAutosaveFiles( const wxString& aProjectPath,
150 const std::vector<wxString>& aExtensions ) const;
151
164 static std::vector<std::pair<wxString, wxString>>
165 CollectAutosaveFilePairs( const wxString& aAutosaveRoot, const wxString& aProjectPath,
166 BACKUP_LOCATION aLocation );
167
175 void RemoveAutosaveFiles( const wxString& aProjectPath ) const;
176
183 void RemoveAutosaveFiles( const wxString& aProjectPath,
184 const std::vector<wxString>& aSourcePaths ) const;
185
187 void NoteFileChange( const wxString& aFile );
188
190 bool CommitPending();
191
193 bool HistoryExists( const wxString& aProjectPath );
194
196 bool TagSave( const wxString& aProjectPath, const wxString& aFileType );
197
200 bool CommitDuplicateOfLastSave( const wxString& aProjectPath, const wxString& aFileType,
201 const wxString& aMessage );
202
205 bool EnforceSizeLimit( const wxString& aProjectPath, size_t aMaxBytes, PROGRESS_REPORTER* aReporter = nullptr );
206
208 bool HeadNewerThanLastSave( const wxString& aProjectPath );
209
211 wxString GetHeadHash( const wxString& aProjectPath );
212
216 bool RestoreCommit( const wxString& aProjectPath, const wxString& aHash, wxWindow* aParent = nullptr,
217 bool aConfirm = true );
218
220 std::vector<LOCAL_HISTORY_SNAPSHOT_INFO> GetSnapshots( const wxString& aProjectPath );
221
227 bool ExtractAllFilesAtCommit( const wxString& aProjectPath, const wxString& aHash, const wxString& aDestDir,
228 const std::vector<wxString>& aExtensions = {} );
229
234 wxString TreeFingerprint( const wxString& aProjectPath, const wxString& aHash, const wxString& aExtension );
235
237 void ShowRestoreDialog( const wxString& aProjectPath, wxWindow* aParent );
238
242 void WaitForPendingSave();
243
245 bool IsCompacting() const;
246
249 void EnforceSizeLimitInBackground( const wxString& aProjectPath, size_t aMaxBytes );
250
251private:
252 std::vector<LOCAL_HISTORY_SNAPSHOT_INFO> LoadSnapshots( const wxString& aProjectPath );
253
255 bool enforceSizeLimit( const wxString& aProjectPath, const wxString& aHistoryPath, size_t aMaxBytes,
256 PROGRESS_REPORTER* aReporter );
257
259 SNAPSHOT_COMMIT_RESULT commitInBackground( const wxString& aProjectPath, const wxString& aTitle,
260 const std::vector<HISTORY_FILE_DATA>& aFileData,
261 bool aIsManualSave );
262
265 void scheduleCompaction( const wxString& aProjectPath, size_t aLooseLimit );
266
268 void pruneExpiredSavers();
269
270 // A registered saver plus the optional liveness token of the document it serializes. Tracking
271 // is opt-in because a live token at registration is what distinguishes a tracked saver from an
272 // untracked one (a default-constructed weak_ptr is indistinguishable from an expired one).
274 {
275 std::function<void( const wxString&, std::vector<HISTORY_FILE_DATA>& )> saver;
276 std::weak_ptr<void> lifetime;
277 bool tracked = false;
278 };
279
280 std::set<wxString> m_pendingFiles;
281 std::map<const void*, SAVER_ENTRY> m_savers;
282
283 std::atomic<bool> m_saveInProgress{ false };
284 std::future<bool> m_pendingFuture;
285
286 std::atomic<bool> m_cancelCompaction{ false };
287 std::future<void> m_compactFuture;
288};
289
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
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.
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)
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