fix<Discard>: wrong discard behavior with changes both in worktree and staged

This commit is contained in:
leo 2024-03-01 11:34:32 +08:00
parent a770ff542d
commit 1c005983c7
4 changed files with 58 additions and 18 deletions

View file

@ -8,7 +8,7 @@ namespace SourceGit.Commands {
new Clean(repo).Exec();
}
public static void Changes(string repo, List<Models.Change> changes) {
public static void ChangesInWorkTree(string repo, List<Models.Change> changes) {
var needClean = new List<string>();
var needCheckout = new List<string>();
@ -30,5 +30,14 @@ namespace SourceGit.Commands {
new Checkout(repo).Files(needCheckout.GetRange(i, count));
}
}
public static void ChangesInStaged(string repo, List<Models.Change> changes) {
for (int i = 0; i < changes.Count; i += 10) {
var count = Math.Min(10, changes.Count - i);
var files = new List<string>();
for (int j = 0; j < count; j++) files.Add(changes[i + j].Path);
new Restore(repo, files, "--staged --worktree").Exec();
}
}
}
}

View file

@ -13,9 +13,17 @@ namespace SourceGit.ViewModels {
private set;
}
public Discard(Repository repo, List<Models.Change> changes = null) {
public Discard(Repository repo) {
_repo = repo;
Mode = new DiscardModeAll();
View = new Views.Discard { DataContext = this };
}
public Discard(Repository repo, List<Models.Change> changes, bool isUnstaged) {
_repo = repo;
_changes = changes;
_isUnstaged = isUnstaged;
if (_changes == null) {
Mode = new DiscardModeAll();
@ -35,8 +43,10 @@ namespace SourceGit.ViewModels {
return Task.Run(() => {
if (_changes == null) {
Commands.Discard.All(_repo.FullPath);
} else if (_isUnstaged) {
Commands.Discard.ChangesInWorkTree(_repo.FullPath, _changes);
} else {
Commands.Discard.Changes(_repo.FullPath, _changes);
Commands.Discard.ChangesInStaged(_repo.FullPath, _changes);
}
CallUIThread(() => _repo.SetWatcherEnabled(true));
@ -46,5 +56,6 @@ namespace SourceGit.ViewModels {
private Repository _repo = null;
private List<Models.Change> _changes = null;
private bool _isUnstaged = true;
}
}

View file

@ -271,12 +271,20 @@ namespace SourceGit.ViewModels {
IsUnstaging = false;
}
public void Discard(List<Models.Change> changes) {
public void Discard(List<Models.Change> changes, bool isUnstaged) {
if (PopupHost.CanCreatePopup()) {
if (changes.Count == _count) {
if (isUnstaged) {
if (changes.Count == _unstaged.Count && _staged.Count == 0) {
PopupHost.ShowPopup(new Discard(_repo));
} else {
PopupHost.ShowPopup(new Discard(_repo, changes));
PopupHost.ShowPopup(new Discard(_repo, changes, true));
}
} else {
if (changes.Count == _staged.Count && _unstaged.Count == 0) {
PopupHost.ShowPopup(new Discard(_repo));
} else {
PopupHost.ShowPopup(new Discard(_repo, changes, false));
}
}
}
}
@ -397,7 +405,7 @@ namespace SourceGit.ViewModels {
discard.Header = App.Text("FileCM.Discard");
discard.Icon = App.CreateMenuIcon("Icons.Undo");
discard.Click += (_, e) => {
Discard(changes);
Discard(changes, true);
e.Handled = true;
};
@ -483,7 +491,7 @@ namespace SourceGit.ViewModels {
discard.Header = App.Text("FileCM.DiscardMulti", changes.Count);
discard.Icon = App.CreateMenuIcon("Icons.Undo");
discard.Click += (_, e) => {
Discard(changes);
Discard(changes, true);
e.Handled = true;
};
@ -561,6 +569,14 @@ namespace SourceGit.ViewModels {
e.Handled = true;
};
var discard = new MenuItem();
discard.Header = App.Text("FileCM.Discard");
discard.Icon = App.CreateMenuIcon("Icons.Undo");
discard.Click += (_, e) => {
Discard(changes, false);
e.Handled = true;
};
var stash = new MenuItem();
stash.Header = App.Text("FileCM.Stash");
stash.Icon = App.CreateMenuIcon("Icons.Stashes");
@ -604,6 +620,7 @@ namespace SourceGit.ViewModels {
menu.Items.Add(openWith);
menu.Items.Add(new MenuItem() { Header = "-" });
menu.Items.Add(unstage);
menu.Items.Add(discard);
menu.Items.Add(stash);
menu.Items.Add(patch);
menu.Items.Add(new MenuItem() { Header = "-" });
@ -617,6 +634,14 @@ namespace SourceGit.ViewModels {
e.Handled = true;
};
var discard = new MenuItem();
discard.Header = App.Text("FileCM.DiscardMulti", changes.Count);
discard.Icon = App.CreateMenuIcon("Icons.Undo");
discard.Click += (_, e) => {
Discard(changes, false);
e.Handled = true;
};
var stash = new MenuItem();
stash.Header = App.Text("FileCM.StashMulti", changes.Count);
stash.Icon = App.CreateMenuIcon("Icons.Stashes");
@ -649,6 +674,7 @@ namespace SourceGit.ViewModels {
};
menu.Items.Add(unstage);
menu.Items.Add(discard);
menu.Items.Add(stash);
menu.Items.Add(patch);
}

View file

@ -680,7 +680,7 @@ namespace SourceGit.Views {
discard.Icon = App.CreateMenuIcon("Icons.Undo");
discard.Click += (_, e) => {
var workcopy = workcopyView.DataContext as ViewModels.WorkingCopy;
workcopy.Discard(new List<Models.Change> { change });
workcopy.Discard(new List<Models.Change> { change }, true);
e.Handled = true;
};
@ -700,14 +700,8 @@ namespace SourceGit.Views {
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);
var workcopy = workcopyView.DataContext as ViewModels.WorkingCopy;
workcopy.Discard(new List<Models.Change> { change }, false);
e.Handled = true;
};