-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathConfig.cs
68 lines (58 loc) · 1.88 KB
/
Config.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections.Generic;
namespace SourceGit.Commands
{
public class Config : Command
{
public Config(string repository)
{
if (string.IsNullOrEmpty(repository))
{
WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
}
else
{
WorkingDirectory = repository;
Context = repository;
_isLocal = true;
}
RaiseError = false;
}
public Dictionary<string, string> ListAll()
{
Args = "config -l";
var output = ReadToEnd();
var rs = new Dictionary<string, string>();
if (output.IsSuccess)
{
var lines = output.StdOut.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
var idx = line.IndexOf('=', StringComparison.Ordinal);
if (idx != -1)
{
var key = line.Substring(0, idx).Trim();
var val = line.Substring(idx + 1).Trim();
rs[key] = val;
}
}
}
return rs;
}
public string Get(string key)
{
Args = $"config {key}";
return ReadToEnd().StdOut.Trim();
}
public bool Set(string key, string value, bool allowEmpty = false)
{
var scope = _isLocal ? "--local" : "--global";
if (!allowEmpty && string.IsNullOrWhiteSpace(value))
Args = $"config {scope} --unset {key}";
else
Args = $"config {scope} {key} \"{value}\"";
return Exec();
}
private bool _isLocal = false;
}
}