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>
24
25#include <atomic>
26#include <future>
27#include <vector>
28#include <set>
29#include <map>
30#include <functional>
31#include <string>
32#include <wx/arrstr.h>
33#include <wx/datetime.h>
34#include <wx/string.h>
35#include <wx/window.h>
36
38
39
47{
48 wxString relativePath;
49 std::string content;
50 wxString sourcePath;
51 bool prettify = false;
52 KICAD_FORMAT::FORMAT_MODE formatMode = KICAD_FORMAT::FORMAT_MODE::NORMAL;
53};
54
56{
57 wxString hash;
58 wxDateTime date;
59 wxString summary;
60 wxString message;
61 int filesChanged = 0;
62 wxArrayString changedFiles;
63};
64
70{
71public:
74
76 bool Init( const wxString& aProjectPath );
77
79 bool CommitSnapshot( const std::vector<wxString>& aFiles, const wxString& aTitle );
80
86 bool CommitFullProjectSnapshot( const wxString& aProjectPath, const wxString& aTitle );
87
93 void RegisterSaver(
94 const void* aSaverObject,
95 const std::function<void( const wxString&, std::vector<HISTORY_FILE_DATA>& )>& aSaver );
96
99 void UnregisterSaver( const void* aSaverObject );
100
102 void ClearAllSavers();
103
107 bool RunRegisteredSaversAndCommit( const wxString& aProjectPath, const wxString& aTitle,
108 const wxString& aTagFileType = wxEmptyString );
109
122 bool RunRegisteredSaversAsAutosaveFiles( const wxString& aProjectPath );
123
134 std::vector<std::pair<wxString, wxString>> FindStaleAutosaveFiles( const wxString& aProjectPath,
135 const std::vector<wxString>& aExtensions ) const;
136
144 void RemoveAutosaveFiles( const wxString& aProjectPath ) const;
145
152 void RemoveAutosaveFiles( const wxString& aProjectPath,
153 const std::vector<wxString>& aSourcePaths ) const;
154
156 void NoteFileChange( const wxString& aFile );
157
159 bool CommitPending();
160
162 bool HistoryExists( const wxString& aProjectPath );
163
165 bool TagSave( const wxString& aProjectPath, const wxString& aFileType );
166
169 bool CommitDuplicateOfLastSave( const wxString& aProjectPath, const wxString& aFileType,
170 const wxString& aMessage );
171
174 bool EnforceSizeLimit( const wxString& aProjectPath, size_t aMaxBytes, PROGRESS_REPORTER* aReporter = nullptr );
175
177 bool HeadNewerThanLastSave( const wxString& aProjectPath );
178
180 wxString GetHeadHash( const wxString& aProjectPath );
181
183 bool RestoreCommit( const wxString& aProjectPath, const wxString& aHash, wxWindow* aParent = nullptr );
184
186 std::vector<LOCAL_HISTORY_SNAPSHOT_INFO> GetSnapshots( const wxString& aProjectPath );
187
193 bool ExtractAllFilesAtCommit( const wxString& aProjectPath, const wxString& aHash, const wxString& aDestDir,
194 const std::vector<wxString>& aExtensions = {} );
195
200 wxString TreeFingerprint( const wxString& aProjectPath, const wxString& aHash, const wxString& aExtension );
201
203 void ShowRestoreDialog( const wxString& aProjectPath, wxWindow* aParent );
204
208 void WaitForPendingSave();
209
210private:
211 std::vector<LOCAL_HISTORY_SNAPSHOT_INFO> LoadSnapshots( const wxString& aProjectPath );
212
214 bool commitInBackground( const wxString& aProjectPath, const wxString& aTitle,
215 const std::vector<HISTORY_FILE_DATA>& aFileData, bool aIsManualSave );
216
217 std::set<wxString> m_pendingFiles;
218 std::map<const void*,
219 std::function<void( const wxString&, std::vector<HISTORY_FILE_DATA>& )>> m_savers;
220
221 std::atomic<bool> m_saveInProgress{ false };
222 std::future<bool> m_pendingFuture;
223};
224
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.
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 RestoreCommit(const wxString &aProjectPath, const wxString &aHash, wxWindow *aParent=nullptr)
Restore the project files to the state recorded by the given 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.
bool HeadNewerThanLastSave(const wxString &aProjectPath)
Return true if the autosave data is newer than the last manual save.
std::set< wxString > m_pendingFiles
std::map< const void *, std::function< void(const wxString &, std::vector< HISTORY_FILE_DATA > &)> > m_savers
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.
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.
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 UnregisterSaver(const void *aSaverObject)
Unregister a previously registered saver callback.
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.
#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.