diff --git a/src/Commands/QueryBranches.cs b/src/Commands/QueryBranches.cs index 8c2db9a2..0ddfdea4 100644 --- a/src/Commands/QueryBranches.cs +++ b/src/Commands/QueryBranches.cs @@ -6,13 +6,12 @@ namespace SourceGit.Commands { public class QueryBranches : Command { private static readonly string PREFIX_LOCAL = "refs/heads/"; private static readonly string PREFIX_REMOTE = "refs/remotes/"; - private static readonly Regex REG_AHEAD = new Regex(@"ahead (\d+)"); - private static readonly Regex REG_BEHIND = new Regex(@"behind (\d+)"); + private static readonly Regex REG_AHEAD_BEHIND = new Regex(@"^(\d+)\s(\d+)$"); public QueryBranches(string repo) { WorkingDirectory = repo; Context = repo; - Args = "branch -l --all -v --format=\"%(refname)$%(objectname)$%(HEAD)$%(upstream)$%(upstream:track)\""; + Args = "branch -l --all -v --format=\"%(refname)$%(objectname)$%(HEAD)$%(upstream)$(upstream:trackshort)\""; } public List Result() { @@ -48,26 +47,31 @@ namespace SourceGit.Commands { branch.Head = parts[1]; branch.IsCurrent = parts[2] == "*"; branch.Upstream = parts[3]; - branch.UpstreamTrackStatus = ParseTrackStatus(parts[4]); + + if (!string.IsNullOrEmpty(parts[4]) && !parts[4].Equals("=")) { + branch.UpstreamTrackStatus = ParseTrackStatus(branch.FullName, branch.Upstream); + } _branches.Add(branch); } - private string ParseTrackStatus(string data) { - if (string.IsNullOrEmpty(data)) return string.Empty; + private string ParseTrackStatus(string local, string upstream) { + var cmd = new Command(); + cmd.WorkingDirectory = WorkingDirectory; + cmd.Context = Context; + cmd.Args = $"rev-list --left-right --count {local}...{upstream}"; - string track = string.Empty; + var rs = cmd.ReadToEnd(); + if (!rs.IsSuccess) return string.Empty; - var ahead = REG_AHEAD.Match(data); - if (ahead.Success) { - track += ahead.Groups[1].Value + "↑ "; - } - - var behind = REG_BEHIND.Match(data); - if (behind.Success) { - track += behind.Groups[1].Value + "↓"; - } + var match = REG_AHEAD_BEHIND.Match(rs.StdOut); + if (!match.Success) return string.Empty; + var ahead = int.Parse(match.Groups[1].Value); + var behind = int.Parse(match.Groups[2].Value); + var track = ""; + if (ahead > 0) track += $"{ahead}↑"; + if (behind > 0) track += $" {behind}↓"; return track.Trim(); }