optimize<Stash>: new stash push implementation - untracked file will be added before stash

This commit is contained in:
leo 2021-04-30 14:21:52 +08:00
parent c25ea618d0
commit 18df69b703
8 changed files with 108 additions and 91 deletions

View file

@ -28,7 +28,7 @@ namespace SourceGit.Commands {
if (needStash) {
var changes = new LocalChanges(Cwd).Result();
if (changes.Count > 0) {
if (!new Stash(Cwd).Push(null, "PULL_AUTO_STASH", true)) {
if (!new Stash(Cwd).Push(changes, "PULL_AUTO_STASH")) {
return false;
}
} else {

View file

@ -1,5 +1,5 @@
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace SourceGit.Commands {
/// <summary>
@ -11,25 +11,37 @@ namespace SourceGit.Commands {
Cwd = repo;
}
public bool Push(List<string> files, string message, bool includeUntracked) {
StringBuilder builder = new StringBuilder();
builder.Append("stash push ");
if (includeUntracked) builder.Append("-u ");
builder.Append("-m \"");
builder.Append(message);
builder.Append("\" ");
public bool Push(List<Models.Change> changes, string message) {
var temp = Path.GetTempFileName();
var stream = new FileStream(temp, FileMode.Create);
var writer = new StreamWriter(stream);
if (files != null && files.Count > 0) {
builder.Append("--");
foreach (var f in files) {
builder.Append(" \"");
builder.Append(f);
builder.Append("\"");
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();
}
Args = builder.ToString();
return Exec();
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) {

View file

@ -379,7 +379,6 @@
<sys:String x:Key="Text.Stashes">Stashes</sys:String>
<sys:String x:Key="Text.Stashes.Stashes">STASHES</sys:String>
<sys:String x:Key="Text.Stashes.Changes">CHANGES</sys:String>
<sys:String x:Key="Text.Stashes.Changes.Tip">Untracked files not shown</sys:String>
<sys:String x:Key="Text.TwoCommitsDiff">COMMIT : {0} -> {1}</sys:String>

View file

@ -379,7 +379,6 @@
<sys:String x:Key="Text.Stashes">贮藏列表</sys:String>
<sys:String x:Key="Text.Stashes.Stashes">贮藏列表</sys:String>
<sys:String x:Key="Text.Stashes.Changes">查看变更</sys:String>
<sys:String x:Key="Text.Stashes.Changes.Tip">不显示未跟踪的文件</sys:String>
<sys:String x:Key="Text.TwoCommitsDiff">对比提交 : {0} -> {1}</sys:String>

View file

@ -62,7 +62,7 @@ namespace SourceGit.Views.Popups {
if (AutoStash) {
var changes = new Commands.LocalChanges(repo).Result();
if (changes.Count > 0) {
if (!new Commands.Stash(repo).Push(null, "NEWBRANCH_AUTO_STASH", true)) {
if (!new Commands.Stash(repo).Push(changes, "NEWBRANCH_AUTO_STASH")) {
return false;
}
} else {

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
namespace SourceGit.Views.Popups {
@ -9,14 +10,14 @@ namespace SourceGit.Views.Popups {
/// </summary>
public partial class Stash : Controls.PopupWidget {
private string repo = null;
private List<string> files = null;
private List<Models.Change> changes = null;
public Stash(string repo, List<string> files) {
public Stash(string repo, List<Models.Change> changes) {
this.repo = repo;
this.files = files;
this.changes = changes;
InitializeComponent();
chkIncludeUntracked.IsEnabled = files == null || files.Count == 0;
chkIncludeUntracked.IsEnabled = changes == null || changes.Count == 0;
}
public override string GetTitle() {
@ -29,15 +30,21 @@ namespace SourceGit.Views.Popups {
return Task.Run(() => {
Models.Watcher.SetEnabled(repo, false);
if (files == null || files.Count == 0) {
new Commands.Stash(repo).Push(null, message, includeUntracked);
if (changes == null || changes.Count == 0) {
changes = new Commands.LocalChanges(repo).Result();
}
var jobs = new List<Models.Change>();
foreach (var c in changes) {
if (c.WorkTree == Models.Change.Status.Added || c.WorkTree == Models.Change.Status.Untracked) {
if (includeUntracked) jobs.Add(c);
} else {
for (int i = 0; i < files.Count; i += 10) {
var count = Math.Min(10, files.Count - i);
var step = files.GetRange(i, count);
new Commands.Stash(repo).Push(step, message, includeUntracked);
jobs.Add(c);
}
}
new Commands.Stash(repo).Push(changes, message);
Models.Watcher.SetEnabled(repo, true);
return true;
});

View file

@ -27,8 +27,14 @@
<!-- Stashes List Group -->
<Border Grid.Row="0" BorderBrush="{StaticResource Brush.Border0}" BorderThickness="0,0,0,1">
<StackPanel Orientation="Horizontal">
<Path
Margin="4,0"
Width="12" Height="12"
Fill="{StaticResource Brush.FG2}"
Data="{StaticResource Icon.Stashes}"/>
<TextBlock
Margin="6,0,0,0"
Margin="4,0,0,0"
Text="{StaticResource Text.Stashes.Stashes}"
Foreground="{StaticResource Brush.FG2}"
FontWeight="Bold"/>
@ -71,25 +77,19 @@
<!-- Change List Group -->
<Border Grid.Row="2" BorderBrush="{StaticResource Brush.Border0}" BorderThickness="0,1">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal">
<Path
Margin="4,0"
Width="12" Height="12"
Fill="{StaticResource Brush.FG2}"
Data="{StaticResource Icon.File}"/>
<TextBlock
Grid.Column="0"
Margin="6,0,0,0"
Margin="4,0,0,0"
Text="{StaticResource Text.Stashes.Changes}"
Foreground="{StaticResource Brush.FG2}"
FontWeight="Bold"/>
<TextBlock
Grid.Column="1"
Margin="0,0,4,0"
Text="{StaticResource Text.Stashes.Changes.Tip}"
Foreground="{StaticResource Brush.FG2}"
FontFamily="Consolas"
FontSize="10"/>
</Grid>
</StackPanel>
</Border>
<!-- Changed Files -->

View file

@ -306,8 +306,8 @@ namespace SourceGit.Views.Widgets {
}
private async void SaveAsPatch(string saveTo, List<Models.Change> changes) {
FileStream stream = new FileStream(saveTo, FileMode.Create);
StreamWriter writer = new StreamWriter(stream);
var stream = new FileStream(saveTo, FileMode.Create);
var writer = new StreamWriter(stream);
foreach (var c in changes) {
await Task.Run(() => new Commands.SaveChangeToStream(repo, c, writer).Exec());
@ -353,7 +353,7 @@ namespace SourceGit.Views.Widgets {
var stash = new MenuItem();
stash.Header = App.Text("FileCM.Stash");
stash.Click += (o, e) => {
new Popups.Stash(repo, files).Show();
new Popups.Stash(repo, changes).Show();
e.Handled = true;
};
@ -416,7 +416,7 @@ namespace SourceGit.Views.Widgets {
var stash = new MenuItem();
stash.Header = App.Text("FileCM.StashMulti", changes.Count);
stash.Click += (o, e) => {
new Popups.Stash(repo, files).Show();
new Popups.Stash(repo, changes).Show();
e.Handled = true;
};
@ -475,7 +475,7 @@ namespace SourceGit.Views.Widgets {
var stash = new MenuItem();
stash.Header = App.Text("FileCM.Stash");
stash.Click += (o, e) => {
new Popups.Stash(repo, files).Show();
new Popups.Stash(repo, changes).Show();
e.Handled = true;
};
@ -538,7 +538,7 @@ namespace SourceGit.Views.Widgets {
var stash = new MenuItem();
stash.Header = App.Text("FileCM.StashMulti", changes.Count);
stash.Click += (o, e) => {
new Popups.Stash(repo, files).Show();
new Popups.Stash(repo, changes).Show();
e.Handled = true;
};