fix<Stash>: fix stash failed when there's too many files to be stashed

This commit is contained in:
leo 2023-10-10 16:59:47 +08:00
parent 905531f2db
commit ccb3c07064
4 changed files with 53 additions and 31 deletions

View file

@ -31,7 +31,7 @@ namespace SourceGit.Commands {
if (needStash) { if (needStash) {
var changes = new LocalChanges(Cwd).Result(); var changes = new LocalChanges(Cwd).Result();
if (changes.Count > 0) { 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; return false;
} }
} else { } else {

View file

@ -11,37 +11,58 @@ namespace SourceGit.Commands {
Cwd = repo; Cwd = repo;
} }
public bool Push(List<Models.Change> changes, string message) { public bool Push(List<Models.Change> changes, string message, bool bFull) {
var temp = Path.GetTempFileName(); if (bFull) {
var stream = new FileStream(temp, FileMode.Create); var needAdd = new List<string>();
var writer = new StreamWriter(stream); foreach (var c in changes) {
if (c.WorkTree == Models.Change.Status.Added || c.WorkTree == Models.Change.Status.Untracked) {
var needAdd = new List<string>(); needAdd.Add(c.Path);
foreach (var c in changes) { if (needAdd.Count > 10) {
writer.WriteLine(c.Path); new Add(Cwd, needAdd).Exec();
needAdd.Clear();
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(); if (needAdd.Count > 0) {
stream.Flush(); new Add(Cwd, needAdd).Exec();
writer.Close(); needAdd.Clear();
stream.Close(); }
Args = $"stash push -m \"{message}\" --pathspec-from-file=\"{temp}\""; Args = $"stash push -m \"{message}\"";
var succ = Exec(); return Exec();
File.Delete(temp); } else {
return succ; var temp = Path.GetTempFileName();
var stream = new FileStream(temp, FileMode.Create);
var writer = new StreamWriter(stream);
var needAdd = new List<string>();
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) { public bool Apply(string name) {

View file

@ -67,7 +67,7 @@ namespace SourceGit.Views.Popups {
var changes = new Commands.LocalChanges(repo).Result(); var changes = new Commands.LocalChanges(repo).Result();
if (changes.Count > 0) { if (changes.Count > 0) {
if (AutoStash) { 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; return false;
} }
} else { } else {

View file

@ -29,7 +29,8 @@ namespace SourceGit.Views.Popups {
return Task.Run(() => { return Task.Run(() => {
Models.Watcher.SetEnabled(repo, false); 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(); 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); Models.Watcher.SetEnabled(repo, true);
return true; return true;
}); });