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

Commit 9d5b279

Browse files
Using a custom comparer to order entries
1 parent 858c0c2 commit 9d5b279

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-2
lines changed

src/GitHub.Api/OutputProcessors/StatusOutputProcessor.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ private void ReturnStatus()
191191
return;
192192

193193
gitStatus.Entries = gitStatus.Entries
194-
.OrderBy(entry => entry.Path)
194+
.OrderBy(entry => entry.Path, StatusOutputPathComparer.Instance)
195195
.ToList();
196196

197197
RaiseOnEntry(gitStatus);
@@ -214,5 +214,41 @@ private void HandleUnexpected(string line)
214214
{
215215
Logger.Error("Unexpected Input:\"{0}\"", line);
216216
}
217+
218+
class StatusOutputPathComparer : IComparer<string>
219+
{
220+
internal static StatusOutputPathComparer Instance => new StatusOutputPathComparer();
221+
222+
public int Compare(string x, string y)
223+
{
224+
Guard.ArgumentNotNull(x, nameof(x));
225+
Guard.ArgumentNotNull(y, nameof(y));
226+
227+
var metaString = ".meta";
228+
var xIsMeta = x.EndsWith(metaString);
229+
var yIsMeta = y.EndsWith(metaString);
230+
231+
if (xIsMeta || yIsMeta)
232+
{
233+
var compareX = !xIsMeta ? x : x.Substring(0, x.Length - 5);
234+
var compareY = !yIsMeta ? y : y.Substring(0, y.Length - 5);
235+
236+
var comparisonResult = StringComparer.InvariantCultureIgnoreCase.Compare(compareX, compareY);
237+
if (comparisonResult != 0)
238+
{
239+
return comparisonResult;
240+
}
241+
242+
if (xIsMeta)
243+
{
244+
return 1;
245+
}
246+
247+
return -1;
248+
}
249+
250+
return StringComparer.InvariantCultureIgnoreCase.Compare(x, y);
251+
}
252+
}
217253
}
218254
}

src/tests/UnitTests/IO/StatusOutputProcessorTests.cs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,64 @@ public void ShouldParseCleanWorkingTreeTracked()
245245
});
246246
}
247247

248+
[Test]
249+
public void ShouldSortOutputCorrectly()
250+
{
251+
var output = new[]
252+
{
253+
"## master",
254+
"?? GitHub.Unity.dll",
255+
"?? GitHub.Unity.dll.mdb",
256+
"?? GitHub.Unity.dll.mdb.meta",
257+
"?? GitHub.Unity.dll.meta",
258+
null
259+
};
260+
261+
AssertProcessOutput(output, new GitStatus
262+
{
263+
LocalBranch = "master",
264+
Entries = new List<GitStatusEntry>
265+
{
266+
new GitStatusEntry(@"GitHub.Unity.dll", TestRootPath + @"\GitHub.Unity.dll", null, GitFileStatus.Untracked),
267+
new GitStatusEntry(@"GitHub.Unity.dll.meta", TestRootPath + @"\GitHub.Unity.dll.meta", null, GitFileStatus.Untracked),
268+
new GitStatusEntry(@"GitHub.Unity.dll.mdb", TestRootPath + @"\GitHub.Unity.dll.mdb", null, GitFileStatus.Untracked),
269+
new GitStatusEntry(@"GitHub.Unity.dll.mdb.meta", TestRootPath + @"\GitHub.Unity.dll.mdb.meta", null, GitFileStatus.Untracked),
270+
}
271+
});
272+
}
273+
274+
[Test]
275+
public void ShouldSortOutputCorrectly2()
276+
{
277+
var output = new[]
278+
{
279+
"## master",
280+
"?? Assets/Assets.Test.dll",
281+
"?? Assets/Assets.Test.dll.meta",
282+
"?? Plugins/GitHub.Unity.dll",
283+
"?? Plugins/GitHub.Unity.dll.mdb",
284+
"?? Plugins/GitHub.Unity.dll.mdb.meta",
285+
"?? Plugins/GitHub.Unity.dll.meta",
286+
"?? blah.txt",
287+
null
288+
};
289+
290+
AssertProcessOutput(output, new GitStatus
291+
{
292+
LocalBranch = "master",
293+
Entries = new List<GitStatusEntry>
294+
{
295+
new GitStatusEntry(@"Assets/Assets.Test.dll", TestRootPath + @"\Assets/Assets.Test.dll", null, GitFileStatus.Untracked),
296+
new GitStatusEntry(@"Assets/Assets.Test.dll.meta", TestRootPath + @"\Assets/Assets.Test.dll.meta", null, GitFileStatus.Untracked),
297+
new GitStatusEntry(@"blah.txt", TestRootPath + @"\blah.txt", null, GitFileStatus.Untracked),
298+
new GitStatusEntry(@"Plugins/GitHub.Unity.dll", TestRootPath + @"\Plugins/GitHub.Unity.dll", null, GitFileStatus.Untracked),
299+
new GitStatusEntry(@"Plugins/GitHub.Unity.dll.meta", TestRootPath + @"\Plugins/GitHub.Unity.dll.meta", null, GitFileStatus.Untracked),
300+
new GitStatusEntry(@"Plugins/GitHub.Unity.dll.mdb", TestRootPath + @"\Plugins/GitHub.Unity.dll.mdb", null, GitFileStatus.Untracked),
301+
new GitStatusEntry(@"Plugins/GitHub.Unity.dll.mdb.meta", TestRootPath + @"\Plugins/GitHub.Unity.dll.mdb.meta", null, GitFileStatus.Untracked),
302+
}
303+
});
304+
}
305+
248306
private void AssertProcessOutput(IEnumerable<string> lines, GitStatus expected)
249307
{
250308
var gitObjectFactory = SubstituteFactory.CreateGitObjectFactory(TestRootPath);
@@ -262,4 +320,4 @@ private void AssertProcessOutput(IEnumerable<string> lines, GitStatus expected)
262320
result.Value.AssertEqual(expected);
263321
}
264322
}
265-
}
323+
}

0 commit comments

Comments
 (0)