Skip to content

Straighten api #210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Sep 6, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion LibGit2Sharp.Tests/BranchFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,18 @@ public void CanGetTrackingInformationForLocalTrackingBranch()
}
}

[Fact]
public void MovingARemoteTrackingBranchThrows()
{
using (var repo = new Repository(StandardTestRepoPath))
{
Branch master = repo.Branches["refs/remotes/origin/master"];
Assert.True(master.IsRemote);

Assert.Throws<LibGit2SharpException>(() => repo.Branches.Move(master, "new_name", true));
}
}

[Fact]
public void CanWalkCommitsFromAnotherBranch()
{
Expand Down Expand Up @@ -454,11 +466,27 @@ private void AssertRemoval(string branchName, bool isRemote, bool shouldPrevious
[Theory]
[InlineData("i-do-numbers", false)]
[InlineData("origin/br2", true)]
public void CanRemoveAnExistingBranch(string branchName, bool isRemote)
public void CanRemoveAnExistingNamedBranch(string branchName, bool isRemote)
{
AssertRemoval(branchName, isRemote, true);
}

[Theory]
[InlineData("i-do-numbers")]
[InlineData("origin/br2")]
public void CanRemoveAnExistingBranch(string branchName)
{
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoPath);

using (var repo = new Repository(path.RepositoryPath))
{
Branch curBranch = repo.Branches[branchName];

repo.Branches.Remove(curBranch);
Branch branch = repo.Branches[branchName];
Assert.Null(branch);
}
}

[Theory]
[InlineData("I-donot-exist", false)]
Expand Down
4 changes: 3 additions & 1 deletion LibGit2Sharp.Tests/CommitFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,9 @@ public void CanEnumerateCommitsFromTwoHeads()
public void CanEnumerateCommitsFromMixedStartingPoints()
{
AssertEnumerationOfCommits(
repo => new Filter { Since = new object[] { repo.Branches["br2"], "refs/heads/master", new ObjectId("e90810b") } },
repo => new Filter { Since = new object[] { repo.Branches["br2"],
"refs/heads/master",
new ObjectId("e90810b8df3e80c413d903f631643c716887138d") } },
new[]
{
"4c062a6", "e90810b", "6dcf9bf", "a4a7dce",
Expand Down
4 changes: 2 additions & 2 deletions LibGit2Sharp.Tests/ObjectIdFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ public void SimilarObjectIdsHaveSameHashCode()
[InlineData("0", false)]
[InlineData("01", false)]
[InlineData("012", false)]
[InlineData("0123", true)]
[InlineData("0123456", true)]
[InlineData("0123", false)]
[InlineData("0123456", false)]
[InlineData(validSha1 + "d", false)]
[InlineData(validSha1, true)]
public void TryParse(string maybeSha, bool isValidSha)
Expand Down
121 changes: 110 additions & 11 deletions LibGit2Sharp.Tests/ReferenceFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ public void CanAddADirectReferenceFromRevParseSpec()
}
}

[Fact]
public void CreatingADirectReferenceWithARevparseSpecPointingAtAnUnknownObjectFails()
{
const string name = "refs/heads/extendedShaSyntaxRulz";

TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo();
using (var repo = new Repository(path.RepositoryPath))
{
Assert.Throws<LibGit2SharpException>(() => repo.Refs.Add(name, "master^42"));
}
}

[Fact]
public void CanAddASymbolicReferenceFromTheTargetName()
{
Expand Down Expand Up @@ -196,6 +208,20 @@ public void CanRemoveAReference()
}
}

[Fact]
public void CanRemoveANonExistingReference()
{
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo();
using (var repo = new Repository(path.RepositoryPath))
{
const string unknown = "refs/heads/dahlbyk/has/hawkeyes";

Assert.Null(repo.Refs[unknown]);
repo.Refs.Remove(unknown);
Assert.Null(repo.Refs[unknown]);
}
}

[Fact]
public void ARemovedReferenceCannotBeLookedUp()
{
Expand Down Expand Up @@ -243,7 +269,8 @@ public void RemoveWithNullNameThrows()
{
using (var repo = new Repository(BareTestRepoPath))
{
Assert.Throws<ArgumentNullException>(() => repo.Refs.Remove(null));
Assert.Throws<ArgumentNullException>(() => repo.Refs.Remove((string)null));
Assert.Throws<ArgumentNullException>(() => repo.Refs.Remove((Reference)null));
}
}

Expand Down Expand Up @@ -343,7 +370,7 @@ public void ResolvingWithNullThrows()
}

[Fact]
public void CanUpdateTargetOnReference()
public void CanUpdateTargetOfADirectReference()
{
const string masterRef = "refs/heads/master";
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo();
Expand All @@ -363,7 +390,27 @@ public void CanUpdateTargetOnReference()
}

[Fact]
public void CanUpdateTargetOnSymbolicReference()
public void CanUpdateTargetOfADirectReferenceWithAnAbbreviatedSha()
{
const string masterRef = "refs/heads/master";
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo();
using (var repo = new Repository(path.RepositoryPath))
{
string sha = repo.Refs["refs/heads/test"].ResolveToDirectReference().Target.Sha;
Reference master = repo.Refs[masterRef];
Assert.NotEqual(sha, master.ResolveToDirectReference().Target.Sha);

Reference updated = repo.Refs.UpdateTarget(masterRef, sha.Substring(0,4));

master = repo.Refs[masterRef];
Assert.Equal(updated, master);

Assert.Equal(sha, master.ResolveToDirectReference().Target.Sha);
}
}

[Fact]
public void CanUpdateTargetOfASymbolicReference()
{
const string name = "refs/heads/unit_test";
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo();
Expand All @@ -382,7 +429,7 @@ public void CanUpdateTargetOnSymbolicReference()
}

[Fact]
public void CanUpdateHeadWithEitherAnOidOrACanonicalHeadReference()
public void CanUpdateHeadWithARevparseSpec()
{
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo();
using (var repo = new Repository(path.RepositoryPath))
Expand All @@ -397,7 +444,47 @@ public void CanUpdateHeadWithEitherAnOidOrACanonicalHeadReference()
Assert.True((symref is SymbolicReference));
Assert.Equal(repo.Refs["HEAD"], symref);
}

}

