refactor: remove scope parameter while getting git configuration (#544)

This commit is contained in:
leo 2024-10-09 14:20:43 +08:00
parent 249706c1ef
commit 4d14302929
No known key found for this signature in database

View file

@ -7,17 +7,23 @@ namespace SourceGit.Commands
{ {
public Config(string repository) public Config(string repository)
{ {
WorkingDirectory = repository; if (string.IsNullOrEmpty(repository))
Context = repository; {
WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
}
else
{
WorkingDirectory = repository;
Context = repository;
_isLocal = true;
}
RaiseError = false; RaiseError = false;
} }
public Dictionary<string, string> ListAll() public Dictionary<string, string> ListAll()
{ {
if (string.IsNullOrEmpty(WorkingDirectory)) Args = "config -l";
Args = "config --global -l";
else
Args = "config -l";
var output = ReadToEnd(); var output = ReadToEnd();
var rs = new Dictionary<string, string>(); var rs = new Dictionary<string, string>();
@ -47,22 +53,16 @@ namespace SourceGit.Commands
public bool Set(string key, string value, bool allowEmpty = false) public bool Set(string key, string value, bool allowEmpty = false)
{ {
var scope = _isLocal ? "--local" : "--global";
if (!allowEmpty && string.IsNullOrWhiteSpace(value)) if (!allowEmpty && string.IsNullOrWhiteSpace(value))
{ Args = $"config {scope} --unset {key}";
if (string.IsNullOrEmpty(WorkingDirectory))
Args = $"config --global --unset {key}";
else
Args = $"config --unset {key}";
}
else else
{ Args = $"config {scope} {key} \"{value}\"";
if (string.IsNullOrWhiteSpace(WorkingDirectory))
Args = $"config --global {key} \"{value}\"";
else
Args = $"config {key} \"{value}\"";
}
return Exec(); return Exec();
} }
private bool _isLocal = false;
} }
} }