Skip to content

Commit 7031693

Browse files
committed
refactor: pass dirs to watcher directly
Signed-off-by: leo <[email protected]>
1 parent b4ab4af commit 7031693

File tree

3 files changed

+13
-22
lines changed

3 files changed

+13
-22
lines changed

src/Models/IRepository.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
{
33
public interface IRepository
44
{
5-
string FullPath { get; set; }
6-
string GitDirForWatcher { get; }
7-
85
void RefreshBranches();
96
void RefreshWorktrees();
107
void RefreshTags();

src/Models/Watcher.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ namespace SourceGit.Models
88
{
99
public class Watcher : IDisposable
1010
{
11-
public Watcher(IRepository repo)
11+
public Watcher(IRepository repo, string fullpath, string gitDir)
1212
{
1313
_repo = repo;
1414

1515
_wcWatcher = new FileSystemWatcher();
16-
_wcWatcher.Path = _repo.FullPath;
16+
_wcWatcher.Path = fullpath;
1717
_wcWatcher.Filter = "*";
1818
_wcWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.Size | NotifyFilters.CreationTime;
1919
_wcWatcher.IncludeSubdirectories = true;
@@ -24,7 +24,7 @@ public Watcher(IRepository repo)
2424
_wcWatcher.EnableRaisingEvents = true;
2525

2626
_repoWatcher = new FileSystemWatcher();
27-
_repoWatcher.Path = _repo.GitDirForWatcher;
27+
_repoWatcher.Path = gitDir;
2828
_repoWatcher.Filter = "*";
2929
_repoWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.DirectoryName | NotifyFilters.FileName;
3030
_repoWatcher.IncludeSubdirectories = true;

src/ViewModels/Repository.cs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,6 @@ public string GitDir
4545
set => SetProperty(ref _gitDir, value);
4646
}
4747

48-
public string GitDirForWatcher
49-
{
50-
get
51-
{
52-
// Only try to get `$GIT_COMMON_DIR` if this repository looks like a worktree.
53-
if (_gitDir.Replace("\\", "/").IndexOf(".git/worktrees/", StringComparison.Ordinal) > 0)
54-
{
55-
var commonDir = new Commands.QueryGitCommonDir(_fullpath).Result();
56-
return string.IsNullOrEmpty(commonDir) ? _gitDir : commonDir;
57-
}
58-
59-
return _gitDir;
60-
}
61-
}
62-
6348
public Models.RepositorySettings Settings
6449
{
6550
get => _settings;
@@ -472,7 +457,16 @@ public void Open()
472457

473458
try
474459
{
475-
_watcher = new Models.Watcher(this);
460+
// For worktrees, we need to watch the $GIT_COMMON_DIR instead of the $GIT_DIR.
461+
var gitDirForWatcher = _gitDir;
462+
if (_gitDir.Replace("\\", "/").IndexOf(".git/worktrees/", StringComparison.Ordinal) > 0)
463+
{
464+
var commonDir = new Commands.QueryGitCommonDir(_fullpath).Result();
465+
if (!string.IsNullOrEmpty(commonDir))
466+
gitDirForWatcher = commonDir;
467+
}
468+
469+
_watcher = new Models.Watcher(this, _fullpath, gitDirForWatcher);
476470
}
477471
catch (Exception ex)
478472
{

0 commit comments

Comments
 (0)