feature<TextDiffView>: supports discard changes from staged directly

This commit is contained in:
leo 2024-02-29 10:59:59 +08:00
parent 1149c768d3
commit 096fd6cb22
3 changed files with 66 additions and 2 deletions

18
src/Commands/Restore.cs Normal file
View file

@ -0,0 +1,18 @@
using System.Collections.Generic;
using System.Text;
namespace SourceGit.Commands {
public class Restore : Command {
public Restore(string repo, List<string> files, string extra) {
WorkingDirectory = repo;
Context = repo;
StringBuilder builder = new StringBuilder();
builder.Append("restore ");
if (!string.IsNullOrEmpty(extra)) builder.Append(extra).Append(" ");
builder.Append("--");
foreach (var f in files) builder.Append(' ').Append('"').Append(f).Append('"');
Args = builder.ToString();
}
}
}

View file

@ -458,8 +458,6 @@ namespace SourceGit.ViewModels {
} }
public void RefreshWorkingCopyChanges() { public void RefreshWorkingCopyChanges() {
_watcher.MarkWorkingCopyRefreshed();
var changes = new Commands.QueryLocalChanges(FullPath, _includeUntracked).Result(); var changes = new Commands.QueryLocalChanges(FullPath, _includeUntracked).Result();
var hasUnsolvedConflict = _workingCopy.SetData(changes); var hasUnsolvedConflict = _workingCopy.SetData(changes);
@ -484,6 +482,8 @@ namespace SourceGit.ViewModels {
HasUnsolvedConflict = hasUnsolvedConflict; HasUnsolvedConflict = hasUnsolvedConflict;
OnPropertyChanged(nameof(WorkingCopyChangesCount)); OnPropertyChanged(nameof(WorkingCopyChangesCount));
}); });
_watcher.MarkWorkingCopyRefreshed();
} }
public void RefreshStashes() { public void RefreshStashes() {

View file

@ -676,7 +676,24 @@ namespace SourceGit.Views {
workcopy.UnstageChanges(new List<Models.Change> { change }); workcopy.UnstageChanges(new List<Models.Change> { change });
e.Handled = true; e.Handled = true;
}; };
var discard = new MenuItem();
discard.Header = App.Text("FileCM.DiscardSelectedLines");
discard.Icon = App.CreateMenuIcon("Icons.Undo");
discard.Click += (_, e) => {
var repoView = this.FindAncestorOfType<Repository>();
if (repoView == null) return;
var repo = repoView.DataContext as ViewModels.Repository;
repo.SetWatcherEnabled(false);
new Commands.Restore(repo.FullPath, new List<string> { change.Path }, "--staged --worktree").Exec();
repo.RefreshWorkingCopyChanges();
repo.SetWatcherEnabled(true);
e.Handled = true;
};
menu.Items.Add(unstage); menu.Items.Add(unstage);
menu.Items.Add(discard);
} }
} else { } else {
var repoView = this.FindAncestorOfType<Repository>(); var repoView = this.FindAncestorOfType<Repository>();
@ -762,7 +779,36 @@ namespace SourceGit.Views {
repo.SetWatcherEnabled(true); repo.SetWatcherEnabled(true);
e.Handled = true; e.Handled = true;
}; };
var discard = new MenuItem();
discard.Header = App.Text("FileCM.DiscardSelectedLines");
discard.Icon = App.CreateMenuIcon("Icons.Undo");
discard.Click += (_, e) => {
var repo = repoView.DataContext as ViewModels.Repository;
repo.SetWatcherEnabled(false);
var tmpFile = Path.GetTempFileName();
if (change.WorkTree == Models.ChangeState.Untracked) {
TextDiff.GenerateNewPatchFromSelection(change, null, selection, true, tmpFile);
} else if (UseCombined) {
var treeGuid = new Commands.QueryStagedFileBlobGuid(ctx.RepositoryPath, change.Path).Result();
TextDiff.GeneratePatchFromSelection(change, treeGuid, selection, true, tmpFile);
} else {
var treeGuid = new Commands.QueryStagedFileBlobGuid(ctx.RepositoryPath, change.Path).Result();
TextDiff.GeneratePatchFromSelectionSingleSide(change, treeGuid, selection, true, isOldSide, tmpFile);
}
new Commands.Apply(ctx.RepositoryPath, tmpFile, true, "nowarn", "--cache --index --reverse").Exec();
new Commands.Apply(ctx.RepositoryPath, tmpFile, true, "nowarn", "--reverse").Exec();
File.Delete(tmpFile);
repo.RefreshWorkingCopyChanges();
repo.SetWatcherEnabled(true);
e.Handled = true;
};
menu.Items.Add(unstage); menu.Items.Add(unstage);
menu.Items.Add(discard);
} }
} }