forked from libgit2/libgit2sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlameHunkCollection.cs
95 lines (87 loc) · 3.15 KB
/
BlameHunkCollection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Handles;
namespace LibGit2Sharp
{
/// <summary>
/// The result of a blame operation.
/// </summary>
public class BlameHunkCollection : IEnumerable<BlameHunk>
{
private readonly IRepository repo;
private readonly List<BlameHunk> hunks = new List<BlameHunk>();
/// <summary>
/// For easy mocking
/// </summary>
protected BlameHunkCollection() { }
internal BlameHunkCollection(Repository repo, RepositorySafeHandle repoHandle, string path, BlameOptions options)
{
this.repo = repo;
var rawopts = new GitBlameOptions
{
version = 1,
flags = options.Strategy.ToGitBlameOptionFlags(),
MinLine = (uint)options.MinLine,
MaxLine = (uint)options.MaxLine,
};
if (options.StartingAt != null)
{
rawopts.NewestCommit = repo.Committish(options.StartingAt).Oid;
}
if (options.StoppingAt != null)
{
rawopts.OldestCommit = repo.Committish(options.StoppingAt).Oid;
}
using (var blameHandle = Proxy.git_blame_file(repoHandle, path, rawopts))
{
var numHunks = NativeMethods.git_blame_get_hunk_count(blameHandle);
for (uint i = 0; i < numHunks; ++i)
{
var rawHunk = Proxy.git_blame_get_hunk_byindex(blameHandle, i);
hunks.Add(new BlameHunk(this.repo, rawHunk));
}
}
}
/// <summary>
/// Access blame hunks by index.
/// </summary>
/// <param name="idx">The index of the hunk to retrieve</param>
/// <returns>The <see cref="BlameHunk"/> at the given index.</returns>
public virtual BlameHunk this[int idx]
{
get { return hunks[idx]; }
}
/// <summary>
/// Access blame hunks by the file line.
/// </summary>
/// <param name="line">Line number to search for</param>
/// <returns>The <see cref="BlameHunk"/> that contains the specified file line.</returns>
public virtual BlameHunk HunkForLine(int line)
{
var hunk = hunks.FirstOrDefault(x => x.ContainsLine(line));
if (hunk != null)
{
return hunk;
}
throw new ArgumentOutOfRangeException("line", "No hunk for that line");
}
/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
/// <returns>
/// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
/// </returns>
/// <filterpriority>2</filterpriority>
public virtual IEnumerator<BlameHunk> GetEnumerator()
{
return hunks.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}