Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
115 changes: 99 additions & 16 deletions LibGit2Sharp.Tests/BranchFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,25 @@ public void CanCreateBranch(string name)
string path = CloneBareTestRepo();
using (var repo = new Repository(path))
{
Branch newBranch = repo.CreateBranch(name, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
EnableRefLog(repo);

const string committish = "be3563ae3f795b2b4353bcce3a527ad0a4f7f644";

Branch newBranch = repo.CreateBranch(name, committish);
Assert.NotNull(newBranch);
Assert.Equal(name, newBranch.Name);
Assert.Equal("refs/heads/" + name, newBranch.CanonicalName);
Assert.NotNull(newBranch.Tip);
Assert.Equal("be3563ae3f795b2b4353bcce3a527ad0a4f7f644", newBranch.Tip.Sha);
Assert.Equal(committish, newBranch.Tip.Sha);
Assert.NotNull(repo.Branches.SingleOrDefault(p => p.Name == name));

AssertRefLogEntry(repo, newBranch.CanonicalName,
newBranch.Tip.Id,
"branch: Created from " + committish,
committer: newBranch.Tip.Committer);

repo.Branches.Remove(newBranch.Name);
Assert.Null(repo.Branches[name]);
}
}

Expand All @@ -38,41 +48,72 @@ public void CanCreateBranchUsingAbbreviatedSha()
string path = CloneBareTestRepo();
using (var repo = new Repository(path))
{
EnableRefLog(repo);

const string name = "unit_test";
Branch newBranch = repo.CreateBranch(name, "be3563a");
const string committish = "be3563a";

Branch newBranch = repo.CreateBranch(name, committish);
Assert.Equal("refs/heads/" + name, newBranch.CanonicalName);
Assert.Equal("be3563ae3f795b2b4353bcce3a527ad0a4f7f644", newBranch.Tip.Sha);

AssertRefLogEntry(repo, newBranch.CanonicalName,
newBranch.Tip.Id,
"branch: Created from " + committish,
committer: newBranch.Tip.Committer);
}
}

[Fact]
public void CanCreateBranchFromImplicitHead()
[Theory]
[InlineData("32eab9cb1f450b5fe7ab663462b77d7f4b703344")]
[InlineData("master")]
public void CanCreateBranchFromImplicitHead(string headCommitOrBranchSpec)
{
string path = CloneBareTestRepo();
string path = CloneStandardTestRepo();
using (var repo = new Repository(path))
{
EnableRefLog(repo);

repo.Checkout(headCommitOrBranchSpec);

const string name = "unit_test";
Branch newBranch = repo.CreateBranch(name);
Assert.NotNull(newBranch);
Assert.Equal(name, newBranch.Name);
Assert.Equal("refs/heads/" + name, newBranch.CanonicalName);
Assert.False(newBranch.IsCurrentRepositoryHead);
Assert.NotNull(newBranch.Tip);
Assert.Equal("4c062a6361ae6959e06292c1fa5e2822d9c96345", newBranch.Tip.Sha);
Assert.Equal("32eab9cb1f450b5fe7ab663462b77d7f4b703344", newBranch.Tip.Sha);
Assert.NotNull(repo.Branches.SingleOrDefault(p => p.Name == name));

AssertRefLogEntry(repo, newBranch.CanonicalName,
newBranch.Tip.Id,
"branch: Created from " + headCommitOrBranchSpec,
committer: newBranch.Tip.Committer);
}
}

[Fact]
public void CanCreateBranchFromExplicitHead()
[Theory]
[InlineData("32eab9cb1f450b5fe7ab663462b77d7f4b703344")]
[InlineData("master")]
public void CanCreateBranchFromExplicitHead(string headCommitOrBranchSpec)
{
string path = CloneBareTestRepo();
string path = CloneStandardTestRepo();
using (var repo = new Repository(path))
{
EnableRefLog(repo);

repo.Checkout(headCommitOrBranchSpec);

const string name = "unit_test";
Branch newBranch = repo.CreateBranch(name, "HEAD");
Assert.NotNull(newBranch);
Assert.Equal("4c062a6361ae6959e06292c1fa5e2822d9c96345", newBranch.Tip.Sha);
Assert.Equal("32eab9cb1f450b5fe7ab663462b77d7f4b703344", newBranch.Tip.Sha);

AssertRefLogEntry(repo, newBranch.CanonicalName,
newBranch.Tip.Id,
"branch: Created from " + headCommitOrBranchSpec,
committer: newBranch.Tip.Committer);
}
}

Expand All @@ -82,11 +123,18 @@ public void CanCreateBranchFromCommit()
string path = CloneBareTestRepo();
using (var repo = new Repository(path))
{
EnableRefLog(repo);

const string name = "unit_test";
var commit = repo.Lookup<Commit>("HEAD");
Branch newBranch = repo.CreateBranch(name, commit);
Assert.NotNull(newBranch);
Assert.Equal("4c062a6361ae6959e06292c1fa5e2822d9c96345", newBranch.Tip.Sha);

AssertRefLogEntry(repo, newBranch.CanonicalName,
newBranch.Tip.Id,
"branch: Created from " + newBranch.Tip.Sha,
committer: newBranch.Tip.Committer);
}
}

