refactor: base command

This commit is contained in:
leo 2024-07-09 18:13:15 +08:00
parent dda1e79c6f
commit 446445ee73
No known key found for this signature in database
7 changed files with 42 additions and 85 deletions

View file

@ -25,14 +25,12 @@
var cmd = new Command(); var cmd = new Command();
cmd.WorkingDirectory = repo; cmd.WorkingDirectory = repo;
cmd.Context = repo; cmd.Context = repo;
if (string.IsNullOrEmpty(upstream)) if (string.IsNullOrEmpty(upstream))
{
cmd.Args = $"branch {name} --unset-upstream"; cmd.Args = $"branch {name} --unset-upstream";
}
else else
{
cmd.Args = $"branch {name} -u {upstream}"; cmd.Args = $"branch {name} -u {upstream}";
}
return cmd.Exec(); return cmd.Exec();
} }
@ -50,14 +48,8 @@
var cmd = new Command(); var cmd = new Command();
cmd.WorkingDirectory = repo; cmd.WorkingDirectory = repo;
cmd.Context = repo; cmd.Context = repo;
cmd.SSHKey = new Config(repo).Get($"remote.{remote}.sshkey");
var sshKey = new Config(repo).Get($"remote.{remote}.sshkey"); cmd.Args = $"push {remote} --delete {name}";
if (string.IsNullOrEmpty(sshKey))
cmd.Args = "-c credential.helper=manager ";
else
cmd.UseSSHKey(sshKey);
cmd.Args += $"push {remote} --delete {name}";
return cmd.Exec(); return cmd.Exec();
} }
} }

View file

@ -11,17 +11,14 @@ namespace SourceGit.Commands
Context = ctx; Context = ctx;
WorkingDirectory = path; WorkingDirectory = path;
TraitErrorAsOutput = true; TraitErrorAsOutput = true;
SSHKey = sshKey;
if (string.IsNullOrEmpty(sshKey)) Args = "clone --progress --verbose --recurse-submodules ";
Args = "-c credential.helper=manager ";
else
UseSSHKey(sshKey);
Args += "clone --progress --verbose --recurse-submodules ";
if (!string.IsNullOrEmpty(extraArgs)) if (!string.IsNullOrEmpty(extraArgs))
Args += $"{extraArgs} "; Args += $"{extraArgs} ";
Args += $"{url} "; Args += $"{url} ";
if (!string.IsNullOrEmpty(localName)) if (!string.IsNullOrEmpty(localName))
Args += localName; Args += localName;

View file

@ -33,23 +33,10 @@ namespace SourceGit.Commands
public CancelToken Cancel { get; set; } = null; public CancelToken Cancel { get; set; } = null;
public string WorkingDirectory { get; set; } = null; public string WorkingDirectory { get; set; } = null;
public EditorType Editor { get; set; } = EditorType.CoreEditor; // Only used in Exec() mode public EditorType Editor { get; set; } = EditorType.CoreEditor; // Only used in Exec() mode
public string SSHKey { get; set; } = string.Empty;
public string Args { get; set; } = string.Empty; public string Args { get; set; } = string.Empty;
public bool RaiseError { get; set; } = true; public bool RaiseError { get; set; } = true;
public bool TraitErrorAsOutput { get; set; } = false; public bool TraitErrorAsOutput { get; set; } = false;
public Dictionary<string, string> Envs { get; set; } = new Dictionary<string, string>();
public void UseSSHKey(string key)
{
UseSSHAskpass();
Envs.Add("GIT_SSH_COMMAND", $"ssh -i '{key}'");
}
public void UseSSHAskpass()
{
Envs.Add("DISPLAY", "required");
Envs.Add("SSH_ASKPASS", $"\"{Process.GetCurrentProcess().MainModule.FileName}\" --askpass");
Envs.Add("SSH_ASKPASS_REQUIRE", "prefer");
}
public bool Exec() public bool Exec()
{ {
@ -63,15 +50,30 @@ namespace SourceGit.Commands
start.StandardOutputEncoding = Encoding.UTF8; start.StandardOutputEncoding = Encoding.UTF8;
start.StandardErrorEncoding = Encoding.UTF8; start.StandardErrorEncoding = Encoding.UTF8;
// Editors // Force using this app as SSH askpass program
var editorProgram = $"\\\"{Process.GetCurrentProcess().MainModule.FileName}\\\""; var selfExecFile = Process.GetCurrentProcess().MainModule.FileName;
start.Environment.Add("DISPLAY", "required");
start.Environment.Add("SSH_ASKPASS", $"\"{selfExecFile}\" --askpass");
start.Environment.Add("SSH_ASKPASS_REQUIRE", "prefer");
// If an SSH private key was provided, sets the environment.
if (!string.IsNullOrEmpty(SSHKey))
start.Environment.Add("GIT_SSH_COMMAND", $"ssh -i '{SSHKey}'");
else
start.Arguments += "-c credential.helper=manager ";
// Force using en_US.UTF-8 locale to avoid GCM crash
if (OperatingSystem.IsLinux())
start.Environment.Add("LANG", "en_US.UTF-8");
// Force using this app as git editor.
switch (Editor) switch (Editor)
{ {
case EditorType.CoreEditor: case EditorType.CoreEditor:
start.Arguments += $"-c core.editor=\"{editorProgram} --core-editor\" "; start.Arguments += $"-c core.editor=\"\\\"{selfExecFile}\\\" --core-editor\" ";
break; break;
case EditorType.RebaseEditor: case EditorType.RebaseEditor:
start.Arguments += $"-c core.editor=\"{editorProgram} --rebase-message-editor\" -c sequence.editor=\"{editorProgram} --rebase-todo-editor\" -c rebase.abbreviateCommands=true "; start.Arguments += $"-c core.editor=\"\\\"{selfExecFile}\\\" --rebase-message-editor\" -c sequence.editor=\"\\\"{selfExecFile}\\\" --rebase-todo-editor\" -c rebase.abbreviateCommands=true ";
break; break;
default: default:
start.Arguments += "-c core.editor=true "; start.Arguments += "-c core.editor=true ";
@ -81,14 +83,7 @@ namespace SourceGit.Commands
// Append command args // Append command args
start.Arguments += Args; start.Arguments += Args;
// User environment overrides. // Working directory
foreach (var kv in Envs)
start.Environment.Add(kv.Key, kv.Value);
// Force using en_US.UTF-8 locale to avoid GCM crash
if (OperatingSystem.IsLinux())
start.Environment.Add("LANG", "en_US.UTF-8");
if (!string.IsNullOrEmpty(WorkingDirectory)) if (!string.IsNullOrEmpty(WorkingDirectory))
start.WorkingDirectory = WorkingDirectory; start.WorkingDirectory = WorkingDirectory;

View file

@ -19,8 +19,6 @@ namespace SourceGit.Commands
Args += " --amend --no-edit"; Args += " --amend --no-edit";
if (allowEmpty) if (allowEmpty)
Args += " --allow-empty"; Args += " --allow-empty";
UseSSHAskpass();
} }
} }
} }

