Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

2008-12-06 Miguel de Icaza <miguel@novell.com>

	* Lib/*: Align some API doc comments, add some API docs.

	* Lib/RefDatabase.cs (Peel): Implement.

	* Lib/Constants.cs: Put all the constants here (not sure where
	some new constants should have gone).

	* Lib/Ref.cs: Update to support `Peeled' property and
	`OriginalName' from newer eGits.
  • Loading branch information...
commit ba4711b80fe57fb99a334a4344897a2dbc4753e0 1 parent e79e1a5
Miguel de Icaza authored
View
12 ChangeLog
@@ -1,3 +1,15 @@
+2008-12-06 Miguel de Icaza <miguel@novell.com>
+
+ * Lib/*: Align some API doc comments, add some API docs.
+
+ * Lib/RefDatabase.cs (Peel): Implement.
+
+ * Lib/Constants.cs: Put all the constants here (not sure where
+ some new constants should have gone).
+
+ * Lib/Ref.cs: Update to support `Peeled' property and
+ `OriginalName' from newer eGits.
+
2008-12-03 Miguel de Icaza <miguel@novell.com>
* test.cs: make the sample program dump all the refs.
View
85 Lib/Constants.cs
@@ -0,0 +1,85 @@
+/*
+ * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
+ * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
+ * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
+ * Copyright (C) 2008, Kevin Thompson <kevin.thompson@theautomaters.com>
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * - Neither the name of the Git Development Community nor the
+ * names of its contributors may be used to endorse or promote
+ * products derived from this software without specific prior
+ * written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+using System.Globalization;
+using System.Text;
+
+namespace Gitty.Lib
+{
+ public sealed class Constants {
+ /// <summary>
+ /// Prefix for branch refs
+ /// </summary>
+ public static readonly string HeadsPrefix = "refs/heads";
+
+ /// <summary>
+ /// Prefix for remote refs
+ /// </summary>
+ public static readonly string RemotesPrefix = "refs/remotes";
+
+ /// <summary>
+ /// Special name for the "HEAD" symbolic ref
+ /// </summary>
+ public static readonly string Head = "HEAD";
+
+ public static readonly string Master = "master";
+
+ /// <summary>
+ /// Text string that identifies an object as an annotated tag.
+ /// <summary>
+ /// <remarks>
+ /// Annotated tags store a pointer to any other object, and an additional
+ /// message. It is most commonly used to record a stable release of the
+ /// project.
+ /// </summary>
+ public static readonly string TypeTag = "tag";
+
+ public static readonly Encoding Encoding = Encoding.UTF8;
+
+ public static readonly string RefsSlash = "refs/";
+
+ public static readonly string TagsPrefix = "refs/tags";
+
+ public static readonly string TagsSlash = TagsPrefix + "/";
+
+ public static readonly string HeadsSlash = HeadsPrefix + "/";
+
+ public static readonly string[] RefSearchPaths = { "", RefsSlash, TagsSlash, HeadsSlash, RemotesPrefix + "/" };
+ }
+}
View
8 Lib/ObjectLoader.cs
@@ -77,10 +77,10 @@ public abstract class ObjectLoader
}
}
- // I'm not entirely sure how Java's protected works, but it seems
- // this method was callable from a class that didn't inherit from this one,
- // so instead I've marked this as internal for now, though I think
- // public would probably be better - NR
+ // I'm not entirely sure how Java's protected works, but it seems
+ // this method was callable from a class that didn't inherit from this one,
+ // so instead I've marked this as internal for now, though I think
+ // public would probably be better - NR
internal bool HasComputedId
{
get
View
33 Lib/Ref.cs
@@ -69,27 +69,42 @@ static Storage()
}
}
- public Ref(Storage storage, string refName, ObjectId id )
- {
- this.StorageFormat = storage;
- this.Name = refName;
- this.ObjectId = id;
+ public Ref(Storage storage, string origName, string refName, ObjectId id)
+ : this (storage, origName, refName, id, null, false)
+ {
}
- public Ref(Storage storage, string refName, ObjectId id, ObjectId peeledObjectId)
- : this(storage, refName, id)
+ public Ref(Storage storage, string refName, ObjectId id)
+ : this (storage, refName, refName, id, null, false)
+ {
+ }
+
+ public Ref(Storage storage, string refName, ObjectId id, ObjectId peeledObjectId, bool peeled)
+ : this (storage, refName, refName, id, peeledObjectId, peeled)
+
+ {
+ }
+
+ public Ref(Storage storage, string origName, string refName, ObjectId id, ObjectId peeledObjectId, bool peeled)
{
- this.PeeledObjectId = peeledObjectId;
+ StorageFormat = storage;
+ OriginalName = origName;
+ Name = refName;
+ ObjectId = id;
+ PeeledObjectId = peeledObjectId;
+ Peeled = peeled;
}
public string Name { get; private set; }
+ public string OriginalName { get; private set; }
public Storage StorageFormat { get; private set; }
public ObjectId ObjectId { get; private set; }
public ObjectId PeeledObjectId { get; private set; }
+ public bool Peeled { get; private set; }
public override string ToString()
{
- return "Ref[" + Name + "=" + this.ObjectId.ToString() + "]";
+ return "Ref[" + (OriginalName == Name ? "" : "(" + OriginalName + ")") + Name + "=" + this.ObjectId.ToString() + "]";
}
}
}
View
39 Lib/RefDatabase.cs
@@ -50,16 +50,6 @@ namespace Gitty.Lib
[Complete]
public class RefDatabase
{
-
- public class Constants
- {
- public readonly static Encoding Encoding = Encoding.UTF8;
- public readonly static string RefsSlash = "refs/";
- public readonly static string TagsSlash = Tag.Constants.TagsPrefix + "/";
- public readonly static string HeadsSlash = Repository.Constants.HeadsPrefix + "/";
- public readonly static string[] RefSearchPaths = { "", RefsSlash, TagsSlash, HeadsSlash, Repository.Constants.RemotesPrefix + "/" };
- }
-
public Repository Repository { get; private set; }
private DirectoryInfo _gitDir;
@@ -169,7 +159,7 @@ public Ref ReadRef(String partialName)
Dictionary<String, Ref> avail = new Dictionary<String, Ref>();
ReadPackedRefs(avail);
ReadLooseRefs(avail, Constants.RefsSlash, _refsDir);
- ReadOneLooseRef(avail, Repository.Constants.Head, PathUtil.CombineFilePath(_gitDir, Repository.Constants.Head));
+ ReadOneLooseRef(avail, Constants.Head, PathUtil.CombineFilePath(_gitDir, Constants.Head));
return avail;
}
@@ -381,7 +371,7 @@ private void RefreshPackedRefs()
throw new IOException("Peeled line before ref.");
ObjectId id = ObjectId.FromString(p.Substring(1));
- last = new Ref(Ref.Storage.Packed, last.Name, last.ObjectId, id);
+ last = new Ref(Ref.Storage.Packed, last.Name, last.Name, last.ObjectId, id, true);
newPackedRefs.Add(last.Name, last);
continue;
}
@@ -415,6 +405,31 @@ private void RefreshPackedRefs()
}
}
+ internal Ref Peel (Ref dref)
+ {
+ if (dref.Peeled)
+ return dref;
+
+ ObjectId peeled = null;
+ try {
+ object target = Repository.MapObject (dref.ObjectId, dref.Name);
+
+ while (target is Tag){
+ Tag tag = (Tag) target;
+ peeled = tag.Id;
+
+ if (tag.TagType == Constants.TypeTag)
+ target = Repository.MapObject (tag.Id, dref.Name);
+ else
+ break;
+ }
+ } catch (IOException){
+ // Ignore a read error.  Callers will also get the same error
+ // if they try to use the result of getPeeledObjectId.
+ }
+ return new Ref (dref.StorageFormat, dref.Name, dref.ObjectId, peeled, true);
+ }
+
private string ReadLine(FileInfo file)
{
using (BufferedReader br = OpenReader(file))
View
19 Lib/Repository.cs
@@ -73,15 +73,6 @@ namespace Gitty.Lib
*/
public class Repository
{
-
- public sealed class Constants
- {
- public static readonly string HeadsPrefix = "refs/heads";
- public static readonly string RemotesPrefix = "refs/remotes";
- public static readonly string Head = "HEAD";
- public static readonly string Master = "master";
- }
-
private RefDatabase _refs;
private List<PackFile> _packs;
@@ -905,6 +896,16 @@ public RepositoryState GetRespositoryState()
{
return _refs.GetAllRefs ();
}
+
+ public Dictionary<string, Ref> GetTags ()
+ {
+ return _refs.GetTags ();
+ }
+
+ public Ref Peel (Ref pRef)
+ {
+ return _refs.Peel (pRef);
+ }
/**
* Check validty of a ref name. It must not contain character that has
View
393 Lib/Tag.cs
@@ -46,220 +46,197 @@
namespace Gitty.Lib
{
public class Tag {
- public class Constants
- {
- public static readonly string TagsPrefix = "refs/tags";
- }
-
public Repository Repository { get; protected set; }
-
-
-
-
-
- private byte[] raw;
-
-
-
-
- /**
- * Construct a new, yet unnamed Tag.
- *
- * @param db
- */
- public Tag( Repository db) {
- Repository = db;
- }
-
- /**
- * Construct a Tag representing an existing with a known name referencing an known object.
- * This could be either a simple or annotated tag.
- *
- * @param db {@link Repository}
- * @param id target id.
- * @param refName tag name or null
- * @param raw data of an annotated tag.
- */
- public Tag(Repository db, ObjectId id, String refName, byte[] raw) {
- Repository = db;
- if (raw != null) {
- TagId = id;
- Id = ObjectId.FromString(raw, 7);
- } else
- Id = id;
- if (refName != null && refName.StartsWith("refs/tags/"))
- refName = refName.Substring(10);
- TagName = refName;
- this.raw = raw;
- }
-
- /**
- * @return tagger of a annotated tag or null
- */
- private PersonIdent author;
- public PersonIdent Author {
- get
- {
- decode();
- return author;
- }
- set
- {
- author = value;
- }
- }
-
- /**
- * @return comment of an annotated tag, or null
- */
- private String message;
- public String Message {
- get
- {
- decode();
- return message;
- }
- set
- {
- message = value;
- }
- }
-
- private void decode()
- {
- // FIXME: handle I/O errors
- if (raw == null) return;
-
- using (var br = new StreamReader(new MemoryStream(raw)))
- {
- String n = br.ReadLine();
- if (n == null || !n.StartsWith("object "))
- {
- throw new CorruptObjectException(TagId, "no object");
- }
- Id = ObjectId.FromString(n.Substring(7));
- n = br.ReadLine();
- if (n == null || !n.StartsWith("type "))
- {
- throw new CorruptObjectException(TagId, "no type");
- }
- TagType = n.Substring("type ".Length);
- n = br.ReadLine();
-
- if (n == null || !n.StartsWith("tag "))
- {
- throw new CorruptObjectException(TagId, "no tag name");
- }
- TagName = n.Substring("tag ".Length);
- n = br.ReadLine();
-
- // We should see a "tagger" header here, but some repos have tags
- // without it.
- if (n == null)
- throw new CorruptObjectException(TagId, "no tagger header");
-
- if (n.Length > 0)
- if (n.StartsWith("tagger "))
- Tagger = new PersonIdent(n.Substring("tagger ".Length));
- else
- throw new CorruptObjectException(TagId, "no tagger/bad header");
-
- // Message should start with an empty line, but
- StringBuilder tempMessage = new StringBuilder();
- char[] readBuf = new char[2048];
- int readLen;
- int readIndex = 0;
- while ((readLen = br.Read(readBuf, readIndex, readBuf.Length)) > 0)
- {
- //readIndex += readLen;
- tempMessage.Append(readBuf, 0, readLen);
- }
- message = tempMessage.ToString();
- if (message.StartsWith("\n"))
- message = message.Substring(1);
- }
-
- raw = null;
- }
-
+ private byte[] raw;
/**
- * Store a tag.
- * If author, message or type is set make the tag an annotated tag.
- *
- * @throws IOException
- */
- public void Save(){ //renamed from Tag
- if (TagId != null)
- throw new InvalidOperationException("exists " + TagId);
- ObjectId id;
-
- if (author!=null || message!=null || tagType!=null) {
- ObjectId tagid = new ObjectWriter(Repository).WriteTag(this);
- TagId = tagid;
- id = tagid;
- } else {
- id = Id;
- }
-
- RefUpdate ru = Repository.UpdateRef(Constants.TagsPrefix + "/" + TagName);
- ru.SetNewObjectId(id);
- ru.SetRefLogMessage("tagged " + TagName, false);
- if (ru.ForceUpdate() == RefUpdate.Result.LockFailure)
- throw new ObjectWritingException("Unable to lock tag " + TagName);
- }
-
- public String toString() {
- return "tag[" + TagName + TagType + Id + " " + Author + "]";
- }
-
- public ObjectId TagId { get; set; }
-
-
- /**
- * @return creator of this tag.
- */
- public PersonIdent Tagger{
- get
- {
- return Author;
- }
- set
- {
- Author = value;
- }
- }
+ * Construct a new, yet unnamed Tag.
+ *
+ * @param db
+ */
+ public Tag (Repository db) {
+ Repository = db;
+ }
+ /**
+ * Construct a Tag representing an existing with a known name referencing an known object.
+ * This could be either a simple or annotated tag.
+ *
+ * @param db {@link Repository}
+ * @param id target id.
+ * @param refName tag name or null
+ * @param raw data of an annotated tag.
+ */
+ public Tag(Repository db, ObjectId id, String refName, byte[] raw) {
+ Repository = db;
+ if (raw != null) {
+ TagId = id;
+ Id = ObjectId.FromString(raw, 7);
+ } else
+ Id = id;
+ if (refName != null && refName.StartsWith("refs/tags/"))
+ refName = refName.Substring(10);
+ TagName = refName;
+ this.raw = raw;
+ }
+
+ /**
+ * @return tagger of a annotated tag or null
+ */
+ private PersonIdent author;
+ public PersonIdent Author {
+ get
+ {
+ decode();
+ return author;
+ }
+ set
+ {
+ author = value;
+ }
+ }
- /**
- * @return tag target type
- */
- private String tagType;
- public String TagType {
- get
- {
- decode();
- return tagType;
- }
- set
- {
- tagType = value;
- }
- }
+ /**
+ * @return comment of an annotated tag, or null
+ */
+ private String message;
+ public String Message {
+ get
+ {
+ decode();
+ return message;
+ }
+ set
+ {
+ message = value;
+ }
+ }
+
+ private void decode()
+ {
+ // FIXME: handle I/O errors
+ if (raw == null) return;
+
+ using (var br = new StreamReader(new MemoryStream(raw)))
+ {
+ String n = br.ReadLine();
+ if (n == null || !n.StartsWith("object "))
+ {
+ throw new CorruptObjectException(TagId, "no object");
+ }
+ Id = ObjectId.FromString(n.Substring(7));
+ n = br.ReadLine();
+ if (n == null || !n.StartsWith("type "))
+ {
+ throw new CorruptObjectException(TagId, "no type");
+ }
+ TagType = n.Substring("type ".Length);
+ n = br.ReadLine();
+
+ if (n == null || !n.StartsWith("tag "))
+ {
+ throw new CorruptObjectException(TagId, "no tag name");
+ }
+ TagName = n.Substring("tag ".Length);
+ n = br.ReadLine();
+
+ // We should see a "tagger" header here, but some repos have tags
+ // without it.
+ if (n == null)
+ throw new CorruptObjectException(TagId, "no tagger header");
+
+ if (n.Length > 0)
+ if (n.StartsWith("tagger "))
+ Tagger = new PersonIdent(n.Substring("tagger ".Length));
+ else
+ throw new CorruptObjectException(TagId, "no tagger/bad header");
+
+ // Message should start with an empty line, but
+ StringBuilder tempMessage = new StringBuilder();
+ char[] readBuf = new char[2048];
+ int readLen;
+ int readIndex = 0;
+ while ((readLen = br.Read(readBuf, readIndex, readBuf.Length)) > 0)
+ {
+ //readIndex += readLen;
+ tempMessage.Append(readBuf, 0, readLen);
+ }
+ message = tempMessage.ToString();
+ if (message.StartsWith("\n"))
+ message = message.Substring(1);
+ }
+
+ raw = null;
+ }
+
+
+ /**
+ * Store a tag.
+ * If author, message or type is set make the tag an annotated tag.
+ *
+ * @throws IOException
+ */
+ public void Save(){ //renamed from Tag
+ if (TagId != null)
+ throw new InvalidOperationException("exists " + TagId);
+ ObjectId id;
+
+ if (author!=null || message!=null || tagType!=null) {
+ ObjectId tagid = new ObjectWriter(Repository).WriteTag(this);
+ TagId = tagid;
+ id = tagid;
+ } else {
+ id = Id;
+ }
+
+ RefUpdate ru = Repository.UpdateRef(Constants.TagsPrefix + "/" + TagName);
+ ru.SetNewObjectId(id);
+ ru.SetRefLogMessage("tagged " + TagName, false);
+ if (ru.ForceUpdate() == RefUpdate.Result.LockFailure)
+ throw new ObjectWritingException("Unable to lock tag " + TagName);
+ }
+
+ public override String ToString () {
+ return "tag[" + TagName + TagType + Id + " " + Author + "]";
+ }
+
+ public ObjectId TagId { get; set; }
+ /**
+ * @return creator of this tag.
+ */
+ public PersonIdent Tagger {
+ get { return Author; }
+ set { Author = value; }
+ }
+
+
+ /**
+ * @return tag target type
+ */
+ String tagType;
+ public String TagType {
+ get {
+ decode();
+ return tagType;
+ }
+ set {
+ tagType = value;
+ }
+ }
- public string TagName { get; set; }
-
- /**
- * @return the SHA'1 of the object this tag refers to.
- */
-
- public ObjectId Id { get; set; }
+ public string TagName { get; set; }
- /**
- * Set the id of the object this tag refers to.
- *
- * @param objId
- */
-}
+ /**
+ * @return the SHA'1 of the object this tag refers to.
+ */
+
+ public ObjectId Id { get; set; }
+
+ /**
+ * Set the id of the object this tag refers to.
+ *
+ * @param objId
+ */
+ }
}
View
3  Makefile
@@ -15,6 +15,7 @@ SOURCES = \
Lib/BinaryDelta.cs \
Lib/ByteArrayWindow.cs \
Lib/Commit.cs \
+ Lib/Constants.cs \
Lib/CoreConfig.cs \
Lib/DeltaOfsPackedObjectLoader.cs \
Lib/DeltaPackedObjectLoader.cs \
@@ -83,7 +84,7 @@ all: Gitty.Lib.CSharp.dll test.exe
Gitty.Lib.CSharp.dll: $(SOURCES)
$(CSC) $(SOURCES) -out:Gitty.Lib.CSharp.dll -debug -target:library -r:ICSharpCode.SharpZipLib
-test.exe: Gitty.Lib.CSharp.dll
+test.exe: Gitty.Lib.CSharp.dll test.cs
$(CSC) -r:Gitty.Lib.CSharp.dll -debug test.cs
run: test.exe
View
8 test.cs
@@ -13,5 +13,13 @@ static void Main (string [] args)
foreach (var k in refs){
Console.WriteLine (" {0} -> {1}", k.Key, k.Value);
}
+
+ Ref heads_master = refs ["refs/heads/master"];
+
+ ObjectLoader ol = repo.OpenObject (heads_master.ObjectId);
+
+ Console.WriteLine ("ObjectType: {0}", ol.ObjectType);
+ Console.WriteLine (" Size: {0}", ol.Size);
+ Console.WriteLine (" RawSize: {0}", ol.RawSize);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.