refactor: read git output once time instead of line-by-line

This commit is contained in:
leo 2024-09-30 09:27:15 +08:00
parent 986c27e1cb
commit f2f8a64b84
No known key found for this signature in database
2 changed files with 41 additions and 26 deletions

View file

@ -19,29 +19,35 @@ namespace SourceGit.Commands
public List<Models.Branch> Result()
{
Exec();
var branches = new List<Models.Branch>();
var rs = ReadToEnd();
if (!rs.IsSuccess)
return branches;
foreach (var b in _needQueryTrackStatus)
b.TrackStatus = new QueryTrackStatus(WorkingDirectory, b.Name, b.Upstream).Result();
return _branches;
var lines = rs.StdOut.Split('\n', StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
var b = ParseLine(line);
if (b != null)
branches.Add(b);
}
protected override void OnReadline(string line)
return branches;
}
private Models.Branch ParseLine(string line)
{
var parts = line.Split('$');
if (parts.Length != 5)
return;
return null;
var branch = new Models.Branch();
var refName = parts[0];
if (refName.EndsWith("/HEAD", StringComparison.Ordinal))
return;
return null;
if (refName.StartsWith(PREFIX_DETACHED_AT, StringComparison.Ordinal) || refName.StartsWith(PREFIX_DETACHED_FROM, StringComparison.Ordinal))
{
branch.IsDetachedHead = true;
}
branch.IsDetachedHead = refName.StartsWith(PREFIX_DETACHED_AT, StringComparison.Ordinal) ||
refName.StartsWith(PREFIX_DETACHED_FROM, StringComparison.Ordinal);
if (refName.StartsWith(PREFIX_LOCAL, StringComparison.Ordinal))
{
@ -53,7 +59,7 @@ namespace SourceGit.Commands
var name = refName.Substring(PREFIX_REMOTE.Length);
var shortNameIdx = name.IndexOf('/', StringComparison.Ordinal);
if (shortNameIdx < 0)
return;
return null;
branch.Remote = name.Substring(0, shortNameIdx);
branch.Name = name.Substring(branch.Remote.Length + 1);
@ -71,14 +77,11 @@ namespace SourceGit.Commands
branch.Upstream = parts[3];
if (branch.IsLocal && !string.IsNullOrEmpty(parts[4]) && !parts[4].Equals("=", StringComparison.Ordinal))
_needQueryTrackStatus.Add(branch);
branch.TrackStatus = new QueryTrackStatus(WorkingDirectory, branch.Name, branch.Upstream).Result();
else
branch.TrackStatus = new Models.BranchTrackStatus();
_branches.Add(branch);
return branch;
}
private readonly List<Models.Branch> _branches = new List<Models.Branch>();
private List<Models.Branch> _needQueryTrackStatus = new List<Models.Branch>();
}
}

View file

@ -14,31 +14,43 @@ namespace SourceGit.Commands
public List<Models.Tag> Result()
{
Exec();
return _loaded;
var tags = new List<Models.Tag>();
var rs = ReadToEnd();
if (!rs.IsSuccess)
return tags;
var lines = rs.StdOut.Split('\n', StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
var tag = ParseLine(line);
if (tag != null)
tags.Add(tag);
}
protected override void OnReadline(string line)
return tags;
}
private Models.Tag ParseLine(string line)
{
var subs = line.Split('$', StringSplitOptions.RemoveEmptyEntries);
if (subs.Length == 2)
{
_loaded.Add(new Models.Tag()
return new Models.Tag()
{
Name = subs[0].Substring(10),
SHA = subs[1],
});
};
}
else if (subs.Length == 3)
{
_loaded.Add(new Models.Tag()
return new Models.Tag()
{
Name = subs[0].Substring(10),
SHA = subs[2],
});
}
};
}
private readonly List<Models.Tag> _loaded = new List<Models.Tag>();
return null;
}
}
}