-
Notifications
You must be signed in to change notification settings - Fork 892
/
Copy pathGitObject.cs
198 lines (172 loc) · 7.24 KB
/
GitObject.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using LibGit2Sharp.Core;
namespace LibGit2Sharp
{
/// <summary>
/// A GitObject
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public abstract class GitObject : IEquatable<GitObject>, IBelongToARepository
{
internal static IDictionary<Type, ObjectType> TypeToKindMap =
new Dictionary<Type, ObjectType>
{
{ typeof(Commit), ObjectType.Commit },
{ typeof(Tree), ObjectType.Tree },
{ typeof(Blob), ObjectType.Blob },
{ typeof(TagAnnotation), ObjectType.Tag },
};
internal static IDictionary<Type, GitObjectType> TypeToGitKindMap =
new Dictionary<Type, GitObjectType>
{
{ typeof(Commit), GitObjectType.Commit },
{ typeof(Tree), GitObjectType.Tree },
{ typeof(Blob), GitObjectType.Blob },
{ typeof(TagAnnotation), GitObjectType.Tag },
};
private static readonly LambdaEqualityHelper<GitObject> equalityHelper =
new LambdaEqualityHelper<GitObject>(x => x.Id);
private readonly ILazy<bool> lazyIsMissing;
/// <summary>
/// The <see cref="Repository"/> containing the object.
/// </summary>
internal readonly Repository repo;
/// <summary>
/// Needed for mocking purposes.
/// </summary>
protected GitObject()
{ }
/// <summary>
/// Initializes a new instance of the <see cref="GitObject"/> class.
/// </summary>
/// <param name="repo">The <see cref="Repository"/> containing the object.</param>
/// <param name="id">The <see cref="ObjectId"/> it should be identified by.</param>
protected GitObject(Repository repo, ObjectId id)
{
this.repo = repo;
Id = id;
lazyIsMissing = GitObjectLazyGroup.Singleton(repo, id, handle => handle == null, throwIfMissing: false);
}
/// <summary>
/// Gets the id of this object
/// </summary>
public virtual ObjectId Id { get; private set; }
/// <summary>
/// Determine if the object is missing
/// </summary>
/// <remarks>
/// This is common when dealing with partially cloned repositories as blobs or trees could be missing
/// </remarks>
public virtual bool IsMissing => lazyIsMissing.Value;
/// <summary>
/// Gets the 40 character sha1 of this object.
/// </summary>
public virtual string Sha => Id.Sha;
internal static GitObject BuildFrom(Repository repo, ObjectId id, GitObjectType type, string path)
{
switch (type)
{
case GitObjectType.Commit:
return new Commit(repo, id);
case GitObjectType.Tree:
return new Tree(repo, id, path);
case GitObjectType.Tag:
return new TagAnnotation(repo, id);
case GitObjectType.Blob:
return new Blob(repo, id);
default:
throw new LibGit2SharpException("Unexpected type '{0}' for object '{1}'.",
type,
id);
}
}
internal T Peel<T>(bool throwOnError) where T : GitObject
{
GitObjectType kind;
if (!TypeToGitKindMap.TryGetValue(typeof(T), out kind))
{
throw new ArgumentException("Invalid type passed to peel");
}
using (var handle = Proxy.git_object_peel(repo.Handle, Id, kind, throwOnError))
{
if (handle == null)
{
return null;
}
return (T)BuildFrom(this.repo, Proxy.git_object_id(handle), kind, null);
}
}
/// <summary>
/// Peel this object to the specified type
///
/// It will throw if the object cannot be peeled to the type.
/// </summary>
/// <typeparam name="T">The kind of <see cref="GitObject"/> to peel to.</typeparam>
/// <returns>The peeled object</returns>
public virtual T Peel<T>() where T : GitObject
{
return Peel<T>(true);
}
/// <summary>
/// Determines whether the specified <see cref="object"/> is equal to the current <see cref="GitObject"/>.
/// </summary>
/// <param name="obj">The <see cref="object"/> to compare with the current <see cref="GitObject"/>.</param>
/// <returns>True if the specified <see cref="object"/> is equal to the current <see cref="GitObject"/>; otherwise, false.</returns>
public override bool Equals(object obj)
{
return Equals(obj as GitObject);
}
/// <summary>
/// Determines whether the specified <see cref="GitObject"/> is equal to the current <see cref="GitObject"/>.
/// </summary>
/// <param name="other">The <see cref="GitObject"/> to compare with the current <see cref="GitObject"/>.</param>
/// <returns>True if the specified <see cref="GitObject"/> is equal to the current <see cref="GitObject"/>; otherwise, false.</returns>
public bool Equals(GitObject other)
{
return equalityHelper.Equals(this, other);
}
/// <summary>
/// Returns the hash code for this instance.
/// </summary>
/// <returns>A 32-bit signed integer hash code.</returns>
public override int GetHashCode()
{
return equalityHelper.GetHashCode(this);
}
/// <summary>
/// Tests if two <see cref="GitObject"/> are equal.
/// </summary>
/// <param name="left">First <see cref="GitObject"/> to compare.</param>
/// <param name="right">Second <see cref="GitObject"/> to compare.</param>
/// <returns>True if the two objects are equal; false otherwise.</returns>
public static bool operator ==(GitObject left, GitObject right)
{
return Equals(left, right);
}
/// <summary>
/// Tests if two <see cref="GitObject"/> are different.
/// </summary>
/// <param name="left">First <see cref="GitObject"/> to compare.</param>
/// <param name="right">Second <see cref="GitObject"/> to compare.</param>
/// <returns>True if the two objects are different; false otherwise.</returns>
public static bool operator !=(GitObject left, GitObject right)
{
return !Equals(left, right);
}
/// <summary>
/// Returns the <see cref="Id"/>, a <see cref="string"/> representation of the current <see cref="GitObject"/>.
/// </summary>
/// <returns>The <see cref="Id"/> that represents the current <see cref="GitObject"/>.</returns>
public override string ToString()
{
return Id.ToString();
}
private string DebuggerDisplay
{
get { return Id.ToString(7); }
}
IRepository IBelongToARepository.Repository { get { return repo; } }
}
}