Skip to content

Commit

Permalink
Allow user scripts to operate on selected files (#11239)
Browse files Browse the repository at this point in the history
  • Loading branch information
SlugFiller committed Apr 14, 2024
1 parent 8a552ac commit 53e31e0
Show file tree
Hide file tree
Showing 20 changed files with 310 additions and 23 deletions.
13 changes: 13 additions & 0 deletions GitCommands/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Text;
using GitCommands.Utils;
using GitExtUtils;

// ReSharper disable once CheckNamespace
Expand Down Expand Up @@ -189,6 +190,18 @@ public static string Quote(this string? s, string q = "\"")
return string.IsNullOrEmpty(s) ? s : s.Quote();
}

/// <summary>
/// Quotes and escapes this string for use as a command line argument.
/// </summary>
[Pure]
public static string QuoteForCommandLine(this string s, bool? forWindows = null)
{
return $"\"{((forWindows ?? EnvUtils.RunningOnWindows()) ? EscapeForWindowsCommandLine(s) : EscapeForPosixCommandLine(s))}\"";

static string EscapeForWindowsCommandLine(string s) => s.Replace("\"", "\"\"");
static string EscapeForPosixCommandLine(string s) => s.Replace(@"\", @"\\").Replace("\"", "\\\"").Replace("'", @"\'");
}

/// <summary>
/// Adds parentheses if string is not null and not empty.
/// </summary>
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.

42 changes: 42 additions & 0 deletions GitUI/CommandsDialogs/FormCommit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,8 @@ void AddToSelectionFilter()
}
});

UICommands.PostRepositoryChanged += UICommands_PostRepositoryChanged;

return;

void ConfigureMessageBox()
Expand All @@ -430,6 +432,11 @@ protected override void Dispose(bool disposing)
{
if (disposing)
{
if (!IsDesignMode && !IsUnitTestActive)
{
UICommands.PostRepositoryChanged -= UICommands_PostRepositoryChanged;
}

_unstagedLoader.Dispose();
_customDiffToolsSequence.Dispose();
_interactiveAddSequence.Dispose();
Expand Down Expand Up @@ -601,6 +608,20 @@ protected override void OnKeyUp(KeyEventArgs e)
return;
}

protected override void OnUICommandsChanged(GitUICommandsChangedEventArgs e)
{
GitUICommands oldCommands = e.OldCommands;

if (oldCommands is not null)
{
oldCommands.PostRepositoryChanged -= UICommands_PostRepositoryChanged;
}

UICommands.PostRepositoryChanged += UICommands_PostRepositoryChanged;

base.OnUICommandsChanged(e);
}

public override bool ProcessHotkey(Keys keyData)
{
if (IsDesignMode || !HotkeysEnabled)
Expand Down Expand Up @@ -889,6 +910,11 @@ protected override bool ExecuteCommand(int cmd)
}
}

public override IScriptOptionsProvider? GetScriptOptionsProvider()
{
return new ScriptOptionsProvider(_currentFilesList, () => _fullPathResolver, () => SelectedDiff.CurrentFileLine);
}

#endregion

private void ComputeUnstagedFiles(Action<IReadOnlyList<GitItemStatus>> onComputed, bool doAsync)
Expand Down Expand Up @@ -1647,6 +1673,8 @@ private void UnstagedFileContext_Opening(object sender, System.ComponentModel.Ca
openWithToolStripMenuItem.Enabled = !isAnyDeleted;
deleteFileToolStripMenuItem.Enabled = !isAnyDeleted;
openContainingFolderToolStripMenuItem.Enabled = !isAnyDeleted;

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

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

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

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

private void UICommands_PostRepositoryChanged(object sender, GitUIEventArgs e)
{
if (!_skipUpdate && !_bypassActivatedEventHandler)
{
ThreadHelper.FileAndForget(async () =>
{
await this.SwitchToMainThreadAsync();
RescanChanges();
});
}
}

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.

8 changes: 8 additions & 0 deletions GitUI/CommandsDialogs/RevisionDiffControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using GitUI.CommandDialogs;
using GitUI.CommandsDialogs.BrowseDialog;
using GitUI.HelperDialogs;
using GitUI.ScriptsEngine;
using GitUI.UserControls;
using GitUI.UserControls.RevisionGrid;
using GitUIPluginInterfaces;
Expand Down Expand Up @@ -188,6 +189,11 @@ bool SelectFirstGroupChangesIfFileNotFocused()
}
}

protected override IScriptOptionsProvider? GetScriptOptionsProvider()
{
return new ScriptOptionsProvider(DiffFiles, () => _fullPathResolver, () => BlameControl.Visible ? BlameControl.CurrentFileLine : DiffText.CurrentFileLine);
}

public void ReloadHotkeys()
{
LoadHotkeys(HotkeySettingsName);
Expand Down Expand Up @@ -747,6 +753,8 @@ private void UpdateStatusOfMenuItems()
{
blameToolStripMenuItem.Checked = false;
}

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

private void DiffContextMenu_Opening(object sender, CancelEventArgs e)
Expand Down
20 changes: 19 additions & 1 deletion GitUI/CommandsDialogs/RevisionFileTreeControl.Designer.cs

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

18 changes: 18 additions & 0 deletions GitUI/CommandsDialogs/RevisionFileTreeControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using GitUI.CommandDialogs;
using GitUI.CommandsDialogs.BrowseDialog;
using GitUI.Properties;
using GitUI.ScriptsEngine;
using GitUI.UserControls;
using GitUIPluginInterfaces;
using Microsoft;
Expand Down Expand Up @@ -290,6 +291,21 @@ protected override bool ExecuteCommand(int cmd)
return true;
}

protected override IScriptOptionsProvider? GetScriptOptionsProvider()
{
return new ScriptOptionsProvider(() =>
{
if (tvGitTree.SelectedNode?.Tag is not GitItem gitItem || gitItem.ObjectType != GitObjectType.Blob)
{
return Array.Empty<string>();
}
return new string[] { gitItem.FileName };
},
() => _fullPathResolver,
() => BlameControl.Visible ? BlameControl.CurrentFileLine : FileText.CurrentFileLine);
}

public override bool ProcessHotkey(Keys keyData)
{
return base.ProcessHotkey(keyData)
Expand Down Expand Up @@ -733,6 +749,8 @@ private void FileTreeContextMenu_Opening(object sender, System.ComponentModel.Ca
findToolStripMenuItem.Enabled = tvGitTree.Nodes.Count > 0;
expandToolStripMenuItem.Visible = isFolder;
collapseAllToolStripMenuItem.Visible = isFolder;

FileTreeContextMenu.AddUserScripts(runScriptToolStripMenuItem, ExecuteCommand, script => script.OnEvent == ScriptEvent.ShowInFileList, UICommands);
}

private void fileTreeOpenContainingFolderToolStripMenuItem_Click(object sender, EventArgs e)
Expand Down

0 comments on commit 53e31e0

Please sign in to comment.