Skip to content
This repository was archived by the owner on Jun 27, 2019. It is now read-only.

Commit 25d0a38

Browse files
Added more extension methods (with some unit tests)
1 parent 549a169 commit 25d0a38

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace GitTools.Tests
2+
{
3+
using NUnit.Framework;
4+
5+
[TestFixture]
6+
public class StringExtensionsFacts
7+
{
8+
[TestCase("/develop", false)]
9+
[TestCase("/master", false)]
10+
[TestCase("/pr/25", true)]
11+
[TestCase("/pull/25", true)]
12+
[TestCase("/pull-requests/25", true)]
13+
public void TheIsPullRequestMethod(string input, bool expectedValue)
14+
{
15+
var actualValue = input.IsPullRequest();
16+
17+
Assert.AreEqual(expectedValue, actualValue);
18+
}
19+
}
20+
}

src/GitTools.Core.Tests/GitTools.Core.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
<Compile Include="ArgumentParserFacts.cs" />
5555
<Compile Include="ContextFacts.cs" />
5656
<Compile Include="Context\Context.cs" />
57+
<Compile Include="Extensions\StringExtensionsFacts.cs" />
5758
<Compile Include="GlobalInitialization.cs" />
5859
<Compile Include="ModuleInitializer.cs" />
5960
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -70,6 +71,7 @@
7071
<ItemGroup>
7172
<None Include="packages.config" />
7273
</ItemGroup>
74+
<ItemGroup />
7375
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
7476
<Import Project="..\..\lib\Fody.1.28.3\build\Fody.targets" Condition="Exists('..\..\lib\Fody.1.28.3\build\Fody.targets')" />
7577
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">

src/GitTools.Core/Extensions/LibGitExtensions.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,91 @@
22
{
33
using System;
44
using System.Collections.Generic;
5+
using System.IO;
56
using System.Linq;
67
using LibGit2Sharp;
8+
using Logging;
79

810
public static class LibGitExtensions
911
{
12+
private static readonly ILog Log = LogProvider.GetCurrentClassLogger();
13+
14+
public static DateTimeOffset When(this Commit commit)
15+
{
16+
return commit.Committer.When;
17+
}
18+
19+
public static Branch FindBranch(this IRepository repository, string branchName)
20+
{
21+
var exact = repository.Branches.FirstOrDefault(x => x.Name == branchName);
22+
if (exact != null)
23+
{
24+
return exact;
25+
}
26+
27+
return repository.Branches.FirstOrDefault(x => x.Name == "origin/" + branchName);
28+
}
29+
1030
public static bool IsDetachedHead(this Branch branch)
1131
{
1232
return branch.CanonicalName.Equals("(no branch)", StringComparison.OrdinalIgnoreCase);
1333
}
1434

35+
public static string GetRepositoryDirectory(this IRepository repository, bool omitGitPostFix = true)
36+
{
37+
var gitDirectory = repository.Info.Path;
38+
39+
gitDirectory = gitDirectory.TrimEnd('\\');
40+
41+
if (omitGitPostFix && gitDirectory.EndsWith(".git"))
42+
{
43+
gitDirectory = gitDirectory.Substring(0, gitDirectory.Length - ".git".Length);
44+
gitDirectory = gitDirectory.TrimEnd('\\');
45+
}
46+
47+
return gitDirectory;
48+
}
49+
50+
public static void CheckoutFilesIfExist(this IRepository repository, params string[] fileNames)
51+
{
52+
if (fileNames == null || fileNames.Length == 0)
53+
{
54+
return;
55+
}
56+
57+
Log.Info("Checking out files that might be needed later in dynamic repository");
58+
59+
foreach (var fileName in fileNames)
60+
{
61+
try
62+
{
63+
Log.Info(" Trying to check out '{0}'", fileName);
64+
65+
var headBranch = repository.Head;
66+
var tip = headBranch.Tip;
67+
68+
var treeEntry = tip[fileName];
69+
if (treeEntry == null)
70+
{
71+
continue;
72+
}
73+
74+
var fullPath = Path.Combine(repository.GetRepositoryDirectory(), fileName);
75+
using (var stream = ((Blob) treeEntry.Target).GetContentStream())
76+
{
77+
using (var streamReader = new BinaryReader(stream))
78+
{
79+
File.WriteAllBytes(fullPath, streamReader.ReadBytes((int)stream.Length));
80+
}
81+
}
82+
}
83+
catch (Exception ex)
84+
{
85+
Log.Warning(" An error occurred while checking out '{0}': '{1}'", fileName, ex.Message);
86+
}
87+
}
88+
}
89+
1590
public static IEnumerable<Branch> GetBranchesContainingCommit(this IRepository repository, string commitSha)
1691
{
1792
var directBranchHasBeenFound = false;

0 commit comments

Comments
 (0)