2 * Copyright (C) 2009, Henon <meinrad.recheis@gmail.com>
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 using System
.Collections
.Generic
;
43 using ObjectId
= GitSharp
.Core
.ObjectId
;
44 using CoreRef
= GitSharp
.Core
.Ref
;
45 using CoreCommit
= GitSharp
.Core
.Commit
;
46 using CoreTree
= GitSharp
.Core
.Tree
;
47 using CoreTag
= GitSharp
.Core
.Tag
;
48 using System
.Diagnostics
;
53 /// AbstractObject is the base class for the classes Blob, Commit, Tag and Tree. It proviedes test methods
54 /// to identify its specialized type (i.e. IsBlob, IsCommit, etc). AbstractObject also defines comparison operators so you can
55 /// safely compare git objects by using the operators == or != which internally efficiently compare the objects hashes.
57 public abstract class AbstractObject
59 protected Repository _repo
;
60 internal ObjectId _id
; // <--- the git object is lazy loaded. only a _id is required until properties are accessed.
62 internal AbstractObject(Repository repo
, ObjectId id
)
68 internal AbstractObject(Repository repo
, string name
)
71 _id
= _repo
._internal_repo
.Resolve(name
);
75 /// The git object's SHA1 hash. This is the long hash, See ShortHash for the abbreviated version.
88 /// The git object's abbreviated SHA1 hash.
90 public string ShortHash
96 return _id
.Abbreviate(_repo
._internal_repo
).name();
101 /// True if this object is a Blob (or Leaf which is a subclass of Blob).
103 public virtual bool IsBlob
109 return _repo
._internal_repo
.MapObject(_id
, null) is byte[];
114 /// True if this object is a Commit.
116 public virtual bool IsCommit
122 return _repo
._internal_repo
.MapObject(_id
, null) is CoreCommit
;
127 /// True if this object is a Tag.
129 public virtual bool IsTag
135 return _repo
._internal_repo
.MapObject(_id
, null) is CoreTag
;
140 /// True if the internal object is a Tree.
142 public virtual bool IsTree
148 return _repo
._internal_repo
.MapObject(_id
, null) is CoreTree
;
153 /// The repository where this git object belongs to.
155 public Repository Repository
164 public Diff
Diff(AbstractObject other
) { }
166 public ?? Grep(?? pattern
) { }
168 public Byte
[] Content { get; }
170 public long Size { get; }
174 /// Internal helper function to create the right object instance for a given hash
176 /// <param name="repo"></param>
177 /// <param name="objectId"></param>
178 /// <returns></returns>
179 internal static AbstractObject
Wrap(Repository repo
, ObjectId objectId
)
181 Debug
.Assert(objectId
!= null, "ObjectId is null");
182 Debug
.Assert(repo
!= null, "Repository is null");
183 var obj
= repo
._internal_repo
.MapObject(objectId
, null);
184 if (obj
is CoreCommit
)
185 return new Commit(repo
, obj
as CoreCommit
);
186 else if (obj
is CoreTag
)
187 return new Tag(repo
, obj
as CoreTag
);
188 else if (obj
is CoreTree
)
189 return new Tree(repo
, obj
as CoreTree
);
190 else if (obj
is byte[])
191 return new Blob(repo
, objectId
, obj
as byte[]);
194 //Debug.Assert(false, "What kind of object do we have here?");
199 #region Equality overrides
202 /// Overriding equals to reflect that different AbstractObject instances with the same hash are in fact equal.
204 /// <param name="obj"></param>
205 /// <returns></returns>
206 public override bool Equals(object obj
)
208 if (obj
is AbstractObject
)
209 return _id
== (obj
as AbstractObject
)._id
;
214 public static bool operator ==(AbstractObject self
, object other
)
216 return Equals(self
, other
);
219 public static bool operator !=(AbstractObject self
, object other
)
221 return !(self
== other
);
224 public override int GetHashCode()
227 return _id
.GetHashCode();
228 return base.GetHashCode();