mirror of
https://github.com/sourcegit-scm/sourcegit
synced 2025-05-23 21:24:59 +00:00
refactor: merge sourcegit.issuetracker.setting to sourcegit.settings.
This commit is contained in:
parent
f754b2c63a
commit
dfd098e131
15 changed files with 100 additions and 122 deletions
|
@ -4,6 +4,7 @@ using System.IO;
|
|||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Avalonia.Collections;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Media.Imaging;
|
||||
using Avalonia.Platform.Storage;
|
||||
|
@ -88,15 +89,15 @@ namespace SourceGit.ViewModels
|
|||
set => SetProperty(ref _viewRevisionFileContent, value);
|
||||
}
|
||||
|
||||
public IssueTrackerRuleSetting IssueTrackerSetting
|
||||
public AvaloniaList<Models.IssueTrackerRule> IssueTrackerRules
|
||||
{
|
||||
get => _issueTrackerSetting;
|
||||
get => _issueTrackerRules;
|
||||
}
|
||||
|
||||
public CommitDetail(string repo, IssueTrackerRuleSetting issueTrackerSetting)
|
||||
public CommitDetail(string repo, AvaloniaList<Models.IssueTrackerRule> issueTrackerRules)
|
||||
{
|
||||
_repo = repo;
|
||||
_issueTrackerSetting = issueTrackerSetting;
|
||||
_issueTrackerRules = issueTrackerRules;
|
||||
}
|
||||
|
||||
public void Cleanup()
|
||||
|
@ -248,7 +249,7 @@ namespace SourceGit.ViewModels
|
|||
history.Icon = App.CreateMenuIcon("Icons.Histories");
|
||||
history.Click += (_, ev) =>
|
||||
{
|
||||
var window = new Views.FileHistories() { DataContext = new FileHistories(_repo, change.Path, _issueTrackerSetting) };
|
||||
var window = new Views.FileHistories() { DataContext = new FileHistories(_repo, change.Path, _issueTrackerRules) };
|
||||
window.Show();
|
||||
ev.Handled = true;
|
||||
};
|
||||
|
@ -311,7 +312,7 @@ namespace SourceGit.ViewModels
|
|||
history.Icon = App.CreateMenuIcon("Icons.Histories");
|
||||
history.Click += (_, ev) =>
|
||||
{
|
||||
var window = new Views.FileHistories() { DataContext = new FileHistories(_repo, file.Path, _issueTrackerSetting) };
|
||||
var window = new Views.FileHistories() { DataContext = new FileHistories(_repo, file.Path, _issueTrackerRules) };
|
||||
window.Show();
|
||||
ev.Handled = true;
|
||||
};
|
||||
|
@ -463,7 +464,7 @@ namespace SourceGit.ViewModels
|
|||
};
|
||||
|
||||
private string _repo;
|
||||
private IssueTrackerRuleSetting _issueTrackerSetting = null;
|
||||
private AvaloniaList<Models.IssueTrackerRule> _issueTrackerRules = null;
|
||||
private int _activePageIndex = 0;
|
||||
private Models.Commit _commit = null;
|
||||
private string _fullMessage = string.Empty;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Avalonia.Collections;
|
||||
using Avalonia.Threading;
|
||||
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
|
@ -54,11 +54,11 @@ namespace SourceGit.ViewModels
|
|||
set => SetProperty(ref _detailContext, value);
|
||||
}
|
||||
|
||||
public FileHistories(string repo, string file, IssueTrackerRuleSetting issueTrackerSetting)
|
||||
public FileHistories(string repo, string file, AvaloniaList<Models.IssueTrackerRule> issueTrackerRules)
|
||||
{
|
||||
_repo = repo;
|
||||
_file = file;
|
||||
_detailContext = new CommitDetail(repo, issueTrackerSetting);
|
||||
_detailContext = new CommitDetail(repo, issueTrackerRules);
|
||||
|
||||
Task.Run(() =>
|
||||
{
|
||||
|
|
|
@ -94,7 +94,7 @@ namespace SourceGit.ViewModels
|
|||
}
|
||||
else
|
||||
{
|
||||
var commitDetail = new CommitDetail(_repo.FullPath, _repo.IssueTrackerSetting);
|
||||
var commitDetail = new CommitDetail(_repo.FullPath, _repo.Settings.IssueTrackerRules);
|
||||
commitDetail.Commit = commit;
|
||||
DetailContext = commitDetail;
|
||||
}
|
||||
|
@ -122,7 +122,7 @@ namespace SourceGit.ViewModels
|
|||
}
|
||||
else
|
||||
{
|
||||
var commitDetail = new CommitDetail(_repo.FullPath, _repo.IssueTrackerSetting);
|
||||
var commitDetail = new CommitDetail(_repo.FullPath, _repo.Settings.IssueTrackerRules);
|
||||
commitDetail.Commit = commit;
|
||||
DetailContext = commitDetail;
|
||||
}
|
||||
|
|
|
@ -114,7 +114,7 @@ namespace SourceGit.ViewModels
|
|||
Current = current;
|
||||
On = on;
|
||||
IsLoading = true;
|
||||
DetailContext = new CommitDetail(repoPath, repo.IssueTrackerSetting);
|
||||
DetailContext = new CommitDetail(repoPath, repo.Settings.IssueTrackerRules);
|
||||
|
||||
Task.Run(() =>
|
||||
{
|
||||
|
|
|
@ -1,164 +0,0 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
using Avalonia.Collections;
|
||||
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
|
||||
namespace SourceGit.ViewModels
|
||||
{
|
||||
public class IssueTrackerMatch
|
||||
{
|
||||
public int Start { get; set; } = 0;
|
||||
public int Length { get; set; } = 0;
|
||||
public string URL { get; set; } = "";
|
||||
|
||||
public bool Intersect(int start, int length)
|
||||
{
|
||||
if (start == Start)
|
||||
return true;
|
||||
|
||||
if (start < Start)
|
||||
return start + length > Start;
|
||||
|
||||
return start < Start + Length;
|
||||
}
|
||||
}
|
||||
|
||||
public class IssueTrackerRule : ObservableObject
|
||||
{
|
||||
public string Name
|
||||
{
|
||||
get => _name;
|
||||
set => SetProperty(ref _name, value);
|
||||
}
|
||||
|
||||
public string RegexString
|
||||
{
|
||||
get => _regexString;
|
||||
set
|
||||
{
|
||||
if (SetProperty(ref _regexString, value))
|
||||
{
|
||||
try
|
||||
{
|
||||
_regex = null;
|
||||
_regex = new Regex(_regexString, RegexOptions.Multiline);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Ignore errors.
|
||||
}
|
||||
}
|
||||
|
||||
OnPropertyChanged(nameof(IsRegexValid));
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsRegexValid
|
||||
{
|
||||
get => _regex != null;
|
||||
}
|
||||
|
||||
public string URLTemplate
|
||||
{
|
||||
get => _urlTemplate;
|
||||
set => SetProperty(ref _urlTemplate, value);
|
||||
}
|
||||
|
||||
public void Matches(List<IssueTrackerMatch> outs, string message)
|
||||
{
|
||||
if (_regex == null || string.IsNullOrEmpty(_urlTemplate))
|
||||
return;
|
||||
|
||||
var matches = _regex.Matches(message);
|
||||
for (int i = 0; i < matches.Count; i++)
|
||||
{
|
||||
var match = matches[i];
|
||||
if (!match.Success)
|
||||
continue;
|
||||
|
||||
var start = match.Index;
|
||||
var len = match.Length;
|
||||
var intersect = false;
|
||||
foreach (var exist in outs)
|
||||
{
|
||||
if (exist.Intersect(start, len))
|
||||
{
|
||||
intersect = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (intersect)
|
||||
continue;
|
||||
|
||||
var range = new IssueTrackerMatch();
|
||||
range.Start = start;
|
||||
range.Length = len;
|
||||
range.URL = _urlTemplate;
|
||||
for (int j = 1; j < match.Groups.Count; j++)
|
||||
{
|
||||
var group = match.Groups[j];
|
||||
if (group.Success)
|
||||
range.URL = range.URL.Replace($"${j}", group.Value);
|
||||
}
|
||||
|
||||
outs.Add(range);
|
||||
}
|
||||
}
|
||||
|
||||
private string _name;
|
||||
private string _regexString;
|
||||
private string _urlTemplate;
|
||||
private Regex _regex = null;
|
||||
}
|
||||
|
||||
public class IssueTrackerRuleSetting
|
||||
{
|
||||
public AvaloniaList<IssueTrackerRule> Rules
|
||||
{
|
||||
get;
|
||||
set;
|
||||
} = new AvaloniaList<IssueTrackerRule>();
|
||||
|
||||
public IssueTrackerRule Add()
|
||||
{
|
||||
var rule = new IssueTrackerRule()
|
||||
{
|
||||
Name = "New Issue Tracker",
|
||||
RegexString = "#(\\d+)",
|
||||
URLTemplate = "https://xxx/$1",
|
||||
};
|
||||
|
||||
Rules.Add(rule);
|
||||
return rule;
|
||||
}
|
||||
|
||||
public IssueTrackerRule AddGithub(string repoURL)
|
||||
{
|
||||
var rule = new IssueTrackerRule()
|
||||
{
|
||||
Name = "Github ISSUE",
|
||||
RegexString = "#(\\d+)",
|
||||
URLTemplate = string.IsNullOrEmpty(repoURL) ? "https://github.com/username/repository/issues/$1" : $"{repoURL}/issues/$1",
|
||||
};
|
||||
|
||||
Rules.Add(rule);
|
||||
return rule;
|
||||
}
|
||||
|
||||
public IssueTrackerRule AddJira()
|
||||
{
|
||||
var rule = new IssueTrackerRule()
|
||||
{
|
||||
Name = "Jira Tracker",
|
||||
RegexString = "PROJ-(\\d+)",
|
||||
URLTemplate = "https://jira.yourcompany.com/browse/PROJ-$1",
|
||||
};
|
||||
|
||||
Rules.Add(rule);
|
||||
return rule;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -44,11 +44,6 @@ namespace SourceGit.ViewModels
|
|||
get => _settings;
|
||||
}
|
||||
|
||||
public IssueTrackerRuleSetting IssueTrackerSetting
|
||||
{
|
||||
get => _issueTrackerSetting;
|
||||
}
|
||||
|
||||
public int SelectedViewIndex
|
||||
{
|
||||
get => _selectedViewIndex;
|
||||
|
@ -324,23 +319,6 @@ namespace SourceGit.ViewModels
|
|||
_settings = new Models.RepositorySettings();
|
||||
}
|
||||
|
||||
var issueTrackerSettingsFile = Path.Combine(_gitDir, "sourcegit.issuetracker.settings");
|
||||
if (File.Exists(issueTrackerSettingsFile))
|
||||
{
|
||||
try
|
||||
{
|
||||
_issueTrackerSetting = JsonSerializer.Deserialize(File.ReadAllText(issueTrackerSettingsFile), JsonCodeGen.Default.IssueTrackerRuleSetting);
|
||||
}
|
||||
catch
|
||||
{
|
||||
_issueTrackerSetting = new IssueTrackerRuleSetting();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_issueTrackerSetting = new IssueTrackerRuleSetting();
|
||||
}
|
||||
|
||||
_watcher = new Models.Watcher(this);
|
||||
_histories = new Histories(this);
|
||||
_workingCopy = new WorkingCopy(this);
|
||||
|
@ -361,10 +339,6 @@ namespace SourceGit.ViewModels
|
|||
File.WriteAllText(Path.Combine(_gitDir, "sourcegit.settings"), settingsSerialized);
|
||||
_settings = null;
|
||||
|
||||
var issueTrackerSerialized = JsonSerializer.Serialize(_issueTrackerSetting, JsonCodeGen.Default.IssueTrackerRuleSetting);
|
||||
File.WriteAllText(Path.Combine(_gitDir, "sourcegit.issuetracker.settings"), issueTrackerSerialized);
|
||||
_issueTrackerSetting = null;
|
||||
|
||||
_watcher.Dispose();
|
||||
_histories.Cleanup();
|
||||
_workingCopy.Cleanup();
|
||||
|
@ -1986,7 +1960,6 @@ namespace SourceGit.ViewModels
|
|||
private string _fullpath = string.Empty;
|
||||
private string _gitDir = string.Empty;
|
||||
private Models.RepositorySettings _settings = null;
|
||||
private IssueTrackerRuleSetting _issueTrackerSetting = null;
|
||||
|
||||
private Models.Watcher _watcher = null;
|
||||
private Histories _histories = null;
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using Avalonia.Collections;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
|
||||
|
@ -43,12 +42,12 @@ namespace SourceGit.ViewModels
|
|||
set => SetProperty(ref _httpProxy, value);
|
||||
}
|
||||
|
||||
public AvaloniaList<IssueTrackerRule> IssueTrackerRules
|
||||
public AvaloniaList<Models.IssueTrackerRule> IssueTrackerRules
|
||||
{
|
||||
get => _repo.IssueTrackerSetting.Rules;
|
||||
get => _repo.Settings.IssueTrackerRules;
|
||||
}
|
||||
|
||||
public IssueTrackerRule SelectedIssueTrackerRule
|
||||
public Models.IssueTrackerRule SelectedIssueTrackerRule
|
||||
{
|
||||
get => _selectedIssueTrackerRule;
|
||||
set => SetProperty(ref _selectedIssueTrackerRule, value);
|
||||
|
@ -86,29 +85,29 @@ namespace SourceGit.ViewModels
|
|||
{
|
||||
if (remote.TryGetVisitURL(out string url))
|
||||
{
|
||||
SelectedIssueTrackerRule = _repo.IssueTrackerSetting.AddGithub(url);
|
||||
SelectedIssueTrackerRule = _repo.Settings.AddGithubIssueTracker(url);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SelectedIssueTrackerRule = _repo.IssueTrackerSetting.AddGithub(null);
|
||||
SelectedIssueTrackerRule = _repo.Settings.AddGithubIssueTracker(null);
|
||||
}
|
||||
|
||||
public void AddSampleJiraIssueTracker()
|
||||
{
|
||||
SelectedIssueTrackerRule = _repo.IssueTrackerSetting.AddJira();
|
||||
SelectedIssueTrackerRule = _repo.Settings.AddJiraIssueTracker();
|
||||
}
|
||||
|
||||
public void NewIssueTracker()
|
||||
{
|
||||
SelectedIssueTrackerRule = _repo.IssueTrackerSetting.Add();
|
||||
SelectedIssueTrackerRule = _repo.Settings.AddNewIssueTracker();
|
||||
}
|
||||
|
||||
public void RemoveSelectedIssueTracker()
|
||||
{
|
||||
if (_selectedIssueTrackerRule != null)
|
||||
_repo.IssueTrackerSetting.Rules.Remove(_selectedIssueTrackerRule);
|
||||
_repo.Settings.RemoveIssueTracker(_selectedIssueTrackerRule);
|
||||
SelectedIssueTrackerRule = null;
|
||||
}
|
||||
|
||||
public void Save()
|
||||
|
@ -142,6 +141,6 @@ namespace SourceGit.ViewModels
|
|||
private readonly Repository _repo = null;
|
||||
private readonly Dictionary<string, string> _cached = null;
|
||||
private string _httpProxy;
|
||||
private IssueTrackerRule _selectedIssueTrackerRule = null;
|
||||
private Models.IssueTrackerRule _selectedIssueTrackerRule = null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -541,7 +541,7 @@ namespace SourceGit.ViewModels
|
|||
history.Icon = App.CreateMenuIcon("Icons.Histories");
|
||||
history.Click += (_, e) =>
|
||||
{
|
||||
var window = new Views.FileHistories() { DataContext = new FileHistories(_repo.FullPath, change.Path, _repo.IssueTrackerSetting) };
|
||||
var window = new Views.FileHistories() { DataContext = new FileHistories(_repo.FullPath, change.Path, _repo.Settings.IssueTrackerRules) };
|
||||
window.Show();
|
||||
e.Handled = true;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue