Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit a82cb50

Browse files
Adding functionality to query specifically for Ahead Behind status
1 parent e0a9c9c commit a82cb50

File tree

8 files changed

+156
-7
lines changed

8 files changed

+156
-7
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace GitHub.Unity
2+
{
3+
public struct GitAheadBehindStatus
4+
{
5+
public int Ahead;
6+
public int Behind;
7+
}
8+
}

src/GitHub.Api/Git/GitClient.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ public interface IGitClient
1515

1616
ITask LfsInstall(IOutputProcessor<string> processor = null);
1717

18+
ITask<GitAheadBehindStatus> AheadBehindStatus(string gitRef, string otherRef,
19+
IOutputProcessor<GitAheadBehindStatus> processor = null);
20+
1821
ITask<GitStatus> Status(IOutputProcessor<GitStatus> processor = null);
1922

2023
ITask<string> GetConfig(string key, GitConfigSource configSource,
@@ -219,6 +222,14 @@ public ITask<GitStatus> Status(IOutputProcessor<GitStatus> processor = null)
219222
.Configure(processManager);
220223
}
221224

225+
public ITask<GitAheadBehindStatus> AheadBehindStatus(string gitRef, string otherRef, IOutputProcessor<GitAheadBehindStatus> processor = null)
226+
{
227+
Logger.Trace("AheadBehindStatus");
228+
229+
return new GitAheadBehindStatusTask(gitRef, otherRef, cancellationToken, processor)
230+
.Configure(processManager);
231+
}
232+
222233
public ITask<List<GitLogEntry>> Log(BaseOutputListProcessor<GitLogEntry> processor = null)
223234
{
224235
Logger.Trace("Log");

src/GitHub.Api/Git/Repository.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public void Initialize(IRepositoryManager initRepositoryManager)
4949
repositoryManager = initRepositoryManager;
5050
repositoryManager.CurrentBranchUpdated += RepositoryManagerOnCurrentBranchUpdated;
5151
repositoryManager.GitStatusUpdated += RepositoryManagerOnGitStatusUpdated;
52+
repositoryManager.GitAheadBehindStatusUpdated += RepositoryManagerOnGitAheadBehindStatusUpdated;
5253
repositoryManager.GitLogUpdated += RepositoryManagerOnGitLogUpdated;
5354
repositoryManager.LocalBranchesUpdated += RepositoryManagerOnLocalBranchesUpdated;
5455
repositoryManager.RemoteBranchesUpdated += RepositoryManagerOnRemoteBranchesUpdated;
@@ -378,6 +379,15 @@ private void RepositoryManagerOnGitStatusUpdated(GitStatus gitStatus)
378379
}) { Affinity = TaskAffinity.UI }.Start();
379380
}
380381

382+
private void RepositoryManagerOnGitAheadBehindStatusUpdated(GitAheadBehindStatus aheadBehindStatus)
383+
{
384+
new ActionTask(CancellationToken.None, () => {
385+
CurrentAhead = aheadBehindStatus.Ahead;
386+
CurrentBehind = aheadBehindStatus.Behind;
387+
})
388+
{ Affinity = TaskAffinity.UI }.Start();
389+
}
390+
381391
private void RepositoryManagerOnGitLogUpdated(List<GitLogEntry> gitLogEntries)
382392
{
383393
new ActionTask(CancellationToken.None, () => {
@@ -392,6 +402,12 @@ private void RepositoryManagerOnRemoteBranchesUpdated(Dictionary<string, ConfigR
392402
cacheContainer.BranchCache.SetRemotes(remotes, branches);
393403
Remotes = ConfigRemotes.Values.Select(GetGitRemote).ToArray();
394404
RemoteBranches = RemoteConfigBranches.Values.SelectMany(x => x.Values).Select(GetRemoteGitBranch).ToArray();
405+
406+
var currentBranch = CurrentBranch;
407+
if(!string.IsNullOrEmpty(currentBranch?.Tracking))
408+
{
409+
repositoryManager.UpdateGitAheadBehindStatus(currentBranch.Value.name, currentBranch.Value.tracking);
410+
}
395411
}) { Affinity = TaskAffinity.UI }.Start();
396412
}
397413

src/GitHub.Api/Git/RepositoryManager.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ public interface IRepositoryManager : IDisposable
3535
ITask UnlockFile(string file, bool force);
3636
void UpdateGitLog();
3737
void UpdateGitStatus();
38+
void UpdateGitAheadBehindStatus(string gitRef, string otherRef);
3839
void UpdateLocks();
3940
int WaitForEvents();
4041

4142
IGitConfig Config { get; }
4243
IGitClient GitClient { get; }
4344
bool IsBusy { get; }
45+
event Action<GitAheadBehindStatus> GitAheadBehindStatusUpdated;
4446
}
4547

4648
interface IRepositoryPathConfiguration
@@ -101,6 +103,7 @@ class RepositoryManager : IRepositoryManager
101103
public event Action<ConfigBranch?, ConfigRemote?> CurrentBranchUpdated;
102104
public event Action<bool> IsBusyChanged;
103105
public event Action<GitStatus> GitStatusUpdated;
106+
public event Action<GitAheadBehindStatus> GitAheadBehindStatusUpdated;
104107
public event Action<List<GitLock>> GitLocksUpdated;
105108
public event Action<List<GitLogEntry>> GitLogUpdated;
106109
public event Action<Dictionary<string, ConfigBranch>> LocalBranchesUpdated;
@@ -293,6 +296,19 @@ public void UpdateGitStatus()
293296
}).Start();
294297
}
295298

