2022-07-13 15:42:45 +00:00
|
|
|
package auto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
Work with all kinds of branch names (#10285)
* Generalise git branch test
In #10118 the git checkout was adapted so it would work with simple
branch names (i.e., "main" rather than "refs/heads/main"). However, it
still won't work with a branch that's not the default branch -- this
test demonstrates so.
* Support all simple and full branch names
If you want to have a non-default branch checked out, you need to fetch
it _and_ make a local reference for it. If you don't giv e a specific
reference, *Repository.Clone(...) updates remote heads, but does not
create local refs.
So, to support the varieties of branch names that people might use, the
provided branch name needs to be interpreted, and used for the
*Repository.Clone(...).
* Support cloning at a tag
Although the field is .Branch, we might expect people to ask for a tag
too. It's probably possible, with some back and forth, to disambiguate
between a simple branch name and a simple tag name; but, for the sake of
simplicity, require people to give `refs/tags/tag`.
* Queue changelog entry
* Check more cases of supplied branch name
- ... including things that should result in an error
- verify that the remote named in a remote ref is "origin"; anything
else is surely a mistake
Signed-off-by: Michael Bridgen <mbridgen@pulumi.com>
2022-08-22 12:41:17 +00:00
|
|
|
"path/filepath"
|
2022-07-13 15:42:45 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-08-09 11:46:28 +00:00
|
|
|
git "github.com/go-git/go-git/v5"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing"
|
Work with all kinds of branch names (#10285)
* Generalise git branch test
In #10118 the git checkout was adapted so it would work with simple
branch names (i.e., "main" rather than "refs/heads/main"). However, it
still won't work with a branch that's not the default branch -- this
test demonstrates so.
* Support all simple and full branch names
If you want to have a non-default branch checked out, you need to fetch
it _and_ make a local reference for it. If you don't giv e a specific
reference, *Repository.Clone(...) updates remote heads, but does not
create local refs.
So, to support the varieties of branch names that people might use, the
provided branch name needs to be interpreted, and used for the
*Repository.Clone(...).
* Support cloning at a tag
Although the field is .Branch, we might expect people to ask for a tag
too. It's probably possible, with some back and forth, to disambiguate
between a simple branch name and a simple tag name; but, for the sake of
simplicity, require people to give `refs/tags/tag`.
* Queue changelog entry
* Check more cases of supplied branch name
- ... including things that should result in an error
- verify that the remote named in a remote ref is "origin"; anything
else is surely a mistake
Signed-off-by: Michael Bridgen <mbridgen@pulumi.com>
2022-08-22 12:41:17 +00:00
|
|
|
"github.com/go-git/go-git/v5/plumbing/object"
|
2022-07-13 15:42:45 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This takes the unusual step of testing an unexported func. The rationale is to be able to test
|
|
|
|
// git code in isolation; testing the user of the unexported func (NewLocalWorkspace) drags in lots
|
|
|
|
// of other factors.
|
|
|
|
|
Work with all kinds of branch names (#10285)
* Generalise git branch test
In #10118 the git checkout was adapted so it would work with simple
branch names (i.e., "main" rather than "refs/heads/main"). However, it
still won't work with a branch that's not the default branch -- this
test demonstrates so.
* Support all simple and full branch names
If you want to have a non-default branch checked out, you need to fetch
it _and_ make a local reference for it. If you don't giv e a specific
reference, *Repository.Clone(...) updates remote heads, but does not
create local refs.
So, to support the varieties of branch names that people might use, the
provided branch name needs to be interpreted, and used for the
*Repository.Clone(...).
* Support cloning at a tag
Although the field is .Branch, we might expect people to ask for a tag
too. It's probably possible, with some back and forth, to disambiguate
between a simple branch name and a simple tag name; but, for the sake of
simplicity, require people to give `refs/tags/tag`.
* Queue changelog entry
* Check more cases of supplied branch name
- ... including things that should result in an error
- verify that the remote named in a remote ref is "origin"; anything
else is surely a mistake
Signed-off-by: Michael Bridgen <mbridgen@pulumi.com>
2022-08-22 12:41:17 +00:00
|
|
|
func TestGitClone(t *testing.T) {
|
2022-07-13 15:42:45 +00:00
|
|
|
t.Parallel()
|
Work with all kinds of branch names (#10285)
* Generalise git branch test
In #10118 the git checkout was adapted so it would work with simple
branch names (i.e., "main" rather than "refs/heads/main"). However, it
still won't work with a branch that's not the default branch -- this
test demonstrates so.
* Support all simple and full branch names
If you want to have a non-default branch checked out, you need to fetch
it _and_ make a local reference for it. If you don't giv e a specific
reference, *Repository.Clone(...) updates remote heads, but does not
create local refs.
So, to support the varieties of branch names that people might use, the
provided branch name needs to be interpreted, and used for the
*Repository.Clone(...).
* Support cloning at a tag
Although the field is .Branch, we might expect people to ask for a tag
too. It's probably possible, with some back and forth, to disambiguate
between a simple branch name and a simple tag name; but, for the sake of
simplicity, require people to give `refs/tags/tag`.
* Queue changelog entry
* Check more cases of supplied branch name
- ... including things that should result in an error
- verify that the remote named in a remote ref is "origin"; anything
else is surely a mistake
Signed-off-by: Michael Bridgen <mbridgen@pulumi.com>
2022-08-22 12:41:17 +00:00
|
|
|
|
|
|
|
// This makes a git repo to clone from, so to avoid relying on something at GitHub that could
|
|
|
|
// change or be inaccessible.
|
2022-12-03 07:17:08 +00:00
|
|
|
tmpDir := t.TempDir()
|
Work with all kinds of branch names (#10285)
* Generalise git branch test
In #10118 the git checkout was adapted so it would work with simple
branch names (i.e., "main" rather than "refs/heads/main"). However, it
still won't work with a branch that's not the default branch -- this
test demonstrates so.
* Support all simple and full branch names
If you want to have a non-default branch checked out, you need to fetch
it _and_ make a local reference for it. If you don't giv e a specific
reference, *Repository.Clone(...) updates remote heads, but does not
create local refs.
So, to support the varieties of branch names that people might use, the
provided branch name needs to be interpreted, and used for the
*Repository.Clone(...).
* Support cloning at a tag
Although the field is .Branch, we might expect people to ask for a tag
too. It's probably possible, with some back and forth, to disambiguate
between a simple branch name and a simple tag name; but, for the sake of
simplicity, require people to give `refs/tags/tag`.
* Queue changelog entry
* Check more cases of supplied branch name
- ... including things that should result in an error
- verify that the remote named in a remote ref is "origin"; anything
else is surely a mistake
Signed-off-by: Michael Bridgen <mbridgen@pulumi.com>
2022-08-22 12:41:17 +00:00
|
|
|
originDir := filepath.Join(tmpDir, "origin")
|
|
|
|
|
|
|
|
origin, err := git.PlainInit(originDir, false)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
w, err := origin.Worktree()
|
2022-07-13 15:42:45 +00:00
|
|
|
assert.NoError(t, err)
|
Work with all kinds of branch names (#10285)
* Generalise git branch test
In #10118 the git checkout was adapted so it would work with simple
branch names (i.e., "main" rather than "refs/heads/main"). However, it
still won't work with a branch that's not the default branch -- this
test demonstrates so.
* Support all simple and full branch names
If you want to have a non-default branch checked out, you need to fetch
it _and_ make a local reference for it. If you don't giv e a specific
reference, *Repository.Clone(...) updates remote heads, but does not
create local refs.
So, to support the varieties of branch names that people might use, the
provided branch name needs to be interpreted, and used for the
*Repository.Clone(...).
* Support cloning at a tag
Although the field is .Branch, we might expect people to ask for a tag
too. It's probably possible, with some back and forth, to disambiguate
between a simple branch name and a simple tag name; but, for the sake of
simplicity, require people to give `refs/tags/tag`.
* Queue changelog entry
* Check more cases of supplied branch name
- ... including things that should result in an error
- verify that the remote named in a remote ref is "origin"; anything
else is surely a mistake
Signed-off-by: Michael Bridgen <mbridgen@pulumi.com>
2022-08-22 12:41:17 +00:00
|
|
|
nondefaultHead, err := w.Commit("nondefault branch", &git.CommitOptions{
|
|
|
|
Author: &object.Signature{
|
|
|
|
Name: "testo",
|
|
|
|
Email: "testo@example.com",
|
|
|
|
},
|
2023-01-26 22:25:32 +00:00
|
|
|
AllowEmptyCommits: true,
|
Work with all kinds of branch names (#10285)
* Generalise git branch test
In #10118 the git checkout was adapted so it would work with simple
branch names (i.e., "main" rather than "refs/heads/main"). However, it
still won't work with a branch that's not the default branch -- this
test demonstrates so.
* Support all simple and full branch names
If you want to have a non-default branch checked out, you need to fetch
it _and_ make a local reference for it. If you don't giv e a specific
reference, *Repository.Clone(...) updates remote heads, but does not
create local refs.
So, to support the varieties of branch names that people might use, the
provided branch name needs to be interpreted, and used for the
*Repository.Clone(...).
* Support cloning at a tag
Although the field is .Branch, we might expect people to ask for a tag
too. It's probably possible, with some back and forth, to disambiguate
between a simple branch name and a simple tag name; but, for the sake of
simplicity, require people to give `refs/tags/tag`.
* Queue changelog entry
* Check more cases of supplied branch name
- ... including things that should result in an error
- verify that the remote named in a remote ref is "origin"; anything
else is surely a mistake
Signed-off-by: Michael Bridgen <mbridgen@pulumi.com>
2022-08-22 12:41:17 +00:00
|
|
|
})
|
2022-07-13 15:42:45 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
Work with all kinds of branch names (#10285)
* Generalise git branch test
In #10118 the git checkout was adapted so it would work with simple
branch names (i.e., "main" rather than "refs/heads/main"). However, it
still won't work with a branch that's not the default branch -- this
test demonstrates so.
* Support all simple and full branch names
If you want to have a non-default branch checked out, you need to fetch
it _and_ make a local reference for it. If you don't giv e a specific
reference, *Repository.Clone(...) updates remote heads, but does not
create local refs.
So, to support the varieties of branch names that people might use, the
provided branch name needs to be interpreted, and used for the
*Repository.Clone(...).
* Support cloning at a tag
Although the field is .Branch, we might expect people to ask for a tag
too. It's probably possible, with some back and forth, to disambiguate
between a simple branch name and a simple tag name; but, for the sake of
simplicity, require people to give `refs/tags/tag`.
* Queue changelog entry
* Check more cases of supplied branch name
- ... including things that should result in an error
- verify that the remote named in a remote ref is "origin"; anything
else is surely a mistake
Signed-off-by: Michael Bridgen <mbridgen@pulumi.com>
2022-08-22 12:41:17 +00:00
|
|
|
// The following sets up some tags and branches: with `default` becoming the "default" branch
|
|
|
|
// when cloning, since it's left as the HEAD of the repo.
|
|
|
|
|
|
|
|
assert.NoError(t, w.Checkout(&git.CheckoutOptions{
|
|
|
|
Branch: plumbing.NewBranchReferenceName("nondefault"),
|
|
|
|
Create: true,
|
|
|
|
}))
|
|
|
|
|
|
|
|
// tag the nondefault head so we can test getting a tag too
|
|
|
|
_, err = origin.CreateTag("v0.0.1", nondefaultHead, nil)
|
2022-07-13 15:42:45 +00:00
|
|
|
assert.NoError(t, err)
|
Work with all kinds of branch names (#10285)
* Generalise git branch test
In #10118 the git checkout was adapted so it would work with simple
branch names (i.e., "main" rather than "refs/heads/main"). However, it
still won't work with a branch that's not the default branch -- this
test demonstrates so.
* Support all simple and full branch names
If you want to have a non-default branch checked out, you need to fetch
it _and_ make a local reference for it. If you don't giv e a specific
reference, *Repository.Clone(...) updates remote heads, but does not
create local refs.
So, to support the varieties of branch names that people might use, the
provided branch name needs to be interpreted, and used for the
*Repository.Clone(...).
* Support cloning at a tag
Although the field is .Branch, we might expect people to ask for a tag
too. It's probably possible, with some back and forth, to disambiguate
between a simple branch name and a simple tag name; but, for the sake of
simplicity, require people to give `refs/tags/tag`.
* Queue changelog entry
* Check more cases of supplied branch name
- ... including things that should result in an error
- verify that the remote named in a remote ref is "origin"; anything
else is surely a mistake
Signed-off-by: Michael Bridgen <mbridgen@pulumi.com>
2022-08-22 12:41:17 +00:00
|
|
|
|
|
|
|
// make a branch with slashes in it, so that can be tested too
|
|
|
|
assert.NoError(t, w.Checkout(&git.CheckoutOptions{
|
|
|
|
Branch: plumbing.NewBranchReferenceName("branch/with/slashes"),
|
|
|
|
Create: true,
|
|
|
|
}))
|
|
|
|
|
|
|
|
assert.NoError(t, w.Checkout(&git.CheckoutOptions{
|
|
|
|
Branch: plumbing.NewBranchReferenceName("default"),
|
|
|
|
Create: true,
|
|
|
|
}))
|
|
|
|
defaultHead, err := w.Commit("default branch", &git.CommitOptions{
|
|
|
|
Author: &object.Signature{
|
|
|
|
Name: "testo",
|
|
|
|
Email: "testo@example.com",
|
|
|
|
},
|
2023-01-26 22:25:32 +00:00
|
|
|
AllowEmptyCommits: true,
|
Work with all kinds of branch names (#10285)
* Generalise git branch test
In #10118 the git checkout was adapted so it would work with simple
branch names (i.e., "main" rather than "refs/heads/main"). However, it
still won't work with a branch that's not the default branch -- this
test demonstrates so.
* Support all simple and full branch names
If you want to have a non-default branch checked out, you need to fetch
it _and_ make a local reference for it. If you don't giv e a specific
reference, *Repository.Clone(...) updates remote heads, but does not
create local refs.
So, to support the varieties of branch names that people might use, the
provided branch name needs to be interpreted, and used for the
*Repository.Clone(...).
* Support cloning at a tag
Although the field is .Branch, we might expect people to ask for a tag
too. It's probably possible, with some back and forth, to disambiguate
between a simple branch name and a simple tag name; but, for the sake of
simplicity, require people to give `refs/tags/tag`.
* Queue changelog entry
* Check more cases of supplied branch name
- ... including things that should result in an error
- verify that the remote named in a remote ref is "origin"; anything
else is surely a mistake
Signed-off-by: Michael Bridgen <mbridgen@pulumi.com>
2022-08-22 12:41:17 +00:00
|
|
|
})
|
2022-07-13 15:42:45 +00:00
|
|
|
assert.NoError(t, err)
|
Work with all kinds of branch names (#10285)
* Generalise git branch test
In #10118 the git checkout was adapted so it would work with simple
branch names (i.e., "main" rather than "refs/heads/main"). However, it
still won't work with a branch that's not the default branch -- this
test demonstrates so.
* Support all simple and full branch names
If you want to have a non-default branch checked out, you need to fetch
it _and_ make a local reference for it. If you don't giv e a specific
reference, *Repository.Clone(...) updates remote heads, but does not
create local refs.
So, to support the varieties of branch names that people might use, the
provided branch name needs to be interpreted, and used for the
*Repository.Clone(...).
* Support cloning at a tag
Although the field is .Branch, we might expect people to ask for a tag
too. It's probably possible, with some back and forth, to disambiguate
between a simple branch name and a simple tag name; but, for the sake of
simplicity, require people to give `refs/tags/tag`.
* Queue changelog entry
* Check more cases of supplied branch name
- ... including things that should result in an error
- verify that the remote named in a remote ref is "origin"; anything
else is surely a mistake
Signed-off-by: Michael Bridgen <mbridgen@pulumi.com>
2022-08-22 12:41:17 +00:00
|
|
|
|
|
|
|
type testcase struct {
|
|
|
|
branchName string
|
|
|
|
commitHash string
|
|
|
|
testName string // use when supplying a hash, for a stable name
|
|
|
|
expectedHead plumbing.Hash
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range []testcase{
|
|
|
|
{branchName: "default", expectedHead: defaultHead},
|
|
|
|
{branchName: "nondefault", expectedHead: nondefaultHead},
|
|
|
|
{branchName: "branch/with/slashes", expectedHead: nondefaultHead},
|
|
|
|
// https://github.com/pulumi/pulumi-kubernetes-operator/issues/103#issuecomment-1107891475
|
|
|
|
// advises using `refs/heads/<default>` for the default, and `refs/remotes/origin/<branch>`
|
|
|
|
// for a non-default branch -- so we can expect all these varieties to be in use.
|
|
|
|
{branchName: "refs/heads/default", expectedHead: defaultHead},
|
|
|
|
{branchName: "refs/heads/nondefault", expectedHead: nondefaultHead},
|
|
|
|
{branchName: "refs/heads/branch/with/slashes", expectedHead: nondefaultHead},
|
|
|
|
|
|
|
|
{branchName: "refs/remotes/origin/default", expectedHead: defaultHead},
|
|
|
|
{branchName: "refs/remotes/origin/nondefault", expectedHead: nondefaultHead},
|
|
|
|
{branchName: "refs/remotes/origin/branch/with/slashes", expectedHead: nondefaultHead},
|
|
|
|
// try the special tag case
|
|
|
|
{branchName: "refs/tags/v0.0.1", expectedHead: nondefaultHead},
|
|
|
|
// ask specifically for the commit hash
|
|
|
|
{testName: "head of default as hash", commitHash: defaultHead.String(), expectedHead: defaultHead},
|
|
|
|
{testName: "head of nondefault as hash", commitHash: nondefaultHead.String(), expectedHead: nondefaultHead},
|
|
|
|
} {
|
|
|
|
tc := tc
|
|
|
|
if tc.testName == "" {
|
|
|
|
tc.testName = tc.branchName
|
|
|
|
}
|
|
|
|
t.Run(tc.testName, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
repo := &GitRepo{
|
|
|
|
URL: originDir,
|
|
|
|
Branch: tc.branchName,
|
|
|
|
CommitHash: tc.commitHash,
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp, err := os.MkdirTemp(tmpDir, "testcase") // i.e., under the tmp dir from earlier
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2023-09-25 12:25:26 +00:00
|
|
|
_, err = setupGitRepo(context.Background(), tmp, repo)
|
Work with all kinds of branch names (#10285)
* Generalise git branch test
In #10118 the git checkout was adapted so it would work with simple
branch names (i.e., "main" rather than "refs/heads/main"). However, it
still won't work with a branch that's not the default branch -- this
test demonstrates so.
* Support all simple and full branch names
If you want to have a non-default branch checked out, you need to fetch
it _and_ make a local reference for it. If you don't giv e a specific
reference, *Repository.Clone(...) updates remote heads, but does not
create local refs.
So, to support the varieties of branch names that people might use, the
provided branch name needs to be interpreted, and used for the
*Repository.Clone(...).
* Support cloning at a tag
Although the field is .Branch, we might expect people to ask for a tag
too. It's probably possible, with some back and forth, to disambiguate
between a simple branch name and a simple tag name; but, for the sake of
simplicity, require people to give `refs/tags/tag`.
* Queue changelog entry
* Check more cases of supplied branch name
- ... including things that should result in an error
- verify that the remote named in a remote ref is "origin"; anything
else is surely a mistake
Signed-off-by: Michael Bridgen <mbridgen@pulumi.com>
2022-08-22 12:41:17 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
r, err := git.PlainOpen(tmp)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
head, err := r.Head()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, tc.expectedHead, head.Hash())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// test that these result in errors
|
|
|
|
for _, tc := range []testcase{
|
|
|
|
{testName: "simple branch doesn't exist", branchName: "doesnotexist"},
|
|
|
|
{testName: "full branch doesn't exist", branchName: "refs/heads/doesnotexist"},
|
|
|
|
{testName: "malformed branch name", branchName: "refs/notathing/default"},
|
|
|
|
{testName: "simple tag name won't work", branchName: "v1.0.0"},
|
|
|
|
{testName: "wrong remote", branchName: "refs/remotes/upstream/default"},
|
|
|
|
} {
|
|
|
|
tc := tc
|
|
|
|
if tc.testName == "" {
|
|
|
|
tc.testName = tc.branchName
|
|
|
|
}
|
|
|
|
t.Run(tc.testName, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
repo := &GitRepo{
|
|
|
|
URL: originDir,
|
|
|
|
Branch: tc.branchName,
|
|
|
|
CommitHash: tc.commitHash,
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp, err := os.MkdirTemp(tmpDir, "testcase") // i.e., under the tmp dir from earlier
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2023-09-25 12:25:26 +00:00
|
|
|
_, err = setupGitRepo(context.Background(), tmp, repo)
|
Work with all kinds of branch names (#10285)
* Generalise git branch test
In #10118 the git checkout was adapted so it would work with simple
branch names (i.e., "main" rather than "refs/heads/main"). However, it
still won't work with a branch that's not the default branch -- this
test demonstrates so.
* Support all simple and full branch names
If you want to have a non-default branch checked out, you need to fetch
it _and_ make a local reference for it. If you don't giv e a specific
reference, *Repository.Clone(...) updates remote heads, but does not
create local refs.
So, to support the varieties of branch names that people might use, the
provided branch name needs to be interpreted, and used for the
*Repository.Clone(...).
* Support cloning at a tag
Although the field is .Branch, we might expect people to ask for a tag
too. It's probably possible, with some back and forth, to disambiguate
between a simple branch name and a simple tag name; but, for the sake of
simplicity, require people to give `refs/tags/tag`.
* Queue changelog entry
* Check more cases of supplied branch name
- ... including things that should result in an error
- verify that the remote named in a remote ref is "origin"; anything
else is surely a mistake
Signed-off-by: Michael Bridgen <mbridgen@pulumi.com>
2022-08-22 12:41:17 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|
2022-07-13 15:42:45 +00:00
|
|
|
}
|