sourcegit/src/Commands/QueryBranches.cs

85 lines
2.9 KiB
C#
Raw Normal View History

using System;
2021-04-29 05:05:55 -07:00
using System.Collections.Generic;
namespace SourceGit.Commands
{
2024-07-22 01:41:35 -07:00
public class QueryBranches : Command
{
private const string PREFIX_LOCAL = "refs/heads/";
private const string PREFIX_REMOTE = "refs/remotes/";
private const string PREFIX_DETACHED_AT = "(HEAD detached at";
private const string PREFIX_DETACHED_FROM = "(HEAD detached from";
public QueryBranches(string repo)
{
WorkingDirectory = repo;
Context = repo;
Args = "branch -l --all -v --format=\"%(refname)$%(objectname)$%(HEAD)$%(upstream)$%(upstream:trackshort)\"";
2021-04-29 05:05:55 -07:00
}
public List<Models.Branch> Result()
{
2021-04-29 05:05:55 -07:00
Exec();
foreach (var b in _needQueryTrackStatus)
b.TrackStatus = new QueryTrackStatus(WorkingDirectory, b.Name, b.Upstream).Result();
return _branches;
2021-04-29 05:05:55 -07:00
}
protected override void OnReadline(string line)
{
var parts = line.Split('$');
if (parts.Length != 5)
return;
2021-04-29 05:05:55 -07:00
var branch = new Models.Branch();
var refName = parts[0];
if (refName.EndsWith("/HEAD", StringComparison.Ordinal))
return;
2024-06-06 00:31:02 -07:00
if (refName.StartsWith(PREFIX_DETACHED_AT, StringComparison.Ordinal) || refName.StartsWith(PREFIX_DETACHED_FROM, StringComparison.Ordinal))
{
branch.IsDetachedHead = true;
}
2024-06-06 00:31:02 -07:00
if (refName.StartsWith(PREFIX_LOCAL, StringComparison.Ordinal))
{
2021-04-29 05:05:55 -07:00
branch.Name = refName.Substring(PREFIX_LOCAL.Length);
branch.IsLocal = true;
}
else if (refName.StartsWith(PREFIX_REMOTE, StringComparison.Ordinal))
{
2021-04-29 05:05:55 -07:00
var name = refName.Substring(PREFIX_REMOTE.Length);
var shortNameIdx = name.IndexOf('/', StringComparison.Ordinal);
if (shortNameIdx < 0)
return;
branch.Remote = name.Substring(0, shortNameIdx);
2021-04-29 05:05:55 -07:00
branch.Name = name.Substring(branch.Remote.Length + 1);
branch.IsLocal = false;
}
else
{
2021-04-29 05:05:55 -07:00
branch.Name = refName;
branch.IsLocal = true;
}
branch.FullName = refName;
branch.Head = parts[1];
branch.IsCurrent = parts[2] == "*";
branch.Upstream = parts[3];
2021-04-29 05:05:55 -07:00
if (branch.IsLocal && !string.IsNullOrEmpty(parts[4]) && !parts[4].Equals("=", StringComparison.Ordinal))
_needQueryTrackStatus.Add(branch);
else
branch.TrackStatus = new Models.BranchTrackStatus();
2021-04-29 05:05:55 -07:00
_branches.Add(branch);
2021-04-29 05:05:55 -07:00
}
private readonly List<Models.Branch> _branches = new List<Models.Branch>();
private List<Models.Branch> _needQueryTrackStatus = new List<Models.Branch>();
2021-04-29 05:05:55 -07:00
}
}