optimize<*>: reduce repository loading time

This commit is contained in:
leo 2023-10-12 12:02:41 +08:00
parent 738daddbc7
commit 918eb48663
8 changed files with 38 additions and 43 deletions

View file

@ -9,8 +9,7 @@ namespace SourceGit.Commands {
public class Branches : Command { public class Branches : Command {
private static readonly string PREFIX_LOCAL = "refs/heads/"; private static readonly string PREFIX_LOCAL = "refs/heads/";
private static readonly string PREFIX_REMOTE = "refs/remotes/"; private static readonly string PREFIX_REMOTE = "refs/remotes/";
private static readonly string CMD = "branch -l --all -v --format=\"$%(refname)$%(objectname)$%(HEAD)$%(upstream)$%(upstream:track)$%(contents:subject)\""; private static readonly string CMD = "branch -l --all -v --format=\"%(refname)$%(objectname)$%(HEAD)$%(upstream)$%(upstream:track)\"";
private static readonly Regex REG_FORMAT = new Regex(@"\$(.*)\$(.*)\$([\* ])\$(.*)\$(.*?)\$(.*)");
private static readonly Regex REG_AHEAD = new Regex(@"ahead (\d+)"); private static readonly Regex REG_AHEAD = new Regex(@"ahead (\d+)");
private static readonly Regex REG_BEHIND = new Regex(@"behind (\d+)"); private static readonly Regex REG_BEHIND = new Regex(@"behind (\d+)");
@ -27,11 +26,11 @@ namespace SourceGit.Commands {
} }
public override void OnReadline(string line) { public override void OnReadline(string line) {
var match = REG_FORMAT.Match(line); var parts = line.Split('$');
if (!match.Success) return; if (parts.Length != 5) return;
var branch = new Models.Branch(); var branch = new Models.Branch();
var refName = match.Groups[1].Value; var refName = parts[0];
if (refName.EndsWith("/HEAD")) return; if (refName.EndsWith("/HEAD")) return;
if (refName.StartsWith(PREFIX_LOCAL, StringComparison.Ordinal)) { if (refName.StartsWith(PREFIX_LOCAL, StringComparison.Ordinal)) {
@ -51,11 +50,10 @@ namespace SourceGit.Commands {
} }
branch.FullName = refName; branch.FullName = refName;
branch.Head = match.Groups[2].Value; branch.Head = parts[1];
branch.IsCurrent = match.Groups[3].Value == "*"; branch.IsCurrent = parts[2] == "*";
branch.Upstream = match.Groups[4].Value; branch.Upstream = parts[3];
branch.UpstreamTrackStatus = ParseTrackStatus(match.Groups[5].Value); branch.UpstreamTrackStatus = ParseTrackStatus(parts[4]);
branch.HeadSubject = match.Groups[6].Value;
loaded.Add(branch); loaded.Add(branch);
} }

View file

@ -17,6 +17,7 @@ namespace SourceGit.Commands {
/// 命令接口 /// 命令接口
/// </summary> /// </summary>
public class Command { public class Command {
private static readonly Regex PROGRESS_REG = new Regex(@"\d+%");
/// <summary> /// <summary>
/// 读取全部输出时的结果 /// 读取全部输出时的结果
@ -68,7 +69,6 @@ namespace SourceGit.Commands {
if (!string.IsNullOrEmpty(Cwd)) start.WorkingDirectory = Cwd; if (!string.IsNullOrEmpty(Cwd)) start.WorkingDirectory = Cwd;
var progressFilter = new Regex(@"\s\d+%\s");
var errs = new List<string>(); var errs = new List<string>();
var proc = new Process() { StartInfo = start }; var proc = new Process() { StartInfo = start };
var isCancelled = false; var isCancelled = false;
@ -97,8 +97,12 @@ namespace SourceGit.Commands {
if (string.IsNullOrEmpty(e.Data)) return; if (string.IsNullOrEmpty(e.Data)) return;
if (TraitErrorAsOutput) OnReadline(e.Data); if (TraitErrorAsOutput) OnReadline(e.Data);
if (progressFilter.IsMatch(e.Data)) return; // 错误信息中忽略进度相关的输出
if (e.Data.StartsWith("remote: Enumerating objects:", StringComparison.Ordinal)) return;
if (e.Data.StartsWith("remote: Counting objects:", StringComparison.Ordinal)) return; if (e.Data.StartsWith("remote: Counting objects:", StringComparison.Ordinal)) return;
if (e.Data.StartsWith("remote: Compressing objects:", StringComparison.Ordinal)) return;
if (e.Data.StartsWith("Filtering content:", StringComparison.Ordinal)) return;
if (PROGRESS_REG.IsMatch(e.Data)) return;
errs.Add(e.Data); errs.Add(e.Data);
}; };

View file

@ -77,13 +77,13 @@ namespace SourceGit.Commands {
} else if (line.StartsWith("author ", StringComparison.Ordinal)) { } else if (line.StartsWith("author ", StringComparison.Ordinal)) {
Models.User user = Models.User.Invalid; Models.User user = Models.User.Invalid;
ulong time = 0; ulong time = 0;
Models.Commit.ParseUserAndTime(line, ref user, ref time); Models.Commit.ParseUserAndTime(line.Substring(7), ref user, ref time);
current.Author = user; current.Author = user;
current.AuthorTime = time; current.AuthorTime = time;
} else if (line.StartsWith("committer ", StringComparison.Ordinal)) { } else if (line.StartsWith("committer ", StringComparison.Ordinal)) {
Models.User user = Models.User.Invalid; Models.User user = Models.User.Invalid;
ulong time = 0; ulong time = 0;
Models.Commit.ParseUserAndTime(line, ref user, ref time); Models.Commit.ParseUserAndTime(line.Substring(10), ref user, ref time);
current.Committer = user; current.Committer = user;
current.CommitterTime = time; current.CommitterTime = time;
} else if (string.IsNullOrEmpty(current.Subject)) { } else if (string.IsNullOrEmpty(current.Subject)) {

View file

@ -39,7 +39,7 @@ namespace SourceGit.Commands {
} else if (line.StartsWith("author ", StringComparison.Ordinal)) { } else if (line.StartsWith("author ", StringComparison.Ordinal)) {
Models.User user = Models.User.Invalid; Models.User user = Models.User.Invalid;
ulong time = 0; ulong time = 0;
Models.Commit.ParseUserAndTime(line, ref user, ref time); Models.Commit.ParseUserAndTime(line.Substring(7), ref user, ref time);
current.Author = user; current.Author = user;
current.Time = time; current.Time = time;
} }

View file

@ -1,5 +1,5 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace SourceGit.Commands { namespace SourceGit.Commands {
/// <summary> /// <summary>
@ -7,8 +7,6 @@ namespace SourceGit.Commands {
/// </summary> /// </summary>
public class Tags : Command { public class Tags : Command {
public static readonly string CMD = "for-each-ref --sort=-creatordate --format=\"$%(refname:short)$%(objectname)$%(*objectname)\" refs/tags"; public static readonly string CMD = "for-each-ref --sort=-creatordate --format=\"$%(refname:short)$%(objectname)$%(*objectname)\" refs/tags";
public static readonly Regex REG_FORMAT = new Regex(@"\$(.*)\$(.*)\$(.*)");
private List<Models.Tag> loaded = new List<Models.Tag>(); private List<Models.Tag> loaded = new List<Models.Tag>();
public Tags(string path) { public Tags(string path) {
@ -22,22 +20,16 @@ namespace SourceGit.Commands {
} }
public override void OnReadline(string line) { public override void OnReadline(string line) {
var match = REG_FORMAT.Match(line); var subs = line.Split(new char[] { '$' }, StringSplitOptions.RemoveEmptyEntries);
if (!match.Success) return; if (subs.Length == 2) {
var name = match.Groups[1].Value;
var commit = match.Groups[2].Value;
var dereference = match.Groups[3].Value;
if (string.IsNullOrEmpty(dereference)) {
loaded.Add(new Models.Tag() { loaded.Add(new Models.Tag() {
Name = name, Name = subs[0],
SHA = commit, SHA = subs[1],
}); });
} else { } else if (subs.Length == 3) {
loaded.Add(new Models.Tag() { loaded.Add(new Models.Tag() {
Name = name, Name = subs[0],
SHA = dereference, SHA = subs[2],
}); });
} }
} }

View file

@ -6,7 +6,6 @@ namespace SourceGit.Models {
public string Name { get; set; } public string Name { get; set; }
public string FullName { get; set; } public string FullName { get; set; }
public string Head { get; set; } public string Head { get; set; }
public string HeadSubject { get; set; }
public bool IsLocal { get; set; } public bool IsLocal { get; set; }
public bool IsCurrent { get; set; } public bool IsCurrent { get; set; }
public string Upstream { get; set; } public string Upstream { get; set; }

View file

@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows; using System.Windows;
namespace SourceGit.Models { namespace SourceGit.Models {
@ -8,7 +7,6 @@ namespace SourceGit.Models {
/// 提交记录 /// 提交记录
/// </summary> /// </summary>
public class Commit { public class Commit {
private static readonly Regex REG_USER_FORMAT = new Regex(@"\w+ (.*) <(.*)> (\d{10}) [\+\-]\d+");
private static readonly DateTime UTC_START = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).ToLocalTime(); private static readonly DateTime UTC_START = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).ToLocalTime();
public string SHA { get; set; } = string.Empty; public string SHA { get; set; } = string.Empty;
@ -31,11 +29,12 @@ namespace SourceGit.Models {
public string CommitterTimeShortStr => UTC_START.AddSeconds(CommitterTime).ToString("yyyy/MM/dd"); public string CommitterTimeShortStr => UTC_START.AddSeconds(CommitterTime).ToString("yyyy/MM/dd");
public static void ParseUserAndTime(string data, ref User user, ref ulong time) { public static void ParseUserAndTime(string data, ref User user, ref ulong time) {
var match = REG_USER_FORMAT.Match(data); var userEndIdx = data.IndexOf('>');
if (!match.Success) return; if (userEndIdx < 0) return;
user = User.FindOrAdd(match.Groups[1].Value, match.Groups[2].Value); var timeEndIdx = data.IndexOf(' ', userEndIdx + 2);
time = ulong.Parse(match.Groups[3].Value); user = User.FindOrAdd(data.Substring(0, userEndIdx));
time = timeEndIdx < 0 ? 0 : ulong.Parse(data.Substring(userEndIdx + 2, timeEndIdx - userEndIdx - 2));
} }
} }
} }

View file

@ -22,13 +22,16 @@ namespace SourceGit.Models {
return base.GetHashCode(); return base.GetHashCode();
} }
public static User FindOrAdd(string name, string email) { public static User FindOrAdd(string data) {
string key = $"{name}#&#{email}"; if (Caches.ContainsKey(data)) {
if (Caches.ContainsKey(key)) { return Caches[data];
return Caches[key];
} else { } else {
var nameEndIdx = data.IndexOf('<');
var name = nameEndIdx >= 2 ? data.Substring(0, nameEndIdx - 1) : string.Empty;
var email = data.Substring(nameEndIdx + 1);
User user = new User() { Name = name, Email = email }; User user = new User() { Name = name, Email = email };
Caches.Add(key, user); Caches.Add(data, user);
return user; return user;
} }
} }