2020-02-15 22:13:50 +00:00
|
|
|
package dehub
|
|
|
|
|
|
|
|
import (
|
2020-04-24 19:33:33 +00:00
|
|
|
"errors"
|
|
|
|
"regexp"
|
2020-02-15 22:13:50 +00:00
|
|
|
"testing"
|
2020-04-05 18:28:32 +00:00
|
|
|
|
2020-04-24 19:33:33 +00:00
|
|
|
"dehub.dev/src/dehub.git/accessctl"
|
|
|
|
"dehub.dev/src/dehub.git/sigcred"
|
|
|
|
"gopkg.in/src-d/go-git.v4"
|
2020-04-05 18:28:32 +00:00
|
|
|
"gopkg.in/src-d/go-git.v4/plumbing"
|
2020-02-15 22:13:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestConfigChange(t *testing.T) {
|
|
|
|
h := newHarness(t)
|
2020-04-18 18:05:56 +00:00
|
|
|
rootSig := h.stageNewAccount("root", false)
|
2020-02-15 22:13:50 +00:00
|
|
|
|
2020-04-26 20:23:03 +00:00
|
|
|
var commits []Commit
|
2020-02-15 22:13:50 +00:00
|
|
|
|
|
|
|
// commit the initial staged changes, which merely include the config and
|
|
|
|
// public key
|
2020-04-26 20:23:03 +00:00
|
|
|
commit := h.assertCommitChange(verifyShouldSucceed, "commit configuration", rootSig)
|
|
|
|
commits = append(commits, commit)
|
2020-02-15 22:13:50 +00:00
|
|
|
|
2020-02-29 20:02:25 +00:00
|
|
|
// create a new account and add it to the configuration. That commit should
|
|
|
|
// not be verifiable, though
|
2020-04-18 18:05:56 +00:00
|
|
|
tootSig := h.stageNewAccount("toot", false)
|
2020-03-14 22:14:18 +00:00
|
|
|
h.stageCfg()
|
2020-04-24 19:33:33 +00:00
|
|
|
h.assertCommitChange(verifyShouldFail, "add toot user", tootSig)
|
2020-02-15 22:13:50 +00:00
|
|
|
|
|
|
|
// now add with the root user, this should work.
|
2020-03-14 22:14:18 +00:00
|
|
|
h.stageCfg()
|
2020-04-26 20:23:03 +00:00
|
|
|
commit = h.assertCommitChange(verifyShouldSucceed, "add toot user", rootSig)
|
|
|
|
commits = append(commits, commit)
|
2020-02-15 22:13:50 +00:00
|
|
|
|
|
|
|
// _now_ the toot user should be able to do things.
|
|
|
|
h.stage(map[string]string{"foo/bar": "what a cool file"})
|
2020-04-26 20:23:03 +00:00
|
|
|
commit = h.assertCommitChange(verifyShouldSucceed, "add a cool file", tootSig)
|
|
|
|
commits = append(commits, commit)
|
2020-02-15 22:13:50 +00:00
|
|
|
|
2020-04-26 20:23:03 +00:00
|
|
|
if err := h.proj.VerifyCommits(MainRefName, commits); err != nil {
|
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
---
type: change
message: |-
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
This commit attempts to normalize git commit interactions in order to reduce
the amount of manual `GitRepo.CommitObject`, `GitRepo.TreeObject`,
`Commit.UnmarshalText`, and `Commit.Interface` calls are done, by creating a
single structure (`GitCommit`) which holds the output of those calls, and is
only created by a single method (`GetGitCommit`), which is then used by a bunch
of other methods to expand its functionality, including implementing a range
request which can be used by verify and the pre-receive hook (though it's only
used by the hook, currently).
change_hash: AMae4PL6+jrxhn2KEGHejstcdT37Gw/jjkl/UuovHcgd
credentials:
- type: pgp_signature
pub_key_id: 95C46FA6A41148AC
body: iQIzBAABAgAdFiEEJ6tQKp6olvZKJ0lwlcRvpqQRSKwFAl5uhvoACgkQlcRvpqQRSKzJrhAAqi2LEQVTyVktfsOBv/CZmefclLLqWTChVoeIZt2EAGDDGygmrx88hI0SEAviOzPMn0kiZFDeY5k7ICJMhJ9RVDU9WjH7fbOboMJW19rVhx6Ke/M2ERtrT0OFLRmFVJVDM0P8SEheQvR3HE/iiypBICVCtp+meHEq9mOJWZlZnoCqMaulAy/Nnq4N1VD0yPPlr16+yxMqedKHcgKbcH8K61ltNAjXDT+tCWwCq1huA5MVSuTm5EwqIeKPN6JKgwATv8Ku2GhYZWHSGUwecP1J3x2XTDPeChCQVDpC232Pxwk8z/D36F3J/XOfkdl0QYQ077xL1IJfYOnuuHir47CokDf3G0XCQnJ/+X4pZdtP387rc045o/2bhUi2U4eJ5HgS7Hvyi6EApT0Czv7SeJePTvdnRUYse8ZYuIwYXj5GWWxnbKQzLpyjcHdQc2a3B3RN84zXqqAOS6ObFrFPZQIfz2rfQojZN8kvcmUvYhJXSaT65XmqFjyJ4n6grrEnK/N+MfbnpzyF/yvlzxWPqGFQOQj9meosbTAdgZbmdwYqa5r1ee8DmlkzNJJxze96h503a733yciN8Ef4hGZNlRV6YFegkK/cCgKaA4NCEALKb1t0Uri5gnPldXk4HsPF+23GANbE7mjytY8ra3fhXG4VhaFt/WsLg3Bu7djQ0H74y+g=
account: mediocregopher
2020-03-15 19:50:24 +00:00
|
|
|
t.Fatal(err)
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-05 18:28:32 +00:00
|
|
|
|
|
|
|
func TestMainAncestryRequirement(t *testing.T) {
|
|
|
|
otherBranch := plumbing.NewBranchReferenceName("other")
|
|
|
|
t.Run("empty repo", func(t *testing.T) {
|
|
|
|
h := newHarness(t)
|
2020-04-18 18:05:56 +00:00
|
|
|
rootSig := h.stageNewAccount("root", false)
|
2020-04-05 18:28:32 +00:00
|
|
|
h.checkout(otherBranch)
|
|
|
|
|
|
|
|
// stage and try to add to the "other" branch, it shouldn't work though
|
|
|
|
h.stageCfg()
|
2020-04-24 19:33:33 +00:00
|
|
|
h.assertCommitChange(verifyShouldFail, "starting new branch at other", rootSig)
|
2020-04-05 18:28:32 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("new branch, single commit", func(t *testing.T) {
|
|
|
|
h := newHarness(t)
|
2020-04-18 18:05:56 +00:00
|
|
|
rootSig := h.stageNewAccount("root", false)
|
2020-04-24 19:33:33 +00:00
|
|
|
h.assertCommitChange(verifyShouldSucceed, "add cfg", rootSig)
|
2020-04-05 18:28:32 +00:00
|
|
|
|
|
|
|
// set HEAD to this other branch which doesn't really exist
|
|
|
|
ref := plumbing.NewSymbolicReference(plumbing.HEAD, otherBranch)
|
2020-04-26 20:23:03 +00:00
|
|
|
if err := h.proj.GitRepo.Storer.SetReference(ref); err != nil {
|
2020-04-05 18:28:32 +00:00
|
|
|
h.t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
h.stageCfg()
|
2020-04-24 19:33:33 +00:00
|
|
|
h.assertCommitChange(verifyShouldFail, "starting new branch at other", rootSig)
|
2020-04-05 18:28:32 +00:00
|
|
|
})
|
|
|
|
}
|
2020-04-11 23:10:18 +00:00
|
|
|
|
|
|
|
func TestAnonymousCommits(t *testing.T) {
|
|
|
|
h := newHarness(t)
|
2020-04-18 18:05:56 +00:00
|
|
|
anonSig := h.stageNewAccount("anon", true)
|
|
|
|
|
|
|
|
h.stageAccessControls(`
|
|
|
|
- action: allow
|
|
|
|
filters:
|
|
|
|
- type: signature
|
|
|
|
any: true
|
|
|
|
`)
|
2020-04-24 19:33:33 +00:00
|
|
|
h.assertCommitChange(verifyShouldSucceed, "this will work", anonSig)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNonFastForwardCommits(t *testing.T) {
|
|
|
|
h := newHarness(t)
|
|
|
|
rootSig := h.stageNewAccount("root", false)
|
|
|
|
initCommit := h.assertCommitChange(verifyShouldSucceed, "init", rootSig)
|
|
|
|
|
|
|
|
// add another commit
|
|
|
|
h.stage(map[string]string{"foo": "foo"})
|
|
|
|
fooCommit := h.assertCommitChange(verifyShouldSucceed, "foo", rootSig)
|
|
|
|
|
2020-04-26 20:23:03 +00:00
|
|
|
commitOn := func(hash plumbing.Hash, msg string) Commit {
|
2020-04-24 19:33:33 +00:00
|
|
|
ref := plumbing.NewHashReference(plumbing.HEAD, hash)
|
2020-04-26 20:23:03 +00:00
|
|
|
if err := h.proj.GitRepo.Storer.SetReference(ref); err != nil {
|
2020-04-24 19:33:33 +00:00
|
|
|
h.t.Fatal(err)
|
2020-04-26 20:23:03 +00:00
|
|
|
} else if commitChange, err := h.proj.NewPayloadChange("bar"); err != nil {
|
2020-04-24 19:33:33 +00:00
|
|
|
h.t.Fatal(err)
|
2020-04-26 20:23:03 +00:00
|
|
|
} else if commitChange, err = h.proj.AccreditPayload(commitChange, rootSig); err != nil {
|
2020-04-24 19:33:33 +00:00
|
|
|
h.t.Fatal(err)
|
2020-04-26 20:23:03 +00:00
|
|
|
} else if gitCommit, err := h.proj.Commit(commitChange); err != nil {
|
2020-04-24 19:33:33 +00:00
|
|
|
h.t.Fatal(err)
|
|
|
|
} else {
|
|
|
|
return gitCommit
|
|
|
|
}
|
|
|
|
panic("can't get here")
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkout initCommit directly, make a new commit on top of it, and try to
|
|
|
|
// verify that (this is too fancy for the harness, must be done manually).
|
|
|
|
h.stage(map[string]string{"bar": "bar"})
|
2020-04-26 20:23:03 +00:00
|
|
|
barCommit := commitOn(initCommit.Hash, "bar")
|
|
|
|
err := h.proj.VerifyCommits(MainRefName, []Commit{barCommit})
|
2020-04-24 19:33:33 +00:00
|
|
|
if !errors.As(err, new(accessctl.ErrCommitRequestDenied)) {
|
|
|
|
h.t.Fatalf("expected ErrCommitRequestDenied, got: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check main back out (fooCommit should be checked out), and modify the
|
|
|
|
// config to allow nonFF commits, and add another bogus commit on top.
|
|
|
|
h.checkout(MainRefName)
|
|
|
|
h.stageAccessControls(`
|
|
|
|
- action: allow
|
|
|
|
filters:
|
|
|
|
- type: commit_attributes
|
|
|
|
non_fast_forward: true`)
|
|
|
|
h.stageCfg()
|
|
|
|
allowNonFFCommit := h.assertCommitChange(verifyShouldSucceed, "allow non-ff", rootSig)
|
|
|
|
|
|
|
|
h.stage(map[string]string{"foo": "foo foo"})
|
|
|
|
h.assertCommitChange(verifyShouldSucceed, "foo foo", rootSig)
|
|
|
|
|
|
|
|
// checking out allowNonFFCommit directly and performing a nonFF commit
|
|
|
|
// should work now.
|
|
|
|
h.stage(map[string]string{"baz": "baz"})
|
2020-04-26 20:23:03 +00:00
|
|
|
bazCommit := commitOn(allowNonFFCommit.Hash, "baz")
|
|
|
|
if err = h.proj.VerifyCommits(MainRefName, []Commit{bazCommit}); err != nil {
|
2020-04-24 19:33:33 +00:00
|
|
|
h.t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// verifying the full history should also work
|
2020-04-26 20:23:03 +00:00
|
|
|
gitCommits := []Commit{initCommit, fooCommit, allowNonFFCommit, bazCommit}
|
|
|
|
if err = h.proj.VerifyCommits(MainRefName, gitCommits); err != nil {
|
2020-04-24 19:33:33 +00:00
|
|
|
h.t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-02 21:28:02 +00:00
|
|
|
func TestVerifyCanSetBranchHEADTo(t *testing.T) {
|
2020-04-24 19:33:33 +00:00
|
|
|
type toTest struct {
|
|
|
|
// branchName and hash are the arguments passed into
|
|
|
|
// VerifyCanSetBranchHEADTo.
|
|
|
|
branchName plumbing.ReferenceName
|
|
|
|
hash plumbing.Hash
|
|
|
|
|
|
|
|
// if set then the branch will have its HEAD reset to this hash prior to
|
|
|
|
// calling VerifyCanSetBranchHEADTo.
|
|
|
|
resetTo plumbing.Hash
|
|
|
|
}
|
|
|
|
|
|
|
|
type test struct {
|
|
|
|
descr string
|
2020-04-26 20:23:03 +00:00
|
|
|
init func(h *harness, rootSig sigcred.Signifier) toTest
|
2020-04-24 19:33:33 +00:00
|
|
|
|
|
|
|
// If true then the verify call is expected to fail. The string is a
|
|
|
|
// regex which should match the unwrapped error returned.
|
|
|
|
expErr string
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []test{
|
|
|
|
{
|
|
|
|
descr: "creation of main",
|
2020-04-26 20:23:03 +00:00
|
|
|
init: func(h *harness, rootSig sigcred.Signifier) toTest {
|
2020-04-24 19:33:33 +00:00
|
|
|
// checkout other and build on top of that, so that when
|
|
|
|
// VerifyCanSetBranchHEADTo is called main won't exist.
|
|
|
|
other := plumbing.NewBranchReferenceName("other")
|
|
|
|
h.checkout(other)
|
|
|
|
|
|
|
|
initCommit := h.assertCommitChange(verifySkip, "init", rootSig)
|
|
|
|
return toTest{
|
|
|
|
branchName: MainRefName,
|
2020-04-26 20:23:03 +00:00
|
|
|
hash: initCommit.Hash,
|
2020-04-24 19:33:33 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "main ff",
|
2020-04-26 20:23:03 +00:00
|
|
|
init: func(h *harness, rootSig sigcred.Signifier) toTest {
|
2020-04-24 19:33:33 +00:00
|
|
|
initCommit := h.assertCommitChange(verifySkip, "init", rootSig)
|
|
|
|
h.stage(map[string]string{"foo": "foo"})
|
|
|
|
nextCommit := h.assertCommitChange(verifySkip, "next", rootSig)
|
|
|
|
return toTest{
|
|
|
|
branchName: MainRefName,
|
2020-04-26 20:23:03 +00:00
|
|
|
hash: nextCommit.Hash,
|
|
|
|
resetTo: initCommit.Hash,
|
2020-04-24 19:33:33 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "new branch, no main",
|
2020-04-26 20:23:03 +00:00
|
|
|
init: func(h *harness, rootSig sigcred.Signifier) toTest {
|
2020-04-24 19:33:33 +00:00
|
|
|
// checkout other and build on top of that, so that when
|
|
|
|
// VerifyCanSetBranchHEADTo is called main won't exist.
|
|
|
|
other := plumbing.NewBranchReferenceName("other")
|
|
|
|
h.checkout(other)
|
|
|
|
|
|
|
|
initCommit := h.assertCommitChange(verifySkip, "init", rootSig)
|
|
|
|
return toTest{
|
|
|
|
branchName: plumbing.NewBranchReferenceName("other2"),
|
2020-04-26 20:23:03 +00:00
|
|
|
hash: initCommit.Hash,
|
2020-04-24 19:33:33 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
expErr: `^cannot verify commits in branch "refs/heads/other2" when no main branch exists$`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// this case isn't generally possible, unless someone manually
|
|
|
|
// creates a branch in an empty repo on the remote
|
|
|
|
descr: "existing branch, no main",
|
2020-04-26 20:23:03 +00:00
|
|
|
init: func(h *harness, rootSig sigcred.Signifier) toTest {
|
2020-04-24 19:33:33 +00:00
|
|
|
// checkout other and build on top of that, so that when
|
|
|
|
// VerifyCanSetBranchHEADTo is called main won't exist.
|
|
|
|
other := plumbing.NewBranchReferenceName("other")
|
|
|
|
h.checkout(other)
|
|
|
|
|
|
|
|
initCommit := h.assertCommitChange(verifySkip, "init", rootSig)
|
|
|
|
h.stage(map[string]string{"foo": "foo"})
|
|
|
|
fooCommit := h.assertCommitChange(verifySkip, "foo", rootSig)
|
|
|
|
|
|
|
|
return toTest{
|
|
|
|
branchName: other,
|
2020-04-26 20:23:03 +00:00
|
|
|
hash: fooCommit.Hash,
|
|
|
|
resetTo: initCommit.Hash,
|
2020-04-24 19:33:33 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
expErr: `^cannot verify commits in branch "refs/heads/other" when no main branch exists$`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "new branch, not ancestor of main",
|
2020-04-26 20:23:03 +00:00
|
|
|
init: func(h *harness, rootSig sigcred.Signifier) toTest {
|
2020-04-24 19:33:33 +00:00
|
|
|
h.assertCommitChange(verifySkip, "init", rootSig)
|
|
|
|
|
|
|
|
// create new branch with no HEAD, and commit on that.
|
|
|
|
other := plumbing.NewBranchReferenceName("other")
|
|
|
|
ref := plumbing.NewSymbolicReference(plumbing.HEAD, other)
|
2020-04-26 20:23:03 +00:00
|
|
|
if err := h.proj.GitRepo.Storer.SetReference(ref); err != nil {
|
2020-04-24 19:33:33 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
h.stageCfg()
|
|
|
|
h.stage(map[string]string{"foo": "foo"})
|
|
|
|
badInitCommit := h.assertCommitChange(verifySkip, "a different init", rootSig)
|
|
|
|
return toTest{
|
|
|
|
branchName: plumbing.NewBranchReferenceName("other2"),
|
2020-04-26 20:23:03 +00:00
|
|
|
hash: badInitCommit.Hash,
|
2020-04-24 19:33:33 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
expErr: `^commit "[0-9a-f]+" must be direct descendant of root commit of "main" \("[0-9a-f]+"\)$`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// this case isn't generally possible, unless someone manually
|
|
|
|
// creates a branch in an empty repo on the remote
|
|
|
|
descr: "existing branch, not ancestor of main",
|
2020-04-26 20:23:03 +00:00
|
|
|
init: func(h *harness, rootSig sigcred.Signifier) toTest {
|
2020-04-24 19:33:33 +00:00
|
|
|
h.assertCommitChange(verifySkip, "init", rootSig)
|
|
|
|
|
|
|
|
// create new branch with no HEAD, and commit on that.
|
|
|
|
other := plumbing.NewBranchReferenceName("other")
|
|
|
|
ref := plumbing.NewSymbolicReference(plumbing.HEAD, other)
|
2020-04-26 20:23:03 +00:00
|
|
|
if err := h.proj.GitRepo.Storer.SetReference(ref); err != nil {
|
2020-04-24 19:33:33 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
h.stageCfg()
|
|
|
|
h.stage(map[string]string{"foo": "foo"})
|
|
|
|
badInitCommit := h.assertCommitChange(verifySkip, "a different init", rootSig)
|
|
|
|
|
|
|
|
h.stage(map[string]string{"bar": "bar"})
|
|
|
|
barCommit := h.assertCommitChange(verifySkip, "bar", rootSig)
|
|
|
|
|
|
|
|
return toTest{
|
|
|
|
branchName: other,
|
2020-04-26 20:23:03 +00:00
|
|
|
hash: barCommit.Hash,
|
|
|
|
resetTo: badInitCommit.Hash,
|
2020-04-24 19:33:33 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
expErr: `^commit "[0-9a-f]+" must be direct descendant of root commit of "main" \("[0-9a-f]+"\)$`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "new branch off of main",
|
2020-04-26 20:23:03 +00:00
|
|
|
init: func(h *harness, rootSig sigcred.Signifier) toTest {
|
2020-04-24 19:33:33 +00:00
|
|
|
initCommit := h.assertCommitChange(verifySkip, "init", rootSig)
|
|
|
|
other := plumbing.NewBranchReferenceName("other")
|
|
|
|
|
|
|
|
h.checkout(other)
|
|
|
|
h.stage(map[string]string{"foo": "foo"})
|
|
|
|
fooCommit := h.assertCommitChange(verifySkip, "foo", rootSig)
|
|
|
|
|
|
|
|
return toTest{
|
|
|
|
branchName: other,
|
2020-04-26 20:23:03 +00:00
|
|
|
hash: fooCommit.Hash,
|
|
|
|
resetTo: initCommit.Hash,
|
2020-04-24 19:33:33 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "new branch off of older main commit",
|
2020-04-26 20:23:03 +00:00
|
|
|
init: func(h *harness, rootSig sigcred.Signifier) toTest {
|
2020-04-24 19:33:33 +00:00
|
|
|
initCommit := h.assertCommitChange(verifySkip, "init", rootSig)
|
|
|
|
|
|
|
|
h.stage(map[string]string{"foo": "foo"})
|
|
|
|
h.assertCommitChange(verifySkip, "foo", rootSig)
|
|
|
|
|
|
|
|
other := plumbing.NewBranchReferenceName("other")
|
|
|
|
h.checkout(other)
|
2020-04-26 20:23:03 +00:00
|
|
|
h.reset(initCommit.Hash, git.HardReset)
|
2020-04-24 19:33:33 +00:00
|
|
|
h.stage(map[string]string{"bar": "bar"})
|
|
|
|
barCommit := h.assertCommitChange(verifySkip, "bar", rootSig)
|
|
|
|
|
|
|
|
return toTest{
|
|
|
|
branchName: other,
|
2020-04-26 20:23:03 +00:00
|
|
|
hash: barCommit.Hash,
|
|
|
|
resetTo: initCommit.Hash,
|
2020-04-24 19:33:33 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "branch ff",
|
2020-04-26 20:23:03 +00:00
|
|
|
init: func(h *harness, rootSig sigcred.Signifier) toTest {
|
2020-04-24 19:33:33 +00:00
|
|
|
h.assertCommitChange(verifySkip, "init", rootSig)
|
|
|
|
|
|
|
|
other := plumbing.NewBranchReferenceName("other")
|
|
|
|
h.checkout(other)
|
|
|
|
|
2020-04-26 20:23:03 +00:00
|
|
|
var commits []Commit
|
2020-04-24 19:33:33 +00:00
|
|
|
for _, str := range []string{"foo", "bar", "baz", "biz", "buz"} {
|
|
|
|
h.stage(map[string]string{str: str})
|
|
|
|
commit := h.assertCommitChange(verifySkip, str, rootSig)
|
|
|
|
commits = append(commits, commit)
|
|
|
|
}
|
|
|
|
|
|
|
|
return toTest{
|
|
|
|
branchName: other,
|
2020-04-26 20:23:03 +00:00
|
|
|
hash: commits[len(commits)-1].Hash,
|
|
|
|
resetTo: commits[0].Hash,
|
2020-04-24 19:33:33 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "main nonff",
|
2020-04-26 20:23:03 +00:00
|
|
|
init: func(h *harness, rootSig sigcred.Signifier) toTest {
|
2020-04-24 19:33:33 +00:00
|
|
|
initCommit := h.assertCommitChange(verifySkip, "init", rootSig)
|
|
|
|
h.stage(map[string]string{"foo": "foo"})
|
|
|
|
h.assertCommitChange(verifySkip, "foo", rootSig)
|
|
|
|
|
|
|
|
// start another branch back at init and make a new commit on it
|
|
|
|
other := plumbing.NewBranchReferenceName("other")
|
|
|
|
h.checkout(other)
|
2020-04-26 20:23:03 +00:00
|
|
|
h.reset(initCommit.Hash, git.HardReset)
|
2020-04-24 19:33:33 +00:00
|
|
|
h.stage(map[string]string{"bar": "bar"})
|
|
|
|
barCommit := h.assertCommitChange(verifySkip, "bar", rootSig)
|
|
|
|
|
|
|
|
return toTest{
|
|
|
|
branchName: MainRefName,
|
2020-04-26 20:23:03 +00:00
|
|
|
hash: barCommit.Hash,
|
2020-04-24 19:33:33 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
expErr: `^commit matched and denied by this access control:`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "branch nonff",
|
2020-04-26 20:23:03 +00:00
|
|
|
init: func(h *harness, rootSig sigcred.Signifier) toTest {
|
2020-04-24 19:33:33 +00:00
|
|
|
h.assertCommitChange(verifySkip, "init", rootSig)
|
|
|
|
|
|
|
|
other := plumbing.NewBranchReferenceName("other")
|
|
|
|
h.checkout(other)
|
|
|
|
h.stage(map[string]string{"foo": "foo"})
|
|
|
|
fooCommit := h.assertCommitChange(verifySkip, "foo", rootSig)
|
|
|
|
h.stage(map[string]string{"bar": "bar"})
|
|
|
|
h.assertCommitChange(verifySkip, "bar", rootSig)
|
|
|
|
|
|
|
|
other2 := plumbing.NewBranchReferenceName("other2")
|
|
|
|
h.checkout(other2)
|
2020-04-26 20:23:03 +00:00
|
|
|
h.reset(fooCommit.Hash, git.HardReset)
|
2020-04-24 19:33:33 +00:00
|
|
|
h.stage(map[string]string{"baz": "baz"})
|
|
|
|
bazCommit := h.assertCommitChange(verifySkip, "baz", rootSig)
|
|
|
|
|
|
|
|
return toTest{
|
|
|
|
branchName: other,
|
2020-04-26 20:23:03 +00:00
|
|
|
hash: bazCommit.Hash,
|
2020-04-24 19:33:33 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2020-05-02 21:28:02 +00:00
|
|
|
{
|
|
|
|
descr: "branch nonff to previous commit",
|
|
|
|
init: func(h *harness, rootSig sigcred.Signifier) toTest {
|
|
|
|
h.assertCommitChange(verifySkip, "init", rootSig)
|
|
|
|
|
|
|
|
other := plumbing.NewBranchReferenceName("other")
|
|
|
|
h.checkout(other)
|
|
|
|
h.stage(map[string]string{"foo": "foo"})
|
|
|
|
fooCommit := h.assertCommitChange(verifySkip, "foo", rootSig)
|
|
|
|
h.stage(map[string]string{"bar": "bar"})
|
|
|
|
h.assertCommitChange(verifySkip, "bar", rootSig)
|
|
|
|
|
|
|
|
return toTest{
|
|
|
|
branchName: other,
|
|
|
|
hash: fooCommit.Hash,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2020-04-24 19:33:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.descr, func(t *testing.T) {
|
|
|
|
h := newHarness(t)
|
|
|
|
rootSig := h.stageNewAccount("root", false)
|
|
|
|
toTest := test.init(h, rootSig)
|
|
|
|
|
|
|
|
if toTest.resetTo != plumbing.ZeroHash {
|
|
|
|
ref := plumbing.NewHashReference(toTest.branchName, toTest.resetTo)
|
2020-04-26 20:23:03 +00:00
|
|
|
if err := h.proj.GitRepo.Storer.SetReference(ref); err != nil {
|
2020-04-24 19:33:33 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-26 20:23:03 +00:00
|
|
|
err := h.proj.VerifyCanSetBranchHEADTo(toTest.branchName, toTest.hash)
|
2020-04-24 19:33:33 +00:00
|
|
|
if test.expErr == "" {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
} else if err == nil {
|
|
|
|
t.Fatal("expected verification to fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
ogErr := err
|
|
|
|
for {
|
|
|
|
if unwrappedErr := errors.Unwrap(err); unwrappedErr != nil {
|
|
|
|
err = unwrappedErr
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errRegex := regexp.MustCompile(test.expErr)
|
|
|
|
if !errRegex.MatchString(err.Error()) {
|
|
|
|
t.Fatalf("\nexpected error of form %q\nbut got: %v", test.expErr, ogErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-04-11 23:10:18 +00:00
|
|
|
}
|