forked from libgit2/libgit2sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubmoduleCollection.cs
113 lines (100 loc) · 3.78 KB
/
SubmoduleCollection.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Handles;
namespace LibGit2Sharp
{
/// <summary>
/// The collection of submodules in a <see cref="Repository"/>
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class SubmoduleCollection : IEnumerable<Submodule>
{
internal readonly Repository repo;
/// <summary>
/// Needed for mocking purposes.
/// </summary>
protected SubmoduleCollection()
{ }
/// <summary>
/// Initializes a new instance of the <see cref="LibGit2Sharp.SubmoduleCollection"/> class.
/// </summary>
/// <param name="repo">The repo.</param>
internal SubmoduleCollection(Repository repo)
{
this.repo = repo;
}
/// <summary>
/// Gets the <see cref="LibGit2Sharp.Submodule"/> with the specified name.
/// </summary>
public virtual Submodule this[string name]
{
get
{
Ensure.ArgumentNotNullOrEmptyString(name, "name");
return Lookup(name, handle =>
new Submodule(repo, name,
Proxy.git_submodule_path(handle),
Proxy.git_submodule_url(handle)));
}
}
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>An <see cref="IEnumerator{T}"/> object that can be used to iterate through the collection.</returns>
public virtual IEnumerator<Submodule> GetEnumerator()
{
return Proxy.git_submodule_foreach(repo.Handle, (h, n) => LaxUtf8Marshaler.FromNative(n))
.Select(n => this[n])
.GetEnumerator();
}
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>An <see cref="IEnumerator"/> object that can be used to iterate through the collection.</returns>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
internal bool TryStage(string relativePath, bool writeIndex)
{
return Lookup(relativePath, handle =>
{
if (handle == null)
return false;
Proxy.git_submodule_add_to_index(handle, writeIndex);
return true;
});
}
internal T Lookup<T>(string name, Func<SubmoduleSafeHandle, T> selector, bool throwIfNotFound = false)
{
using (var handle = Proxy.git_submodule_lookup(repo.Handle, name))
{
if (handle != null)
{
Proxy.git_submodule_reload(handle);
return selector(handle);
}
if (throwIfNotFound)
{
throw new LibGit2SharpException(string.Format(
CultureInfo.InvariantCulture,
"Submodule lookup failed for '{0}'.", name));
}
return default(T);
}
}
private string DebuggerDisplay
{
get
{
return string.Format(CultureInfo.InvariantCulture,
"Count = {0}", this.Count());
}
}
}
}