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
41
49{
50 wxString relativePath;
51 std::string content;
52 wxString sourcePath;
53 bool prettify = false;
54 KICAD_FORMAT::FORMAT_MODE formatMode = KICAD_FORMAT::FORMAT_MODE::NORMAL;
55};
56
58{
59 wxString hash;
60 wxDateTime date;
61 wxString summary;
62 wxString message;
63 int filesChanged = 0;
64 wxArrayString changedFiles;
65};
66
72{
73public:
76
78 bool Init( const wxString& aProjectPath );
79
81 bool CommitSnapshot( const std::vector<wxString>& aFiles, const wxString& aTitle );
82
88 bool CommitFullProjectSnapshot( const wxString& aProjectPath, const wxString& aTitle );
89
100 void RegisterSaver(
101 const void* aSaverObject,
102 const std::function<void( const wxString&, std::vector<HISTORY_FILE_DATA>& )>& aSaver,
103 const std::weak_ptr<void>& aLifetime = {} );
104
107 void UnregisterSaver( const void* aSaverObject );
108
110 void ClearAllSavers();
111
115 bool RunRegisteredSaversAndCommit( const wxString& aProjectPath, const wxString& aTitle,
116 const wxString& aTagFileType = wxEmptyString );
117
130 bool RunRegisteredSaversAsAutosaveFiles( const wxString& aProjectPath );
131
142 std::vector<std::pair<wxString, wxString>> FindStaleAutosaveFiles( const wxString& aProjectPath,
143 const std::vector<wxString>& aExtensions ) const;
144
157 static std::vector<std::pair<wxString, wxString>>
158 CollectAutosaveFilePairs( const wxString& aAutosaveRoot, const wxString& aProjectPath,
159 BACKUP_LOCATION aLocation );
160
168 void RemoveAutosaveFiles( const wxString& aProjectPath ) const;
169
176 void RemoveAutosaveFiles( const wxString& aProjectPath,
177 const std::vector<wxString>& aSourcePaths ) const;
178
180 void NoteFileChange( const wxString& aFile );
181
183 bool CommitPending();
184
186 bool HistoryExists( const wxString& aProjectPath );
187
189 bool TagSave( const wxString& aProjectPath, const wxString& aFileType );
190
193 bool CommitDuplicateOfLastSave( const wxString& aProjectPath, const wxString& aFileType,
194 const wxString& aMessage );
195
198 bool EnforceSizeLimit( const wxString& aProjectPath, size_t aMaxBytes, PROGRESS_REPORTER* aReporter = nullptr );
199
201 bool HeadNewerThanLastSave( const wxString& aProjectPath );
202
204 wxString GetHeadHash( const wxString& aProjectPath );
205
209 bool RestoreCommit( const wxString& aProjectPath, const wxString& aHash, wxWindow* aParent = nullptr,
210 bool aConfirm = true );
211
213 std::vector<LOCAL_HISTORY_SNAPSHOT_INFO> GetSnapshots( const wxString& aProjectPath );
214
220 bool ExtractAllFilesAtCommit( const wxString& aProjectPath, const wxString& aHash, const wxString& aDestDir,
221 const std::vector<wxString>& aExtensions = {} );
222
227 wxString TreeFingerprint( const wxString& aProjectPath, const wxString& aHash, const wxString& aExtension );
228
230 void ShowRestoreDialog( const wxString& aProjectPath, wxWindow* aParent );
231
235 void WaitForPendingSave();
236
237private:
238 std::vector<LOCAL_HISTORY_SNAPSHOT_INFO> LoadSnapshots( const wxString& aProjectPath );
239
241 bool commitInBackground( const wxString& aProjectPath, const wxString& aTitle,
242 const std::vector<HISTORY_FILE_DATA>& aFileData, bool aIsManualSave );
243
245 void pruneExpiredSavers();
246
247 // A registered saver plus the optional liveness token of the document it serializes. Tracking
248 // is opt-in because a live token at registration is what distinguishes a tracked saver from an
249 // untracked one (a default-constructed weak_ptr is indistinguishable from an expired one).
251 {
252 std::function<void( const wxString&, std::vector<HISTORY_FILE_DATA>& )> saver;
253 std::weak_ptr<void> lifetime;
254 bool tracked = false;
255 };
256
257 std::set<wxString> m_pendingFiles;
258 std::map<const void*, SAVER_ENTRY> m_savers;
259
260 std::atomic<bool> m_saveInProgress{ false };
261 std::future<bool> m_pendingFuture;
262};
263
std::vector< LOCAL_HISTORY_SNAPSHOT_INFO > LoadSnapshots(const wxString &aProjectPath)
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.
bool 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.
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 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::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
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