Skip to content

Commit a289bbf

Browse files
committed
[WIP] Introduce CommitFilter.Parse()
1 parent 9c45030 commit a289bbf

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using LibGit2Sharp.Tests.TestHelpers;
3+
using Xunit;
4+
using Xunit.Extensions;
5+
6+
namespace LibGit2Sharp.Tests
7+
{
8+
public class CommitFilterFixture : BaseFixture
9+
{
10+
[Theory]
11+
[InlineData("HEAD^", "HEAD^", null)]
12+
[InlineData("HEAD^..HEAD", "HEAD", "HEAD^")]
13+
[InlineData("HEAD^..", null, "HEAD^")]
14+
public void tada(string expression, object includeReachableFrom, object excludeReachableFrom)
15+
{
16+
CommitFilter cf = CommitFilter.Parse(expression);
17+
18+
Assert.Equal(includeReachableFrom, cf.IncludeReachableFrom);
19+
Assert.Equal(excludeReachableFrom, cf.ExcludeReachableFrom);
20+
}
21+
22+
[Fact]
23+
public void throws()
24+
{
25+
Assert.Throws<ArgumentException>(() => CommitFilter.Parse(""));
26+
Assert.Throws<ArgumentNullException>(() => CommitFilter.Parse(default(string)));
27+
}
28+
}
29+
}

LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
<Compile Include="ArchiveTarFixture.cs" />
6161
<Compile Include="CheckoutFixture.cs" />
6262
<Compile Include="CherryPickFixture.cs" />
63+
<Compile Include="CommitFilterFixture.cs" />
6364
<Compile Include="DescribeFixture.cs" />
6465
<Compile Include="FileHistoryFixture.cs" />
6566
<Compile Include="FilterFixture.cs" />

LibGit2Sharp/CommitFilter.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.Diagnostics;
45
using System.Linq;
6+
using LibGit2Sharp.Core;
57

68
namespace LibGit2Sharp
79
{
@@ -121,5 +123,43 @@ private static IList<object> ToList(object obj)
121123
list.AddRange(((IEnumerable)obj).Cast<object>());
122124
return list;
123125
}
126+
127+
public static CommitFilter Parse(string expression)
128+
{
129+
Ensure.ArgumentNotNullOrEmptyString(expression, "expression");
130+
131+
if (expression.Contains(" "))
132+
{
133+
throw new NotSupportedException();
134+
}
135+
136+
if (expression.Contains("..."))
137+
{
138+
return MergeRangeParse(expression);
139+
}
140+
141+
if (expression.Contains(".."))
142+
{
143+
return RangeParse(expression);
144+
}
145+
146+
return new CommitFilter { IncludeReachableFrom = expression };
147+
}
148+
149+
private static CommitFilter MergeRangeParse(string expression)
150+
{
151+
throw new NotImplementedException();
152+
}
153+
154+
private static CommitFilter RangeParse(string expression)
155+
{
156+
var parts = expression.Split(new[] { ".." }, StringSplitOptions.None);
157+
158+
Debug.Assert(parts.Length == 2);
159+
160+
Func<string, string> nullify = (s) => s == string.Empty ? null : s;
161+
162+
return new CommitFilter { IncludeReachableFrom = nullify(parts[1]), ExcludeReachableFrom = nullify(parts[0]) };
163+
}
124164
}
125165
}

0 commit comments

Comments
 (0)