2024-02-05 23:08:37 -08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
2021-04-29 05:05:55 -07:00
|
|
|
|
namespace SourceGit.Commands {
|
2024-02-05 23:08:37 -08:00
|
|
|
|
public class Config : Command {
|
|
|
|
|
public Config(string repository) {
|
|
|
|
|
WorkingDirectory = repository;
|
|
|
|
|
Context = repository;
|
|
|
|
|
RaiseError = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Dictionary<string, string> ListAll() {
|
|
|
|
|
Args = "config -l";
|
2021-04-29 05:05:55 -07:00
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var output = ReadToEnd();
|
|
|
|
|
var rs = new Dictionary<string, string>();
|
|
|
|
|
if (output.IsSuccess) {
|
|
|
|
|
var lines = output.StdOut.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
foreach (var line in lines) {
|
|
|
|
|
var idx = line.IndexOf('=');
|
|
|
|
|
if (idx != -1) {
|
|
|
|
|
var key = line.Substring(0, idx).Trim();
|
|
|
|
|
var val = line.Substring(idx+1).Trim();
|
|
|
|
|
if (rs.ContainsKey(key)) {
|
|
|
|
|
rs[key] = val;
|
|
|
|
|
} else {
|
|
|
|
|
rs.Add(key, val);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-29 05:05:55 -07:00
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
|
return rs;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Get(string key) {
|
|
|
|
|
Args = $"config {key}";
|
2024-02-05 23:08:37 -08:00
|
|
|
|
return ReadToEnd().StdOut.Trim();
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
|
public bool Set(string key, string value, bool allowEmpty = false) {
|
|
|
|
|
if (!allowEmpty && string.IsNullOrWhiteSpace(value)) {
|
|
|
|
|
if (string.IsNullOrEmpty(WorkingDirectory)) {
|
2021-04-29 05:05:55 -07:00
|
|
|
|
Args = $"config --global --unset {key}";
|
|
|
|
|
} else {
|
|
|
|
|
Args = $"config --unset {key}";
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2024-02-05 23:08:37 -08:00
|
|
|
|
if (string.IsNullOrWhiteSpace(WorkingDirectory)) {
|
|
|
|
|
Args = $"config --global {key} \"{value}\"";
|
2021-04-29 05:05:55 -07:00
|
|
|
|
} else {
|
2024-02-05 23:08:37 -08:00
|
|
|
|
Args = $"config {key} \"{value}\"";
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Exec();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|