299+
public void UpdateGitAheadBehindStatus(string gitRef, string otherRef)
300+
{
301+
var task = GitClient.AheadBehindStatus(gitRef, otherRef);
302+
task = HookupHandlers(task, true, false);
303+
task.Then((success, status) =>
304+
{
305+
if (success)
306+
{
307+
GitAheadBehindStatusUpdated?.Invoke(status);
308+
}
309+
}).Start();
310+
}
311+
296312
public void UpdateLocks()
297313
{
298314
var task = GitClient.ListLocks(false);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Threading;
2+
3+
namespace GitHub.Unity
4+
{
5+
class GitAheadBehindStatusTask : ProcessTask<GitAheadBehindStatus>
6+
{
7+
private const string TaskName = "git rev-list";
8+
private readonly string arguments;
9+
10+
public GitAheadBehindStatusTask(string gitRef, string otherRef,
11+
CancellationToken token, IOutputProcessor<GitAheadBehindStatus> processor = null)
12+
: base(token, processor ?? new GitAheadBehindStatusOutputProcessor())
13+
{
14+
Name = TaskName;
15+
arguments = $"rev-list --left-right --count {gitRef}...{otherRef}";
16+
}
17+
18+
public override string ProcessArguments => arguments;
19+
public override TaskAffinity Affinity { get { return TaskAffinity.Exclusive; } }
20+
}
21+
}

src/GitHub.Api/GitHub.Api.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@
104104
<Compile Include="Cache\CacheInterfaces.cs" />
105105
<Compile Include="Extensions\ListExtensions.cs" />
106106
<Compile Include="Git\ManagedCacheExtensions.cs" />
107+
<Compile Include="Git\GitAheadBehindStatus.cs" />
108+
<Compile Include="Git\Tasks\GitAheadBehindStatusTask.cs" />
107109
<Compile Include="Git\Tasks\GitLfsVersionTask.cs" />
108110
<Compile Include="Git\Tasks\GitVersionTask.cs" />
109111
<Compile Include="Git\ValidateGitInstallResult.cs" />
@@ -115,6 +117,7 @@
115117
<Compile Include="Application\ApplicationManagerBase.cs" />
116118
<Compile Include="Helpers\Constants.cs" />
117119
<Compile Include="Helpers\Validation.cs" />
120+
<Compile Include="OutputProcessors\GitAheadBehindStatusOutputProcessor.cs" />
118121
<Compile Include="OutputProcessors\LfsVersionOutputProcessor.cs" />
119122
<Compile Include="OutputProcessors\VersionOutputProcessor.cs" />
120123
<Compile Include="Helpers\TaskHelpers.cs" />
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace GitHub.Unity
2+
{
3+
class GitAheadBehindStatusOutputProcessor : BaseOutputProcessor<GitAheadBehindStatus>
4+
{
5+
public override void LineReceived(string line)
6+
{
7+
if (line == null)
8+
{
9+
return;
10+
}
11+
12+
var proc = new LineParser(line);
13+
14+
var ahead = int.Parse(proc.ReadUntilWhitespace());
15+
var behind = int.Parse(proc.ReadToEnd());
16+
17+
RaiseOnEntry(new GitAheadBehindStatus { Ahead = ahead, Behind = behind });
18+
}
19+
}
20+
}

src/UnityExtension/Assets/Editor/GitHub.Unity/ApplicationCache.cs

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ sealed class GitStatusCache : ManagedCacheBase<GitStatusCache>, IGitStatusCache
838838
[SerializeField] private string initializedAtString = DateTimeOffset.MinValue.ToString();
839839
[SerializeField] private int ahead;
840840
[SerializeField] private int behind;
841-
[SerializeField] private List<GitStatusEntry> entries;
841+
[SerializeField] private List<GitStatusEntry> entries = new List<GitStatusEntry>();
842842

843843
public GitStatusCache() : base(true)
844844
{ }
@@ -868,20 +868,74 @@ public override TimeSpan DataTimeout
868868

869869
public int Ahead
870870
{
871-
get { return ahead; }
872-
set { ahead = value; }
871+
get
872+
{
873+
ValidateData();
874+
return ahead;
875+
}
876+
set
877+
{
878+
var now = DateTimeOffset.Now;
879+
var isUpdated = false;
880+
881+
Logger.Trace("Updating: {0} ahead:{1}", now, value);
882+
883+
if (ahead != value)
884+
{
885+
ahead = value;
886+
isUpdated = true;
887+
}
888+
889+
SaveData(now, isUpdated);
890+
}
873891
}
874892

875893
public int Behind
876894
{
877-
get { return behind; }
878-
set { behind = value; }
895+
get
896+
{
897+
ValidateData();
898+
return behind;
899+
}
900+
set
901+
{
902+
var now = DateTimeOffset.Now;
903+
var isUpdated = false;
904+
905+
Logger.Trace("Updating: {0} behind:{1}", now, value);
906+
907+
if (behind != value)
908+
{
909+
behind = value;
910+
isUpdated = true;
911+
}
912+
913+
SaveData(now, isUpdated);
914+
}
879915
}
880916

881917
public List<GitStatusEntry> Entries
882918
{
883-
get { return entries; }
884-
set { entries = value; }
919+
get
920+
{
921+
ValidateData();
922+
return entries;
923+
}
924+
set
925+
{
926+
var now = DateTimeOffset.Now;
927+
var isUpdated = false;
928+
929+
Logger.Trace("Updating: {0} entries:{1}", now, value.Count);
930+
931+
if (!entries.SequenceEqual(value))
932+
{
933+
entries = value;
934+
isUpdated = true;
935+
}
936+
937+
SaveData(now, isUpdated);
938+
}
885939
}
886940
}
887941

0 commit comments

Comments
 (0)