Skip to content

Commit b4ab4af

Browse files
committed
code_review: PR #1103
Since we only use `$GIT_COMMON_DIR` in filesystem watcher, it is unnecessary to store this value in `Repository`, and we can query the `$GIT_COMMON_DIR` only when it looks like a worktree Signed-off-by: leo <[email protected]>
1 parent cea8a90 commit b4ab4af

File tree

4 files changed

+16
-17
lines changed

4 files changed

+16
-17
lines changed

src/Models/IRepository.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
public interface IRepository
44
{
55
string FullPath { get; set; }
6-
string GitDir { get; set; }
7-
string GitCommonDir { get; set; }
6+
string GitDirForWatcher { get; }
87

98
void RefreshBranches();
109
void RefreshWorktrees();

src/Models/Watcher.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,8 @@ public Watcher(IRepository repo)
2323
_wcWatcher.Deleted += OnWorkingCopyChanged;
2424
_wcWatcher.EnableRaisingEvents = true;
2525

26-
// If this repository is a worktree repository, just watch the main repository's gitdir.
27-
var repoWatchDir = _repo.GitCommonDir.Replace("\\", "/");
28-
2926
_repoWatcher = new FileSystemWatcher();
30-
_repoWatcher.Path = repoWatchDir;
27+
_repoWatcher.Path = _repo.GitDirForWatcher;
3128
_repoWatcher.Filter = "*";
3229
_repoWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.DirectoryName | NotifyFilters.FileName;
3330
_repoWatcher.IncludeSubdirectories = true;

src/ViewModels/Launcher.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,6 @@ public void OpenRepositoryInTab(RepositoryNode node, LauncherPage page)
282282

283283
var isBare = new Commands.IsBareRepository(node.Id).Result();
284284
var gitDir = node.Id;
285-
var gitCommonDir = gitDir;
286285
if (!isBare)
287286
{
288287
gitDir = new Commands.QueryGitDir(node.Id).Result();
@@ -292,12 +291,9 @@ public void OpenRepositoryInTab(RepositoryNode node, LauncherPage page)
292291
App.RaiseException(ctx, "Given path is not a valid git repository!");
293292
return;
294293
}
295-
gitCommonDir = new Commands.QueryGitCommonDir(node.Id).Result();
296-
if (string.IsNullOrEmpty(gitCommonDir))
297-
gitCommonDir = gitDir;
298294
}
299295

300-
var repo = new Repository(isBare, node.Id, gitDir, gitCommonDir);
296+
var repo = new Repository(isBare, node.Id, gitDir);
301297
repo.Open();
302298

303299
if (page == null)

src/ViewModels/Repository.cs

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

48-
public string GitCommonDir
48+
public string GitDirForWatcher
4949
{
50-
get => _gitCommonDir;
51-
set => SetProperty(ref _gitCommonDir, value);
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+
}
5261
}
5362

5463
public Models.RepositorySettings Settings
@@ -435,12 +444,11 @@ public int CommitDetailActivePageIndex
435444
set;
436445
} = 0;
437446

438-
public Repository(bool isBare, string path, string gitDir, string gitCommonDir)
447+
public Repository(bool isBare, string path, string gitDir)
439448
{
440449
IsBare = isBare;
441450
FullPath = path;
442451
GitDir = gitDir;
443-
GitCommonDir = gitCommonDir;
444452
}
445453

446454
public void Open()
@@ -2444,7 +2452,6 @@ private void AutoFetchImpl(object sender)
24442452

24452453
private string _fullpath = string.Empty;
24462454
private string _gitDir = string.Empty;
2447-
private string _gitCommonDir = string.Empty;
24482455
private Models.RepositorySettings _settings = null;
24492456
private Models.FilterMode _historiesFilterMode = Models.FilterMode.None;
24502457
private bool _hasAllowedSignersFile = false;

0 commit comments

Comments
 (0)