2020-08-27 17:43:23 +00:00
|
|
|
// Copyright 2016-2020, Pulumi Corporation.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-07-22 21:28:03 +00:00
|
|
|
package auto
|
|
|
|
|
|
|
|
import (
|
2020-08-22 05:20:32 +00:00
|
|
|
"context"
|
2023-01-06 22:09:19 +00:00
|
|
|
"errors"
|
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
|
|
|
"fmt"
|
2020-07-22 21:28:03 +00:00
|
|
|
"path/filepath"
|
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
|
|
|
"strings"
|
2023-11-24 14:34:43 +00:00
|
|
|
"sync"
|
2020-07-22 21:28:03 +00:00
|
|
|
|
2022-08-09 11:46:28 +00:00
|
|
|
git "github.com/go-git/go-git/v5"
|
2023-03-02 00:34:52 +00:00
|
|
|
"github.com/go-git/go-git/v5/config"
|
2022-08-09 11:46:28 +00:00
|
|
|
"github.com/go-git/go-git/v5/plumbing"
|
2023-01-26 22:25:32 +00:00
|
|
|
"github.com/go-git/go-git/v5/plumbing/protocol/packp/capability"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing/transport"
|
2022-08-09 11:46:28 +00:00
|
|
|
"github.com/go-git/go-git/v5/plumbing/transport/http"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
|
2020-07-22 21:28:03 +00:00
|
|
|
)
|
|
|
|
|
2023-11-24 14:34:43 +00:00
|
|
|
var transportMutex sync.Mutex
|
|
|
|
|
2020-08-22 05:20:32 +00:00
|
|
|
func setupGitRepo(ctx context.Context, workDir string, repoArgs *GitRepo) (string, error) {
|
2020-09-14 19:24:57 +00:00
|
|
|
cloneOptions := &git.CloneOptions{
|
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
|
|
|
RemoteName: "origin", // be explicit so we can require it in remote refs
|
|
|
|
URL: repoArgs.URL,
|
2020-09-14 19:24:57 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 17:21:52 +00:00
|
|
|
if repoArgs.Shallow {
|
|
|
|
cloneOptions.Depth = 1
|
|
|
|
cloneOptions.SingleBranch = true
|
|
|
|
}
|
|
|
|
|
2020-09-14 19:24:57 +00:00
|
|
|
if repoArgs.Auth != nil {
|
|
|
|
authDetails := repoArgs.Auth
|
|
|
|
// Each of the authentication options are mutually exclusive so let's check that only 1 is specified
|
2020-10-12 18:51:26 +00:00
|
|
|
if authDetails.SSHPrivateKeyPath != "" && authDetails.Username != "" ||
|
|
|
|
authDetails.PersonalAccessToken != "" && authDetails.Username != "" ||
|
|
|
|
authDetails.PersonalAccessToken != "" && authDetails.SSHPrivateKeyPath != "" ||
|
|
|
|
authDetails.Username != "" && authDetails.SSHPrivateKey != "" {
|
2020-09-14 19:24:57 +00:00
|
|
|
return "", errors.New("please specify one authentication option of `Personal Access Token`, " +
|
2020-10-12 18:51:26 +00:00
|
|
|
"`Username\\Password`, `SSH Private Key Path` or `SSH Private Key`")
|
2020-09-14 19:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Firstly we will try to check that an SSH Private Key Path has been specified
|
|
|
|
if authDetails.SSHPrivateKeyPath != "" {
|
|
|
|
publicKeys, err := ssh.NewPublicKeysFromFile("git", repoArgs.Auth.SSHPrivateKeyPath, repoArgs.Auth.Password)
|
2020-10-12 18:51:26 +00:00
|
|
|
if err != nil {
|
2023-01-06 22:09:19 +00:00
|
|
|
return "", fmt.Errorf("unable to use SSH Private Key Path: %w", err)
|
2020-10-12 18:51:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cloneOptions.Auth = publicKeys
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then we check if the details of a SSH Private Key as passed
|
|
|
|
if authDetails.SSHPrivateKey != "" {
|
|
|
|
publicKeys, err := ssh.NewPublicKeys("git", []byte(repoArgs.Auth.SSHPrivateKey), repoArgs.Auth.Password)
|
2020-09-14 19:24:57 +00:00
|
|
|
if err != nil {
|
2023-01-06 22:09:19 +00:00
|
|
|
return "", fmt.Errorf("unable to use SSH Private Key: %w", err)
|
2020-09-14 19:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cloneOptions.Auth = publicKeys
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then we check to see if a Personal Access Token has been specified
|
|
|
|
// the username for use with a PAT can be *anything* but an empty string
|
|
|
|
// so we are setting this to `git`
|
|
|
|
if authDetails.PersonalAccessToken != "" {
|
|
|
|
cloneOptions.Auth = &http.BasicAuth{
|
|
|
|
Username: "git",
|
|
|
|
Password: repoArgs.Auth.PersonalAccessToken,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// then we check to see if a username and a password has been specified
|
|
|
|
if authDetails.Password != "" && authDetails.Username != "" {
|
|
|
|
cloneOptions.Auth = &http.BasicAuth{
|
|
|
|
Username: repoArgs.Auth.Username,
|
|
|
|
Password: repoArgs.Auth.Password,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// *Repository.Clone() will do appropriate fetching given a branch name. We must deal with
|
|
|
|
// different varieties, since people have been advised to use these as a workaround while only
|
|
|
|
// "refs/heads/<default>" worked.
|
|
|
|
//
|
|
|
|
// If a reference name is not supplied, then .Clone will fetch all refs (and all objects
|
|
|
|
// referenced by those), and checking out a commit later will work as expected.
|
|
|
|
if repoArgs.Branch != "" {
|
|
|
|
refName := plumbing.ReferenceName(repoArgs.Branch)
|
|
|
|
switch {
|
|
|
|
case refName.IsRemote(): // e.g., refs/remotes/origin/branch
|
|
|
|
shorter := refName.Short() // this gives "origin/branch"
|
|
|
|
parts := strings.SplitN(shorter, "/", 2)
|
|
|
|
if len(parts) == 2 && parts[0] == "origin" {
|
|
|
|
refName = plumbing.NewBranchReferenceName(parts[1])
|
|
|
|
} else {
|
|
|
|
return "", fmt.Errorf("a remote ref must begin with 'refs/remote/origin/', but got %q", repoArgs.Branch)
|
|
|
|
}
|
|
|
|
case refName.IsTag(): // looks like `refs/tags/v1.0.0` -- respect this even though the field is `.Branch`
|
|
|
|
// nothing to do
|
|
|
|
case !refName.IsBranch(): // not a remote, not refs/heads/branch; treat as a simple branch name
|
|
|
|
refName = plumbing.NewBranchReferenceName(repoArgs.Branch)
|
|
|
|
default:
|
|
|
|
// already looks like a full branch name, so use as is
|
|
|
|
}
|
|
|
|
cloneOptions.ReferenceName = refName
|
|
|
|
}
|
|
|
|
|
2023-11-24 14:34:43 +00:00
|
|
|
// Azure DevOps requires multi_ack and multi_ack_detailed capabilities, which go-git doesn't implement.
|
|
|
|
// But: it's possible to do a full clone by saying it's _not_ _un_supported, in which case the library
|
|
|
|
// happily functions so long as it doesn't _actually_ get a multi_ack packet. See
|
2023-01-26 22:25:32 +00:00
|
|
|
// https://github.com/go-git/go-git/blob/v5.5.1/_examples/azure_devops/main.go.
|
2023-11-24 14:34:43 +00:00
|
|
|
repo, err := func() (*git.Repository, error) {
|
|
|
|
// Because transport.UnsupportedCapabilities is a global variable, we need a global lock around the
|
|
|
|
// use of this.
|
|
|
|
transportMutex.Lock()
|
|
|
|
defer transportMutex.Unlock()
|
|
|
|
|
|
|
|
oldUnsupportedCaps := transport.UnsupportedCapabilities
|
|
|
|
// This check is crude, but avoids having another dependency to parse the git URL.
|
|
|
|
if strings.Contains(repoArgs.URL, "dev.azure.com") {
|
|
|
|
transport.UnsupportedCapabilities = []capability.Capability{
|
|
|
|
capability.ThinPack,
|
|
|
|
}
|
2023-01-26 22:25:32 +00:00
|
|
|
}
|
|
|
|
|
2023-11-24 14:34:43 +00:00
|
|
|
// clone
|
|
|
|
repo, err := git.PlainCloneContext(ctx, workDir, false, cloneOptions)
|
|
|
|
|
|
|
|
// Regardless of error we need to restore the UnsupportedCapabilities
|
|
|
|
transport.UnsupportedCapabilities = oldUnsupportedCaps
|
|
|
|
return repo, err
|
|
|
|
}()
|
2020-07-22 21:28:03 +00:00
|
|
|
if err != nil {
|
2023-01-06 22:09:19 +00:00
|
|
|
return "", fmt.Errorf("unable to clone repo: %w", err)
|
2020-07-22 21:28:03 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 18:13:42 +00:00
|
|
|
if repoArgs.CommitHash != "" {
|
2023-03-02 00:34:52 +00:00
|
|
|
// ensure that the commit has been fetched
|
|
|
|
err = repo.FetchContext(ctx, &git.FetchOptions{
|
|
|
|
RemoteName: "origin",
|
|
|
|
Auth: cloneOptions.Auth,
|
2023-11-01 17:21:52 +00:00
|
|
|
Depth: cloneOptions.Depth,
|
2023-03-02 00:34:52 +00:00
|
|
|
RefSpecs: []config.RefSpec{config.RefSpec(repoArgs.CommitHash + ":" + repoArgs.CommitHash)},
|
|
|
|
})
|
|
|
|
if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) && !errors.Is(err, git.ErrExactSHA1NotSupported) {
|
|
|
|
return "", fmt.Errorf("fetching commit: %w", 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
|
|
|
// checkout commit if specified
|
|
|
|
w, err := repo.Worktree()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2022-07-13 15:42:45 +00:00
|
|
|
}
|
2020-07-22 21:28:03 +00:00
|
|
|
|
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
|
|
|
hash := repoArgs.CommitHash
|
|
|
|
err = w.Checkout(&git.CheckoutOptions{
|
|
|
|
Hash: plumbing.NewHash(hash),
|
|
|
|
Force: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-01-06 22:09:19 +00:00
|
|
|
return "", fmt.Errorf("unable to checkout commit: %w", 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
|
|
|
}
|
2020-07-22 21:28:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var relPath string
|
2020-08-19 18:13:42 +00:00
|
|
|
if repoArgs.ProjectPath != "" {
|
|
|
|
relPath = repoArgs.ProjectPath
|
2020-07-22 21:28:03 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 18:13:42 +00:00
|
|
|
workDir = filepath.Join(workDir, relPath)
|
|
|
|
return workDir, nil
|
2020-07-22 21:28:03 +00:00
|
|
|
}
|