Skip to content

Commit

Permalink
Escape quotations when quoting filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
gerhardol committed Nov 8, 2020
1 parent a3e88ac commit bda7376
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 18 deletions.
6 changes: 3 additions & 3 deletions GitCommands/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,18 +179,18 @@ public static string Combine([CanBeNull] this string left, [NotNull] string sep,
}

/// <summary>
/// Quotes this string with the specified <paramref name="quotationMark"/>
/// Quotes this string with the specified <paramref name="q"/>
/// </summary>
[Pure]
[NotNull]
public static string Quote([CanBeNull] this string s, [NotNull] string quotationMark = "\"")
public static string Quote([CanBeNull] this string s, [NotNull] string q = "\"")
{
if (s == null)
{
return "";
}

return quotationMark + s + quotationMark;
return $"{q}{s.Replace(q, "\\" + q)}{q}";
}

/// <summary>
Expand Down
17 changes: 3 additions & 14 deletions UnitTests/GitCommands.Tests/Config/ConfigFileTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,16 @@ private static string GetDefaultConfigFileContent()
return content.ToString();
}

private void AddConfigValue(string cfgFile, string section, string value)
{
var args = new GitArgumentBuilder("config")
{
"-f",
cfgFile.Quote(),
"--add",
section,
value
};
Module.GitExecutable.GetOutput(args);
}

private string GetConfigValue(string cfgFile, string key)
{
var args = new GitArgumentBuilder("config")
{
"-f",
cfgFile.Quote(),
"--get",
key.Quote()

// Quote the key without unescaping internal quotes
$"\"{key}\""
};
return Module.GitExecutable.GetOutput(args).TrimEnd('\n');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void GetEnvironmentValidPaths()
public void GetEnvironmentValidPaths_quoted()
{
var paths = GetValidPaths().Concat(GetInvalidPaths());
var quotedPaths = paths.Select(path => path.Quote(" ")).Select(path => path.Quote());
var quotedPaths = paths.Select(path => path.Quote());
string pathVariable = string.Join(_separator, quotedPaths);
_environment.GetEnvironmentVariable("PATH").Returns(pathVariable);

Expand Down

0 comments on commit bda7376

Please sign in to comment.