Expand All @@ -96,24 +144,42 @@ public void CanCreateBranchFromRevparseSpec()
string path = CloneBareTestRepo();
using (var repo = new Repository(path))
{
EnableRefLog(repo);

const string name = "revparse_branch";
var target = repo.Lookup<Commit>("master~2");
Branch newBranch = repo.CreateBranch(name, target);
const string committish = "master~2";

Branch newBranch = repo.CreateBranch(name, committish);
Assert.NotNull(newBranch);
Assert.Equal("9fd738e8f7967c078dceed8190330fc8648ee56a", newBranch.Tip.Sha);

AssertRefLogEntry(repo, newBranch.CanonicalName,
newBranch.Tip.Id,
"branch: Created from " + committish,
committer: newBranch.Tip.Committer);
}
}

[Fact]
public void CreatingABranchFromATagPeelsToTheCommit()
[Theory]
[InlineData("test")]
[InlineData("refs/tags/test")]
public void CreatingABranchFromATagPeelsToTheCommit(string committish)
{
string path = CloneBareTestRepo();
using (var repo = new Repository(path))
{
EnableRefLog(repo);

const string name = "i-peel-tag";
Branch newBranch = repo.CreateBranch(name, "refs/tags/test");

Branch newBranch = repo.CreateBranch(name, committish);
Assert.NotNull(newBranch);
Assert.Equal("e90810b8df3e80c413d903f631643c716887138d", newBranch.Tip.Sha);

AssertRefLogEntry(repo, newBranch.CanonicalName,
newBranch.Tip.Id,
"branch: Created from " + committish,
committer: newBranch.Tip.Committer);
}
}

Expand Down Expand Up @@ -725,13 +791,23 @@ public void CanMoveABranch()
string path = CloneBareTestRepo();
using (var repo = new Repository(path))
{
EnableRefLog(repo);

Assert.Null(repo.Branches["br3"]);
var br2 = repo.Branches["br2"];
Assert.NotNull(br2);

Branch newBranch = repo.Branches.Move("br2", "br3");

Assert.Equal("br3", newBranch.Name);

Assert.Null(repo.Branches["br2"]);
Assert.NotNull(repo.Branches["br3"]);

AssertRefLogEntry(repo, newBranch.CanonicalName,
newBranch.Tip.Id,
string.Format("Branch: renamed {0} to {1}", br2.CanonicalName, newBranch.CanonicalName),
committer: newBranch.Tip.Committer);
}
}

Expand All @@ -750,6 +826,8 @@ public void CanMoveABranchWhileOverwritingAnExistingOne()
string path = CloneBareTestRepo();
using (var repo = new Repository(path))
{
EnableRefLog(repo);

Branch test = repo.Branches["test"];
Assert.NotNull(test);

Expand All @@ -766,6 +844,11 @@ public void CanMoveABranchWhileOverwritingAnExistingOne()
Assert.Equal(newBranch, newTest);

Assert.Equal(br2.Tip, newTest.Tip);

AssertRefLogEntry(repo, newBranch.CanonicalName,
newBranch.Tip.Id,
string.Format("Branch: renamed {0} to {1}", br2.CanonicalName, newBranch.CanonicalName),
committer: newBranch.Tip.Committer);
}
}

Expand Down
10 changes: 5 additions & 5 deletions LibGit2Sharp.Tests/CommitFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -773,11 +773,11 @@ public void CanAmendACommitWithMoreThanOneParent()

AssertCommitHasBeenAmended(repo, amendedCommit, mergedCommit);

// Assert a reflog entry is created
var reflogEntry = repo.Refs.Log("HEAD").First();
Assert.Equal(amendedCommit.Committer, reflogEntry.Commiter);
Assert.Equal(amendedCommit.Id, reflogEntry.To);
Assert.Equal(string.Format("commit (amend): {0}", commitMessage), reflogEntry.Message);
AssertRefLogEntry(repo, "HEAD",
amendedCommit.Id,
string.Format("commit (amend): {0}", commitMessage),
mergedCommit.Id,
amendedCommit.Committer);
}
}

Expand Down
45 changes: 45 additions & 0 deletions LibGit2Sharp.Tests/ConfigurationFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ public void CanReadBooleanValue()
using (var repo = new Repository(StandardTestRepoPath))
{
Assert.True(repo.Config.Get<bool>("core.ignorecase").Value);
Assert.True(repo.Config.GetValueOrDefault<bool>("core.ignorecase"));

Assert.Equal(false, repo.Config.GetValueOrDefault<bool>("missing.key"));
Assert.Equal(true, repo.Config.GetValueOrDefault<bool>("missing.key", true));
Assert.Equal(true, repo.Config.GetValueOrDefault<bool>("missing.key", () => true));
}
}

