Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Use ArgumentList for commands instead of manual escaping #473

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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: 4 additions & 9 deletions src/Commands/Add.cs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,14 @@ public Add(string repo, List<Models.Change> changes = null)

if (changes == null || changes.Count == 0)
{
Args = "add .";
Args = ["add", "."];
}
else
{
var builder = new StringBuilder();
builder.Append("add --");
Args.AddRange(["add", "--"]);

foreach (var c in changes)
{
builder.Append(" \"");
builder.Append(c.Path);
builder.Append("\"");
}
Args = builder.ToString();
Args.Add(c.Path);
}
}
}
Expand Down
17 changes: 9 additions & 8 deletions src/Commands/Apply.cs
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
namespace SourceGit.Commands
using System.Collections.Generic;

namespace SourceGit.Commands
{
public class Apply : Command
{
public Apply(string repo, string file, bool ignoreWhitespace, string whitespaceMode, string extra)
public Apply(string repo, string file, bool ignoreWhitespace, string whitespaceMode, IEnumerable<string> extra)
{
WorkingDirectory = repo;
Context = repo;
Args = "apply ";
Args = ["apply"];
if (ignoreWhitespace)
Args += "--ignore-whitespace ";
Args.Add("--ignore-whitespace");
else
Args += $"--whitespace={whitespaceMode} ";
if (!string.IsNullOrEmpty(extra))
Args += $"{extra} ";
Args += $"\"{file}\"";
Args.Add($"--whitespace={whitespaceMode}");
Args.AddRange(extra);
Args.Add(file);
}
}
}
2 changes: 1 addition & 1 deletion src/Commands/Archive.cs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public Archive(string repo, string revision, string saveTo, Action<string> outpu
{
WorkingDirectory = repo;
Context = repo;
Args = $"archive--format=zip--verbose --output=\"{saveTo}\"{revision}";
Args = ["archive","--format=zip","--verbose","-o",saveTo,revision];
TraitErrorAsOutput = true;
_outputHandler = outputHandler;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/AssumeUnchanged.cs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ViewCommand : Command
public ViewCommand(string repo)
{
WorkingDirectory = repo;
Args = "ls-files-v";
Args = ["ls-files", "-v"];
RaiseError = false;
}

Expand Down Expand Up @@ -46,7 +46,7 @@ public ModCommand(string repo, string file, bool bAdd)

WorkingDirectory = repo;
Context = repo;
Args = $"update-index {mode} -- \"{file}\"";
Args = ["update-index", mode, "--", file];
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Blame.cs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public Blame(string repo, string file, string revision)
{
WorkingDirectory = repo;
Context = repo;
Args = $"blame -t {revision} -- \"{file}\"";
Args = ["blame","-t",revision,"--",file];
RaiseError = false;

_result.File = file;
Expand Down
12 changes: 6 additions & 6 deletions src/Commands/Branch.cs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public static bool Create(string repo, string name, string basedOn)
var cmd = new Command();
cmd.WorkingDirectory = repo;
cmd.Context = repo;
cmd.Args = $"branch {name} {basedOn}";
cmd.Args = ["branch", name, basedOn];
return cmd.Exec();
}

Expand All @@ -16,7 +16,7 @@ public static bool Rename(string repo, string name, string to)
var cmd = new Command();
cmd.WorkingDirectory = repo;
cmd.Context = repo;
cmd.Args = $"branch -M {name} {to}";
cmd.Args = ["branch", "-M", name, to];
return cmd.Exec();
}

Expand All @@ -27,9 +27,9 @@ public static bool SetUpstream(string repo, string name, string upstream)
cmd.Context = repo;

if (string.IsNullOrEmpty(upstream))
cmd.Args = $"branch {name} --unset-upstream";
cmd.Args = ["branch", name, "--unset-upstream"];
else
cmd.Args = $"branch {name} -u {upstream}";
cmd.Args = ["branch", name, "-u", upstream];

return cmd.Exec();
}
Expand All @@ -39,7 +39,7 @@ public static bool DeleteLocal(string repo, string name)
var cmd = new Command();
cmd.WorkingDirectory = repo;
cmd.Context = repo;
cmd.Args = $"branch -D {name}";
cmd.Args = ["branch", "-D", name];
return cmd.Exec();
}

