Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit cbab840

Browse files
committed
dotgit: add CheckAndSetReference tests
1 parent 83e7046 commit cbab840

File tree

4 files changed

+50
-13
lines changed

4 files changed

+50
-13
lines changed

plumbing/storer/reference.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ var ErrMaxResolveRecursion = errors.New("max. recursion level reached")
1616
// ReferenceStorer is a generic storage of references.
1717
type ReferenceStorer interface {
1818
SetReference(*plumbing.Reference) error
19+
// CheckAndSetReference sets the reference `new`, but if `old` is
20+
// not `nil`, it first checks that the current stored value for
21+
// `old.Name()` matches the given reference value in `old`. If
22+
// not, it returns an error and doesn't update `new`.
1923
CheckAndSetReference(new, old *plumbing.Reference) error
2024
Reference(plumbing.ReferenceName) (*plumbing.Reference, error)
2125
IterReferences() (ReferenceIter, error)

remote_test.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,8 @@ func (s *RemoteSuite) doTestFetchNoErrAlreadyUpToDate(c *C, url string) {
308308
c.Assert(err, Equals, NoErrAlreadyUpToDate)
309309
}
310310

311-
func (s *RemoteSuite) TestFetchFastForward(c *C) {
312-
r := newRemote(memory.NewStorage(), &config.RemoteConfig{
311+
func (s *RemoteSuite) testFetchFastForward(c *C, sto storage.Storer) {
312+
r := newRemote(sto, &config.RemoteConfig{
313313
URLs: []string{s.GetBasicLocalRepositoryURL()},
314314
})
315315

@@ -350,6 +350,23 @@ func (s *RemoteSuite) TestFetchFastForward(c *C) {
350350
})
351351
}
352352

353+
func (s *RemoteSuite) TestFetchFastForwardMem(c *C) {
354+
s.testFetchFastForward(c, memory.NewStorage())
355+
}
356+
357+
func (s *RemoteSuite) TestFetchFastForwardFS(c *C) {
358+
dir, err := ioutil.TempDir("", "fetch")
359+
c.Assert(err, IsNil)
360+
361+
defer os.RemoveAll(dir) // clean up
362+
363+
fss, err := filesystem.NewStorage(osfs.New(dir))
364+
c.Assert(err, IsNil)
365+
366+
// This exercises `storage.filesystem.Storage.CheckAndSetReference()`.
367+
s.testFetchFastForward(c, fss)
368+
}
369+
353370
func (s *RemoteSuite) TestString(c *C) {
354371
r := newRemote(nil, &config.RemoteConfig{
355372
Name: "foo",

storage/filesystem/internal/dotgit/dotgit_test.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ func (s *SuiteDotGit) TestSetRefs(c *C) {
5555
fs := osfs.New(tmp)
5656
dir := New(fs)
5757

58-
err = dir.SetRef(plumbing.NewReferenceFromStrings(
58+
firstFoo := plumbing.NewReferenceFromStrings(
5959
"refs/heads/foo",
6060
"e8d3ffab552895c19b9fcf7aa264d277cde33881",
61-
), nil)
61+
)
62+
err = dir.SetRef(firstFoo, nil)
6263

6364
c.Assert(err, IsNil)
6465

@@ -105,6 +106,20 @@ func (s *SuiteDotGit) TestSetRefs(c *C) {
105106
c.Assert(ref, NotNil)
106107
c.Assert(ref.Hash().String(), Equals, "e8d3ffab552895c19b9fcf7aa264d277cde33881")
107108

109+
// Check that SetRef with a non-nil `old` works.
110+
err = dir.SetRef(plumbing.NewReferenceFromStrings(
111+
"refs/heads/foo",
112+
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
113+
), firstFoo)
114+
c.Assert(err, IsNil)
115+
116+
// `firstFoo` is no longer the right `old` reference, so this
117+
// should fail.
118+
err = dir.SetRef(plumbing.NewReferenceFromStrings(
119+
"refs/heads/foo",
120+
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
121+
), firstFoo)
122+
c.Assert(err, NotNil)
108123
}
109124

110125
func (s *SuiteDotGit) TestRefsFromPackedRefs(c *C) {
@@ -192,7 +207,7 @@ func (s *SuiteDotGit) TestRemoveRefFromReferenceFileAndPackedRefs(c *C) {
192207
err := dir.SetRef(plumbing.NewReferenceFromStrings(
193208
"refs/remotes/origin/branch",
194209
"e8d3ffab552895c19b9fcf7aa264d277cde33881",
195-
))
210+
), nil)
196211

197212
// Make sure it only appears once in the refs list.
198213
refs, err := dir.Refs()

storage/memory/storage.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -204,16 +204,17 @@ func (r ReferenceStorage) SetReference(ref *plumbing.Reference) error {
204204
}
205205

206206
func (r ReferenceStorage) CheckAndSetReference(ref, old *plumbing.Reference) error {
207-
if ref != nil {
208-
if old != nil {
209-
tmp := r[ref.Name()]
210-
if tmp != nil && tmp.Hash() != old.Hash() {
211-
return ErrRefHasChanged
212-
}
213-
}
214-
r[ref.Name()] = ref
207+
if ref == nil {
208+
return nil
215209
}
216210

211+
if old != nil {
212+
tmp := r[ref.Name()]
213+
if tmp != nil && tmp.Hash() != old.Hash() {
214+
return ErrRefHasChanged
215+
}
216+
}
217+
r[ref.Name()] = ref
217218
return nil
218219
}
219220

0 commit comments

Comments
 (0)