repo.FetchContext reads transport.UnsupportedCapabilities, which we
modify, leading to a potential data race in oncurrent calls to
setupGitRepo.
Wrap the call to repo.FetchContext with the global transportMutex.
Fixes https://github.com/pulumi/pulumi/issues/16516
<!---
Thanks so much for your contribution! If this is your first time
contributing, please ensure that you have read the
[CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md)
documentation.
-->
# Description
<!--- Please include a summary of the change and which issue is fixed.
Please also include relevant motivation and context. -->
Fixes a datarace in setupGitRepo seen in CI:
```
=== FAIL: go/auto TestNewStackRemoteSource (44.69s)
==================
WARNING: DATA RACE
Read at 0x000002d35650 by goroutine 103:
github.com/pulumi/pulumi/sdk/v3/go/auto.setupGitRepo()
/Users/runner/work/pulumi/pulumi/sdk/go/auto/git.go:125 +0xf78
github.com/pulumi/pulumi/sdk/v3/go/auto.NewLocalWorkspace()
/Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace.go:778 +0x435
github.com/pulumi/pulumi/sdk/v3/go/auto.NewStackRemoteSource()
/Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace.go:1114 +0x2c4
github.com/pulumi/pulumi/sdk/v3/go/auto.TestNewStackRemoteSource()
/Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace_test.go:465 +0x326
testing.tRunner()
/Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1595 +0x238
testing.(*T).Run.func1()
/Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1648 +0x44
Previous write at 0x000002d35650 by goroutine 104:
github.com/pulumi/pulumi/sdk/v3/go/auto.setupGitRepo()
/Users/runner/work/pulumi/pulumi/sdk/go/auto/git.go:139 +0x11d1
github.com/pulumi/pulumi/sdk/v3/go/auto.NewLocalWorkspace()
/Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace.go:778 +0x435
github.com/pulumi/pulumi/sdk/v3/go/auto.UpsertStackRemoteSource()
/Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace.go:1133 +0x2c4
github.com/pulumi/pulumi/sdk/v3/go/auto.TestUpsertStackRemoteSource()
/Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace_test.go:559 +0x326
testing.tRunner()
/Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1595 +0x238
testing.(*T).Run.func1()
/Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1648 +0x44
Goroutine 103 (running) created at:
testing.(*T).Run()
/Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1648 +0x82a
testing.runTests.func1()
/Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:2054 +0x84
testing.tRunner()
/Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1595 +0x238
testing.runTests()
/Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:2052 +0x896
testing.(*M).Run()
/Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1925 +0xb57
main.main()
_testmain.go:159 +0x2e4
Goroutine 104 (running) created at:
testing.(*T).Run()
/Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1648 +0x82a
testing.runTests.func1()
/Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:2054 +0x84
testing.tRunner()
/Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1595 +0x238
testing.runTests()
/Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:2052 +0x896
testing.(*M).Run()
/Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1925 +0xb57
main.main()
_testmain.go:159 +0x2e4
==================
testing.go:1465: race detected during execution of test
```
Pretty clear culprit of mutating a global variable in the go-git module.
## Checklist
- [x] I have run `make tidy` to update any new dependencies
- [x] I have run `make lint` to verify my code passes the lint check
- [ ] I have formatted my code using `gofumpt`
<!--- Please provide details if the checkbox below is to be left
unchecked. -->
- [ ] I have added tests that prove my fix is effective or that my
feature works - This was caught be existing tests
<!---
User-facing changes require a CHANGELOG entry.
-->
- [x] I have run `make changelog` and committed the
`changelog/pending/<file>` documenting my change
<!--
If the change(s) in this PR is a modification of an existing call to the
Pulumi Cloud,
then the service should honor older versions of the CLI where this
change would not exist.
You must then bump the API version in
/pkg/backend/httpstate/client/api.go, as well as add
it to the service.
-->
- [ ] Yes, there are changes in this PR that warrants bumping the Pulumi
Cloud API version
<!-- @Pulumi employees: If yes, you must submit corresponding changes in
the service repo. -->
<!---
Thanks so much for your contribution! If this is your first time
contributing, please ensure that you have read the
[CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md)
documentation.
-->
# Description
This exposes a GitRepo option to enable shallow cloning repositories.
This is helpful in cases where the repo has a large history.
## Checklist
- [ ] I have run `make tidy` to update any new dependencies
- [ ] I have run `make lint` to verify my code passes the lint check
- [ ] I have formatted my code using `gofumpt`
<!--- Please provide details if the checkbox below is to be left
unchecked. -->
- [ ] I have added tests that prove my fix is effective or that my
feature works
<!---
User-facing changes require a CHANGELOG entry.
-->
- [ ] I have run `make changelog` and committed the
`changelog/pending/<file>` documenting my change
<!--
If the change(s) in this PR is a modification of an existing call to the
Pulumi Cloud,
then the service should honor older versions of the CLI where this
change would not exist.
You must then bump the API version in
/pkg/backend/httpstate/client/api.go, as well as add
it to the service.
-->
- [ ] Yes, there are changes in this PR that warrants bumping the Pulumi
Cloud API version
<!-- @Pulumi employees: If yes, you must submit corresponding changes in
the service repo. -->
If a commit hash is specified for a Git repo, explicitly fetch it before
attempting to check it out. This allows using commits that are not
fetched by default (e.g. GitHub test merge commits).
In general, go-git can't clone from Azure DevOps, because the latter
requires the capabilities multi_ack and multi_ack_detailed, which aren't
implemented. However, there's now a workaround, which boils down to
this: pretend, for the initial clone, that those capabilities _are_
supported, and expect them not to be used.
(See https://github.com/go-git/go-git/pull/613 for more on this
workaround.)
Signed-off-by: Michael Bridgen <mbridgen@pulumi.com>
* 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>
* Accept short branch names in GitRepo.Branch
Users often give short names for branches -- e.g., `dev`, rather than
`refs/heads/dev`. This commit makes that work rather than produce an
error, by adopting the heuristic that anything that doesn't look like a
full branch name (i.e., not starting with ref/heads/) must be a short
name.
Signed-off-by: Michael Bridgen <mbridgen@pulumi.com>