-
Notifications
You must be signed in to change notification settings - Fork 924
Expand file tree
/
Copy pathSubmoduleCollection.cs
More file actions
192 lines (170 loc) · 7.14 KB
/
SubmoduleCollection.cs
File metadata and controls
192 lines (170 loc) · 7.14 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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>
/// Add a new submodule from the given url to the given path. The
/// repository should have already been cloned into the destination
/// path. After this call the submodule and updated .gitmodules
/// file will be added to the index.
/// </summary>
/// <param name="url">Url to use for the submodule</param>
/// <param name="path">Path of the submodule</param>
/// <param name="useGitLink">Non-zero to use git link</param>
public virtual void Add(string url, string path, int useGitLink)
{
SubmoduleHandle handle = Proxy.git_submodule_add_setup(repo.Handle, url, path, useGitLink);
Proxy.git_submodule_add_finalize(handle);
handle.Free();
}
/// <summary>
/// Initialize specified submodule.
/// <para>
/// Existing entries in the config file for this submodule are not be
/// modified unless <paramref name="overwrite"/> is true.
/// </para>
/// </summary>
/// <param name="name">The name of the submodule to update.</param>
/// <param name="overwrite">Overwrite existing entries.</param>
public virtual void Init(string name, bool overwrite)
{
using (var handle = Proxy.git_submodule_lookup(repo.Handle, name))
{
if (handle == null)
{
throw new NotFoundException("Submodule lookup failed for '{0}'.",
name);
}
Proxy.git_submodule_init(handle, overwrite);
}
}
/// <summary>
/// Update specified submodule.
/// <para>
/// This will:
/// 1) Optionally initialize the if it not already initialzed,
/// 2) clone the sub repository if it has not already been cloned, and
/// 3) checkout the commit ID for the submodule in the sub repository.
/// </para>
/// </summary>
/// <param name="name">The name of the submodule to update.</param>
/// <param name="options">Options controlling submodule udpate behavior and callbacks.</param>
public virtual void Update(string name, SubmoduleUpdateOptions options)
{
options = options ?? new SubmoduleUpdateOptions();
using (var handle = Proxy.git_submodule_lookup(repo.Handle, name))
{
if (handle == null)
{
throw new NotFoundException("Submodule lookup failed for '{0}'.",
name);
}
using (GitCheckoutOptsWrapper checkoutOptionsWrapper = new GitCheckoutOptsWrapper(options))
{
var gitCheckoutOptions = checkoutOptionsWrapper.Options;
var remoteCallbacks = new RemoteCallbacks(options);
var gitRemoteCallbacks = remoteCallbacks.GenerateCallbacks();
var gitSubmoduleUpdateOpts = new GitSubmoduleUpdateOptions
{
Version = 1,
CheckoutOptions = gitCheckoutOptions,
FetchOptions = new GitFetchOptions { ProxyOptions = new GitProxyOptions { Version = 1 }, RemoteCallbacks = gitRemoteCallbacks },
CloneCheckoutStrategy = CheckoutStrategy.GIT_CHECKOUT_SAFE
};
Proxy.git_submodule_update(handle, options.Init, ref gitSubmoduleUpdateOpts);
}
}
}
/// <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<SubmoduleHandle, 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("Submodule lookup failed for '{0}'.", name);
}
return default(T);
}
}
private string DebuggerDisplay
{
get
{
return string.Format(CultureInfo.InvariantCulture, "Count = {0}", this.Count());
}
}
}
}