Skip to content

Refactor the Reflog entries datetime comparisons #999

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 1 commit into from
Jun 10, 2015
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
4 changes: 3 additions & 1 deletion LibGit2Sharp.Tests/ArchiveFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public void CanArchiveATree()

var archiver = new MockArchiver();

var before = DateTimeOffset.Now.TruncateMilliseconds();

repo.ObjectDatabase.Archive(tree, archiver);

var expected = new ArrayList
Expand All @@ -30,7 +32,7 @@ public void CanArchiveATree()
};
Assert.Equal(expected, archiver.Files);
Assert.Null(archiver.ReceivedCommitSha);
Assert.InRange(archiver.ModificationTime, DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMilliseconds(100)), DateTimeOffset.UtcNow);
Assert.InRange(archiver.ModificationTime, before, DateTimeOffset.UtcNow);
}
}

Expand Down
57 changes: 44 additions & 13 deletions LibGit2Sharp.Tests/BranchFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public void CanCreateBranch(string name)

const string committish = "be3563ae3f795b2b4353bcce3a527ad0a4f7f644";

var before = DateTimeOffset.Now.TruncateMilliseconds();

Branch newBranch = repo.CreateBranch(name, committish);
Assert.NotNull(newBranch);
Assert.Equal(name, newBranch.FriendlyName);
Expand All @@ -42,7 +44,7 @@ public void CanCreateBranch(string name)
"branch: Created from " + committish,
null,
newBranch.Tip.Id,
Constants.Identity, DateTimeOffset.Now);
Constants.Identity, before);

repo.Branches.Remove(newBranch.FriendlyName);
Assert.Null(repo.Branches[name]);
Expand Down Expand Up @@ -95,6 +97,8 @@ public void CanCreateBranchUsingAbbreviatedSha()
const string name = "unit_test";
const string committish = "be3563a";

var before = DateTimeOffset.Now.TruncateMilliseconds();

Branch newBranch = repo.CreateBranch(name, committish);
Assert.Equal("refs/heads/" + name, newBranch.CanonicalName);
Assert.Equal("be3563ae3f795b2b4353bcce3a527ad0a4f7f644", newBranch.Tip.Sha);
Expand All @@ -103,7 +107,7 @@ public void CanCreateBranchUsingAbbreviatedSha()
"branch: Created from " + committish,
null,
newBranch.Tip.Id,
Constants.Identity, DateTimeOffset.Now);
Constants.Identity, before);
}
}

Expand All @@ -120,6 +124,9 @@ public void CanCreateBranchFromImplicitHead(string headCommitOrBranchSpec)
repo.Checkout(headCommitOrBranchSpec);

const string name = "unit_test";

var before = DateTimeOffset.Now.TruncateMilliseconds();

Branch newBranch = repo.CreateBranch(name);
Assert.NotNull(newBranch);
Assert.Equal(name, newBranch.FriendlyName);
Expand All @@ -133,7 +140,7 @@ public void CanCreateBranchFromImplicitHead(string headCommitOrBranchSpec)
"branch: Created from " + headCommitOrBranchSpec,
null,
newBranch.Tip.Id,
Constants.Identity, DateTimeOffset.Now);
Constants.Identity, before);
}
}

Expand All @@ -150,6 +157,9 @@ public void CanCreateBranchFromExplicitHead(string headCommitOrBranchSpec)
repo.Checkout(headCommitOrBranchSpec);

const string name = "unit_test";

var before = DateTimeOffset.Now.TruncateMilliseconds();

Branch newBranch = repo.CreateBranch(name, "HEAD");
Assert.NotNull(newBranch);
Assert.Equal("32eab9cb1f450b5fe7ab663462b77d7f4b703344", newBranch.Tip.Sha);
Expand All @@ -158,7 +168,7 @@ public void CanCreateBranchFromExplicitHead(string headCommitOrBranchSpec)
"branch: Created from HEAD",
null,
newBranch.Tip.Id,
Constants.Identity, DateTimeOffset.Now);
Constants.Identity, before);
}
}

Expand All @@ -172,6 +182,9 @@ public void CanCreateBranchFromCommit()

const string name = "unit_test";
var commit = repo.Lookup<Commit>("HEAD");

var before = DateTimeOffset.Now.TruncateMilliseconds();

Branch newBranch = repo.CreateBranch(name, commit);
Assert.NotNull(newBranch);
Assert.Equal("4c062a6361ae6959e06292c1fa5e2822d9c96345", newBranch.Tip.Sha);
Expand All @@ -180,7 +193,7 @@ public void CanCreateBranchFromCommit()
"branch: Created from " + newBranch.Tip.Sha,
null,
newBranch.Tip.Id,
Constants.Identity, DateTimeOffset.Now);
Constants.Identity, before);
}
}

