Skip to content

Commit 8008e2d

Browse files
committed
Merge pull request #999 from libgit2/ntk/inrange
Refactor the Reflog entries datetime comparisons
2 parents 289f176 + b9a888b commit 8008e2d

11 files changed

+134
-40
lines changed

LibGit2Sharp.Tests/ArchiveFixture.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public void CanArchiveATree()
1818

1919
var archiver = new MockArchiver();
2020

21+
var before = DateTimeOffset.Now.TruncateMilliseconds();
22+
2123
repo.ObjectDatabase.Archive(tree, archiver);
2224

2325
var expected = new ArrayList
@@ -30,7 +32,7 @@ public void CanArchiveATree()
3032
};
3133
Assert.Equal(expected, archiver.Files);
3234
Assert.Null(archiver.ReceivedCommitSha);
33-
Assert.InRange(archiver.ModificationTime, DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMilliseconds(100)), DateTimeOffset.UtcNow);
35+
Assert.InRange(archiver.ModificationTime, before, DateTimeOffset.UtcNow);
3436
}
3537
}
3638

LibGit2Sharp.Tests/BranchFixture.cs

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public void CanCreateBranch(string name)
2424

2525
const string committish = "be3563ae3f795b2b4353bcce3a527ad0a4f7f644";
2626

27+
var before = DateTimeOffset.Now.TruncateMilliseconds();
28+
2729
Branch newBranch = repo.CreateBranch(name, committish);
2830
Assert.NotNull(newBranch);
2931
Assert.Equal(name, newBranch.FriendlyName);
@@ -42,7 +44,7 @@ public void CanCreateBranch(string name)
4244
"branch: Created from " + committish,
4345
null,
4446
newBranch.Tip.Id,
45-
Constants.Identity, DateTimeOffset.Now);
47+
Constants.Identity, before);
4648

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

100+
var before = DateTimeOffset.Now.TruncateMilliseconds();
101+
98102
Branch newBranch = repo.CreateBranch(name, committish);
99103
Assert.Equal("refs/heads/" + name, newBranch.CanonicalName);
100104
Assert.Equal("be3563ae3f795b2b4353bcce3a527ad0a4f7f644", newBranch.Tip.Sha);
@@ -103,7 +107,7 @@ public void CanCreateBranchUsingAbbreviatedSha()
103107
"branch: Created from " + committish,
104108
null,
105109
newBranch.Tip.Id,
106-
Constants.Identity, DateTimeOffset.Now);
110+
Constants.Identity, before);
107111
}
108112
}
109113

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

122126
const string name = "unit_test";
127+
128+
var before = DateTimeOffset.Now.TruncateMilliseconds();
129+
123130
Branch newBranch = repo.CreateBranch(name);
124131
Assert.NotNull(newBranch);
125132
Assert.Equal(name, newBranch.FriendlyName);
@@ -133,7 +140,7 @@ public void CanCreateBranchFromImplicitHead(string headCommitOrBranchSpec)
133140
"branch: Created from " + headCommitOrBranchSpec,
134141
null,
135142
newBranch.Tip.Id,
136-
Constants.Identity, DateTimeOffset.Now);
143+
Constants.Identity, before);
137144
}
138145
}
139146

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

152159
const string name = "unit_test";
160+
161+
var before = DateTimeOffset.Now.TruncateMilliseconds();
162+
153163
Branch newBranch = repo.CreateBranch(name, "HEAD");
154164
Assert.NotNull(newBranch);
155165
Assert.Equal("32eab9cb1f450b5fe7ab663462b77d7f4b703344", newBranch.Tip.Sha);
@@ -158,7 +168,7 @@ public void CanCreateBranchFromExplicitHead(string headCommitOrBranchSpec)
158168
"branch: Created from HEAD",
159169
null,
160170
newBranch.Tip.Id,
161-
Constants.Identity, DateTimeOffset.Now);
171+
Constants.Identity, before);
162172
}
163173
}
164174

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

173183
const string name = "unit_test";
174184
var commit = repo.Lookup<Commit>("HEAD");
185+
186+
var before = DateTimeOffset.Now.TruncateMilliseconds();
187+
175188
Branch newBranch = repo.CreateBranch(name, commit);
176189
Assert.NotNull(newBranch);
177190
Assert.Equal("4c062a6361ae6959e06292c1fa5e2822d9c96345", newBranch.Tip.Sha);
@@ -180,7 +193,7 @@ public void CanCreateBranchFromCommit()
180193
"branch: Created from " + newBranch.Tip.Sha,
181194
null,
182195
newBranch.Tip.Id,
183-
Constants.Identity, DateTimeOffset.Now);
196+
Constants.Identity, before);
184197
}
185198
}
186199

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

