2024-02-05 23:08:37 -08:00
|
|
|
|
using System;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
namespace SourceGit.Commands
|
|
|
|
|
{
|
|
|
|
|
public partial class QueryBranches : Command
|
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
|
private const string PREFIX_LOCAL = "refs/heads/";
|
|
|
|
|
private const string PREFIX_REMOTE = "refs/remotes/";
|
2024-03-17 18:37:06 -07:00
|
|
|
|
|
2024-03-16 02:09:27 -07:00
|
|
|
|
[GeneratedRegex(@"^(\d+)\s(\d+)$")]
|
|
|
|
|
private static partial Regex REG_AHEAD_BEHIND();
|
2021-04-29 05:05:55 -07:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public QueryBranches(string repo)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
WorkingDirectory = repo;
|
|
|
|
|
Context = repo;
|
2024-03-08 01:16:54 -08:00
|
|
|
|
Args = "branch -l --all -v --format=\"%(refname)$%(objectname)$%(HEAD)$%(upstream)$%(upstream:trackshort)\"";
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public List<Models.Branch> Result()
|
|
|
|
|
{
|
2021-04-29 05:05:55 -07:00
|
|
|
|
Exec();
|
2024-03-08 01:16:54 -08:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
foreach (var b in _branches)
|
|
|
|
|
{
|
|
|
|
|
if (b.IsLocal && !string.IsNullOrEmpty(b.UpstreamTrackStatus))
|
|
|
|
|
{
|
|
|
|
|
if (b.UpstreamTrackStatus == "=")
|
|
|
|
|
{
|
2024-03-08 01:16:54 -08:00
|
|
|
|
b.UpstreamTrackStatus = string.Empty;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-03-08 01:16:54 -08:00
|
|
|
|
b.UpstreamTrackStatus = ParseTrackStatus(b.Name, b.Upstream);
|
2024-03-08 01:41:10 -08:00
|
|
|
|
}
|
2024-03-08 01:16:54 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
|
return _branches;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
protected override void OnReadline(string line)
|
|
|
|
|
{
|
2023-10-11 21:02:41 -07:00
|
|
|
|
var parts = line.Split('$');
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (parts.Length != 5)
|
|
|
|
|
return;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
|
|
|
|
|
var branch = new Models.Branch();
|
2023-10-11 21:02:41 -07:00
|
|
|
|
var refName = parts[0];
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (refName.EndsWith("/HEAD", StringComparison.Ordinal))
|
|
|
|
|
return;
|
2024-05-25 11:09:40 -07:00
|
|
|
|
|
|
|
|
|
if (refName.StartsWith("(HEAD detached at"))
|
|
|
|
|
{
|
|
|
|
|
branch.isHead = true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -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;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else if (refName.StartsWith(PREFIX_REMOTE, StringComparison.Ordinal))
|
|
|
|
|
{
|
2021-04-29 05:05:55 -07:00
|
|
|
|
var name = refName.Substring(PREFIX_REMOTE.Length);
|
2024-03-13 20:09:05 -07:00
|
|
|
|
var shortNameIdx = name.IndexOf('/', StringComparison.Ordinal);
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (shortNameIdx < 0)
|
|
|
|
|
return;
|
2021-08-03 18:07:31 -07:00
|
|
|
|
|
|
|
|
|
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;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-04-29 05:05:55 -07:00
|
|
|
|
branch.Name = refName;
|
|
|
|
|
branch.IsLocal = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
branch.FullName = refName;
|
2023-10-11 21:02:41 -07:00
|
|
|
|
branch.Head = parts[1];
|
|
|
|
|
branch.IsCurrent = parts[2] == "*";
|
|
|
|
|
branch.Upstream = parts[3];
|
2024-03-08 01:16:54 -08:00
|
|
|
|
branch.UpstreamTrackStatus = parts[4];
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_branches.Add(branch);
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
private string ParseTrackStatus(string local, string upstream)
|
|
|
|
|
{
|
2024-03-08 00:32:44 -08:00
|
|
|
|
var cmd = new Command();
|
|
|
|
|
cmd.WorkingDirectory = WorkingDirectory;
|
|
|
|
|
cmd.Context = Context;
|
|
|
|
|
cmd.Args = $"rev-list --left-right --count {local}...{upstream}";
|
2021-04-29 05:05:55 -07:00
|
|
|
|
|
2024-03-08 00:32:44 -08:00
|
|
|
|
var rs = cmd.ReadToEnd();
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (!rs.IsSuccess)
|
|
|
|
|
return string.Empty;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
|
2024-03-16 02:09:27 -07:00
|
|
|
|
var match = REG_AHEAD_BEHIND().Match(rs.StdOut);
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (!match.Success)
|
|
|
|
|
return string.Empty;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
|
2024-03-08 00:32:44 -08:00
|
|
|
|
var ahead = int.Parse(match.Groups[1].Value);
|
|
|
|
|
var behind = int.Parse(match.Groups[2].Value);
|
|
|
|
|
var track = "";
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (ahead > 0)
|
|
|
|
|
track += $"{ahead}↑";
|
|
|
|
|
if (behind > 0)
|
|
|
|
|
track += $" {behind}↓";
|
2021-04-29 05:05:55 -07:00
|
|
|
|
return track.Trim();
|
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
private readonly List<Models.Branch> _branches = new List<Models.Branch>();
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
|
}
|