Skip to content

Commit

Permalink
Escape quotes in arguments (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
jzebedee authored Mar 15, 2019
1 parent 43ebafb commit bc221b8
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/corgit/GitArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ namespace corgit
{
public static class GitArguments
{
private static string QuoteEscape(string value)
=> value.AsSpan().IndexOfAny(' ', '"') >= 0 ? $"\"{value.Replace("\"", "\\\"")}\"" : value;

public struct CommitOptions
{
public readonly bool? All;
Expand Down Expand Up @@ -94,7 +97,7 @@ public static IEnumerable<string> Log(LogOptions options = default, IEnumerable<
if (paths != null)
{
yield return "--";
foreach (var path in paths)
foreach (var path in paths.Select(QuoteEscape))
{
yield return path;
}
Expand All @@ -111,7 +114,7 @@ public static IEnumerable<string> Add(IEnumerable<string> paths = null)
else
{
yield return "--";
foreach (var path in paths)
foreach (var path in paths.Select(QuoteEscape))
{
yield return path;
}
Expand All @@ -125,7 +128,7 @@ public static IEnumerable<string> Remove(IEnumerable<string> paths)

yield return "rm";
yield return "--";
foreach (var path in paths)
foreach (var path in paths.Select(QuoteEscape))
{
yield return path;
}
Expand All @@ -141,7 +144,7 @@ public static IEnumerable<string> Config(string key, string value = null, string
yield return key;
if (!string.IsNullOrEmpty(value))
{
yield return value;
yield return QuoteEscape(value);
}
}

Expand Down Expand Up @@ -172,7 +175,7 @@ public static IEnumerable<string> Checkout(string treeish, IEnumerable<string> p
if (paths != null)
{
yield return "--";
foreach (var path in paths)
foreach (var path in paths.Select(QuoteEscape))
{
yield return path;
}
Expand Down Expand Up @@ -255,15 +258,15 @@ public static IEnumerable<string> Archive(string treeish, string output, Archive

if (!string.IsNullOrEmpty(options.Format))
{
yield return $"--format={options.Format}";
yield return $"--format={QuoteEscape(options.Format)}";
}

if (!string.IsNullOrEmpty(options.Prefix))
{
yield return $"--prefix={options.Prefix}";
yield return $"--prefix={QuoteEscape(options.Prefix)}";
}

yield return $"--output={output}";
yield return $"--output={QuoteEscape(output)}";

if (options.WorktreeAttributes.GetValueOrDefault())
{
Expand All @@ -280,20 +283,20 @@ public static IEnumerable<string> Archive(string treeish, string output, Archive

if (!string.IsNullOrEmpty(options.Remote))
{
yield return $"--remote={options.Remote}";
yield return $"--remote={QuoteEscape(options.Remote)}";
}

if (!string.IsNullOrEmpty(options.Exec))
{
yield return $"--exec={options.Exec}";
yield return $"--exec={QuoteEscape(options.Exec)}";
}

yield return treeish;

if (options.Paths != null)
{
yield return "--";
foreach (var path in options.Paths)
foreach (var path in options.Paths.Select(QuoteEscape))
{
yield return path;
}
Expand Down

0 comments on commit bc221b8

Please sign in to comment.