Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/libgit2sharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornulltoken <emeric.fermas@gmail.com>2011-06-13 22:33:28 +0400
committernulltoken <emeric.fermas@gmail.com>2011-06-13 22:33:28 +0400
commit54096d525ce820deaf36180f13ed9a61735e8e3f (patch)
tree926792bc3bc01b02e1eede8a47918d3b9fa6ae10
parent7f862ab8f0cf37a55329ab9e2aaec1fb0be82fa7 (diff)
Make Repository.Head return a Branch instead of a Reference
-rw-r--r--LibGit2Sharp.Tests/BranchFixture.cs8
-rw-r--r--LibGit2Sharp.Tests/ReferenceFixture.cs15
-rw-r--r--LibGit2Sharp.Tests/RepositoryFixture.cs12
-rw-r--r--LibGit2Sharp.Tests/TagFixture.cs8
-rw-r--r--LibGit2Sharp/Branch.cs2
-rw-r--r--LibGit2Sharp/CommitCollection.cs2
-rw-r--r--LibGit2Sharp/Reference.cs14
-rw-r--r--LibGit2Sharp/Repository.cs14
-rw-r--r--LibGit2Sharp/RepositoryExtensions.cs2
-rw-r--r--LibGit2Sharp/RepositoryInformation.cs2
10 files changed, 47 insertions, 32 deletions
diff --git a/LibGit2Sharp.Tests/BranchFixture.cs b/LibGit2Sharp.Tests/BranchFixture.cs
index e002d94e..70c1bd88 100644
--- a/LibGit2Sharp.Tests/BranchFixture.cs
+++ b/LibGit2Sharp.Tests/BranchFixture.cs
@@ -161,7 +161,7 @@ namespace LibGit2Sharp.Tests
{
using (var repo = new Repository(Constants.TestRepoPath))
{
- Assert.Throws<ArgumentException>(() => repo.Branches.Create(string.Empty, repo.Head.ResolveToDirectReference().TargetIdentifier));
+ Assert.Throws<ArgumentException>(() => repo.Branches.Create(string.Empty, repo.Head.CanonicalName));
}
}
@@ -198,7 +198,7 @@ namespace LibGit2Sharp.Tests
{
using (var repo = new Repository(Constants.TestRepoPath))
{
- Assert.Throws<ArgumentNullException>(() => repo.Branches.Create(null, repo.Head.ResolveToDirectReference().TargetIdentifier));
+ Assert.Throws<ArgumentNullException>(() => repo.Branches.Create(null, repo.Head.CanonicalName));
}
}
@@ -324,8 +324,8 @@ namespace LibGit2Sharp.Tests
var newBranch = repo.CreateBranch("clone-of-master");
newBranch.IsCurrentRepositoryHead.ShouldBeFalse();
- var commitId = repo.Head.ResolveToDirectReference().TargetIdentifier;
- newBranch.Tip.Sha.ShouldEqual(commitId);
+ var commitId = repo.Head.Tip.Id;
+ newBranch.Tip.Id.ShouldEqual(commitId);
var reference = repo.Refs[newBranch.CanonicalName];
reference.ShouldNotBeNull();
diff --git a/LibGit2Sharp.Tests/ReferenceFixture.cs b/LibGit2Sharp.Tests/ReferenceFixture.cs
index 95f732a5..dd5fed21 100644
--- a/LibGit2Sharp.Tests/ReferenceFixture.cs
+++ b/LibGit2Sharp.Tests/ReferenceFixture.cs
@@ -101,7 +101,7 @@ namespace LibGit2Sharp.Tests
newRef.CanonicalName.ShouldEqual(name);
newRef.Target.ShouldNotBeNull();
newRef.ResolveToDirectReference().Target.Sha.ShouldEqual("a4a7dce85cf63874e984719f4fdd239f5145052f");
- ((SymbolicReference)repo.Head).Target.CanonicalName.ShouldEqual(target);
+ ((SymbolicReference)repo.Refs["HEAD"]).Target.CanonicalName.ShouldEqual(target);
}
}
@@ -226,15 +226,14 @@ namespace LibGit2Sharp.Tests
head.CanonicalName.ShouldEqual("HEAD");
head.Target.ShouldNotBeNull();
head.Target.CanonicalName.ShouldEqual("refs/heads/master");
- ((DirectReference) head.Target).Target.Sha.ShouldEqual("4c062a6361ae6959e06292c1fa5e2822d9c96345");
+ head.ResolveToDirectReference().Target.Sha.ShouldEqual("4c062a6361ae6959e06292c1fa5e2822d9c96345");
Assert.IsInstanceOf<Commit>(((DirectReference) head.Target).Target);
- var head2 = (SymbolicReference) repo.Head;
- head2.CanonicalName.ShouldEqual("HEAD");
- head2.Target.ShouldNotBeNull();
- Assert.IsInstanceOf<Commit>(((DirectReference) head.Target).Target);
+ var head2 = repo.Head;
+ head2.CanonicalName.ShouldEqual("refs/heads/master");
+ head2.Tip.ShouldNotBeNull();
- head.Equals(head2).ShouldBeTrue();
+ head2.Tip.Equals(head.ResolveToDirectReference().Target);
}
}
@@ -401,7 +400,7 @@ namespace LibGit2Sharp.Tests
const string newName = "refs/heads/o/sole";
const string oldName = newName + "/mio";
- repo.Refs.Create(oldName, repo.Head.ResolveToDirectReference().TargetIdentifier);
+ repo.Refs.Create(oldName, repo.Head.CanonicalName);
Reference moved = repo.Refs.Move(oldName, newName);
moved.ShouldNotBeNull();
moved.CanonicalName.ShouldEqual(newName);
diff --git a/LibGit2Sharp.Tests/RepositoryFixture.cs b/LibGit2Sharp.Tests/RepositoryFixture.cs
index 231518ad..4c137b73 100644
--- a/LibGit2Sharp.Tests/RepositoryFixture.cs
+++ b/LibGit2Sharp.Tests/RepositoryFixture.cs
@@ -56,9 +56,15 @@ namespace LibGit2Sharp.Tests
repo.Info.Path.ShouldNotBeNull();
repo.Info.IsEmpty.ShouldBeTrue();
repo.Info.IsHeadDetached.ShouldBeFalse();
- repo.Refs["HEAD"].ShouldNotBeNull();
- repo.Head.TargetIdentifier.ShouldEqual("refs/heads/master");
- repo.Head.ResolveToDirectReference().ShouldBeNull();
+
+ var headRef = repo.Refs["HEAD"];
+ headRef.ShouldNotBeNull();
+ headRef.TargetIdentifier.ShouldEqual("refs/heads/master");
+ headRef.ResolveToDirectReference().ShouldBeNull();
+
+ repo.Head.ShouldNotBeNull();
+ repo.Head.CanonicalName.ShouldEqual(headRef.TargetIdentifier);
+ repo.Head.Tip.ShouldBeNull();
repo.Commits.Count().ShouldEqual(0);
repo.Commits.QueryBy(new Filter { Since = repo.Head }).Count().ShouldEqual(0);
diff --git a/LibGit2Sharp.Tests/TagFixture.cs b/LibGit2Sharp.Tests/TagFixture.cs
index 8592ad23..290dae2f 100644
--- a/LibGit2Sharp.Tests/TagFixture.cs
+++ b/LibGit2Sharp.Tests/TagFixture.cs
@@ -214,7 +214,7 @@ namespace LibGit2Sharp.Tests
var tag = repo.ApplyTag("mytag");
tag.ShouldNotBeNull();
- tag.Target.Id.ShouldEqual(repo.Head.ResolveToDirectReference().Target.Id);
+ tag.Target.Id.ShouldEqual(repo.Head.Tip.Id);
var retrievedTag = repo.Tags[tag.CanonicalName];
tag.ShouldEqual(retrievedTag);
@@ -259,7 +259,7 @@ namespace LibGit2Sharp.Tests
var tag = repo.ApplyTag("mytag", "HEAD");
tag.ShouldNotBeNull();
- tag.Target.Id.ShouldEqual(repo.Head.ResolveToDirectReference().Target.Id);
+ tag.Target.Id.ShouldEqual(repo.Head.Tip.Id);
var retrievedTag = repo.Tags[tag.CanonicalName];
tag.ShouldEqual(retrievedTag);
@@ -272,7 +272,7 @@ namespace LibGit2Sharp.Tests
using (var path = new TemporaryCloneOfTestRepo())
using (var repo = new Repository(path.RepositoryPath))
{
- var headCommit = (Commit)repo.Head.ResolveToDirectReference().Target;
+ var headCommit = (Commit)repo.Head.Tip;
var tree = headCommit.Tree;
var tag = repo.ApplyTag("tree-tag", tree.Sha);
@@ -291,7 +291,7 @@ namespace LibGit2Sharp.Tests
using (var path = new TemporaryCloneOfTestRepo())
using (var repo = new Repository(path.RepositoryPath))
{
- var headCommit = (Commit)repo.Head.ResolveToDirectReference().Target;
+ var headCommit = (Commit)repo.Head.Tip;
var blob = headCommit.Tree.Files.First();
var tag = repo.ApplyTag("blob-tag", blob.Sha);
diff --git a/LibGit2Sharp/Branch.cs b/LibGit2Sharp/Branch.cs
index 9b0eb932..8344c998 100644
--- a/LibGit2Sharp/Branch.cs
+++ b/LibGit2Sharp/Branch.cs
@@ -59,7 +59,7 @@ namespace LibGit2Sharp
/// </value>
public bool IsCurrentRepositoryHead
{
- get { return repo.Refs[CanonicalName] == repo.Head.ResolveToDirectReference(); }
+ get { return repo.Refs[CanonicalName].ResolveToDirectReference() == repo.Refs["HEAD"].ResolveToDirectReference(); }
}
/// <summary>
diff --git a/LibGit2Sharp/CommitCollection.cs b/LibGit2Sharp/CommitCollection.cs
index 777a59af..008055d1 100644
--- a/LibGit2Sharp/CommitCollection.cs
+++ b/LibGit2Sharp/CommitCollection.cs
@@ -109,7 +109,7 @@ namespace LibGit2Sharp
int res = NativeMethods.git_tree_create_fromindex(out treeOid, repo.Index.Handle);
Ensure.Success(res);
- Reference head = repo.Head;
+ Reference head = repo.Refs["HEAD"];
GitOid[] gitOids = RetrieveCommitParent(head);
GitOid commitOid;
diff --git a/LibGit2Sharp/Reference.cs b/LibGit2Sharp/Reference.cs
index 387df15e..c063cfaf 100644
--- a/LibGit2Sharp/Reference.cs
+++ b/LibGit2Sharp/Reference.cs
@@ -39,7 +39,7 @@ namespace LibGit2Sharp
targetIdentifier = NativeMethods.git_reference_target(ptr).MarshallAsString();
int res = NativeMethods.git_reference_resolve(out resolveRef, ptr);
- if (res == (int) GitErrorCode.GIT_ENOTFOUND)
+ if (res == (int)GitErrorCode.GIT_ENOTFOUND)
{
reference = new SymbolicReference { CanonicalName = name, Target = null, TargetIdentifier = targetIdentifier };
break;
@@ -48,7 +48,7 @@ namespace LibGit2Sharp
Ensure.Success(res);
var targetRef = BuildFromPtr<Reference>(resolveRef, repo);
- reference = new SymbolicReference { CanonicalName = name, Target = targetRef, TargetIdentifier = targetIdentifier};
+ reference = new SymbolicReference { CanonicalName = name, Target = targetRef, TargetIdentifier = targetIdentifier };
break;
case GitReferenceType.Oid:
@@ -58,7 +58,7 @@ namespace LibGit2Sharp
targetIdentifier = targetId.Sha;
var targetResolver = new Func<GitObject>(() => repo.Lookup(targetId));
- reference = new DirectReference(targetResolver) { CanonicalName = name, TargetIdentifier = targetIdentifier};
+ reference = new DirectReference(targetResolver) { CanonicalName = name, TargetIdentifier = targetIdentifier };
break;
default:
@@ -71,21 +71,21 @@ namespace LibGit2Sharp
}
GitObject targetGitObject = repo.Lookup(targetIdentifier);
-
+
if (Equals(typeof(T), typeof(Tag)))
{
return new Tag(reference.CanonicalName, targetGitObject, targetGitObject as TagAnnotation) as T;
}
- if (Equals(typeof(T), typeof(Branch)))
+ if (Equals(typeof(T), typeof(Branch)))
{
return new Branch(reference.CanonicalName, targetGitObject as Commit, repo) as T;
}
throw new InvalidOperationException(
string.Format(CultureInfo.InvariantCulture, "Unable to build a new instance of '{0}' from a reference of type '{1}'.",
- typeof (T),
- Enum.GetName(typeof (GitReferenceType), type)));
+ typeof(T),
+ Enum.GetName(typeof(GitReferenceType), type)));
}
/// <summary>
diff --git a/LibGit2Sharp/Repository.cs b/LibGit2Sharp/Repository.cs
index b1d76c3a..b347c416 100644
--- a/LibGit2Sharp/Repository.cs
+++ b/LibGit2Sharp/Repository.cs
@@ -51,9 +51,19 @@ namespace LibGit2Sharp
/// Shortcut to return the reference to HEAD
/// </summary>
/// <returns></returns>
- public Reference Head
+ public Branch Head
{
- get { return Refs["HEAD"]; }
+ get
+ {
+ Reference headRef = Refs["HEAD"];
+
+ if (Info.IsEmpty)
+ {
+ return new Branch(headRef.TargetIdentifier, null, this);
+ }
+
+ return Refs.Resolve<Branch>(headRef.ResolveToDirectReference().CanonicalName);
+ }
}
/// <summary>
diff --git a/LibGit2Sharp/RepositoryExtensions.cs b/LibGit2Sharp/RepositoryExtensions.cs
index cd34cfff..59d6d0b0 100644
--- a/LibGit2Sharp/RepositoryExtensions.cs
+++ b/LibGit2Sharp/RepositoryExtensions.cs
@@ -82,7 +82,7 @@
/// <param name="branchName">The name of the branch to create.</param>
public static Branch CreateBranch(this Repository repository, string branchName)
{
- return CreateBranch(repository, branchName, repository.Head.TargetIdentifier); //TODO: To be replaced by Head.CanonicalName
+ return CreateBranch(repository, branchName, repository.Head.CanonicalName);
}
/// <summary>
diff --git a/LibGit2Sharp/RepositoryInformation.cs b/LibGit2Sharp/RepositoryInformation.cs
index 7129806b..5d6baf55 100644
--- a/LibGit2Sharp/RepositoryInformation.cs
+++ b/LibGit2Sharp/RepositoryInformation.cs
@@ -58,7 +58,7 @@ namespace LibGit2Sharp
get
{
if (repo.Info.IsEmpty) return false; // Detached HEAD doesn't mean anything for an empty repo, just return false
- return repo.Head is DirectReference;
+ return repo.Refs["HEAD"] is DirectReference;
}
}
}