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

Commit fdac15b

Browse files
Functionality to send commands for and parse git and git lfs version
1 parent 70e238c commit fdac15b

12 files changed

+256
-0
lines changed

src/GitHub.Api/Git/GitClient.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ ITask<string> Unlock(string file, bool force,
7777
IOutputProcessor<string> processor = null);
7878

7979
ITask<List<GitLogEntry>> Log(BaseOutputListProcessor<GitLogEntry> processor = null);
80+
81+
ITask<SoftwareVersion> Version(IOutputProcessor<SoftwareVersion> processor = null);
82+
83+
ITask<SoftwareVersion> LfsVersion(IOutputProcessor<SoftwareVersion> processor = null);
8084
}
8185

8286
class GitClient : IGitClient
@@ -183,6 +187,22 @@ public ITask<List<GitLogEntry>> Log(BaseOutputListProcessor<GitLogEntry> process
183187
.Configure(processManager);
184188
}
185189

190+
public ITask<SoftwareVersion> Version(IOutputProcessor<SoftwareVersion> processor = null)
191+
{
192+
Logger.Trace("Version");
193+
194+
return new GitVersionTask(cancellationToken, processor)
195+
.Configure(processManager);
196+
}
197+
198+
public ITask<SoftwareVersion> LfsVersion(IOutputProcessor<SoftwareVersion> processor = null)
199+
{
200+
Logger.Trace("LfsVersion");
201+
202+
return new GitLfsVersionTask(cancellationToken, processor)
203+
.Configure(processManager);
204+
}
205+
186206
public ITask<string> GetConfig(string key, GitConfigSource configSource, IOutputProcessor<string> processor = null)
187207
{
188208
Logger.Trace("GetConfig");
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Threading;
2+
3+
namespace GitHub.Unity
4+
{
5+
class GitLfsVersionTask : ProcessTask<SoftwareVersion>
6+
{
7+
private const string TaskName = "git lfs version";
8+
9+
public GitLfsVersionTask(CancellationToken token, IOutputProcessor<SoftwareVersion> processor = null)
10+
: base(token, processor ?? new LfsVersionOutputProcessor())
11+
{
12+
Name = TaskName;
13+
}
14+
15+
public override string ProcessArguments { get { return "lfs version"; } }
16+
public override TaskAffinity Affinity { get { return TaskAffinity.Concurrent; } }
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Threading;
2+
3+
namespace GitHub.Unity
4+
{
5+
class GitVersionTask : ProcessTask<SoftwareVersion>
6+
{
7+
private const string TaskName = "git --version";
8+
9+
public GitVersionTask(CancellationToken token, IOutputProcessor<SoftwareVersion> processor = null)
10+
: base(token, processor ?? new VersionOutputProcessor())
11+
{
12+
Name = TaskName;
13+
}
14+
15+
public override string ProcessArguments { get { return "--version"; } }
16+
public override TaskAffinity Affinity { get { return TaskAffinity.Concurrent; } }
17+
}
18+
}

src/GitHub.Api/GitHub.Api.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@
100100
<Compile Include="Application\LoginResult.cs" />
101101
<Compile Include="Application\AppConfiguration.cs" />
102102
<Compile Include="Extensions\ListExtensions.cs" />
103+
<Compile Include="Git\Tasks\GitLfsVersionTask.cs" />
104+
<Compile Include="Git\Tasks\GitVersionTask.cs" />
103105
<Compile Include="Helpers\AssemblyResources.cs" />
104106
<Compile Include="Authentication\IKeychain.cs" />
105107
<Compile Include="Authentication\Keychain.cs" />
@@ -109,6 +111,9 @@
109111
<Compile Include="Helpers\Constants.cs" />
110112
<Compile Include="Cache\IBranchCache.cs" />
111113
<Compile Include="Helpers\Validation.cs" />
114+
<Compile Include="OutputProcessors\LfsVersionOutputProcessor.cs" />
115+
<Compile Include="OutputProcessors\SoftwareVersion.cs" />
116+
<Compile Include="OutputProcessors\VersionOutputProcessor.cs" />
112117
<Compile Include="Platform\DefaultEnvironment.cs" />
113118
<Compile Include="Extensions\EnvironmentExtensions.cs" />
114119
<Compile Include="Extensions\FileEventExtensions.cs" />
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
3+
namespace GitHub.Unity
4+
{
5+
class LfsVersionOutputProcessor : BaseOutputProcessor<SoftwareVersion>
6+
{
7+
public override void LineReceived(string line)
8+
{
9+
if (String.IsNullOrEmpty(line))
10+
return;
11+
12+
var gitVersion = "git-lfs/";
13+
if (line.StartsWith(gitVersion))
14+
{
15+
line = line.Substring(gitVersion.Length, line.IndexOf(" ", StringComparison.InvariantCultureIgnoreCase) - gitVersion.Length);
16+
var strings = line.Split(new[] { "." }, StringSplitOptions.None);
17+
18+
RaiseOnEntry(new SoftwareVersion(strings[0], strings[1], strings[2]));
19+
}
20+
}
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace GitHub.Unity
2+
{
3+
public struct SoftwareVersion
4+
{
5+
public int Major;
6+
public int Minor;
7+
public int Build;
8+
9+
public SoftwareVersion(int major, int minor, int build)
10+
{
11+
Major = major;
12+
Minor = minor;
13+
Build = build;
14+
}
15+
16+
public SoftwareVersion(string major, string minor, string build):
17+
this(int.Parse(major), int.Parse(minor), int.Parse(build))
18+
{
19+
20+
}
21+
}
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace GitHub.Unity
5+
{
6+
class VersionOutputProcessor : BaseOutputProcessor<SoftwareVersion>
7+
{
8+
public override void LineReceived(string line)
9+
{
10+
if (String.IsNullOrEmpty(line))
11+
return;
12+
13+
var gitVersion = "git version ";
14+
if (line.StartsWith(gitVersion))
15+
{
16+
line = line.Substring(gitVersion.Length);
17+
var strings = line.Split(new[] { "." }, StringSplitOptions.None);
18+
19+
RaiseOnEntry(new SoftwareVersion(strings[0], strings[1], strings[2]));
20+
}
21+
}
22+
}
23+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System.Threading.Tasks;
2+
using FluentAssertions;
3+
using GitHub.Unity;
4+
using NUnit.Framework;
5+
6+
namespace IntegrationTests
7+
{
8+
[TestFixture/*, Category("TimeSensitive")*/]
9+
class GitClientTests : BaseGitEnvironmentTest
10+
{
11+
[Test]
12+
public async Task ShouldGetGitVersion()
13+
{
14+
await Initialize(TestRepoMasterCleanSynchronized);
15+
16+
var version = GitClient.Version();
17+
version.Start().Wait();
18+
19+
var versionResult = version.Result;
20+
if (Environment.IsWindows)
21+
{
22+
versionResult.Should().Be($"2.11.1");
23+
}
24+
else
25+
{
26+
versionResult.Should().NotBe(string.Empty);
27+
}
28+
}
29+
30+
[Test]
31+
public async Task ShouldGetGitLfsVersion()
32+
{
33+
await Initialize(TestRepoMasterCleanSynchronized);
34+
35+
var version = GitClient.LfsVersion();
36+
version.Start().Wait();
37+
38+
var versionResult = version.Result;
39+
if (Environment.IsWindows)
40+
{
41+
versionResult.Should().Be("2.2.0");
42+
}
43+
else
44+
{
45+
versionResult.Should().NotBe(string.Empty);
46+
}
47+
}
48+
}
49+
}

src/tests/IntegrationTests/IntegrationTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
<Compile Include="BaseGitEnvironmentTest.cs" />
7979
<Compile Include="BaseGitRepoTest.cs" />
8080
<Compile Include="BaseIntegrationTest.cs" />
81+
<Compile Include="GitClientTests.cs" />
8182
<Compile Include="Git\GitSetupTests.cs" />
8283
<Compile Include="Git\IntegrationTestEnvironment.cs" />
8384
<Compile Include="ProcessManagerExtensions.cs" />
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using FluentAssertions;
4+
using GitHub.Unity;
5+
using NUnit.Framework;
6+
7+
namespace UnitTests
8+
{
9+
[TestFixture]
10+
class LfsVersionOutputProcessorTests : BaseOutputProcessorTests
11+
{
12+
public static IEnumerable<TestCaseData> ShouldParseVersionOutputs_TestCases()
13+
{
14+
TestCaseData testCase;
15+
16+
testCase = new TestCaseData(
17+
$"git-lfs/2.2.0 (GitHub; windows amd64; go 1.8.3; git a99f4b21){Environment.NewLine}",
18+
new SoftwareVersion(2, 2, 0));
19+
20+
testCase.SetName("Windows GitLFS 2.2.0");
21+
yield return testCase;
22+
}
23+
24+
[TestCaseSource(nameof(ShouldParseVersionOutputs_TestCases))]
25+
public void ShouldParseVersionOutputs(string line, SoftwareVersion expected)
26+
{
27+
SoftwareVersion? version = null;
28+
29+
var outputProcessor = new LfsVersionOutputProcessor();
30+
outputProcessor.OnEntry += output => { version = output; };
31+
outputProcessor.LineReceived(line);
32+
33+
version.HasValue.Should().BeTrue();
34+
version.Value.Should().Be(expected);
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)