Skip to content

Commit

Permalink
Allow user scripts to operate on selected files
Browse files Browse the repository at this point in the history
  • Loading branch information
SlugFiller committed Oct 3, 2023
1 parent f2bff50 commit eeb9700
Show file tree
Hide file tree
Showing 21 changed files with 417 additions and 65 deletions.
6 changes: 3 additions & 3 deletions GitUI/CommandsDialogs/FormBrowse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,10 @@ internal FormBrowse(GitUICommands commands, BrowseArguments args, ISettingsSourc
_aheadBehindDataProvider = new AheadBehindDataProvider(() => Module.GitExecutable);
toolStripButtonPush.Initialize(_aheadBehindDataProvider);
repoObjectsTree.Initialize(_aheadBehindDataProvider, filterRevisionGridBySpaceSeparatedRefs: ToolStripFilters.SetBranchFilter, refsSource: RevisionGrid, revisionGridInfo: RevisionGrid, scriptRunner: RevisionGrid);
revisionDiff.Bind(revisionGridInfo: RevisionGrid, revisionGridUpdate: RevisionGrid, revisionFileTree: fileTree, () => RevisionGrid.CurrentFilter.PathFilter, RefreshGitStatusMonitor);
revisionDiff.Bind(revisionGridInfo: RevisionGrid, revisionGridUpdate: RevisionGrid, revisionFileTree: fileTree, () => RevisionGrid.CurrentFilter.PathFilter, RefreshGitStatusMonitor, scriptRunner: RevisionGrid);

// Show blame by default if not started from command line
fileTree.Bind(revisionGridInfo: RevisionGrid, revisionGridUpdate: RevisionGrid, RefreshGitStatusMonitor, _isFileBlameHistory);
fileTree.Bind(revisionGridInfo: RevisionGrid, revisionGridUpdate: RevisionGrid, RefreshGitStatusMonitor, _isFileBlameHistory, scriptRunner: RevisionGrid);
RevisionGrid.ResumeRefreshRevisions();

// Application is init, the repo related operations are triggered in OnLoad()
Expand Down Expand Up @@ -1025,7 +1025,7 @@ void LoadUserMenu()

button.Click += delegate
{
if (ScriptRunner.RunScript(this, Module, script.Name, UICommands, RevisionGrid).NeedsGridRefresh)
if (ScriptRunner.RunScript(this, Module, script.Name, UICommands, RevisionGrid, null).NeedsGridRefresh)
{
RefreshRevisions();
}
Expand Down
40 changes: 38 additions & 2 deletions GitUI/CommandsDialogs/FormCommit.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 76 additions & 1 deletion GitUI/CommandsDialogs/FormCommit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

namespace GitUI.CommandsDialogs
{
public sealed partial class FormCommit : GitModuleForm
public sealed partial class FormCommit : GitModuleForm, IRunScript.IFileListSource
{
#region Translation

Expand Down Expand Up @@ -184,6 +184,8 @@ public sealed partial class FormCommit : GitModuleForm
private IReadOnlyList<GitItemStatus>? _currentSelection;
private int _alreadyLoadedTemplatesCount = -1;
private EventHandler? _branchNameLabelOnClick;
private IRunScript.IFileListSource _unstagedFileListSource;
private IRunScript.IFileListSource _stagedFileListSource;

private CommitKind CommitKind
{
Expand Down Expand Up @@ -220,6 +222,8 @@ public FormCommit(GitUICommands commands, CommitKind commitKind = CommitKind.Nor
ThreadHelper.ThrowIfNotOnUIThread();

_editedCommit = editedCommit;
_unstagedFileListSource = new RunScriptFileListSource(this, false);
_stagedFileListSource = new RunScriptFileListSource(this, true);

InitializeComponent();

Expand Down Expand Up @@ -1660,6 +1664,8 @@ private void UnstagedFileContext_Opening(object sender, System.ComponentModel.Ca
openWithToolStripMenuItem.Enabled = !isAnyDeleted;
deleteFileToolStripMenuItem.Enabled = !isAnyDeleted;
openContainingFolderToolStripMenuItem.Enabled = !isAnyDeleted;

UnstagedFileContext.AddUserScripts(runScriptToolStripMenuItem, ExecuteScriptUnstaged, (script) => script.OnEvent == ScriptEvent.ShowInFileList);
}

private void StagedFileContext_Opening(object sender, System.ComponentModel.CancelEventArgs e)
Expand All @@ -1679,6 +1685,8 @@ private void StagedFileContext_Opening(object sender, System.ComponentModel.Canc
stagedOpenToolStripMenuItem7.Enabled = !isAnyDeleted;
stagedOpenWithToolStripMenuItem8.Enabled = !isAnyDeleted;
stagedOpenFolderToolStripMenuItem10.Enabled = !isAnyDeleted;

StagedFileContext.AddUserScripts(stagedRunScriptToolStripMenuItem, ExecuteScriptStaged, (script) => script.OnEvent == ScriptEvent.ShowInFileList);
}

private void UnstagedSubmoduleContext_Opening(object sender, System.ComponentModel.CancelEventArgs e)
Expand Down Expand Up @@ -3317,6 +3325,73 @@ private void UpdateButtonStates()
: TranslatedStrings.ButtonPush;
}

private class RunScriptFileListSource : IRunScript.IFileListSource
{
private FormCommit _formCommit;
private bool _staged;

public RunScriptFileListSource(FormCommit formCommit, bool staged)
{
_formCommit = formCommit;
_staged = staged;
}

List<string> IRunScript.IFileListSource.GetFiles()
{
IReadOnlyList<FileStatusItem> selectedItems = (_staged ? _formCommit.Staged : _formCommit.Unstaged).SelectedItems.ToList();

if (!selectedItems.Any())
{
return null;
}

return selectedItems.Select(item => _formCommit._fullPathResolver.Resolve(item.Item.Name)).ToList();
}

int? IRunScript.IFileListSource.GetLineNumber()
{
return _formCommit.SelectedDiff.CurrentFileLine;
}
}

private void ExecuteScriptUnstaged(string name)
{
if (ScriptRunner.RunScript(this, Module, name, UICommands, null, _unstagedFileListSource).NeedsGridRefresh)
{
RescanChanges();
}
}

private void ExecuteScriptStaged(string name)
{
if (ScriptRunner.RunScript(this, Module, name, UICommands, null, _stagedFileListSource).NeedsGridRefresh)
{
RescanChanges();
}
}

List<string> IRunScript.IFileListSource.GetFiles()
{
if (_currentFilesList is null)
{
return null;
}

IReadOnlyList<FileStatusItem> selectedItems = _currentFilesList.SelectedItems.ToList();

if (!selectedItems.Any())
{
return null;
}

return selectedItems.Select(item => _fullPathResolver.Resolve(item.Item.Name)).ToList();
}

int? IRunScript.IFileListSource.GetLineNumber()
{
return SelectedDiff.CurrentFileLine;
}

internal TestAccessor GetTestAccessor()
=> new(this);

Expand Down
20 changes: 19 additions & 1 deletion GitUI/CommandsDialogs/RevisionDiffControl.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 29 additions & 2 deletions GitUI/CommandsDialogs/RevisionDiffControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using GitUI.CommandsDialogs.BrowseDialog;
using GitUI.HelperDialogs;
using GitUI.Hotkey;
using GitUI.Script;
using GitUI.UserControls;
using GitUI.UserControls.RevisionGrid;
using GitUIPluginInterfaces;
Expand All @@ -16,7 +17,7 @@

namespace GitUI.CommandsDialogs
{
public partial class RevisionDiffControl : GitModuleControl
public partial class RevisionDiffControl : GitModuleControl, IRunScript.IFileListSource
{
private readonly TranslationString _saveFileFilterCurrentFormat = new("Current format");
private readonly TranslationString _saveFileFilterAllFiles = new("All files");
Expand Down Expand Up @@ -50,6 +51,7 @@ public partial class RevisionDiffControl : GitModuleControl
private string? _fallbackFollowedFile;
private FileStatusItem? _lastExplicitlySelectedItem;
private bool _isImplicitListSelection = false;
private IRunScript _scriptRunner;

public RevisionDiffControl()
{
Expand Down Expand Up @@ -306,13 +308,14 @@ private async Task SetDiffsAsync(IReadOnlyList<GitRevision> revisions)
}
}

public void Bind(IRevisionGridInfo revisionGridInfo, IRevisionGridUpdate revisionGridUpdate, RevisionFileTreeControl revisionFileTree, Func<string>? pathFilter, Action? refreshGitStatus)
public void Bind(IRevisionGridInfo revisionGridInfo, IRevisionGridUpdate revisionGridUpdate, RevisionFileTreeControl revisionFileTree, Func<string>? pathFilter, Action? refreshGitStatus, IRunScript scriptRunner)
{
_revisionGridInfo = revisionGridInfo;
_revisionGridUpdate = revisionGridUpdate;
_revisionFileTree = revisionFileTree;
_pathFilter = pathFilter;
_refreshGitStatus = refreshGitStatus;
_scriptRunner = scriptRunner;
DiffFiles.Bind(objectId => DescribeRevision(objectId), _revisionGridInfo.GetActualRevision);
}

Expand Down Expand Up @@ -748,6 +751,8 @@ private void UpdateStatusOfMenuItems()
{
blameToolStripMenuItem.Checked = false;
}

DiffContextMenu.AddUserScripts(runScriptToolStripMenuItem, Execute, (script) => script.OnEvent == ScriptEvent.ShowInFileList);
}

private void DiffContextMenu_Opening(object sender, CancelEventArgs e)
Expand Down Expand Up @@ -1453,6 +1458,28 @@ private void ResetSelectedItemsWithConfirmation(bool resetToParent)

private static bool RenamedIndexItem(FileStatusItem item) => item.Item.IsRenamed && item.Item.Staged == StagedStatus.Index;

List<string> IRunScript.IFileListSource.GetFiles()
{
IReadOnlyList<FileStatusItem> selectedItems = DiffFiles.SelectedItems.ToList();

if (!selectedItems.Any())
{
return null;
}

return selectedItems.Select(item => _fullPathResolver.Resolve(item.Item.Name)).ToList();
}

int? IRunScript.IFileListSource.GetLineNumber()
{
return BlameControl.Visible ? BlameControl.CurrentFileLine : DiffText.CurrentFileLine;
}

private void Execute(string name)
{
_scriptRunner.Execute(name, this);
}

internal void RegisterGitHostingPluginInBlameControl()
{
BlameControl.ConfigureRepositoryHostPlugin(PluginRegistry.TryGetGitHosterForModule(Module));
Expand Down

0 comments on commit eeb9700

Please sign in to comment.