[Fact]
public void CanUpdateHeadWithEitherAnObjectIdOrAReference()
{
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo();
using (var repo = new Repository(path.RepositoryPath))
{
Reference head = repo.Refs["HEAD"];
Reference test = repo.Refs["refs/heads/test"];

Reference direct = repo.Refs.UpdateTarget(head, new ObjectId(test.TargetIdentifier));
Assert.True((direct is DirectReference));
Assert.Equal(test.TargetIdentifier, direct.TargetIdentifier);
Assert.Equal(repo.Refs["HEAD"], direct);

Reference symref = repo.Refs.UpdateTarget(head, test);
Assert.True((symref is SymbolicReference));
Assert.Equal(test.CanonicalName, symref.TargetIdentifier);
Assert.Equal(repo.Refs["HEAD"], symref);
}
}

[Fact]
public void CanUpdateTargetOfADirectReferenceWithARevparseSpec()
{
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo();
using (var repo = new Repository(path.RepositoryPath))
{
const string name = "refs/heads/master";

var master = (DirectReference) repo.Refs[name];

var newRef = (DirectReference)repo.Refs.UpdateTarget(master, "master^1^2");
Assert.NotNull(newRef);
Assert.Equal(name, newRef.CanonicalName);
Assert.NotNull(newRef.Target);
Assert.Equal("c47800c7266a2be04c571c04d5a6614691ea99bd", newRef.Target.Sha);
Assert.Equal(newRef.Target.Sha, newRef.TargetIdentifier);
Assert.NotNull(repo.Refs[name]);
}
}

[Fact]
Expand All @@ -418,12 +505,14 @@ public void UpdatingADirectRefWithSymbolFails()
}

[Fact]
public void UpdatingASymbolicRefWithOidFails()
public void CanUpdateTargetOfADirectReferenceWithAShortReferenceNameAsARevparseSpec()
{
const string masterRef = "refs/heads/master";
using (var repo = new Repository(BareTestRepoPath))
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo();
using (var repo = new Repository(path.RepositoryPath))
{
Assert.Throws<ArgumentException>(() => repo.Refs.UpdateTarget(masterRef, "refs/heads/test"));
Reference updatedMaster = repo.Refs.UpdateTarget(masterRef, "heads/test");
Assert.Equal(repo.Refs["refs/heads/test"].TargetIdentifier, updatedMaster.TargetIdentifier);
}
}

Expand All @@ -433,9 +522,19 @@ public void UpdatingAReferenceTargetWithBadParametersFails()
using (var repo = new Repository(BareTestRepoPath))
{
Assert.Throws<ArgumentException>(() => repo.Refs.UpdateTarget(string.Empty, "refs/heads/packed"));
Assert.Throws<ArgumentException>(() => repo.Refs.UpdateTarget("master", string.Empty));
Assert.Throws<ArgumentNullException>(() => repo.Refs.UpdateTarget(null, "refs/heads/packed"));
Assert.Throws<ArgumentNullException>(() => repo.Refs.UpdateTarget("master", null));
Assert.Throws<ArgumentException>(() => repo.Refs.UpdateTarget("refs/heads/master", string.Empty));
Assert.Throws<ArgumentNullException>(() => repo.Refs.UpdateTarget((string)null, "refs/heads/packed"));
Assert.Throws<ArgumentNullException>(() => repo.Refs.UpdateTarget((DirectReference)null, "refs/heads/packed"));
Assert.Throws<ArgumentNullException>(() => repo.Refs.UpdateTarget("refs/heads/master", null));
}
}

[Fact]
public void UpdatingADirectReferenceTargetWithARevparsePointingAtAnUnknownObjectFails()
{
using (var repo = new Repository(BareTestRepoPath))
{
Assert.Throws<LibGit2SharpException>(() => repo.Refs.UpdateTarget(repo.Refs["refs/heads/master"], "refs/heads/nope"));
}
}

