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

View file

@ -17,6 +17,7 @@ namespace SourceGit.Commands {
/// 命令接口
/// </summary>
public class Command {
private static readonly Regex PROGRESS_REG = new Regex(@"\d+%");
/// <summary>
/// 读取全部输出时的结果
@ -68,7 +69,6 @@ namespace SourceGit.Commands {
if (!string.IsNullOrEmpty(Cwd)) start.WorkingDirectory = Cwd;
var progressFilter = new Regex(@"\s\d+%\s");
var errs = new List<string>();
var proc = new Process() { StartInfo = start };
var isCancelled = false;
@ -97,8 +97,12 @@ namespace SourceGit.Commands {
if (string.IsNullOrEmpty(e.Data)) return;
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: 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);
};

View file

@ -77,13 +77,13 @@ namespace SourceGit.Commands {
} else if (line.StartsWith("author ", StringComparison.Ordinal)) {
Models.User user = Models.User.Invalid;
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.AuthorTime = time;
} else if (line.StartsWith("committer ", StringComparison.Ordinal)) {
Models.User user = Models.User.Invalid;
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.CommitterTime = time;
} else if (string.IsNullOrEmpty(current.Subject)) {

View file

@ -39,7 +39,7 @@ namespace SourceGit.Commands {
} else if (line.StartsWith("author ", StringComparison.Ordinal)) {
Models.User user = Models.User.Invalid;
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.Time = time;
}

View file

@ -1,5 +1,5 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace SourceGit.Commands {
/// <summary>
@ -7,8 +7,6 @@ namespace SourceGit.Commands {
/// </summary>
public class Tags : Command {
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>();
public Tags(string path) {
@ -22,22 +20,16 @@ namespace SourceGit.Commands {
}
public override void OnReadline(string line) {
var match = REG_FORMAT.Match(line);
if (!match.Success) return;
var name = match.Groups[1].Value;
var commit = match.Groups[2].Value;
var dereference = match.Groups[3].Value;
if (string.IsNullOrEmpty(dereference)) {
var subs = line.Split(new char[] { '$' }, StringSplitOptions.RemoveEmptyEntries);
if (subs.Length == 2) {
loaded.Add(new Models.Tag() {
Name = name,
SHA = commit,
Name = subs[0],
SHA = subs[1],
});
} else {
} else if (subs.Length == 3) {
loaded.Add(new Models.Tag() {
Name = name,
SHA = dereference,
Name = subs[0],
SHA = subs[2],
});
}
}

View file

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

View file

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows;
namespace SourceGit.Models {
@ -8,7 +7,6 @@ namespace SourceGit.Models {
/// 提交记录
/// </summary>
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();
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 static void ParseUserAndTime(string data, ref User user, ref ulong time) {
var match = REG_USER_FORMAT.Match(data);
if (!match.Success) return;
var userEndIdx = data.IndexOf('>');
if (userEndIdx < 0) return;
user = User.FindOrAdd(match.Groups[1].Value, match.Groups[2].Value);
time = ulong.Parse(match.Groups[3].Value);
var timeEndIdx = data.IndexOf(' ', userEndIdx + 2);
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();
}
public static User FindOrAdd(string name, string email) {
string key = $"{name}#&#{email}";
if (Caches.ContainsKey(key)) {
return Caches[key];
public static User FindOrAdd(string data) {
if (Caches.ContainsKey(data)) {
return Caches[data];
} 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 };
Caches.Add(key, user);
Caches.Add(data, user);
return user;
}
}