Expand All @@ -195,6 +208,8 @@ public void CanCreateBranchFromRevparseSpec()
const string name = "revparse_branch";
const string committish = "master~2";

var before = DateTimeOffset.Now.TruncateMilliseconds();

Branch newBranch = repo.CreateBranch(name, committish);
Assert.NotNull(newBranch);
Assert.Equal("9fd738e8f7967c078dceed8190330fc8648ee56a", newBranch.Tip.Sha);
Expand All @@ -203,7 +218,7 @@ public void CanCreateBranchFromRevparseSpec()
"branch: Created from " + committish,
null,
newBranch.Tip.Id,
Constants.Identity, DateTimeOffset.Now);
Constants.Identity, before);
}
}

Expand All @@ -219,6 +234,8 @@ public void CreatingABranchFromATagPeelsToTheCommit(string committish)

const string name = "i-peel-tag";

var before = DateTimeOffset.Now.TruncateMilliseconds();

Branch newBranch = repo.CreateBranch(name, committish);
Assert.NotNull(newBranch);
Assert.Equal("e90810b8df3e80c413d903f631643c716887138d", newBranch.Tip.Sha);
Expand All @@ -227,7 +244,7 @@ public void CreatingABranchFromATagPeelsToTheCommit(string committish)
"branch: Created from " + committish,
null,
newBranch.Tip.Id,
Constants.Identity, DateTimeOffset.Now);
Constants.Identity, before);
}
}

Expand Down Expand Up @@ -986,6 +1003,8 @@ public void CanRenameABranch()
var br2 = repo.Branches["br2"];
Assert.NotNull(br2);

var before = DateTimeOffset.Now.TruncateMilliseconds();

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

Assert.Equal("br3", newBranch.FriendlyName);
Expand All @@ -997,7 +1016,7 @@ public void CanRenameABranch()
string.Format("branch: renamed {0} to {1}", br2.CanonicalName, newBranch.CanonicalName),
br2.Tip.Id,
newBranch.Tip.Id,
Constants.Identity, DateTimeOffset.Now);
Constants.Identity, before);
}
}

Expand Down Expand Up @@ -1025,6 +1044,8 @@ public void CanRenameABranchWhileOverwritingAnExistingOne()
Branch br2 = repo.Branches["br2"];
Assert.NotNull(br2);

var before = DateTimeOffset.Now.TruncateMilliseconds();

Branch newBranch = repo.Branches.Rename("br2", "test", true);
Assert.Equal("test", newBranch.FriendlyName);

Expand All @@ -1040,7 +1061,7 @@ public void CanRenameABranchWhileOverwritingAnExistingOne()
string.Format("branch: renamed {0} to {1}", br2.CanonicalName, newBranch.CanonicalName),
br2.Tip.Id,
newTest.Tip.Id,
Constants.Identity, DateTimeOffset.Now);
Constants.Identity, before);
}
}

Expand Down Expand Up @@ -1149,19 +1170,24 @@ public void CreatingABranchIncludesTheCorrectReflogEntries()
using (var repo = new Repository(path, new RepositoryOptions { Identity = Constants.Identity }))
{
EnableRefLog(repo);

var before = DateTimeOffset.Now.TruncateMilliseconds();

var branch = repo.Branches.Add("foo", repo.Head.Tip);

AssertRefLogEntry(repo, branch.CanonicalName,
string.Format("branch: Created from {0}", repo.Head.Tip.Sha),
null, branch.Tip.Id,
Constants.Identity, DateTimeOffset.Now);
Constants.Identity, before);

before = DateTimeOffset.Now.TruncateMilliseconds();

branch = repo.Branches.Add("bar", repo.Head.Tip);

AssertRefLogEntry(repo, branch.CanonicalName,
"branch: Created from " + repo.Head.Tip.Sha,
null, repo.Head.Tip.Id,
Constants.Identity, DateTimeOffset.Now);
Constants.Identity, before);
}
}

Expand All @@ -1173,15 +1199,20 @@ public void RenamingABranchIncludesTheCorrectReflogEntries()
{
EnableRefLog(repo);
var master = repo.Branches["master"];

var before = DateTimeOffset.Now.TruncateMilliseconds();

var newMaster = repo.Branches.Rename(master, "new-master");
AssertRefLogEntry(repo, newMaster.CanonicalName, "branch: renamed refs/heads/master to refs/heads/new-master",
newMaster.Tip.Id, newMaster.Tip.Id,
Constants.Identity, DateTimeOffset.Now);
Constants.Identity, before);

before = DateTimeOffset.Now.TruncateMilliseconds();

