mirror of
https://github.com/sourcegit-scm/sourcegit.git
synced 2024-12-25 21:07:20 -08:00
173 lines
5.1 KiB
C#
173 lines
5.1 KiB
C#
using System.Collections.Generic;
|
|
using Avalonia.Collections;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
namespace SourceGit.ViewModels
|
|
{
|
|
public class RepositoryConfigure : ObservableObject
|
|
{
|
|
public string UserName
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
public string UserEmail
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
public bool GPGCommitSigningEnabled
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
public bool GPGTagSigningEnabled
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
public string GPGUserSigningKey
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
public string HttpProxy
|
|
{
|
|
get => _httpProxy;
|
|
set => SetProperty(ref _httpProxy, value);
|
|
}
|
|
|
|
public AvaloniaList<Models.CommitTemplate> CommitTemplates
|
|
{
|
|
get => _repo.Settings.CommitTemplates;
|
|
}
|
|
|
|
public Models.CommitTemplate SelectedCommitTemplate
|
|
{
|
|
get => _selectedCommitTemplate;
|
|
set => SetProperty(ref _selectedCommitTemplate, value);
|
|
}
|
|
|
|
public AvaloniaList<Models.IssueTrackerRule> IssueTrackerRules
|
|
{
|
|
get => _repo.Settings.IssueTrackerRules;
|
|
}
|
|
|
|
public Models.IssueTrackerRule SelectedIssueTrackerRule
|
|
{
|
|
get => _selectedIssueTrackerRule;
|
|
set => SetProperty(ref _selectedIssueTrackerRule, value);
|
|
}
|
|
|
|
public RepositoryConfigure(Repository repo)
|
|
{
|
|
_repo = repo;
|
|
|
|
_cached = new Commands.Config(repo.FullPath).ListAll();
|
|
if (_cached.TryGetValue("user.name", out var name))
|
|
UserName = name;
|
|
if (_cached.TryGetValue("user.email", out var email))
|
|
UserEmail = email;
|
|
if (_cached.TryGetValue("commit.gpgsign", out var gpgCommitSign))
|
|
GPGCommitSigningEnabled = gpgCommitSign == "true";
|
|
if (_cached.TryGetValue("tag.gpgsign", out var gpgTagSign))
|
|
GPGTagSigningEnabled = gpgTagSign == "true";
|
|
if (_cached.TryGetValue("user.signingkey", out var signingKey))
|
|
GPGUserSigningKey = signingKey;
|
|
if (_cached.TryGetValue("http.proxy", out var proxy))
|
|
HttpProxy = proxy;
|
|
}
|
|
|
|
public void ClearHttpProxy()
|
|
{
|
|
HttpProxy = string.Empty;
|
|
}
|
|
|
|
public void AddCommitTemplate()
|
|
{
|
|
var template = new Models.CommitTemplate() { Name = "New Template" };
|
|
_repo.Settings.CommitTemplates.Add(template);
|
|
SelectedCommitTemplate = template;
|
|
}
|
|
|
|
public void RemoveSelectedCommitTemplate()
|
|
{
|
|
if (_selectedCommitTemplate != null)
|
|
_repo.Settings.CommitTemplates.Remove(_selectedCommitTemplate);
|
|
SelectedCommitTemplate = null;
|
|
}
|
|
|
|
public void AddSampleGithubIssueTracker()
|
|
{
|
|
foreach (var remote in _repo.Remotes)
|
|
{
|
|
if (remote.URL.Contains("github.com", System.StringComparison.Ordinal))
|
|
{
|
|
if (remote.TryGetVisitURL(out string url))
|
|
{
|
|
SelectedIssueTrackerRule = _repo.Settings.AddGithubIssueTracker(url);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
SelectedIssueTrackerRule = _repo.Settings.AddGithubIssueTracker(null);
|
|
}
|
|
|
|
public void AddSampleJiraIssueTracker()
|
|
{
|
|
SelectedIssueTrackerRule = _repo.Settings.AddJiraIssueTracker();
|
|
}
|
|
|
|
public void NewIssueTracker()
|
|
{
|
|
SelectedIssueTrackerRule = _repo.Settings.AddNewIssueTracker();
|
|
}
|
|
|
|
public void RemoveSelectedIssueTracker()
|
|
{
|
|
if (_selectedIssueTrackerRule != null)
|
|
_repo.Settings.RemoveIssueTracker(_selectedIssueTrackerRule);
|
|
SelectedIssueTrackerRule = null;
|
|
}
|
|
|
|
public void Save()
|
|
{
|
|
SetIfChanged("user.name", UserName);
|
|
SetIfChanged("user.email", UserEmail);
|
|
SetIfChanged("commit.gpgsign", GPGCommitSigningEnabled ? "true" : "false");
|
|
SetIfChanged("tag.gpgsign", GPGTagSigningEnabled ? "true" : "false");
|
|
SetIfChanged("user.signingkey", GPGUserSigningKey);
|
|
SetIfChanged("http.proxy", HttpProxy);
|
|
}
|
|
|
|
private void SetIfChanged(string key, string value)
|
|
{
|
|
bool changed = false;
|
|
if (_cached.TryGetValue(key, out var old))
|
|
{
|
|
changed = old != value;
|
|
}
|
|
else if (!string.IsNullOrEmpty(value))
|
|
{
|
|
changed = true;
|
|
}
|
|
|
|
if (changed)
|
|
{
|
|
new Commands.Config(_repo.FullPath).Set(key, value);
|
|
}
|
|
}
|
|
|
|
private readonly Repository _repo = null;
|
|
private readonly Dictionary<string, string> _cached = null;
|
|
private string _httpProxy;
|
|
private Models.CommitTemplate _selectedCommitTemplate = null;
|
|
private Models.IssueTrackerRule _selectedIssueTrackerRule = null;
|
|
}
|
|
}
|