211+
var before = DateTimeOffset.Now.TruncateMilliseconds();
212+
198213
Branch newBranch = repo.CreateBranch(name, committish);
199214
Assert.NotNull(newBranch);
200215
Assert.Equal("9fd738e8f7967c078dceed8190330fc8648ee56a", newBranch.Tip.Sha);
@@ -203,7 +218,7 @@ public void CanCreateBranchFromRevparseSpec()
203218
"branch: Created from " + committish,
204219
null,
205220
newBranch.Tip.Id,
206-
Constants.Identity, DateTimeOffset.Now);
221+
Constants.Identity, before);
207222
}
208223
}
209224

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

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

237+
var before = DateTimeOffset.Now.TruncateMilliseconds();
238+
222239
Branch newBranch = repo.CreateBranch(name, committish);
223240
Assert.NotNull(newBranch);
224241
Assert.Equal("e90810b8df3e80c413d903f631643c716887138d", newBranch.Tip.Sha);
@@ -227,7 +244,7 @@ public void CreatingABranchFromATagPeelsToTheCommit(string committish)
227244
"branch: Created from " + committish,
228245
null,
229246
newBranch.Tip.Id,
230-
Constants.Identity, DateTimeOffset.Now);
247+
Constants.Identity, before);
231248
}
232249
}
233250

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

1006+
var before = DateTimeOffset.Now.TruncateMilliseconds();
1007+
9891008
Branch newBranch = repo.Branches.Rename("br2", "br3");
9901009

9911010
Assert.Equal("br3", newBranch.FriendlyName);
@@ -997,7 +1016,7 @@ public void CanRenameABranch()
9971016
string.Format("branch: renamed {0} to {1}", br2.CanonicalName, newBranch.CanonicalName),
9981017
br2.Tip.Id,
9991018
newBranch.Tip.Id,
1000-
Constants.Identity, DateTimeOffset.Now);
1019+
Constants.Identity, before);
10011020
}
10021021
}
10031022

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

1047+
var before = DateTimeOffset.Now.TruncateMilliseconds();
1048+
10281049
Branch newBranch = repo.Branches.Rename("br2", "test", true);
10291050
Assert.Equal("test", newBranch.FriendlyName);
10301051

@@ -1040,7 +1061,7 @@ public void CanRenameABranchWhileOverwritingAnExistingOne()
10401061
string.Format("branch: renamed {0} to {1}", br2.CanonicalName, newBranch.CanonicalName),
10411062
br2.Tip.Id,
10421063
newTest.Tip.Id,
1043-
Constants.Identity, DateTimeOffset.Now);
1064+
Constants.Identity, before);
10441065
}
10451066
}
10461067

@@ -1149,19 +1170,24 @@ public void CreatingABranchIncludesTheCorrectReflogEntries()
11491170
using (var repo = new Repository(path, new RepositoryOptions { Identity = Constants.Identity }))
11501171
{
11511172
EnableRefLog(repo);
1173+
1174+
var before = DateTimeOffset.Now.TruncateMilliseconds();
1175+
11521176
var branch = repo.Branches.Add("foo", repo.Head.Tip);
11531177

11541178
AssertRefLogEntry(repo, branch.CanonicalName,
11551179
string.Format("branch: Created from {0}", repo.Head.Tip.Sha),
11561180
null, branch.Tip.Id,
1157-
Constants.Identity, DateTimeOffset.Now);
1181+
Constants.Identity, before);
1182+
1183+
before = DateTimeOffset.Now.TruncateMilliseconds();
11581184

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

11611187
AssertRefLogEntry(repo, branch.CanonicalName,
11621188
"branch: Created from " + repo.Head.Tip.Sha,
11631189
null, repo.Head.Tip.Id,
1164-
Constants.Identity, DateTimeOffset.Now);
1190+
Constants.Identity, before);
11651191
}
11661192
}
11671193

@@ -1173,15 +1199,20 @@ public void RenamingABranchIncludesTheCorrectReflogEntries()
11731199
{
11741200
EnableRefLog(repo);
11751201
var master = repo.Branches["master"];
1202+
1203+
var before = DateTimeOffset.Now.TruncateMilliseconds();
1204+
11761205
var newMaster = repo.Branches.Rename(master, "new-master");
11771206
AssertRefLogEntry(repo, newMaster.CanonicalName, "branch: renamed refs/heads/master to refs/heads/new-master",
11781207
newMaster.Tip.Id, newMaster.Tip.Id,
1179-
Constants.Identity, DateTimeOffset.Now);
1208+
Constants.Identity, before);
1209+
1210+
before = DateTimeOffset.Now.TruncateMilliseconds();
11801211

