Update README.txt
[GitSharp.git] / GitSharp / Ref.cs
blobc7a011057ba35ab5c82113154b26dc0b1ba23ae6
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;
42 using GitSharp.Core;
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;
49 namespace GitSharp
52 /// <summary>
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.
55 /// </summary>
56 public class Ref : IReferenceObject
58 internal Repository _repo;
59 //private _internal_ref;
61 public Ref(Repository repo, string name)
63 _repo = repo;
64 Name = name;
67 internal Ref(Repository repo, CoreRef @ref)
68 : this(repo, @ref.Name)
72 public string Name
74 get;
75 protected set;
78 protected virtual string RefName {
79 get { return Name; }
82 /// <summary>
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.
85 /// </summary>
86 public AbstractObject Target
88 get
90 var id = _repo._internal_repo.Resolve(Name);
91 if (id == null)
92 return null;
93 return AbstractObject.Wrap(_repo, id);
97 public bool IsBranch
99 get
101 IDictionary<string, CoreRef> branches = _repo._internal_repo._refDb.getRefs(Constants.R_HEADS);
102 return branches.ContainsKey(Name);
106 /// <summary>
107 /// Updates this ref by linking it to the given ref's target.
108 /// </summary>
109 /// <param name="reference">The ref this ref shall reference.</param>
110 public void Update(Ref reference)
112 Update(reference.Target);
115 /// <summary>
116 /// Updates this ref by forwarding it to the given object.
117 /// </summary>
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;
125 updateRef.update();
126 //db.WriteSymref(Name, other.Name);
129 public static void Update(string name, AbstractObject reference)
131 new Ref(reference.Repository, name).Update(reference);
134 /// <summary>
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.
138 /// </summary>
139 /// <param name="refName"></param>
140 /// <returns>
141 /// Returns true if <paramref name="refName"/> is a valid ref name.
142 /// </returns>
143 public static bool IsValidName(string refName)
145 return CoreRepository.IsValidRefName(refName);
148 #region Equality overrides
150 public override bool Equals(object obj)
152 if (obj is Ref)
154 var other = obj as Ref;
155 return _repo._internal_repo.Resolve(Name) == _repo._internal_repo.Resolve(other.Name);
157 else
158 return false;
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);
174 if (id != null)
175 return id.GetHashCode();
176 return base.GetHashCode();
179 #endregion
181 public override string ToString()
183 return "Ref[" + Name + "]";