fix: commit stucks when using SSH formatted GPG signing with a key contains non-empty passphrase

This commit is contained in:
leo 2024-07-09 17:56:23 +08:00
parent 386c92fa28
commit 9db050e8c2
No known key found for this signature in database
5 changed files with 19 additions and 19 deletions

View file

@ -459,11 +459,7 @@ namespace SourceGit
if (args.Length <= 1 || !args[0].Equals("--askpass", StringComparison.Ordinal)) if (args.Length <= 1 || !args[0].Equals("--askpass", StringComparison.Ordinal))
return false; return false;
var match = REG_ASKPASS().Match(args[1]); desktop.MainWindow = new Views.Askpass(args[1]);
if (!match.Success)
return false;
desktop.MainWindow = new Views.Askpass(Path.GetFileName(match.Groups[1].Value));
return true; return true;
} }
@ -483,9 +479,6 @@ namespace SourceGit
} }
} }
[GeneratedRegex(@"Enter\s+passphrase\s*for\s*key\s*['""]([^'""]+)['""]\:\s*", RegexOptions.IgnoreCase)]
private static partial Regex REG_ASKPASS();
private ViewModels.Launcher _launcher = null; private ViewModels.Launcher _launcher = null;
private ResourceDictionary _activeLocale = null; private ResourceDictionary _activeLocale = null;
private ResourceDictionary _themeOverrides = null; private ResourceDictionary _themeOverrides = null;

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Avalonia.Threading; using Avalonia.Threading;
namespace SourceGit.Commands namespace SourceGit.Commands
@ -38,11 +39,16 @@ namespace SourceGit.Commands
public Dictionary<string, string> Envs { get; set; } = new Dictionary<string, string>(); public Dictionary<string, string> Envs { get; set; } = new Dictionary<string, string>();
public void UseSSHKey(string key) public void UseSSHKey(string key)
{
UseSSHAskpass();
Envs.Add("GIT_SSH_COMMAND", $"ssh -i '{key}'");
}
public void UseSSHAskpass()
{ {
Envs.Add("DISPLAY", "required"); Envs.Add("DISPLAY", "required");
Envs.Add("SSH_ASKPASS", $"\"{Process.GetCurrentProcess().MainModule.FileName}\" --askpass"); Envs.Add("SSH_ASKPASS", $"\"{Process.GetCurrentProcess().MainModule.FileName}\" --askpass");
Envs.Add("SSH_ASKPASS_REQUIRE", "prefer"); Envs.Add("SSH_ASKPASS_REQUIRE", "prefer");
Envs.Add("GIT_SSH_COMMAND", $"ssh -i '{key}'");
} }
public bool Exec() public bool Exec()

View file

@ -1,4 +1,5 @@
using System.IO; using System.Diagnostics;
using System.IO;
namespace SourceGit.Commands namespace SourceGit.Commands
{ {
@ -11,6 +12,7 @@ namespace SourceGit.Commands
WorkingDirectory = repo; WorkingDirectory = repo;
Context = repo; Context = repo;
TraitErrorAsOutput = true;
Args = $"commit --file=\"{file}\""; Args = $"commit --file=\"{file}\"";
if (autoStage) if (autoStage)
Args += " --all"; Args += " --all";
@ -18,6 +20,8 @@ namespace SourceGit.Commands
Args += " --amend --no-edit"; Args += " --amend --no-edit";
if (allowEmpty) if (allowEmpty)
Args += " --allow-empty"; Args += " --allow-empty";
UseSSHAskpass();
} }
} }
} }

View file

@ -50,12 +50,9 @@
</Grid> </Grid>
<StackPanel Grid.Row="1" Margin="0,16" Orientation="Vertical"> <StackPanel Grid.Row="1" Margin="0,16" Orientation="Vertical">
<StackPanel Orientation="Horizontal" Margin="16,0"> <Border Margin="16,0">
<TextBlock Text="Enter passphrase for key: "/> <TextBlock Text="{Binding Description}" TextWrapping="Wrap"/>
<Border Background="{DynamicResource Brush.Accent}" CornerRadius="4">
<TextBlock Text="{Binding KeyName}" Classes="monospace" Margin="4,0" Foreground="#FFDDDDDD"/>
</Border> </Border>
</StackPanel>
<TextBox Margin="16" <TextBox Margin="16"
MinWidth="300" MinWidth="300"

View file

@ -17,7 +17,7 @@ namespace SourceGit.Views
set => SetValue(ShowPasswordProperty, value); set => SetValue(ShowPasswordProperty, value);
} }
public string KeyName public string Description
{ {
get; get;
private set; private set;
@ -35,9 +35,9 @@ namespace SourceGit.Views
InitializeComponent(); InitializeComponent();
} }
public Askpass(string keyname) public Askpass(string description)
{ {
KeyName = keyname; Description = description;
DataContext = this; DataContext = this;
InitializeComponent(); InitializeComponent();
} }