From f8bc48c49c7e7abf3cec01b8d30fcf5619c01427 Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 8 Aug 2024 09:33:46 +0800 Subject: [PATCH] enhance: exec git command directly instead of call methods from WorkingCopy (#330) --- src/Views/TextDiffView.axaml.cs | 102 ++++++++++++++------------------ 1 file changed, 45 insertions(+), 57 deletions(-) diff --git a/src/Views/TextDiffView.axaml.cs b/src/Views/TextDiffView.axaml.cs index fd82d852..d5f6194a 100644 --- a/src/Views/TextDiffView.axaml.cs +++ b/src/Views/TextDiffView.axaml.cs @@ -1147,27 +1147,22 @@ namespace SourceGit.Views if (!selection.HasChanges) return; + var repoView = this.FindAncestorOfType(); + if (repoView == null) + return; + + var repo = repoView.DataContext as ViewModels.Repository; + if (repo == null) + return; + + repo.SetWatcherEnabled(false); + if (!selection.HasLeftChanges) { - var workcopyView = this.FindAncestorOfType(); - if (workcopyView == null) - return; - - var workcopy = workcopyView.DataContext as ViewModels.WorkingCopy; - workcopy?.StageChanges(new List { change }); + new Commands.Add(repo.FullPath, [change]).Exec(); } else { - var repoView = this.FindAncestorOfType(); - if (repoView == null) - return; - - var repo = repoView.DataContext as ViewModels.Repository; - if (repo == null) - return; - - repo.SetWatcherEnabled(false); - var tmpFile = Path.GetTempFileName(); if (change.WorkTree == Models.ChangeState.Untracked) { @@ -1186,10 +1181,10 @@ namespace SourceGit.Views new Commands.Apply(diff.Repo, tmpFile, true, "nowarn", "--cache --index").Exec(); File.Delete(tmpFile); - - repo.MarkWorkingCopyDirtyManually(); - repo.SetWatcherEnabled(true); } + + repo.MarkWorkingCopyDirtyManually(); + repo.SetWatcherEnabled(true); } private void OnUnstageChunk(object sender, RoutedEventArgs e) @@ -1210,27 +1205,25 @@ namespace SourceGit.Views if (!selection.HasChanges) return; + var repoView = this.FindAncestorOfType(); + if (repoView == null) + return; + + var repo = repoView.DataContext as ViewModels.Repository; + if (repo == null) + return; + + repo.SetWatcherEnabled(false); + if (!selection.HasLeftChanges) { - var workcopyView = this.FindAncestorOfType(); - if (workcopyView == null) - return; - - var workcopy = workcopyView.DataContext as ViewModels.WorkingCopy; - workcopy?.UnstageChanges(new List { change }); + if (change.DataForAmend != null) + new Commands.UnstageChangesForAmend(repo.FullPath, [change]).Exec(); + else + new Commands.Reset(repo.FullPath, [change]).Exec(); } else { - var repoView = this.FindAncestorOfType(); - if (repoView == null) - return; - - var repo = repoView.DataContext as ViewModels.Repository; - if (repo == null) - return; - - repo.SetWatcherEnabled(false); - var treeGuid = new Commands.QueryStagedFileBlobGuid(diff.Repo, change.Path).Result(); var tmpFile = Path.GetTempFileName(); if (change.Index == Models.ChangeState.Added) @@ -1242,10 +1235,10 @@ namespace SourceGit.Views new Commands.Apply(diff.Repo, tmpFile, true, "nowarn", "--cache --index --reverse").Exec(); File.Delete(tmpFile); - - repo.MarkWorkingCopyDirtyManually(); - repo.SetWatcherEnabled(true); } + + repo.MarkWorkingCopyDirtyManually(); + repo.SetWatcherEnabled(true); } private void OnDiscardChunk(object sender, RoutedEventArgs e) @@ -1266,27 +1259,22 @@ namespace SourceGit.Views if (!selection.HasChanges) return; + var repoView = this.FindAncestorOfType(); + if (repoView == null) + return; + + var repo = repoView.DataContext as ViewModels.Repository; + if (repo == null) + return; + + repo.SetWatcherEnabled(false); + if (!selection.HasLeftChanges) { - var workcopyView = this.FindAncestorOfType(); - if (workcopyView == null) - return; - - var workcopy = workcopyView.DataContext as ViewModels.WorkingCopy; - workcopy?.Discard(new List { change }); + Commands.Discard.Changes(repo.FullPath, [change]); } else { - var repoView = this.FindAncestorOfType(); - if (repoView == null) - return; - - var repo = repoView.DataContext as ViewModels.Repository; - if (repo == null) - return; - - repo.SetWatcherEnabled(false); - var tmpFile = Path.GetTempFileName(); if (change.Index == Models.ChangeState.Added) { @@ -1305,10 +1293,10 @@ namespace SourceGit.Views new Commands.Apply(diff.Repo, tmpFile, true, "nowarn", "--reverse").Exec(); File.Delete(tmpFile); - - repo.MarkWorkingCopyDirtyManually(); - repo.SetWatcherEnabled(true); } + + repo.MarkWorkingCopyDirtyManually(); + repo.SetWatcherEnabled(true); } } }