Skip to content

Commit

Permalink
Merge pull request #5820 from spdr870/feature/findgit
Browse files Browse the repository at this point in the history
Add try catch around method to guess system encoding.
  • Loading branch information
RussKie committed Nov 27, 2018
2 parents 1552672 + 4450d3e commit db6c0e3
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions GitCommands/Git/GitModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,31 +292,39 @@ public static Encoding SystemEncoding
{
if (_systemEncoding == null)
{
// check whether GitExtensions works with standard msysgit or msysgit-unicode
try
{
// check whether GitExtensions works with standard msysgit or msysgit-unicode

// invoke a git command that returns an invalid argument in its response, and
// check if a unicode-only character is reported back. If so assume msysgit-unicode
// invoke a git command that returns an invalid argument in its response, and
// check if a unicode-only character is reported back. If so assume msysgit-unicode

// git config --get with a malformed key (no section) returns:
// "error: key does not contain a section: <key>"
const string controlStr = "ą"; // "a caudata"
var arguments = new GitArgumentBuilder("config")
{
"--get",
controlStr
};
// git config --get with a malformed key (no section) returns:
// "error: key does not contain a section: <key>"
const string controlStr = "ą"; // "a caudata"
var arguments = new GitArgumentBuilder("config")
{
"--get",
controlStr
};

string s = new GitModule("").RunGitCmd(arguments, Encoding.UTF8);
if (s != null && s.IndexOf(controlStr) != -1)
{
_systemEncoding = new UTF8Encoding(false);
string s = new GitModule("").RunGitCmd(arguments, Encoding.UTF8);
if (s != null && s.IndexOf(controlStr) != -1)
{
_systemEncoding = new UTF8Encoding(false);
}
else
{
_systemEncoding = Encoding.Default;
}

Debug.WriteLine("System encoding: " + _systemEncoding.EncodingName);
}
else
catch (Exception)
{
_systemEncoding = Encoding.Default;
// Ignore exception. If the git location itself is not configured correctly yet, we could never execute it.
return Encoding.Default;
}

Debug.WriteLine("System encoding: " + _systemEncoding.EncodingName);
}

return _systemEncoding;
Expand Down

0 comments on commit db6c0e3

Please sign in to comment.