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

View file

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

View file

@ -33,23 +33,10 @@ namespace SourceGit.Commands
public CancelToken Cancel { get; set; } = null;
public string WorkingDirectory { get; set; } = null;
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 bool RaiseError { get; set; } = true;
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()
{
@ -63,15 +50,30 @@ namespace SourceGit.Commands
start.StandardOutputEncoding = Encoding.UTF8;
start.StandardErrorEncoding = Encoding.UTF8;
// Editors
var editorProgram = $"\\\"{Process.GetCurrentProcess().MainModule.FileName}\\\"";
// Force using this app as SSH askpass program
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)
{
case EditorType.CoreEditor:
start.Arguments += $"-c core.editor=\"{editorProgram} --core-editor\" ";
start.Arguments += $"-c core.editor=\"\\\"{selfExecFile}\\\" --core-editor\" ";
break;
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;
default:
start.Arguments += "-c core.editor=true ";
@ -81,14 +83,7 @@ namespace SourceGit.Commands
// Append command args
start.Arguments += Args;
// User environment overrides.
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");
// Working directory
if (!string.IsNullOrEmpty(WorkingDirectory))
start.WorkingDirectory = WorkingDirectory;

View file

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

View file

@ -13,14 +13,9 @@ namespace SourceGit.Commands
WorkingDirectory = repo;
Context = repo;
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)
Args += "--prune ";
@ -40,14 +35,8 @@ namespace SourceGit.Commands
WorkingDirectory = repo;
Context = repo;
TraitErrorAsOutput = true;
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 {remote} {remoteBranch}:{localBranch}";
SSHKey = new Config(repo).Get($"remote.{remote}.sshkey");
Args = $"fetch --progress --verbose {remote} {remoteBranch}:{localBranch}";
}
protected override void OnReadline(string line)

View file

@ -10,14 +10,9 @@ namespace SourceGit.Commands
WorkingDirectory = repo;
Context = repo;
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)
Args += "--rebase ";
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)
{
_outputHandler = onProgress;
WorkingDirectory = repo;
Context = repo;
TraitErrorAsOutput = true;
_outputHandler = onProgress;
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 ";
SSHKey = new Config(repo).Get($"remote.{remote}.sshkey");
Args = "push --progress --verbose ";
if (withTags)
Args += "--tags ";
@ -33,16 +28,12 @@ namespace SourceGit.Commands
{
WorkingDirectory = 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)
Args += "--delete ";
Args += $"{remote} refs/tags/{tag}";
}