2024-02-05 23:08:37 -08:00
|
|
|
|
using System;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
namespace SourceGit.Commands
|
|
|
|
|
{
|
2024-07-22 01:41:35 -07:00
|
|
|
|
public class QueryBranches : Command
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
|
private const string PREFIX_LOCAL = "refs/heads/";
|
|
|
|
|
private const string PREFIX_REMOTE = "refs/remotes/";
|
2024-05-25 23:25:21 -07:00
|
|
|
|
private const string PREFIX_DETACHED = "(HEAD detached at";
|
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-07-18 18:29:16 -07:00
|
|
|
|
foreach (var b in _needQueryTrackStatus)
|
|
|
|
|
b.TrackStatus = new QueryTrackStatus(WorkingDirectory, b.Name, b.Upstream).Result();
|
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-06-06 00:31:02 -07:00
|
|
|
|
|
2024-05-25 23:25:21 -07:00
|
|
|
|
if (refName.StartsWith(PREFIX_DETACHED, StringComparison.Ordinal))
|
2024-05-25 11:09:40 -07:00
|
|
|
|
{
|
2024-05-25 23:25:21 -07:00
|
|
|
|
branch.IsHead = true;
|
2024-05-25 11:09:40 -07:00
|
|
|
|
}
|
2024-06-06 00:31:02 -07:00
|
|
|
|
|
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];
|
2021-04-29 05:05:55 -07:00
|
|
|
|
|
2024-07-22 01:01:26 -07:00
|
|
|
|
if (branch.IsLocal && !string.IsNullOrEmpty(parts[4]) && !parts[4].Equals("=", StringComparison.Ordinal))
|
2024-07-18 18:29:16 -07:00
|
|
|
|
_needQueryTrackStatus.Add(branch);
|
|
|
|
|
else
|
|
|
|
|
branch.TrackStatus = new Models.BranchTrackStatus();
|
2021-04-29 05:05:55 -07:00
|
|
|
|
|
2024-07-18 18:29:16 -07:00
|
|
|
|
_branches.Add(branch);
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
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>();
|
2024-07-18 18:29:16 -07:00
|
|
|
|
private List<Models.Branch> _needQueryTrackStatus = new List<Models.Branch>();
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
|
}
|