mirror of
https://github.com/sourcegit-scm/sourcegit.git
synced 2024-12-24 20:57:19 -08:00
optimize<Stash>: new stash push implementation - untracked file will be added before stash
This commit is contained in:
parent
c25ea618d0
commit
18df69b703
8 changed files with 108 additions and 91 deletions
|
@ -28,7 +28,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(null, "PULL_AUTO_STASH", true)) {
|
if (!new Stash(Cwd).Push(changes, "PULL_AUTO_STASH")) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.IO;
|
||||||
|
|
||||||
namespace SourceGit.Commands {
|
namespace SourceGit.Commands {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -11,25 +11,37 @@ namespace SourceGit.Commands {
|
||||||
Cwd = repo;
|
Cwd = repo;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Push(List<string> files, string message, bool includeUntracked) {
|
public bool Push(List<Models.Change> changes, string message) {
|
||||||
StringBuilder builder = new StringBuilder();
|
var temp = Path.GetTempFileName();
|
||||||
builder.Append("stash push ");
|
var stream = new FileStream(temp, FileMode.Create);
|
||||||
if (includeUntracked) builder.Append("-u ");
|
var writer = new StreamWriter(stream);
|
||||||
builder.Append("-m \"");
|
|
||||||
builder.Append(message);
|
|
||||||
builder.Append("\" ");
|
|
||||||
|
|
||||||
if (files != null && files.Count > 0) {
|
var needAdd = new List<string>();
|
||||||
builder.Append("--");
|
foreach (var c in changes) {
|
||||||
foreach (var f in files) {
|
writer.WriteLine(c.Path);
|
||||||
builder.Append(" \"");
|
|
||||||
builder.Append(f);
|
if (c.WorkTree == Models.Change.Status.Added || c.WorkTree == Models.Change.Status.Untracked) {
|
||||||
builder.Append("\"");
|
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();
|
writer.Flush();
|
||||||
return Exec();
|
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) {
|
||||||
|
|
|
@ -379,7 +379,6 @@
|
||||||
<sys:String x:Key="Text.Stashes">Stashes</sys:String>
|
<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.Stashes">STASHES</sys:String>
|
||||||
<sys:String x:Key="Text.Stashes.Changes">CHANGES</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>
|
<sys:String x:Key="Text.TwoCommitsDiff">COMMIT : {0} -> {1}</sys:String>
|
||||||
|
|
||||||
|
|
|
@ -379,7 +379,6 @@
|
||||||
<sys:String x:Key="Text.Stashes">贮藏列表</sys:String>
|
<sys:String x:Key="Text.Stashes">贮藏列表</sys:String>
|
||||||
<sys:String x:Key="Text.Stashes.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">查看变更</sys:String>
|
||||||
<sys:String x:Key="Text.Stashes.Changes.Tip">不显示未跟踪的文件</sys:String>
|
|
||||||
|
|
||||||
<sys:String x:Key="Text.TwoCommitsDiff">对比提交 : {0} -> {1}</sys:String>
|
<sys:String x:Key="Text.TwoCommitsDiff">对比提交 : {0} -> {1}</sys:String>
|
||||||
|
|
||||||
|
|
|
@ -62,7 +62,7 @@ namespace SourceGit.Views.Popups {
|
||||||
if (AutoStash) {
|
if (AutoStash) {
|
||||||
var changes = new Commands.LocalChanges(repo).Result();
|
var changes = new Commands.LocalChanges(repo).Result();
|
||||||
if (changes.Count > 0) {
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace SourceGit.Views.Popups {
|
namespace SourceGit.Views.Popups {
|
||||||
|
@ -9,14 +10,14 @@ namespace SourceGit.Views.Popups {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public partial class Stash : Controls.PopupWidget {
|
public partial class Stash : Controls.PopupWidget {
|
||||||
private string repo = null;
|
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.repo = repo;
|
||||||
this.files = files;
|
this.changes = changes;
|
||||||
|
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
chkIncludeUntracked.IsEnabled = files == null || files.Count == 0;
|
chkIncludeUntracked.IsEnabled = changes == null || changes.Count == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string GetTitle() {
|
public override string GetTitle() {
|
||||||
|
@ -29,15 +30,21 @@ namespace SourceGit.Views.Popups {
|
||||||
|
|
||||||
return Task.Run(() => {
|
return Task.Run(() => {
|
||||||
Models.Watcher.SetEnabled(repo, false);
|
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 {
|
} else {
|
||||||
for (int i = 0; i < files.Count; i += 10) {
|
jobs.Add(c);
|
||||||
var count = Math.Min(10, files.Count - i);
|
|
||||||
var step = files.GetRange(i, count);
|
|
||||||
new Commands.Stash(repo).Push(step, message, includeUntracked);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
new Commands.Stash(repo).Push(changes, message);
|
||||||
Models.Watcher.SetEnabled(repo, true);
|
Models.Watcher.SetEnabled(repo, true);
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
|
@ -27,8 +27,14 @@
|
||||||
<!-- Stashes List Group -->
|
<!-- Stashes List Group -->
|
||||||
<Border Grid.Row="0" BorderBrush="{StaticResource Brush.Border0}" BorderThickness="0,0,0,1">
|
<Border Grid.Row="0" BorderBrush="{StaticResource Brush.Border0}" BorderThickness="0,0,0,1">
|
||||||
<StackPanel Orientation="Horizontal">
|
<StackPanel Orientation="Horizontal">
|
||||||
|
<Path
|
||||||
|
Margin="4,0"
|
||||||
|
Width="12" Height="12"
|
||||||
|
Fill="{StaticResource Brush.FG2}"
|
||||||
|
Data="{StaticResource Icon.Stashes}"/>
|
||||||
|
|
||||||
<TextBlock
|
<TextBlock
|
||||||
Margin="6,0,0,0"
|
Margin="4,0,0,0"
|
||||||
Text="{StaticResource Text.Stashes.Stashes}"
|
Text="{StaticResource Text.Stashes.Stashes}"
|
||||||
Foreground="{StaticResource Brush.FG2}"
|
Foreground="{StaticResource Brush.FG2}"
|
||||||
FontWeight="Bold"/>
|
FontWeight="Bold"/>
|
||||||
|
@ -71,25 +77,19 @@
|
||||||
|
|
||||||
<!-- Change List Group -->
|
<!-- Change List Group -->
|
||||||
<Border Grid.Row="2" BorderBrush="{StaticResource Brush.Border0}" BorderThickness="0,1">
|
<Border Grid.Row="2" BorderBrush="{StaticResource Brush.Border0}" BorderThickness="0,1">
|
||||||
<Grid>
|
<StackPanel Orientation="Horizontal">
|
||||||
<Grid.ColumnDefinitions>
|
<Path
|
||||||
<ColumnDefinition Width="*"/>
|
Margin="4,0"
|
||||||
<ColumnDefinition Width="Auto"/>
|
Width="12" Height="12"
|
||||||
</Grid.ColumnDefinitions>
|
Fill="{StaticResource Brush.FG2}"
|
||||||
|
Data="{StaticResource Icon.File}"/>
|
||||||
|
|
||||||
<TextBlock
|
<TextBlock
|
||||||
Grid.Column="0"
|
Margin="4,0,0,0"
|
||||||
Margin="6,0,0,0"
|
|
||||||
Text="{StaticResource Text.Stashes.Changes}"
|
Text="{StaticResource Text.Stashes.Changes}"
|
||||||
Foreground="{StaticResource Brush.FG2}"
|
Foreground="{StaticResource Brush.FG2}"
|
||||||
FontWeight="Bold"/>
|
FontWeight="Bold"/>
|
||||||
<TextBlock
|
</StackPanel>
|
||||||
Grid.Column="1"
|
|
||||||
Margin="0,0,4,0"
|
|
||||||
Text="{StaticResource Text.Stashes.Changes.Tip}"
|
|
||||||
Foreground="{StaticResource Brush.FG2}"
|
|
||||||
FontFamily="Consolas"
|
|
||||||
FontSize="10"/>
|
|
||||||
</Grid>
|
|
||||||
</Border>
|
</Border>
|
||||||
|
|
||||||
<!-- Changed Files -->
|
<!-- Changed Files -->
|
||||||
|
|
|
@ -306,8 +306,8 @@ namespace SourceGit.Views.Widgets {
|
||||||
}
|
}
|
||||||
|
|
||||||
private async void SaveAsPatch(string saveTo, List<Models.Change> changes) {
|
private async void SaveAsPatch(string saveTo, List<Models.Change> changes) {
|
||||||
FileStream stream = new FileStream(saveTo, FileMode.Create);
|
var stream = new FileStream(saveTo, FileMode.Create);
|
||||||
StreamWriter writer = new StreamWriter(stream);
|
var writer = new StreamWriter(stream);
|
||||||
|
|
||||||
foreach (var c in changes) {
|
foreach (var c in changes) {
|
||||||
await Task.Run(() => new Commands.SaveChangeToStream(repo, c, writer).Exec());
|
await Task.Run(() => new Commands.SaveChangeToStream(repo, c, writer).Exec());
|
||||||
|
@ -353,7 +353,7 @@ namespace SourceGit.Views.Widgets {
|
||||||
var stash = new MenuItem();
|
var stash = new MenuItem();
|
||||||
stash.Header = App.Text("FileCM.Stash");
|
stash.Header = App.Text("FileCM.Stash");
|
||||||
stash.Click += (o, e) => {
|
stash.Click += (o, e) => {
|
||||||
new Popups.Stash(repo, files).Show();
|
new Popups.Stash(repo, changes).Show();
|
||||||
e.Handled = true;
|
e.Handled = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -416,7 +416,7 @@ namespace SourceGit.Views.Widgets {
|
||||||
var stash = new MenuItem();
|
var stash = new MenuItem();
|
||||||
stash.Header = App.Text("FileCM.StashMulti", changes.Count);
|
stash.Header = App.Text("FileCM.StashMulti", changes.Count);
|
||||||
stash.Click += (o, e) => {
|
stash.Click += (o, e) => {
|
||||||
new Popups.Stash(repo, files).Show();
|
new Popups.Stash(repo, changes).Show();
|
||||||
e.Handled = true;
|
e.Handled = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -475,7 +475,7 @@ namespace SourceGit.Views.Widgets {
|
||||||
var stash = new MenuItem();
|
var stash = new MenuItem();
|
||||||
stash.Header = App.Text("FileCM.Stash");
|
stash.Header = App.Text("FileCM.Stash");
|
||||||
stash.Click += (o, e) => {
|
stash.Click += (o, e) => {
|
||||||
new Popups.Stash(repo, files).Show();
|
new Popups.Stash(repo, changes).Show();
|
||||||
e.Handled = true;
|
e.Handled = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -538,7 +538,7 @@ namespace SourceGit.Views.Widgets {
|
||||||
var stash = new MenuItem();
|
var stash = new MenuItem();
|
||||||
stash.Header = App.Text("FileCM.StashMulti", changes.Count);
|
stash.Header = App.Text("FileCM.StashMulti", changes.Count);
|
||||||
stash.Click += (o, e) => {
|
stash.Click += (o, e) => {
|
||||||
new Popups.Stash(repo, files).Show();
|
new Popups.Stash(repo, changes).Show();
|
||||||
e.Handled = true;
|
e.Handled = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue