Skip to content

Commit 92a812b

Browse files
committed
Add support for simple Refspecs
1 parent 99292f7 commit 92a812b

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
<Compile Include="TreeDefinitionFixture.cs" />
8989
<Compile Include="TreeFixture.cs" />
9090
<Compile Include="TupleFixture.cs" />
91+
<Compile Include="RefspecFixture.cs" />
9192
</ItemGroup>
9293
<ItemGroup>
9394
<ProjectReference Include="..\LibGit2Sharp\LibGit2Sharp.csproj">

LibGit2Sharp.Tests/RefspecFixture.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Linq;
3+
using LibGit2Sharp.Tests.TestHelpers;
4+
using Xunit;
5+
using Xunit.Extensions;
6+
7+
namespace LibGit2Sharp.Tests
8+
{
9+
public class RefspecFixture
10+
{
11+
[Theory]
12+
[InlineData("refs/heads/master:refs/remotes/origin/master", false, "refs/heads/master", "refs/remotes/origin/master")]
13+
[InlineData("+refs/heads/master:refs/remotes/origin/master", true, "refs/heads/master", "refs/remotes/origin/master")]
14+
public void CanParseRefspecs(string spec, bool force, string left, string right)
15+
{
16+
var refspec = new Refspec(spec);
17+
Assert.True(refspec.Src.Equals(left));
18+
Assert.True(refspec.Dst.Equals(right));
19+
Assert.True(refspec.Force == force);
20+
}
21+
22+
23+
[Fact]
24+
public void CanMatchNonWildcards()
25+
{
26+
var refspec = new Refspec("refs/heads/master:refs/heads/master");
27+
Assert.True(refspec.Matches("refs/heads/master"));
28+
}
29+
}
30+
}
31+

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@
165165
<Compile Include="Pkt.cs" />
166166
<Compile Include="ITransport.cs" />
167167
<Compile Include="GitTransport.cs" />
168+
<Compile Include="Refspec.cs" />
168169
</ItemGroup>
169170
<ItemGroup>
170171
<CodeAnalysisDictionary Include="CustomDictionary.xml" />

LibGit2Sharp/Refspec.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
namespace LibGit2Sharp
3+
{
4+
public class Refspec
5+
{
6+
public string Src { get; private set; }
7+
public string Dst { get; private set; }
8+
public bool Force { get; private set; }
9+
bool wildcard;
10+
11+
public Refspec(string str)
12+
{
13+
if (str[0] == '+') {
14+
Force = true;
15+
str = str.Remove(0, 1);
16+
} else {
17+
Force = false;
18+
}
19+
20+
var parts = str.Split(':');
21+
if (parts.Length != 2)
22+
throw new ArgumentException("Wrong refspec format");
23+
24+
Src = parts[0];
25+
Dst = parts[1];
26+
wildcard = Src.EndsWith("*");
27+
if (wildcard && !Dst.EndsWith("*"))
28+
throw new ArgumentException("Left side has wildcard, but right one doesn't");
29+
if (!wildcard && Dst.EndsWith("*"))
30+
throw new ArgumentException("Right side has wildcard, but left side doesn't");
31+
}
32+
33+
public bool Matches(string str)
34+
{
35+
/* If it's not a wildcard, then it's a straight string match */
36+
if (!wildcard)
37+
return Src.Equals(str);
38+
39+
return false;
40+
}
41+
}
42+
}
43+

0 commit comments

Comments
 (0)