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, you may find one here:
18 * http://www.gnu.org/licenses/gpl-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#pragma once
25
26#include <kicommon.h>
28
29#include <atomic>
30#include <future>
31#include <vector>
32#include <set>
33#include <map>
34#include <functional>
35#include <string>
36#include <wx/arrstr.h>
37#include <wx/datetime.h>
38#include <wx/string.h>
39#include <wx/window.h>
40
42
43
49{
50 wxString path;
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
95 void RegisterSaver(
96 const void* aSaverObject,
97 const std::function<void( const wxString&, std::vector<HISTORY_FILE_DATA>& )>& aSaver );
98
101 void UnregisterSaver( const void* aSaverObject );
102
104 void ClearAllSavers();
105
107 bool RunRegisteredSaversAndCommit( const wxString& aProjectPath, const wxString& aTitle );
108
110 void NoteFileChange( const wxString& aFile );
111
113 bool CommitPending();
114
116 bool HistoryExists( const wxString& aProjectPath );
117
119 bool TagSave( const wxString& aProjectPath, const wxString& aFileType );
120
123 bool CommitDuplicateOfLastSave( const wxString& aProjectPath, const wxString& aFileType,
124 const wxString& aMessage );
125
128 bool EnforceSizeLimit( const wxString& aProjectPath, size_t aMaxBytes, PROGRESS_REPORTER* aReporter = nullptr );
129
131 bool HeadNewerThanLastSave( const wxString& aProjectPath );
132
134 wxString GetHeadHash( const wxString& aProjectPath );
135
137 bool RestoreCommit( const wxString& aProjectPath, const wxString& aHash, wxWindow* aParent = nullptr );
138
140 void ShowRestoreDialog( const wxString& aProjectPath, wxWindow* aParent );
141
142private:
143 std::vector<LOCAL_HISTORY_SNAPSHOT_INFO> LoadSnapshots( const wxString& aProjectPath );
144
146 bool commitInBackground( const wxString& aProjectPath, const wxString& aTitle,
147 const std::vector<HISTORY_FILE_DATA>& aFileData );
148
150 void WaitForPendingSave();
151
152 std::set<wxString> m_pendingFiles;
153 std::map<const void*,
154 std::function<void( const wxString&, std::vector<HISTORY_FILE_DATA>& )>> m_savers;
155
156 std::atomic<bool> m_saveInProgress{ false };
157 std::future<bool> m_pendingFuture;
158};
159
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.
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 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...
bool commitInBackground(const wxString &aProjectPath, const wxString &aTitle, const std::vector< HISTORY_FILE_DATA > &aFileData)
Execute file writes and git commit on a background thread.
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
bool RunRegisteredSaversAndCommit(const wxString &aProjectPath, const wxString &aTitle)
Run all registered savers and, if any staged changes differ from HEAD, create a commit.
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 CommitFullProjectSnapshot(const wxString &aProjectPath, const wxString &aTitle)
Commit a snapshot of the entire project directory (excluding the .history directory and ignored trans...
std::future< bool > m_pendingFuture
void UnregisterSaver(const void *aSaverObject)
Unregister a previously registered saver callback.
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 the background commit thread.
wxString sourcePath
For file-copy savers (small files like .kicad_pro)
std::string content
Serialized content (mutually exclusive with sourcePath)
wxString path
Destination inside .history/.
KICAD_FORMAT::FORMAT_MODE formatMode