View file

@ -13,14 +13,9 @@ namespace SourceGit.Commands
WorkingDirectory = repo; WorkingDirectory = repo;
Context = repo; Context = repo;
TraitErrorAsOutput = true; TraitErrorAsOutput = true;
SSHKey = new Config(repo).Get($"remote.{remote}.sshkey");
Args = "fetch --progress --verbose ";
var sshKey = new Config(repo).Get($"remote.{remote}.sshkey");
if (string.IsNullOrEmpty(sshKey))
Args = "-c credential.helper=manager ";
else
UseSSHKey(sshKey);
Args += "fetch --progress --verbose ";
if (prune) if (prune)
Args += "--prune "; Args += "--prune ";
@ -40,14 +35,8 @@ namespace SourceGit.Commands
WorkingDirectory = repo; WorkingDirectory = repo;
Context = repo; Context = repo;
TraitErrorAsOutput = true; TraitErrorAsOutput = true;
SSHKey = new Config(repo).Get($"remote.{remote}.sshkey");
var sshKey = new Config(repo).Get($"remote.{remote}.sshkey"); Args = $"fetch --progress --verbose {remote} {remoteBranch}:{localBranch}";
if (string.IsNullOrEmpty(sshKey))
Args = "-c credential.helper=manager ";
else
UseSSHKey(sshKey);
Args += $"fetch --progress --verbose {remote} {remoteBranch}:{localBranch}";
} }
protected override void OnReadline(string line) protected override void OnReadline(string line)

View file

@ -10,14 +10,9 @@ namespace SourceGit.Commands
WorkingDirectory = repo; WorkingDirectory = repo;
Context = repo; Context = repo;
TraitErrorAsOutput = true; TraitErrorAsOutput = true;
SSHKey = new Config(repo).Get($"remote.{remote}.sshkey");
Args = "pull --verbose --progress --tags ";
var sshKey = new Config(repo).Get($"remote.{remote}.sshkey");
if (string.IsNullOrEmpty(sshKey))
Args = "-c credential.helper=manager ";
else
UseSSHKey(sshKey);
Args += "pull --verbose --progress --tags ";
if (useRebase) if (useRebase)
Args += "--rebase "; Args += "--rebase ";
if (noTags) if (noTags)

View file

@ -6,18 +6,13 @@ namespace SourceGit.Commands
{ {
public Push(string repo, string local, string remote, string remoteBranch, bool withTags, bool force, bool track, Action<string> onProgress) public Push(string repo, string local, string remote, string remoteBranch, bool withTags, bool force, bool track, Action<string> onProgress)
{ {
_outputHandler = onProgress;
WorkingDirectory = repo; WorkingDirectory = repo;
Context = repo; Context = repo;
TraitErrorAsOutput = true; TraitErrorAsOutput = true;
_outputHandler = onProgress; SSHKey = new Config(repo).Get($"remote.{remote}.sshkey");
Args = "push --progress --verbose ";
var sshKey = new Config(repo).Get($"remote.{remote}.sshkey");
if (string.IsNullOrEmpty(sshKey))
Args = "-c credential.helper=manager ";
else
UseSSHKey(sshKey);
Args += "push --progress --verbose ";
if (withTags) if (withTags)
Args += "--tags "; Args += "--tags ";
@ -33,16 +28,12 @@ namespace SourceGit.Commands
{ {
WorkingDirectory = repo; WorkingDirectory = repo;
Context = repo; Context = repo;
SSHKey = new Config(repo).Get($"remote.{remote}.sshkey");
Args = "push ";
var sshKey = new Config(repo).Get($"remote.{remote}.sshkey");
if (string.IsNullOrEmpty(sshKey))
Args = "-c credential.helper=manager ";
else
UseSSHKey(sshKey);
Args += "push ";
if (isDelete) if (isDelete)
Args += "--delete "; Args += "--delete ";
Args += $"{remote} refs/tags/{tag}"; Args += $"{remote} refs/tags/{tag}";
} }