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