From dead9d252795381ea6efb2c61a69f30ed3e3a882 Mon Sep 17 00:00:00 2001 From: Nadav Tau Date: Sat, 19 Oct 2024 14:57:21 -0700 Subject: [PATCH] Fix untracked files being staged when hidden --- src/Commands/QueryHasUntrackedFiles.cs | 24 ++++++++++++++++++++++++ src/ViewModels/WorkingCopy.cs | 13 ++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/Commands/QueryHasUntrackedFiles.cs diff --git a/src/Commands/QueryHasUntrackedFiles.cs b/src/Commands/QueryHasUntrackedFiles.cs new file mode 100644 index 00000000..69488dc1 --- /dev/null +++ b/src/Commands/QueryHasUntrackedFiles.cs @@ -0,0 +1,24 @@ +namespace SourceGit.Commands +{ + public partial class QueryHasUntrackedFiles : Command + { + private bool _hasUntracked = false; + + public QueryHasUntrackedFiles(string repo) { + WorkingDirectory = repo; + Context = repo; + Args = "ls-files --others --exclude-standard"; + } + + public bool Result() + { + Exec(); + return _hasUntracked; + } + + protected override void OnReadline(string line) + { + _hasUntracked = true; + } + } +} diff --git a/src/ViewModels/WorkingCopy.cs b/src/ViewModels/WorkingCopy.cs index e4acf8ca..29adaf4b 100644 --- a/src/ViewModels/WorkingCopy.cs +++ b/src/ViewModels/WorkingCopy.cs @@ -343,7 +343,18 @@ namespace SourceGit.ViewModels IsStaging = true; _repo.SetWatcherEnabled(false); - if (changes.Count == _unstaged.Count) + + var canAddAll = changes.Count == _unstaged.Count; + if (canAddAll && !IncludeUntracked) { + // If the user chose to hide untracked files, we have to make sure to not + // add those by mistake when performing `git add `, otherwise those + // untracked files will be added as well + var hasUntracked = new Commands.QueryHasUntrackedFiles(_repo.FullPath).Exec(); + + canAddAll = !hasUntracked; + } + + if (canAddAll) { await Task.Run(() => new Commands.Add(_repo.FullPath).Exec()); }