Expand All @@ -119,6 +124,12 @@ public void CanReadIntValue()
using (var repo = new Repository(StandardTestRepoPath))
{
Assert.Equal(2, repo.Config.Get<int>("unittests.intsetting").Value);
Assert.Equal(2, repo.Config.GetValueOrDefault<int>("unittests.intsetting"));
Assert.Equal(2, repo.Config.GetValueOrDefault<int>("unittests.intsetting", ConfigurationLevel.Local));

Assert.Equal(0, repo.Config.GetValueOrDefault<int>("missing.key"));
Assert.Equal(4, repo.Config.GetValueOrDefault<int>("missing.key", 4));
Assert.Equal(4, repo.Config.GetValueOrDefault<int>("missing.key", () => 4));
}
}

Expand All @@ -128,6 +139,11 @@ public void CanReadLongValue()
using (var repo = new Repository(StandardTestRepoPath))
{
Assert.Equal(15234, repo.Config.Get<long>("unittests.longsetting").Value);
Assert.Equal(15234, repo.Config.GetValueOrDefault<long>("unittests.longsetting"));

Assert.Equal(0, repo.Config.GetValueOrDefault<long>("missing.key"));
Assert.Equal(4, repo.Config.GetValueOrDefault<long>("missing.key", 4));
Assert.Equal(4, repo.Config.GetValueOrDefault<long>("missing.key", () => 4));
}
}

Expand All @@ -138,6 +154,35 @@ public void CanReadStringValue()
{
Assert.Equal("+refs/heads/*:refs/remotes/origin/*", repo.Config.Get<string>("remote.origin.fetch").Value);
Assert.Equal("+refs/heads/*:refs/remotes/origin/*", repo.Config.Get<string>("remote", "origin", "fetch").Value);

Assert.Equal("+refs/heads/*:refs/remotes/origin/*", repo.Config.GetValueOrDefault<string>("remote.origin.fetch"));
Assert.Equal("+refs/heads/*:refs/remotes/origin/*", repo.Config.GetValueOrDefault<string>("remote.origin.fetch", ConfigurationLevel.Local));
Assert.Equal("+refs/heads/*:refs/remotes/origin/*", repo.Config.GetValueOrDefault<string>("remote", "origin", "fetch"));
Assert.Equal("+refs/heads/*:refs/remotes/origin/*", repo.Config.GetValueOrDefault<string>(new[] { "remote", "origin", "fetch" }));

Assert.Equal(null, repo.Config.GetValueOrDefault<string>("missing.key"));
Assert.Equal(null, repo.Config.GetValueOrDefault<string>("missing.key", default(string)));
Assert.Throws<ArgumentNullException>(() => repo.Config.GetValueOrDefault<string>("missing.key", default(Func<string>)));
Assert.Equal("value", repo.Config.GetValueOrDefault<string>("missing.key", "value"));
Assert.Equal("value", repo.Config.GetValueOrDefault<string>("missing.key", () => "value"));

Assert.Equal(null, repo.Config.GetValueOrDefault<string>("missing.key", ConfigurationLevel.Local));
Assert.Equal(null, repo.Config.GetValueOrDefault<string>("missing.key", ConfigurationLevel.Local, default(string)));
Assert.Throws<ArgumentNullException>(() => repo.Config.GetValueOrDefault<string>("missing.key", ConfigurationLevel.Local, default(Func<string>)));
Assert.Equal("value", repo.Config.GetValueOrDefault<string>("missing.key", ConfigurationLevel.Local, "value"));
Assert.Equal("value", repo.Config.GetValueOrDefault<string>("missing.key", ConfigurationLevel.Local, () => "value"));

Assert.Equal(null, repo.Config.GetValueOrDefault<string>("missing", "config", "key"));
Assert.Equal(null, repo.Config.GetValueOrDefault<string>("missing", "config", "key", default(string)));
Assert.Throws<ArgumentNullException>(() => repo.Config.GetValueOrDefault<string>("missing", "config", "key", default(Func<string>)));
Assert.Equal("value", repo.Config.GetValueOrDefault<string>("missing", "config", "key", "value"));
Assert.Equal("value", repo.Config.GetValueOrDefault<string>("missing", "config", "key", () => "value"));

Assert.Equal(null, repo.Config.GetValueOrDefault<string>(new[] { "missing", "key" }));
Assert.Equal(null, repo.Config.GetValueOrDefault<string>(new[] { "missing", "key" }, default(string)));
Assert.Throws<ArgumentNullException>(() => repo.Config.GetValueOrDefault<string>(new[] { "missing", "key" }, default(Func<string>)));
Assert.Equal("value", repo.Config.GetValueOrDefault<string>(new[] { "missing", "key" }, "value"));
Assert.Equal("value", repo.Config.GetValueOrDefault<string>(new[] { "missing", "key" }, () => "value"));
}
}

Expand Down
2 changes: 2 additions & 0 deletions LibGit2Sharp.Tests/FilterBranchFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,8 @@ public void CanRewriteParentWithRewrittenCommit()
[Fact]
public void WritesCorrectReflogMessagesForSimpleRewrites()
{
EnableRefLog(repo);

repo.Refs.RewriteHistory(new RewriteHistoryOptions
{
CommitHeaderRewriter =
Expand Down
Loading