[short] skip 'generates a vcstest git repo'
[!git] skip

env GOPROXY=direct

# Control case: fetching a nested module at a tag that exists should
# emit Origin metadata for that tag and commit, and the origin should
# be reusable for that tag.

go list -json -m --versions -e vcs-test.golang.org/git/issue61415.git/nested@has-nested
cp stdout has-nested.json
stdout '"Origin":'
stdout '"VCS": "git"'
stdout '"URL":'  # randomly-chosen vcweb localhost URL
stdout '"Subdir": "nested"'
stdout '"TagPrefix": "nested/"'
stdout '"TagSum": "t1:47DEQpj8HBSa\+/TImW\+5JCeuQeRkm5NMpJWZG3hSuFU="'
stdout '"Ref": "refs/tags/has-nested"'
stdout '"Hash": "08a4fa6bb9c04ffba03b26ae427b0d6335d90a2a"'

go list -reuse=has-nested.json -json -m --versions -e vcs-test.golang.org/git/issue61415.git/nested@has-nested
stdout '"Origin":'
stdout '"VCS": "git"'
stdout '"URL":'  # randomly-chosen vcweb localhost URL
stdout '"Subdir": "nested"'
stdout '"TagPrefix": "nested/"'
stdout '"TagSum": "t1:47DEQpj8HBSa\+/TImW\+5JCeuQeRkm5NMpJWZG3hSuFU="'
stdout '"Ref": "refs/tags/has-nested"'
stdout '"Hash": "08a4fa6bb9c04ffba03b26ae427b0d6335d90a2a"'
stdout '"Reuse": true'


# Experiment case: if the nested module doesn't exist at "latest",
# the Origin metadata should include the ref that we tried to resolve
# (HEAD for a repo without version tags) and the hash to which it refers,
# so that changing the HEAD ref will invalidate the result.

go list -json -m --versions -e vcs-test.golang.org/git/issue61415.git/nested@latest
cp stdout no-nested.json
stdout '"Err": "module vcs-test.golang.org/git/issue61415.git/nested: no matching versions for query \\"latest\\""'
stdout '"URL":'  # randomly-chosen vcweb localhost URL
stdout '"Subdir": "nested"'
stdout '"TagPrefix": "nested/"'
stdout '"TagSum": "t1:47DEQpj8HBSa\+/TImW\+5JCeuQeRkm5NMpJWZG3hSuFU="'

stdout '"Ref": "HEAD"'
stdout '"Hash": "f213069baa68ec26412fb373c7cf6669db1f8e69"'

# The error result should be reusable.

go list -reuse=no-nested.json -json -m --versions -e vcs-test.golang.org/git/issue61415.git/nested@latest

stdout '"Err": "module vcs-test.golang.org/git/issue61415.git/nested: no matching versions for query \\"latest\\""'
stdout '"URL":'  # randomly-chosen vcweb localhost URL
stdout '"Subdir": "nested"'
stdout '"TagPrefix": "nested/"'
stdout '"TagSum": "t1:47DEQpj8HBSa\+/TImW\+5JCeuQeRkm5NMpJWZG3hSuFU="'
stdout '"Ref": "HEAD"'
stdout '"Hash": "f213069baa68ec26412fb373c7cf6669db1f8e69"'
stdout '"Reuse": true'


# If the hash refers to some other commit instead, the
# result should not be reused.

replace f213069baa68ec26412fb373c7cf6669db1f8e69 08a4fa6bb9c04ffba03b26ae427b0d6335d90a2a no-nested.json

go list -reuse=no-nested.json -json -m --versions -e vcs-test.golang.org/git/issue61415.git/nested@latest
stdout '"Err": "module vcs-test.golang.org/git/issue61415.git/nested: no matching versions for query \\"latest\\""'
stdout '"URL":'  # randomly-chosen vcweb localhost URL
stdout '"Subdir": "nested"'
stdout '"TagPrefix": "nested/"'
stdout '"TagSum": "t1:47DEQpj8HBSa\+/TImW\+5JCeuQeRkm5NMpJWZG3hSuFU="'
stdout '"Ref": "HEAD"'
stdout '"Hash": "f213069baa68ec26412fb373c7cf6669db1f8e69"'
! stdout '"Reuse"'
