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 CoreRepository
= GitSharp
.Core
.Repository
;
53 /// Ref is a named symbolic reference that is a pointing to a specific git object. It is not resolved
54 /// until you explicitly retrieve the link target. The Target is not cached.
56 public class Ref
: IReferenceObject
58 internal Repository _repo
;
59 //private _internal_ref;
61 public Ref(Repository repo
, string name
)
67 internal Ref(Repository repo
, CoreRef
@ref)
68 : this(repo
, @ref.Name
)
78 protected virtual string RefName
{
83 /// Resolve the symbolic reference and return the object that it is currently pointing at. Target is not cached
84 /// in order to match the behavior of a real git ref.
86 public AbstractObject Target
90 var id
= _repo
._internal_repo
.Resolve(Name
);
93 return AbstractObject
.Wrap(_repo
, id
);
101 IDictionary
<string, CoreRef
> branches
= _repo
._internal_repo
._refDb
.getRefs(Constants
.R_HEADS
);
102 return branches
.ContainsKey(Name
);
107 /// Updates this ref by linking it to the given ref's target.
109 /// <param name="reference">The ref this ref shall reference.</param>
110 public void Update(Ref reference
)
112 Update(reference
.Target
);
116 /// Updates this ref by forwarding it to the given object.
118 /// <param name="reference">The ref this object shall reference.</param>
119 public void Update(AbstractObject reference
)
121 var db
= _repo
._internal_repo
;
122 var updateRef
= db
.UpdateRef(RefName
);
123 updateRef
.NewObjectId
= reference
._id
;
124 updateRef
.IsForceUpdate
= true;
126 //db.WriteSymref(Name, other.Name);
129 public static void Update(string name
, AbstractObject reference
)
131 new Ref(reference
.Repository
, name
).Update(reference
);
135 /// Check validity of a ref name. It must not contain a character that has
136 /// a special meaning in a Git object reference expression. Some other
137 /// dangerous characters are also excluded.
139 /// <param name="refName"></param>
141 /// Returns true if <paramref name="refName"/> is a valid ref name.
143 public static bool IsValidName(string refName
)
145 return CoreRepository
.IsValidRefName(refName
);
148 #region Equality overrides
150 public override bool Equals(object obj
)
154 var other
= obj
as Ref
;
155 return _repo
._internal_repo
.Resolve(Name
) == _repo
._internal_repo
.Resolve(other
.Name
);
161 public static bool operator ==(Ref self
, object other
)
163 return self
.Equals(other
);
166 public static bool operator !=(Ref self
, object other
)
168 return !self
.Equals(other
);
171 public override int GetHashCode()
173 var id
= _repo
._internal_repo
.Resolve(Name
);
175 return id
.GetHashCode();
176 return base.GetHashCode();
181 public override string ToString()
183 return "Ref[" + Name
+ "]";