Expand Down
11 changes: 11 additions & 0 deletions LibGit2Sharp.Tests/TagFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,17 @@ public void CanRemoveATagThroughItsCanonicalName()
}
}

[Fact]
public void CanRemoveATag()
{
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo();
using (var repo = new Repository(path.RepositoryPath))
{
Tag tag = repo.Tags["e90810b"];
repo.Tags.Remove(tag);
}
}

[Fact]
public void ARemovedTagCannotBeLookedUp()
{
Expand Down
19 changes: 0 additions & 19 deletions LibGit2Sharp/AbbreviatedObjectId.cs

This file was deleted.

55 changes: 28 additions & 27 deletions LibGit2Sharp/BranchCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace LibGit2Sharp
/// </summary>
public class BranchCollection : IEnumerable<Branch>
{
private readonly Repository repo;
internal readonly Repository repo;

/// <summary>
/// Needed for mocking purposes.
Expand Down Expand Up @@ -137,16 +137,15 @@ IEnumerator IEnumerable.GetEnumerator()
/// Create a new local branch with the specified name
/// </summary>
/// <param name = "name">The name of the branch.</param>
/// <param name = "commitish">Revparse spec for the target commit.</param>
/// <param name = "commit">The target commit.</param>
/// <param name = "allowOverwrite">True to allow silent overwriting a potentially existing branch, false otherwise.</param>
/// <returns></returns>
public virtual Branch Add(string name, string commitish, bool allowOverwrite = false)
/// <returns>A new <see cref="Branch"/>.</returns>
public virtual Branch Add(string name, Commit commit, bool allowOverwrite = false)
{
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNull(commit, "commit");

ObjectId commitId = repo.LookupCommit(commitish).Id;

Proxy.git_branch_create(repo.Handle, name, commitId, allowOverwrite);
Proxy.git_branch_create(repo.Handle, name, commit.Id, allowOverwrite);

return this[ShortToLocalName(name)];
}
Expand All @@ -161,19 +160,18 @@ public virtual Branch Add(string name, string commitish, bool allowOverwrite = f
[Obsolete("This method will be removed in the next release. Please use Add() instead.")]
public virtual Branch Create(string name, string commitish, bool allowOverwrite = false)
{
return Add(name, commitish, allowOverwrite);
return this.Add(name, commitish, allowOverwrite);
}

/// <summary>
/// Deletes the branch with the specified name.
/// Deletes the specified branch.
/// </summary>
/// <param name = "name">The name of the branch to delete.</param>
/// <param name = "isRemote">True if the provided <paramref name="name"/> is the name of a remote branch, false otherwise.</param>
public virtual void Remove(string name, bool isRemote = false)
/// <param name = "branch">The branch to delete.</param>
public virtual void Remove(Branch branch)
{
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNull(branch, "branch");

Proxy.git_branch_delete(repo.Handle, name, isRemote ? GitBranchType.GIT_BRANCH_REMOTE : GitBranchType.GIT_BRANCH_LOCAL);
this.Remove(branch.Name, branch.IsRemote);
}

/// <summary>
Expand All @@ -184,24 +182,27 @@ public virtual void Remove(string name, bool isRemote = false)
[Obsolete("This method will be removed in the next release. Please use Remove() instead.")]
public virtual void Delete(string name, bool isRemote = false)
{
Remove(name, isRemote);
this.Remove(name, isRemote);
}

///<summary>
/// Rename an existing local branch with a new name.
///</summary>
///<param name = "currentName">The current branch name.</param>
///<param name = "newName">The new name of the existing branch should bear.</param>
///<param name = "allowOverwrite">True to allow silent overwriting a potentially existing branch, false otherwise.</param>
///<returns></returns>
public virtual Branch Move(string currentName, string newName, bool allowOverwrite = false)
/// <summary>
/// Renames an existing local branch with a new name.
/// </summary>
/// <param name = "branch">The current local branch.</param>
/// <param name = "newName">The new name the existing branch should bear.</param>
/// <param name = "allowOverwrite">True to allow silent overwriting a potentially existing branch, false otherwise.</param>
/// <returns>A new <see cref="Branch"/>.</returns>
public virtual Branch Move(Branch branch, string newName, bool allowOverwrite = false)
{
Ensure.ArgumentNotNullOrEmptyString(currentName, "currentName");
Ensure.ArgumentNotNullOrEmptyString(newName, "name");
Ensure.ArgumentNotNull(branch, "branch");
Ensure.ArgumentNotNullOrEmptyString(newName, "newName");

Proxy.git_branch_move(repo.Handle, currentName, newName, allowOverwrite);
if (branch.IsRemote)
{
throw new LibGit2SharpException(string.Format("Cannot rename branch '{0}'. It's a remote tracking branch.", branch.Name));
}

return this[newName];
return this.Move(branch.Name, newName, allowOverwrite);
}

private static bool LooksLikeABranchName(string referenceName)
Expand Down
Loading