25#include <wx/filename.h>
29#include <wx/datetime.h>
33#define HISTORY_LOCK_TRACE wxT( "KICAD_HISTORY_LOCK" )
45 return historyPath( aProjectPath ) + wxS(
".repo" );
56 int aStaleTimeoutSec ) :
65 wxLogTrace(
HISTORY_LOCK_TRACE,
"Attempting to acquire lock for project: %s (timeout: %d sec)",
89 wxLogTrace(
HISTORY_LOCK_TRACE,
"Successfully acquired all locks for project: %s", aProjectPath );
106 git_repository_free(
m_repo );
130 return wxString::Format( wxS(
"%s@%s" ),
134 return wxEmptyString;
143 wxLogNull suppressSysErrorPopups;
145 if( !wxFileName::Mkdir(
m_historyPath, 0777, wxPATH_MKDIR_FULL ) )
158 _(
"History repository is locked by %s@%s" ),
166 if( !legacyLock.
Valid() )
168 m_lockError =
_(
"History repository is locked by an older KiCad instance." );
190 const git_error* err = git_error_last();
192 _(
"Failed to open git repository: %s" ),
193 err ? wxString::FromUTF8( err->message ) : wxString(
"Unknown error" ) );
202 const git_error* err = git_error_last();
204 err ? wxString::FromUTF8( err->message ) : wxString(
"Unknown error" ) );
212 if( ignoreFile.FileExists() )
214 wxFFile f( ignoreFile.GetFullPath(), wxT(
"rb" ) );
217 if( f.IsOpened() && f.ReadAll( &content ) )
218 git_ignore_add_rule(
m_repo, content.utf8_str() );
230 m_lockError =
_(
"Cannot acquire index lock: repository not open" );
239 const git_error* err = git_error_last();
241 _(
"Failed to acquire git index lock (another operation in progress?): %s" ),
242 err ? wxString::FromUTF8( err->message ) : wxString(
"Unknown error" ) );
254 if( aStaleTimeoutSec <= 0 )
259 if( !lockPath.FileExists() )
263 if( !lockPath.GetTimes(
nullptr, &modTime,
nullptr ) )
266 wxDateTime now = wxDateTime::Now();
267 wxTimeSpan age = now - modTime;
270 (
int)age.GetSeconds().ToLong(), aStaleTimeoutSec );
272 return age.GetSeconds().ToLong() > aStaleTimeoutSec
296 git_repository_free(
m_repo );
312 m_lockError =
_(
"Cannot re-open history repository without the file lock" );
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
wxString GetLockHolder() const
Get information about who currently holds the lock.
HISTORY_LOCK_MANAGER(const wxString &aProjectPath, int aStaleTimeoutSec=0)
Construct a lock manager and attempt to acquire locks.
void ReleaseRepository()
Release git repository and index handles early, but keep the file lock.
~HISTORY_LOCK_MANAGER()
Destructor releases all locks and closes git repository.
wxString GetLockError() const
Get error message describing why lock could not be acquired.
static bool BreakStaleLock(const wxString &aProjectPath)
Claim and remove an abandoned lock belonging to this user.
std::unique_ptr< LOCKFILE > m_fileLock
git_repository * ReopenRepository()
Re-open the git repository and index after ReleaseRepository().
static bool IsLockStale(const wxString &aProjectPath, int aStaleTimeoutSec=0)
Check if a lock file exists and is stale (older than timeout).
bool IsLocked() const
Check if locks were successfully acquired.
Advisory lock over a file, taken by writing a sibling lock file and holding an exclusive lock on it f...
static wxString LockPathFor(const wxString &aFilename)
static LOCKFILE Inspect(const wxString &aFilename)
Look at a lock without taking it: nothing is created, nothing is claimed and nothing is removed on re...
virtual SETTINGS_MANAGER & GetSettingsManager() const
wxString GetLocalHistoryDirForPath(const wxString &aProjectPath) const
Resolve the local-history directory for a project given by its on-disk path.
int m_HistoryLockStaleTimeout
Stale lock timeout for local history repository locks, in seconds.
static wxString historyPath(const wxString &aProjectPath)
static wxString historyLockPath(const wxString &aProjectPath)
#define HISTORY_LOCK_TRACE
PGM_BASE & Pgm()
The global program "get" accessor.