Expand All @@ -49,7 +49,7 @@ public static bool DeleteRemote(string repo, string remote, string name)
cmd.WorkingDirectory = repo;
cmd.Context = repo;
cmd.SSHKey = new Config(repo).Get($"remote.{remote}.sshkey");
cmd.Args = $"push {remote} --delete {name}";
cmd.Args = ["push", remote, "--delete", name];
return cmd.Exec();
}
}
Expand Down
38 changes: 7 additions & 31 deletions src/Commands/Checkout.cs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -14,73 +14,49 @@ public Checkout(string repo)

public bool Branch(string branch, Action<string> onProgress)
{
Args = $"checkout--progress {branch}";
Args = ["checkout", "--progress", branch];
TraitErrorAsOutput = true;
_outputHandler = onProgress;
return Exec();
}

public bool Branch(string branch, string basedOn, Action<string> onProgress)
{
Args = $"checkout--progress -b {branch} {basedOn}";
Args = ["checkout", "--progress", "-b", branch, basedOn];
TraitErrorAsOutput = true;
_outputHandler = onProgress;
return Exec();
}

public bool UseTheirs(List<string> files)
{
StringBuilder builder = new StringBuilder();
builder.Append("checkout --theirs --");
foreach (var f in files)
{
builder.Append(" \"");
builder.Append(f);
builder.Append("\"");
}
Args = builder.ToString();
Args = ["checkout", "--theirs", "--", ..files];
return Exec();
}

public bool UseMine(List<string> files)
{
StringBuilder builder = new StringBuilder();
builder.Append("checkout --ours --");
foreach (var f in files)
{
builder.Append(" \"");
builder.Append(f);
builder.Append("\"");
}
Args = builder.ToString();
Args = ["checkout", "--ours", "--", ..files];
return Exec();
}

public bool FileWithRevision(string file, string revision)
{
Args = $"checkout--no-overlay {revision} -- \"{file}\"";
Args = ["checkout", "--no-overlay", revision, "--", file];
return Exec();
}

public bool Commit(string commitId, Action<string> onProgress)
{
Args = $"checkout--detach--progress {commitId}";
Args = ["checkout", "--detach", "--progress", commitId];
TraitErrorAsOutput = true;
_outputHandler = onProgress;
return Exec();
}

public bool Files(List<string> files)
{
StringBuilder builder = new StringBuilder();
builder.Append("checkout -f -q --");
foreach (var f in files)
{
builder.Append(" \"");
builder.Append(f);
builder.Append("\"");
}
Args = builder.ToString();
Args = ["checkout", "-f", "-q", "--", ..files];
return Exec();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Commands/CherryPick.cs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public CherryPick(string repo, string commits, bool noCommit)
var mode = noCommit ? "-n" : "--ff";
WorkingDirectory = repo;
Context = repo;
Args = $"cherry-pick{mode}{commits}";
Args = ["cherry-pick",mode,commits];
}
}
}
12 changes: 2 additions & 10 deletions src/Commands/Clean.cs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,15 @@ public Clean(string repo)
{
WorkingDirectory = repo;
Context = repo;
Args = "clean-qfd";
Args = ["clean", "-qfd"];
}

public Clean(string repo, List<string> files)
{
StringBuilder builder = new StringBuilder();
builder.Append("clean -qfd --");
foreach (var f in files)
{
builder.Append(" \"");
builder.Append(f);
builder.Append("\"");
}
Args = ["clean", "-qfd", "--", ..files];

WorkingDirectory = repo;
Context = repo;
Args = builder.ToString();
}
}
}
12 changes: 6 additions & 6 deletions src/Commands/Clone.cs
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
using System;
using System.Collections.Generic;

