From ccb3c07064bda3331172a9493d81b09cfcae986e Mon Sep 17 00:00:00 2001 From: leo Date: Tue, 10 Oct 2023 16:59:47 +0800 Subject: [PATCH] fix: fix stash failed when there's too many files to be stashed --- src/Commands/Pull.cs | 2 +- src/Commands/Stash.cs | 75 +++++++++++++++++---------- src/Views/Popups/CreateBranch.xaml.cs | 2 +- src/Views/Popups/Stash.xaml.cs | 5 +- 4 files changed, 53 insertions(+), 31 deletions(-) diff --git a/src/Commands/Pull.cs b/src/Commands/Pull.cs index 83b27dc4..e2ab522a 100644 --- a/src/Commands/Pull.cs +++ b/src/Commands/Pull.cs @@ -31,7 +31,7 @@ namespace SourceGit.Commands { if (needStash) { var changes = new LocalChanges(Cwd).Result(); if (changes.Count > 0) { - if (!new Stash(Cwd).Push(changes, "PULL_AUTO_STASH")) { + if (!new Stash(Cwd).Push(changes, "PULL_AUTO_STASH", true)) { return false; } } else { diff --git a/src/Commands/Stash.cs b/src/Commands/Stash.cs index 68d63488..0a923f2c 100644 --- a/src/Commands/Stash.cs +++ b/src/Commands/Stash.cs @@ -11,37 +11,58 @@ namespace SourceGit.Commands { Cwd = repo; } - public bool Push(List changes, string message) { - var temp = Path.GetTempFileName(); - var stream = new FileStream(temp, FileMode.Create); - var writer = new StreamWriter(stream); - - var needAdd = new List(); - foreach (var c in changes) { - writer.WriteLine(c.Path); - - if (c.WorkTree == Models.Change.Status.Added || c.WorkTree == Models.Change.Status.Untracked) { - needAdd.Add(c.Path); - if (needAdd.Count > 10) { - new Add(Cwd, needAdd).Exec(); - needAdd.Clear(); + public bool Push(List changes, string message, bool bFull) { + if (bFull) { + var needAdd = new List(); + foreach (var c in changes) { + if (c.WorkTree == Models.Change.Status.Added || c.WorkTree == Models.Change.Status.Untracked) { + needAdd.Add(c.Path); + if (needAdd.Count > 10) { + new Add(Cwd, needAdd).Exec(); + needAdd.Clear(); + } } } - } - if (needAdd.Count > 0) { - new Add(Cwd, needAdd).Exec(); - needAdd.Clear(); - } - writer.Flush(); - stream.Flush(); - writer.Close(); - stream.Close(); + if (needAdd.Count > 0) { + new Add(Cwd, needAdd).Exec(); + needAdd.Clear(); + } - Args = $"stash push -m \"{message}\" --pathspec-from-file=\"{temp}\""; - var succ = Exec(); - File.Delete(temp); - return succ; + Args = $"stash push -m \"{message}\""; + return Exec(); + } else { + var temp = Path.GetTempFileName(); + var stream = new FileStream(temp, FileMode.Create); + var writer = new StreamWriter(stream); + + var needAdd = new List(); + foreach (var c in changes) { + writer.WriteLine(c.Path); + + if (c.WorkTree == Models.Change.Status.Added || c.WorkTree == Models.Change.Status.Untracked) { + needAdd.Add(c.Path); + if (needAdd.Count > 10) { + new Add(Cwd, needAdd).Exec(); + needAdd.Clear(); + } + } + } + if (needAdd.Count > 0) { + new Add(Cwd, needAdd).Exec(); + needAdd.Clear(); + } + + writer.Flush(); + stream.Flush(); + writer.Close(); + stream.Close(); + + Args = $"stash push -m \"{message}\" --pathspec-from-file=\"{temp}\""; + var succ = Exec(); + File.Delete(temp); + return succ; + } } public bool Apply(string name) { diff --git a/src/Views/Popups/CreateBranch.xaml.cs b/src/Views/Popups/CreateBranch.xaml.cs index b7d2deaa..a4ef1731 100644 --- a/src/Views/Popups/CreateBranch.xaml.cs +++ b/src/Views/Popups/CreateBranch.xaml.cs @@ -67,7 +67,7 @@ namespace SourceGit.Views.Popups { var changes = new Commands.LocalChanges(repo).Result(); if (changes.Count > 0) { if (AutoStash) { - if (!new Commands.Stash(repo).Push(changes, "NEWBRANCH_AUTO_STASH")) { + if (!new Commands.Stash(repo).Push(changes, "NEWBRANCH_AUTO_STASH", true)) { return false; } } else { diff --git a/src/Views/Popups/Stash.xaml.cs b/src/Views/Popups/Stash.xaml.cs index 30f5e54e..cd03790b 100644 --- a/src/Views/Popups/Stash.xaml.cs +++ b/src/Views/Popups/Stash.xaml.cs @@ -29,7 +29,8 @@ namespace SourceGit.Views.Popups { return Task.Run(() => { Models.Watcher.SetEnabled(repo, false); - if (changes == null || changes.Count == 0) { + var bFull = changes == null || changes.Count == 0; + if (bFull) { changes = new Commands.LocalChanges(repo).Result(); } @@ -42,7 +43,7 @@ namespace SourceGit.Views.Popups { } } - if (jobs.Count > 0) new Commands.Stash(repo).Push(changes, message); + if (jobs.Count > 0) new Commands.Stash(repo).Push(changes, message, bFull); Models.Watcher.SetEnabled(repo, true); return true; });