var newMaster2 = repo.Branches.Rename(newMaster, "new-master2");
AssertRefLogEntry(repo, newMaster2.CanonicalName, "branch: renamed refs/heads/new-master to refs/heads/new-master2",
newMaster.Tip.Id, newMaster2.Tip.Id,
Constants.Identity, DateTimeOffset.Now);
Constants.Identity, before);
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions LibGit2Sharp.Tests/CheckoutFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -789,12 +789,14 @@ public void CheckoutBranchFromDetachedHead()

Assert.True(repo.Info.IsHeadDetached);

var before = DateTimeOffset.Now.TruncateMilliseconds();

Branch newHead = repo.Checkout(repo.Branches["master"]);

// Assert reflog entry is created
AssertRefLogEntry(repo, "HEAD",
string.Format("checkout: moving from {0} to {1}", initialHead.Tip.Sha, newHead.FriendlyName),
initialHead.Tip.Id, newHead.Tip.Id, Constants.Identity, DateTimeOffset.Now);
initialHead.Tip.Id, newHead.Tip.Id, Constants.Identity, before);
}
}

Expand Down Expand Up @@ -862,12 +864,15 @@ public void CheckoutCurrentReference()

Assert.Equal(reflogEntriesCount, repo.Refs.Log(repo.Refs.Head).Count());

var before = DateTimeOffset.Now.TruncateMilliseconds();

// Checkout in detached mode
repo.Checkout(master.Tip.Sha);

Assert.True(repo.Info.IsHeadDetached);
AssertRefLogEntry(repo, "HEAD",
string.Format("checkout: moving from master to {0}", master.Tip.Sha), master.Tip.Id, master.Tip.Id, Constants.Identity, DateTimeOffset.Now);
string.Format("checkout: moving from master to {0}", master.Tip.Sha),
master.Tip.Id, master.Tip.Id, Constants.Identity, before);

// Checkout detached "HEAD" => nothing should happen
reflogEntriesCount = repo.Refs.Log(repo.Refs.Head).Count();
Expand Down
8 changes: 6 additions & 2 deletions LibGit2Sharp.Tests/CommitFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,8 @@ public void CanCommitALittleBit()
const string shortMessage = "Initial egotistic commit";
const string commitMessage = shortMessage + "\n\nOnly the coolest commits from us";

var before = DateTimeOffset.Now.TruncateMilliseconds();

Commit commit = repo.Commit(commitMessage, author, author);

AssertBlobContent(repo.Head[relativeFilepath], "nulltoken\n");
Expand All @@ -698,7 +700,7 @@ public void CanCommitALittleBit()
Assert.Equal(identity.Email, reflogEntry.Committer.Email);

var now = DateTimeOffset.Now;
Assert.InRange(reflogEntry.Committer.When, now - TimeSpan.FromSeconds(1), now);
Assert.InRange(reflogEntry.Committer.When, before, now);

Assert.Equal(commit.Id, reflogEntry.To);
Assert.Equal(ObjectId.Zero, reflogEntry.From);
Expand Down Expand Up @@ -824,6 +826,8 @@ public void CanAmendACommitWithMoreThanOneParent()
CreateAndStageANewFile(repo);
const string commitMessage = "I'm rewriting the history!";

var before = DateTimeOffset.Now.TruncateMilliseconds();

Commit amendedCommit = repo.Commit(commitMessage, Constants.Signature, Constants.Signature,
new CommitOptions { AmendPreviousCommit = true });

Expand All @@ -833,7 +837,7 @@ public void CanAmendACommitWithMoreThanOneParent()
string.Format("commit (amend): {0}", commitMessage),
mergedCommit.Id,
amendedCommit.Id,
Constants.Identity, DateTimeOffset.Now);
Constants.Identity, before);
}
}

Expand Down
1 change: 1 addition & 0 deletions LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
<Compile Include="ReferenceFixture.cs" />
<Compile Include="RepositoryFixture.cs" />
<Compile Include="TagFixture.cs" />
<Compile Include="TestHelpers\DateTimeOffsetExtensions.cs" />
<Compile Include="TestHelpers\OdbHelper.cs" />
<Compile Include="TestHelpers\DirectoryHelper.cs" />
<Compile Include="TestHelpers\ExpectedFetchState.cs" />
Expand Down
5 changes: 4 additions & 1 deletion LibGit2Sharp.Tests/PushFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,17 @@ public void CanForcePush()

// Force push the new commit
string pushRefSpec = string.Format("+{0}:{0}", localRepo.Head.CanonicalName);

var before = DateTimeOffset.Now.TruncateMilliseconds();

localRepo.Network.Push(localRepo.Network.Remotes.Single(), pushRefSpec);

AssertRemoteHeadTipEquals(localRepo, second.Sha);

AssertRefLogEntry(localRepo, "refs/remotes/origin/master",
"update by push",
oldId, localRepo.Head.Tip.Id,
Constants.Identity, DateTimeOffset.Now);
Constants.Identity, before);
}
}

Expand Down
Loading