Skip to content

Commit 3cce6c5

Browse files
committed
feature: share settings between worktrees (#1665)
NOTE: we can only share settings between worktrees for reading! It means that the repository settings only saved by the main repository not worktrees Signed-off-by: leo <[email protected]>
1 parent 04055f6 commit 3cce6c5

File tree

2 files changed

+23
-43
lines changed

2 files changed

+23
-43
lines changed

src/Commands/QueryGitCommonDir.cs

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/ViewModels/Repository.cs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -508,11 +508,28 @@ public Repository(bool isBare, string path, string gitDir)
508508
IsBare = isBare;
509509
FullPath = path;
510510
GitDir = gitDir;
511+
512+
var commonDirFile = Path.Combine(_gitDir, "commondir");
513+
_isWorktree = _gitDir.Replace('\\', '/').IndexOf("/worktrees/", StringComparison.Ordinal) > 0 &&
514+
File.Exists(commonDirFile);
515+
516+
if (_isWorktree)
517+
{
518+
var commonDir = File.ReadAllText(commonDirFile).Trim();
519+
if (!Path.IsPathRooted(commonDir))
520+
commonDir = new DirectoryInfo(Path.Combine(_gitDir, commonDir)).FullName;
521+
522+
_gitCommonDir = commonDir;
523+
}
524+
else
525+
{
526+
_gitCommonDir = _gitDir;
527+
}
511528
}
512529

513530
public void Open()
514531
{
515-
var settingsFile = Path.Combine(_gitDir, "sourcegit.settings");
532+
var settingsFile = Path.Combine(_gitCommonDir, "sourcegit.settings");
516533
if (File.Exists(settingsFile))
517534
{
518535
try
@@ -532,16 +549,7 @@ public void Open()
532549

533550
try
534551
{
535-
// For worktrees, we need to watch the $GIT_COMMON_DIR instead of the $GIT_DIR.
536-
var gitDirForWatcher = _gitDir;
537-
if (_gitDir.Replace('\\', '/').IndexOf("/worktrees/", StringComparison.Ordinal) > 0)
538-
{
539-
var commonDir = new Commands.QueryGitCommonDir(_fullpath).GetResult();
540-
if (!string.IsNullOrEmpty(commonDir))
541-
gitDirForWatcher = commonDir;
542-
}
543-
544-
_watcher = new Models.Watcher(this, _fullpath, gitDirForWatcher);
552+
_watcher = new Models.Watcher(this, _fullpath, _gitCommonDir);
545553
}
546554
catch (Exception ex)
547555
{
@@ -570,16 +578,12 @@ public void Close()
570578
SelectedView = null; // Do NOT modify. Used to remove exists widgets for GC.Collect
571579
Logs.Clear();
572580

573-
try
581+
if (!_isWorktree)
574582
{
575583
_settings.LastCommitMessage = _workingCopy.CommitMessage;
576-
using var stream = File.Create(Path.Combine(_gitDir, "sourcegit.settings"));
584+
using var stream = File.Create(Path.Combine(_gitCommonDir, "sourcegit.settings"));
577585
JsonSerializer.Serialize(stream, _settings, JsonCodeGen.Default.RepositorySettings);
578586
}
579-
catch
580-
{
581-
// Ignore
582-
}
583587

584588
_autoFetchTimer.Dispose();
585589
_autoFetchTimer = null;
@@ -2007,6 +2011,8 @@ private void AutoFetchInBackground(object sender)
20072011

20082012
private string _fullpath = string.Empty;
20092013
private string _gitDir = string.Empty;
2014+
private string _gitCommonDir = string.Empty;
2015+
private bool _isWorktree = false;
20102016
private Models.RepositorySettings _settings = null;
20112017
private Models.FilterMode _historiesFilterMode = Models.FilterMode.None;
20122018
private bool _hasAllowedSignersFile = false;

0 commit comments

Comments
 (0)