Update README.txt
[GitSharp.git] / GitSharp / AbstractObject.cs
blob9333c62beee4fb740696cce9f293b16501fd2784
1 /*
2 * Copyright (C) 2009, Henon <meinrad.recheis@gmail.com>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
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
21 * written permission.
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.
38 using System;
39 using System.Collections.Generic;
40 using System.Linq;
41 using System.Text;
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;
50 namespace GitSharp
52 /// <summary>
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.
56 /// </summary>
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)
64 _repo = repo;
65 _id = id;
68 internal AbstractObject(Repository repo, string name)
70 _repo = repo;
71 _id = _repo._internal_repo.Resolve(name);
74 /// <summary>
75 /// The git object's SHA1 hash. This is the long hash, See ShortHash for the abbreviated version.
76 /// </summary>
77 public string Hash
79 get
81 if (_id == null)
82 return null;
83 return _id.Name;
87 /// <summary>
88 /// The git object's abbreviated SHA1 hash.
89 /// </summary>
90 public string ShortHash
92 get
94 if (_id == null)
95 return null;
96 return _id.Abbreviate(_repo._internal_repo).name();
100 /// <summary>
101 /// True if this object is a Blob (or Leaf which is a subclass of Blob).
102 /// </summary>
103 public virtual bool IsBlob
107 if (_id == null)
108 return false;
109 return _repo._internal_repo.MapObject(_id, null) is byte[];
113 /// <summary>
114 /// True if this object is a Commit.
115 /// </summary>
116 public virtual bool IsCommit
120 if (_id == null)
121 return false;
122 return _repo._internal_repo.MapObject(_id, null) is CoreCommit;
126 /// <summary>
127 /// True if this object is a Tag.
128 /// </summary>
129 public virtual bool IsTag
133 if (_id == null)
134 return false;
135 return _repo._internal_repo.MapObject(_id, null) is CoreTag;
139 /// <summary>
140 /// True if the internal object is a Tree.
141 /// </summary>
142 public virtual bool IsTree
146 if (_id == null)
147 return false;
148 return _repo._internal_repo.MapObject(_id, null) is CoreTree;
152 /// <summary>
153 /// The repository where this git object belongs to.
154 /// </summary>
155 public Repository Repository
159 return _repo;
163 #if implemented
164 public Diff Diff(AbstractObject other) { }
166 public ?? Grep(?? pattern) { }
168 public Byte[] Content { get; }
170 public long Size { get; }
171 #endif
173 /// <summary>
174 /// Internal helper function to create the right object instance for a given hash
175 /// </summary>
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[]);
192 else
194 //Debug.Assert(false, "What kind of object do we have here?");
195 return null;
199 #region Equality overrides
201 /// <summary>
202 /// Overriding equals to reflect that different AbstractObject instances with the same hash are in fact equal.
203 /// </summary>
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;
210 else
211 return false;
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()
226 if (_id != null)
227 return _id.GetHashCode();
228 return base.GetHashCode();
231 #endregion