Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow user scripts to operate on selected files #11239

Merged
merged 4 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))}\"";
mstv marked this conversation as resolved.
Show resolved Hide resolved
gerhardol marked this conversation as resolved.
Show resolved Hide resolved

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 @@ -394,6 +394,8 @@ void AddToSelectionFilter()
}
});

UICommands.PostRepositoryChanged += UICommands_PostRepositoryChanged;
mstv marked this conversation as resolved.
Show resolved Hide resolved

return;

void ConfigureMessageBox()
Expand All @@ -420,6 +422,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 @@ -595,6 +602,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);
}
mstv marked this conversation as resolved.
Show resolved Hide resolved

public override bool ProcessHotkey(Keys keyData)
{
if (IsDesignMode || !HotkeysEnabled)
Expand Down Expand Up @@ -884,6 +905,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 @@ -1642,6 +1668,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 @@ -1661,6 +1689,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 @@ -3302,6 +3332,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 @@ -6,6 +6,7 @@
using GitUI.CommandsDialogs.BrowseDialog;
using GitUI.HelperDialogs;
using GitUI.Hotkey;
using GitUI.ScriptsEngine;
using GitUI.UserControls;
using GitUI.UserControls.RevisionGrid;
using GitUIPluginInterfaces;
Expand Down Expand Up @@ -187,6 +188,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 @@ -748,6 +754,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 @@ -8,6 +8,7 @@
using GitUI.CommandsDialogs.BrowseDialog;
using GitUI.Hotkey;
using GitUI.Properties;
using GitUI.ScriptsEngine;
using GitUI.UserControls;
using GitUIPluginInterfaces;
using Microsoft;
Expand Down Expand Up @@ -291,6 +292,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 @@ -740,6 +756,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
Loading
Loading