11811212
var newMaster2 = repo.Branches.Rename(newMaster, "new-master2");
11821213
AssertRefLogEntry(repo, newMaster2.CanonicalName, "branch: renamed refs/heads/new-master to refs/heads/new-master2",
11831214
newMaster.Tip.Id, newMaster2.Tip.Id,
1184-
Constants.Identity, DateTimeOffset.Now);
1215+
Constants.Identity, before);
11851216
}
11861217
}
11871218
}

LibGit2Sharp.Tests/CheckoutFixture.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -789,12 +789,14 @@ public void CheckoutBranchFromDetachedHead()
789789

790790
Assert.True(repo.Info.IsHeadDetached);
791791

792+
var before = DateTimeOffset.Now.TruncateMilliseconds();
793+
792794
Branch newHead = repo.Checkout(repo.Branches["master"]);
793795

794796
// Assert reflog entry is created
795797
AssertRefLogEntry(repo, "HEAD",
796798
string.Format("checkout: moving from {0} to {1}", initialHead.Tip.Sha, newHead.FriendlyName),
797-
initialHead.Tip.Id, newHead.Tip.Id, Constants.Identity, DateTimeOffset.Now);
799+
initialHead.Tip.Id, newHead.Tip.Id, Constants.Identity, before);
798800
}
799801
}
800802

@@ -862,12 +864,15 @@ public void CheckoutCurrentReference()
862864

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

867+
var before = DateTimeOffset.Now.TruncateMilliseconds();
868+
865869
// Checkout in detached mode
866870
repo.Checkout(master.Tip.Sha);
867871

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

872877
// Checkout detached "HEAD" => nothing should happen
873878
reflogEntriesCount = repo.Refs.Log(repo.Refs.Head).Count();

LibGit2Sharp.Tests/CommitFixture.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,8 @@ public void CanCommitALittleBit()
682682
const string shortMessage = "Initial egotistic commit";
683683
const string commitMessage = shortMessage + "\n\nOnly the coolest commits from us";
684684

685+
var before = DateTimeOffset.Now.TruncateMilliseconds();
686+
685687
Commit commit = repo.Commit(commitMessage, author, author);
686688

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

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

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

829+
var before = DateTimeOffset.Now.TruncateMilliseconds();
830+
827831
Commit amendedCommit = repo.Commit(commitMessage, Constants.Signature, Constants.Signature,
828832
new CommitOptions { AmendPreviousCommit = true });
829833

@@ -833,7 +837,7 @@ public void CanAmendACommitWithMoreThanOneParent()
833837
string.Format("commit (amend): {0}", commitMessage),
834838
mergedCommit.Id,
835839
amendedCommit.Id,
836-
Constants.Identity, DateTimeOffset.Now);
840+
Constants.Identity, before);
837841
}
838842
}
839843

LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
<Compile Include="ReferenceFixture.cs" />
116116
<Compile Include="RepositoryFixture.cs" />
117117
<Compile Include="TagFixture.cs" />
118+
<Compile Include="TestHelpers\DateTimeOffsetExtensions.cs" />
118119
<Compile Include="TestHelpers\OdbHelper.cs" />
119120
<Compile Include="TestHelpers\DirectoryHelper.cs" />
120121
<Compile Include="TestHelpers\ExpectedFetchState.cs" />

LibGit2Sharp.Tests/PushFixture.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,17 @@ public void CanForcePush()
182182

183183
// Force push the new commit
184184
string pushRefSpec = string.Format("+{0}:{0}", localRepo.Head.CanonicalName);
185+
186+
var before = DateTimeOffset.Now.TruncateMilliseconds();
187+
185188
localRepo.Network.Push(localRepo.Network.Remotes.Single(), pushRefSpec);
186189

187190
AssertRemoteHeadTipEquals(localRepo, second.Sha);
188191

189192
AssertRefLogEntry(localRepo, "refs/remotes/origin/master",
190193
"update by push",
191194
oldId, localRepo.Head.Tip.Id,
192-
Constants.Identity, DateTimeOffset.Now);
195+
Constants.Identity, before);
193196
}
194197
}
195198

0 commit comments

Comments
 (0)