namespace SourceGit.Commands
{
public class Clone : Command
{
private readonly Action<string> _notifyProgress;

public Clone(string ctx, string path, string url, string localName, string sshKey, string extraArgs, Action<string> ouputHandler)
public Clone(string ctx, string path, string url, string localName, string sshKey, IEnumerable<string> extraArgs, Action<string> ouputHandler)
{
Context = ctx;
WorkingDirectory = path;
TraitErrorAsOutput = true;
SSHKey = sshKey;
Args = "clone--progress--verbose--recurse-submodules ";
Args = ["clone", "--progress", "--verbose", "--recurse-submodules"];

if (!string.IsNullOrEmpty(extraArgs))
Args += $"{extraArgs} ";
Args.AddRange(extraArgs);

Args += $"{url} ";
Args.Add(url);

if (!string.IsNullOrEmpty(localName))
Args += localName;
Args.Add(localName);

_notifyProgress = ouputHandler;
}
Expand Down
33 changes: 21 additions & 12 deletions src/Commands/Command.cs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ public enum EditorType
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 List<string> Args { get; set; } = [];
public bool RaiseError { get; set; } = true;
public bool TraitErrorAsOutput { get; set; } = false;

public bool Exec()
{
var start = new ProcessStartInfo();
start.FileName = Native.OS.GitExecutable;
start.Arguments = "--no-pager -c core.quotepath=off ";
List<string> gitArgs = ["--no-pager", "-c", "core.quotepath=off"];

var start = new ProcessStartInfo(Native.OS.GitExecutable);
start.UseShellExecute = false;
start.CreateNoWindow = true;
start.RedirectStandardOutput = true;
Expand All @@ -61,7 +61,7 @@ public bool Exec()
if (!string.IsNullOrEmpty(SSHKey))
start.Environment.Add("GIT_SSH_COMMAND", $"ssh -o StrictHostKeyChecking=accept-new -i '{SSHKey}'");
else
start.Arguments += "-ccredential.helper=manager ";
gitArgs.AddRange(["-c", "credential.helper=manager"]);

// Force using en_US.UTF-8 locale to avoid GCM crash
if (OperatingSystem.IsLinux())
Expand All @@ -71,18 +71,28 @@ public bool Exec()
switch (Editor)
{
case EditorType.CoreEditor:
start.Arguments += $"-ccore.editor=\"\\\"{selfExecFile}\\\" --core-editor\" ";
gitArgs.AddRange(["-c", $"core.editor=\"{selfExecFile}\" --core-editor"]);
break;
case EditorType.RebaseEditor:
start.Arguments += $"-c core.editor=\"\\\"{selfExecFile}\\\" --rebase-message-editor\" -c sequence.editor=\"\\\"{selfExecFile}\\\" --rebase-todo-editor\" -c rebase.abbreviateCommands=true ";
gitArgs.AddRange([
"-c", $"core.editor=\"{selfExecFile}\" --rebase-message-editor",
"-c", $"sequence.editor=\"{selfExecFile}\" --rebase-todo-editor",
"-c", "rebase.abbreviateCommands=true"
]);
break;
default:
start.Arguments += "-ccore.editor=true ";
gitArgs.AddRange(["-c", "core.editor=true"]);
break;
}

// Append command args
start.Arguments += Args;
gitArgs.AddRange(Args);

// Append git args
foreach (var arg in gitArgs)
{
start.ArgumentList.Add(arg);
}

// Working directory
if (!string.IsNullOrEmpty(WorkingDirectory))
Expand Down Expand Up @@ -181,9 +191,8 @@ public bool Exec()

public ReadToEndResult ReadToEnd()
{
var start = new ProcessStartInfo();
start.FileName = Native.OS.GitExecutable;
start.Arguments = "--no-pager -c core.quotepath=off " + Args;
List<string> gitArgs = ["--no-pager", "-c", "core.quotepath=off", ..Args];
var start = new ProcessStartInfo(Native.OS.GitExecutable, gitArgs);
start.UseShellExecute = false;
start.CreateNoWindow = true;
start.RedirectStandardOutput = true;
Expand Down
6 changes: 3 additions & 3 deletions src/Commands/Commit.cs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public Commit(string repo, string message, bool amend, bool allowEmpty = false)
WorkingDirectory = repo;
Context = repo;
TraitErrorAsOutput = true;
Args = $"commit--file=\"{file}\"";
Args = ["commit", $"--file={file}"];
if (amend)
Args += " --amend--no-edit";
Args.AddRange(["--amend", "--no-edit"]);
if (allowEmpty)
Args += " --allow-empty";
Args.Add("--allow-empty");
}
}
}
2 changes: 1 addition & 1 deletion src/Commands/CompareRevisions.cs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public CompareRevisions(string repo, string start, string end)
Context = repo;

var based = string.IsNullOrEmpty(start) ? "-R" : start;
Args = $"diff--name-status{based}{end}";
Args = ["diff","--name-status",based,end];
}

public List<Models.Change> Result()
Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /