From b4ab4afd3ab8eded049ea7b4849d821f64d14273 Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 17 Mar 2025 16:17:20 +0800 Subject: [PATCH] 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 --- src/Models/IRepository.cs | 3 +-- src/Models/Watcher.cs | 5 +---- src/ViewModels/Launcher.cs | 6 +----- src/ViewModels/Repository.cs | 19 +++++++++++++------ 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/Models/IRepository.cs b/src/Models/IRepository.cs index 90409c2f..9283266e 100644 --- a/src/Models/IRepository.cs +++ b/src/Models/IRepository.cs @@ -3,8 +3,7 @@ public interface IRepository { string FullPath { get; set; } - string GitDir { get; set; } - string GitCommonDir { get; set; } + string GitDirForWatcher { get; } void RefreshBranches(); void RefreshWorktrees(); diff --git a/src/Models/Watcher.cs b/src/Models/Watcher.cs index 320f31d6..d419363a 100644 --- a/src/Models/Watcher.cs +++ b/src/Models/Watcher.cs @@ -23,11 +23,8 @@ namespace SourceGit.Models _wcWatcher.Deleted += OnWorkingCopyChanged; _wcWatcher.EnableRaisingEvents = true; - // If this repository is a worktree repository, just watch the main repository's gitdir. - var repoWatchDir = _repo.GitCommonDir.Replace("\\", "/"); - _repoWatcher = new FileSystemWatcher(); - _repoWatcher.Path = repoWatchDir; + _repoWatcher.Path = _repo.GitDirForWatcher; _repoWatcher.Filter = "*"; _repoWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.DirectoryName | NotifyFilters.FileName; _repoWatcher.IncludeSubdirectories = true; diff --git a/src/ViewModels/Launcher.cs b/src/ViewModels/Launcher.cs index 4307665d..06479394 100644 --- a/src/ViewModels/Launcher.cs +++ b/src/ViewModels/Launcher.cs @@ -282,7 +282,6 @@ namespace SourceGit.ViewModels var isBare = new Commands.IsBareRepository(node.Id).Result(); var gitDir = node.Id; - var gitCommonDir = gitDir; if (!isBare) { gitDir = new Commands.QueryGitDir(node.Id).Result(); @@ -292,12 +291,9 @@ namespace SourceGit.ViewModels App.RaiseException(ctx, "Given path is not a valid git repository!"); return; } - gitCommonDir = new Commands.QueryGitCommonDir(node.Id).Result(); - if (string.IsNullOrEmpty(gitCommonDir)) - gitCommonDir = gitDir; } - var repo = new Repository(isBare, node.Id, gitDir, gitCommonDir); + var repo = new Repository(isBare, node.Id, gitDir); repo.Open(); if (page == null) diff --git a/src/ViewModels/Repository.cs b/src/ViewModels/Repository.cs index 5cc933e9..85665634 100644 --- a/src/ViewModels/Repository.cs +++ b/src/ViewModels/Repository.cs @@ -45,10 +45,19 @@ namespace SourceGit.ViewModels set => SetProperty(ref _gitDir, value); } - public string GitCommonDir + public string GitDirForWatcher { - get => _gitCommonDir; - set => SetProperty(ref _gitCommonDir, value); + get + { + // Only try to get `$GIT_COMMON_DIR` if this repository looks like a worktree. + if (_gitDir.Replace("\\", "/").IndexOf(".git/worktrees/", StringComparison.Ordinal) > 0) + { + var commonDir = new Commands.QueryGitCommonDir(_fullpath).Result(); + return string.IsNullOrEmpty(commonDir) ? _gitDir : commonDir; + } + + return _gitDir; + } } public Models.RepositorySettings Settings @@ -435,12 +444,11 @@ namespace SourceGit.ViewModels set; } = 0; - public Repository(bool isBare, string path, string gitDir, string gitCommonDir) + public Repository(bool isBare, string path, string gitDir) { IsBare = isBare; FullPath = path; GitDir = gitDir; - GitCommonDir = gitCommonDir; } public void Open() @@ -2444,7 +2452,6 @@ namespace SourceGit.ViewModels private string _fullpath = string.Empty; private string _gitDir = string.Empty; - private string _gitCommonDir = string.Empty; private Models.RepositorySettings _settings = null; private Models.FilterMode _historiesFilterMode = Models.FilterMode.None; private bool _hasAllowedSignersFile = false;