/
Repository.cs
279 lines (233 loc) · 9.51 KB
/
Repository.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
using System;
using System.IO;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Compat;
namespace LibGit2Sharp
{
/// <summary>
/// A Repository is the primary interface into a git repository
/// </summary>
public class Repository : IDisposable
{
private readonly BranchCollection branches;
private readonly CommitCollection commits;
private readonly Lazy<Configuration> config;
private readonly RepositorySafeHandle handle;
private readonly Index index;
private readonly ReferenceCollection refs;
private readonly Lazy<RemoteCollection> remotes;
private readonly TagCollection tags;
private readonly Lazy<RepositoryInformation> info;
private readonly bool isBare;
/// <summary>
/// Initializes a new instance of the <see cref = "Repository" /> class.
/// <para>For a standard repository, <paramref name = "path" /> should point to the ".git" folder. For a bare repository, <paramref name = "path" /> should directly point to the repository folder.</para>
/// </summary>
/// <param name = "path">The path to the git repository to open.</param>
public Repository(string path)
{
Ensure.ArgumentNotNullOrEmptyString(path, "path");
int res = NativeMethods.git_repository_open(out handle, PosixPathHelper.ToPosix(path));
Ensure.Success(res);
isBare = NativeMethods.git_repository_is_bare(handle);
if (!isBare)
{
index = new Index(this);
}
commits = new CommitCollection(this);
refs = new ReferenceCollection(this);
branches = new BranchCollection(this);
tags = new TagCollection(this);
info = new Lazy<RepositoryInformation>(() => new RepositoryInformation(this, isBare));
config = new Lazy<Configuration>(() => new Configuration(this));
remotes = new Lazy<RemoteCollection>(() => new RemoteCollection(this));
}
internal RepositorySafeHandle Handle
{
get { return handle; }
}
/// <summary>
/// Shortcut to return the reference to HEAD
/// </summary>
/// <returns></returns>
public Branch Head
{
get
{
Reference reference = Refs["HEAD"];
if (reference is SymbolicReference)
{
return new Branch(this, reference);
}
return new DetachedHead(this, reference);
}
}
/// <summary>
/// Provides access to the configuration settings for this repository.
/// </summary>
public Configuration Config
{
get { return config.Value; }
}
/// <summary>
/// Gets the index.
/// </summary>
public Index Index
{
get { return index; }
}
/// <summary>
/// Lookup and enumerate references in the repository.
/// </summary>
public ReferenceCollection Refs
{
get { return refs; }
}
/// <summary>
/// Lookup and manage remotes in the repository.
/// </summary>
public RemoteCollection Remotes
{
get { return remotes.Value; }
}
/// <summary>
/// Lookup and enumerate commits in the repository.
/// Iterating this collection directly starts walking from the HEAD.
/// </summary>
public IQueryableCommitCollection Commits
{
get { return commits; }
}
/// <summary>
/// Lookup and enumerate branches in the repository.
/// </summary>
public BranchCollection Branches
{
get { return branches; }
}
/// <summary>
/// Lookup and enumerate tags in the repository.
/// </summary>
public TagCollection Tags
{
get { return tags; }
}
/// <summary>
/// Provides high level information about this repository.
/// </summary>
public RepositoryInformation Info
{
get { return info.Value; }
}
#region IDisposable Members
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
protected virtual void Dispose(bool disposing)
{
handle.SafeDispose();
if (index != null)
{
index.Dispose();
}
}
#endregion
/// <summary>
/// Init a repo at the specified <paramref name = "path" />.
/// </summary>
/// <param name = "path">The path to the working folder when initializing a standard ".git" repository. Otherwise, when initializing a bare repository, the path to the expected location of this later.</param>
/// <param name = "isBare">true to initialize a bare repository. False otherwise, to initialize a standard ".git" repository.</param>
/// <returns>Path the git repository.</returns>
public static string Init(string path, bool isBare = false)
{
Ensure.ArgumentNotNullOrEmptyString(path, "path");
RepositorySafeHandle repo;
int res = NativeMethods.git_repository_init(out repo, PosixPathHelper.ToPosix(path), isBare);
Ensure.Success(res);
string normalizedPath = NativeMethods.git_repository_path(repo).MarshallAsString();
repo.SafeDispose();
string nativePath = PosixPathHelper.ToNative(normalizedPath);
return nativePath;
}
/// <summary>
/// Try to lookup an object by its <see cref = "ObjectId" /> and <see cref = "GitObjectType" />. If no matching object is found, null will be returned.
/// </summary>
/// <param name = "id">The id to lookup.</param>
/// <param name = "type">The kind of GitObject being looked up</param>
/// <returns>The <see cref = "GitObject" /> or null if it was not found.</returns>
public GitObject Lookup(ObjectId id, GitObjectType type = GitObjectType.Any)
{
Ensure.ArgumentNotNull(id, "id");
GitOid oid = id.Oid;
IntPtr obj;
int res;
if (id is AbbreviatedObjectId)
{
res = NativeMethods.git_object_lookup_prefix(out obj, handle, ref oid, (uint)((AbbreviatedObjectId)id).Length, type);
}
else
{
res = NativeMethods.git_object_lookup(out obj, handle, ref oid, type);
}
if (res == (int)GitErrorCode.GIT_ENOTFOUND || res == (int)GitErrorCode.GIT_EINVALIDTYPE)
{
return null;
}
Ensure.Success(res);
if (id is AbbreviatedObjectId)
{
id = GitObject.ObjectIdOf(obj);
}
return GitObject.CreateFromPtr(obj, id, this);
}
/// <summary>
/// Try to lookup an object by its sha or a reference canonical name and <see cref = "GitObjectType" />. If no matching object is found, null will be returned.
/// </summary>
/// <param name = "shaOrReferenceName">The sha or reference canonical name to lookup.</param>
/// <param name = "type">The kind of <see cref = "GitObject" /> being looked up</param>
/// <returns>The <see cref = "GitObject" /> or null if it was not found.</returns>
public GitObject Lookup(string shaOrReferenceName, GitObjectType type = GitObjectType.Any)
{
ObjectId id;
if (ObjectId.TryParse(shaOrReferenceName, out id))
{
return Lookup(id, type);
}
Reference reference = Refs[shaOrReferenceName];
if (!IsReferencePeelable(reference))
{
return null;
}
return Lookup(reference.ResolveToDirectReference().TargetIdentifier, type);
}
private static bool IsReferencePeelable(Reference reference)
{
return reference != null && ((reference is DirectReference) || (reference is SymbolicReference && ((SymbolicReference)reference).Target != null));
}
/// <summary>
/// Probe for a git repository.
/// <para>The lookup start from <paramref name = "startingPath" /> and walk upward parent directories if nothing has been found.</para>
/// </summary>
/// <param name = "startingPath">The base path where the lookup starts.</param>
/// <returns>The path to the git repository.</returns>
public static string Discover(string startingPath)
{
var buffer = new byte[NativeMethods.GIT_PATH_MAX];
int result = NativeMethods.git_repository_discover(buffer, buffer.Length, PosixPathHelper.ToPosix(startingPath), false, null);
if ((GitErrorCode)result == GitErrorCode.GIT_ENOTAREPO)
{
return null;
}
Ensure.Success(result);
return PosixPathHelper.ToNative(Utf8Marshaler.Utf8FromBuffer(buffer));
}
}
}