From 6ddbc6f92a3aafa0142feead788420b8400a19da Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 4 Aug 2025 20:35:24 +0800 Subject: [PATCH] code_review: PR #1660 Since `$GIT_DIR` is the same with `$GIT_COMMON_DIR` for submodules, use `$GIT_DIR` directly to ignore submodule's main worktree while querying worktrees Signed-off-by: leo --- src/Commands/Worktree.cs | 18 ++++++++++++------ src/Models/Worktree.cs | 1 - src/ViewModels/Repository.cs | 35 +++++++++++++++++++++-------------- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/Commands/Worktree.cs b/src/Commands/Worktree.cs index af03029f..1c3f8915 100644 --- a/src/Commands/Worktree.cs +++ b/src/Commands/Worktree.cs @@ -30,26 +30,32 @@ namespace SourceGit.Commands last = new Models.Worktree() { FullPath = line.Substring(9).Trim() }; last.RelativePath = Path.GetRelativePath(WorkingDirectory, last.FullPath); worktrees.Add(last); + continue; } - else if (line.StartsWith("bare", StringComparison.Ordinal)) + + if (last == null) + continue; + + if (line.StartsWith("bare", StringComparison.Ordinal)) { - last!.IsBare = true; + worktrees.Remove(last); + last = null; } else if (line.StartsWith("HEAD ", StringComparison.Ordinal)) { - last!.Head = line.Substring(5).Trim(); + last.Head = line.Substring(5).Trim(); } else if (line.StartsWith("branch ", StringComparison.Ordinal)) { - last!.Branch = line.Substring(7).Trim(); + last.Branch = line.Substring(7).Trim(); } else if (line.StartsWith("detached", StringComparison.Ordinal)) { - last!.IsDetached = true; + last.IsDetached = true; } else if (line.StartsWith("locked", StringComparison.Ordinal)) { - last!.IsLocked = true; + last.IsLocked = true; } } } diff --git a/src/Models/Worktree.cs b/src/Models/Worktree.cs index 26f88a8a..00eb33a0 100644 --- a/src/Models/Worktree.cs +++ b/src/Models/Worktree.cs @@ -9,7 +9,6 @@ namespace SourceGit.Models public string FullPath { get; set; } = string.Empty; public string RelativePath { get; set; } = string.Empty; public string Head { get; set; } = string.Empty; - public bool IsBare { get; set; } = false; public bool IsDetached { get; set; } = false; public bool IsLocked diff --git a/src/ViewModels/Repository.cs b/src/ViewModels/Repository.cs index 3718909d..b2ffaf0e 100644 --- a/src/ViewModels/Repository.cs +++ b/src/ViewModels/Repository.cs @@ -1219,25 +1219,32 @@ namespace SourceGit.ViewModels public void RefreshWorktrees() { var worktrees = new Commands.Worktree(_fullpath).ReadAllAsync().Result; - string commonDir = null; if (worktrees.Count > 0) - commonDir = new Commands.QueryGitCommonDir(_fullpath).GetResultAsync().Result; - var cleaned = new List(); - - foreach (var worktree in worktrees) { - if (worktree.IsBare || worktree.FullPath.Equals(_fullpath)) - continue; - if (!string.IsNullOrEmpty(commonDir) && worktree.FullPath.Equals(commonDir)) - continue; + var cleaned = new List(); + var normalizedGitDir = _gitDir.Replace('\\', '/'); - cleaned.Add(worktree); + foreach (var worktree in worktrees) + { + if (worktree.FullPath.Equals(_fullpath, StringComparison.Ordinal) || + worktree.FullPath.Equals(normalizedGitDir, StringComparison.Ordinal)) + continue; + + cleaned.Add(worktree); + } + + Dispatcher.UIThread.Invoke(() => + { + Worktrees = cleaned; + }); } - - Dispatcher.UIThread.Invoke(() => + else { - Worktrees = cleaned; - }); + Dispatcher.UIThread.Invoke(() => + { + Worktrees = worktrees; + }); + } } public void RefreshTags()