cmd/go: implement accurate pseudo-versions for Mercurial

This is CL 124515 (which only did Git) but for Mercurial,
quite a few years late. I'm not sure why we didn't do it
at the time - probably Mercurial blindness. Do it now.

Change-Id: I28f448a19143f7ce3de337cd1891bae86023b499
Reviewed-on: https://go-review.googlesource.com/c/go/+/718502
Reviewed-by: Michael Matloob <matloob@golang.org>
Reviewed-by: Michael Matloob <matloob@google.com>
Auto-Submit: Russ Cox <rsc@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Russ Cox 2025-11-06 13:55:25 -05:00 committed by Gopher Robot
parent b709a3e8b4
commit 2750f95291
7 changed files with 407 additions and 44 deletions

View file

@ -794,15 +794,18 @@ func (r *gitRepo) RecentTag(ctx context.Context, rev, prefix string, allowed fun
// There are plausible tags, but we don't know if rev is a descendent of any of them.
// Fetch the history to find out.
// Note: do not use defer unlock, because describe calls allowed,
// which uses retracted, which calls ReadFile, which may end up
// back at a method that acquires r.mu.
unlock, err := r.mu.Lock()
if err != nil {
return "", err
}
defer unlock()
if err := r.fetchRefsLocked(ctx); err != nil {
unlock()
return "", err
}
unlock()
// If we've reached this point, we have all of the commits that are reachable
// from all heads and tags.

View file

@ -25,6 +25,8 @@ import (
"cmd/go/internal/lockedfile"
"cmd/go/internal/str"
"cmd/internal/par"
"golang.org/x/mod/semver"
)
// A VCSError indicates an error using a version control system.
@ -167,6 +169,8 @@ type vcsCmd struct {
parseStat func(rev, out string) (*RevInfo, error) // func to parse output of statLocal
fetch []string // cmd to fetch everything from remote
latest string // name of latest commit on remote (tip, HEAD, etc)
descendsFrom func(rev, tag string) []string // cmd to check whether rev descends from tag
recentTags func(rev string) []string // cmd to print tag ancestors of rev
readFile func(rev, file, remote string) []string // cmd to read rev's file
readZip func(rev, subdir, remote, target string) []string // cmd to read rev's subdir as zip file
@ -217,6 +221,12 @@ var vcsCmds = map[string]*vcsCmd{
parseStat: hgParseStat,
fetch: []string{"hg", "pull", "-f"},
latest: "tip",
descendsFrom: func(rev, tag string) []string {
return []string{"hg", "log", "-r", "ancestors(" + rev + ") and " + tag}
},
recentTags: func(rev string) []string {
return []string{"hg", "log", "-r", "ancestors(" + rev + ") and tag()", "--template", "{tags}\n"}
},
readFile: func(rev, file, remote string) []string {
return []string{"hg", "cat", "-r", rev, file}
},
@ -558,16 +568,37 @@ func (r *vcsRepo) ReadFile(ctx context.Context, rev, file string, maxSize int64)
}
func (r *vcsRepo) RecentTag(ctx context.Context, rev, prefix string, allowed func(string) bool) (tag string, err error) {
// We don't technically need to lock here since we're returning an error
// unconditionally, but doing so anyway will help to avoid baking in
// lock-inversion bugs.
// Only lock for the subprocess execution, not for the tag scan.
// allowed may call other methods that acquire the lock.
unlock, err := r.mu.Lock()
if err != nil {
return "", err
}
defer unlock()
return "", vcsErrorf("vcs %s: RecentTag: %w", r.cmd.vcs, errors.ErrUnsupported)
if r.cmd.recentTags == nil {
unlock()
return "", vcsErrorf("vcs %s: RecentTag: %w", r.cmd.vcs, errors.ErrUnsupported)
}
out, err := Run(ctx, r.dir, r.cmd.recentTags(rev))
unlock()
if err != nil {
return "", err
}
highest := ""
for _, tag := range strings.Fields(string(out)) {
if !strings.HasPrefix(tag, prefix) || !allowed(tag) {
continue
}
semtag := tag[len(prefix):]
if semver.Compare(semtag, highest) > 0 {
highest = semtag
}
}
if highest != "" {
return prefix + highest, nil
}
return "", nil
}
func (r *vcsRepo) DescendsFrom(ctx context.Context, rev, tag string) (bool, error) {
@ -577,7 +608,15 @@ func (r *vcsRepo) DescendsFrom(ctx context.Context, rev, tag string) (bool, erro
}
defer unlock()
return false, vcsErrorf("vcs %s: DescendsFrom: %w", r.cmd.vcs, errors.ErrUnsupported)
if r.cmd.descendsFrom == nil {
return false, vcsErrorf("vcs %s: DescendsFrom: %w", r.cmd.vcs, errors.ErrUnsupported)
}
out, err := Run(ctx, r.dir, r.cmd.descendsFrom(rev, tag))
if err != nil {
return false, err
}
return strings.TrimSpace(string(out)) != "", nil
}
func (r *vcsRepo) ReadZip(ctx context.Context, rev, subdir string, maxSize int64) (zip io.ReadCloser, err error) {

View file

@ -1,82 +1,83 @@
env GO111MODULE=on
# Testing git->module converter's generation of +incompatible tags; turn off proxy.
[!net:github.com] skip
[!git] skip
[short] skip
# Testing git->module converter's generation of +incompatible tags; turn off proxy.
env GOPROXY=direct
env GOSUMDB=off
# We can resolve the @master branch without unshallowing the local repository
# (even with older gits), so try that before we do anything else.
# (This replicates https://golang.org/issue/26713 with git 2.7.4.)
go get github.com/rsc/legacytest@master
go get vcs-test.golang.org/git/legacytest.git@master
go list -m all
stdout '^github.com/rsc/legacytest v2\.0\.1-0\.\d{14}-7303f7796364\+incompatible$'
stdout '^vcs-test.golang.org/git/legacytest.git v2\.0\.1-0\.\d{14}-7303f7796364\+incompatible$'
# get should include incompatible tags in "latest" calculation.
go mod edit -droprequire github.com/rsc/legacytest
go get github.com/rsc/legacytest@latest
go mod edit -droprequire vcs-test.golang.org/git/legacytest.git
go get vcs-test.golang.org/git/legacytest.git@latest
go list
go list -m all
stdout '^github.com/rsc/legacytest v2\.0\.0\+incompatible$'
stdout '^vcs-test.golang.org/git/legacytest.git v2\.0\.0\+incompatible$'
# v2.0.1-0.pseudo+incompatible
go get ...test@7303f77
go get ...test.git@7303f77
go list -m all
stdout '^github.com/rsc/legacytest v2\.0\.1-0\.\d{14}-7303f7796364\+incompatible$'
stdout '^vcs-test.golang.org/git/legacytest.git v2\.0\.1-0\.\d{14}-7303f7796364\+incompatible$'
# v2.0.0+incompatible by tag+incompatible
go get ...test@v2.0.0+incompatible
go get ...test.git@v2.0.0+incompatible
go list -m all
stdout '^github.com/rsc/legacytest v2\.0\.0\+incompatible$'
stdout '^vcs-test.golang.org/git/legacytest.git v2\.0\.0\+incompatible$'
# v2.0.0+incompatible by tag
go get ...test@v2.0.0
go get ...test.git@v2.0.0
go list -m all
stdout '^github.com/rsc/legacytest v2\.0\.0\+incompatible$'
stdout '^vcs-test.golang.org/git/legacytest.git v2\.0\.0\+incompatible$'
# v2.0.0+incompatible by hash (back on master)
go get ...test@d7ae1e4
go get ...test.git@d7ae1e4
go list -m all
stdout '^github.com/rsc/legacytest v2\.0\.0\+incompatible$'
stdout '^vcs-test.golang.org/git/legacytest.git v2\.0\.0\+incompatible$'
# v1.2.1-0.pseudo
go get ...test@d2d4c3e
go get ...test.git@d2d4c3e
go list -m all
stdout '^github.com/rsc/legacytest v1\.2\.1-0\.\d{14}-d2d4c3ea6623$'
stdout '^vcs-test.golang.org/git/legacytest.git v1\.2\.1-0\.\d{14}-d2d4c3ea6623$'
# v1.2.0
go get ...test@9f6f860
go get ...test.git@9f6f860
go list -m all
stdout '^github.com/rsc/legacytest v1\.2\.0$'
stdout '^vcs-test.golang.org/git/legacytest.git v1\.2\.0$'
# v1.1.0-pre.0.pseudo
go get ...test@fb3c628
go get ...test.git@fb3c628
go list -m all
stdout '^github.com/rsc/legacytest v1\.1\.0-pre\.0\.\d{14}-fb3c628075e3$'
stdout '^vcs-test.golang.org/git/legacytest.git v1\.1\.0-pre\.0\.\d{14}-fb3c628075e3$'
# v1.1.0-pre (no longer on master)
go get ...test@731e3b1
go get ...test.git@731e3b1
go list -m all
stdout '^github.com/rsc/legacytest v1\.1\.0-pre$'
stdout '^vcs-test.golang.org/git/legacytest.git v1\.1\.0-pre$'
# v1.0.1-0.pseudo
go get ...test@fa4f5d6
go get ...test.git@fa4f5d6
go list -m all
stdout '^github.com/rsc/legacytest v1\.0\.1-0\.\d{14}-fa4f5d6a71c6$'
stdout '^vcs-test.golang.org/git/legacytest.git v1\.0\.1-0\.\d{14}-fa4f5d6a71c6$'
# v1.0.0
go get ...test@7fff7f3
go get ...test.git@7fff7f3
go list -m all
stdout '^github.com/rsc/legacytest v1\.0\.0$'
stdout '^vcs-test.golang.org/git/legacytest.git v1\.0\.0$'
# v0.0.0-pseudo
go get ...test@52853eb
go get ...test.git@52853eb
go list -m all
stdout '^github.com/rsc/legacytest v0\.0\.0-\d{14}-52853eb7b552$'
stdout '^vcs-test.golang.org/git/legacytest.git v0\.0\.0-\d{14}-52853eb7b552$'
-- go.mod --
module x
-- x.go --
package x
import "github.com/rsc/legacytest"
import "vcs-test.golang.org/git/legacytest.git"

View file

@ -0,0 +1,81 @@
env GO111MODULE=on
[!exec:hg] skip
[short] skip
# Testing hg->module converter's generation of +incompatible tags; turn off proxy.
env GOPROXY=direct
env GOSUMDB=off
# get default
go get vcs-test.golang.org/hg/legacytest.hg@default
go list -m all
stdout '^vcs-test.golang.org/hg/legacytest.hg v1\.2\.1-0\.20180717164942-2840708d1294$'
# get should include incompatible tags in "latest" calculation.
go mod edit -droprequire vcs-test.golang.org/hg/legacytest.hg
go get vcs-test.golang.org/hg/legacytest.hg@latest
go list
go list -m all
stdout '^vcs-test.golang.org/hg/legacytest.hg v2\.0\.0\+incompatible$'
# v2.0.1-0.pseudo+incompatible
go get ...test.hg@d6ad6040
go list -m all
stdout '^vcs-test.golang.org/hg/legacytest.hg v2\.0\.1-0\.\d{14}-d6ad604046f6\+incompatible$'
# v2.0.0+incompatible by tag+incompatible
go get ...test.hg@v2.0.0+incompatible
go list -m all
stdout '^vcs-test.golang.org/hg/legacytest.hg v2\.0\.0\+incompatible$'
# v2.0.0+incompatible by tag
go get ...test.hg@v2.0.0
go list -m all
stdout '^vcs-test.golang.org/hg/legacytest.hg v2\.0\.0\+incompatible$'
# v2.0.0+incompatible by hash (back on master)
go get ...test.hg@e64782f
go list -m all
stdout '^vcs-test.golang.org/hg/legacytest.hg v2\.0\.0\+incompatible$'
# v1.2.1-0.pseudo
go get ...test.hg@ed9a22e
go list -m all
stdout '^vcs-test.golang.org/hg/legacytest.hg v1\.2\.1-0\.\d{14}-ed9a22ebb8a1$'
# v1.2.0
go get ...test.hg@07462d
go list -m all
stdout '^vcs-test.golang.org/hg/legacytest.hg v1\.2\.0$'
# v1.1.0-pre.0.pseudo
go get ...test.hg@accb16
go list -m all
stdout '^vcs-test.golang.org/hg/legacytest.hg v1\.1\.0-pre\.0\.\d{14}-accb169a3696$'
# v1.1.0-pre (no longer on master)
go get ...test.hg@90da67a9
go list -m all
stdout '^vcs-test.golang.org/hg/legacytest.hg v1\.1\.0-pre$'
# v1.0.1-0.pseudo
go get ...test.hg@c6260a
go list -m all
stdout '^vcs-test.golang.org/hg/legacytest.hg v1\.0\.1-0\.\d{14}-c6260ab8dc3e$'
# v1.0.0
go get ...test.hg@d6ad17
go list -m all
stdout '^vcs-test.golang.org/hg/legacytest.hg v1\.0\.0$'
# v0.0.0-pseudo
go get ...test.hg@ee0106d
go list -m all
stdout '^vcs-test.golang.org/hg/legacytest.hg v0\.0\.0-\d{14}-ee0106da3c7c$'
-- go.mod --
module x
-- x.go --
package x
import "vcs-test.golang.org/hg/legacytest.hg"

View file

@ -128,7 +128,7 @@ stdout '"Hash": "1e531550e864b16f25013cfbbf2d8e7cf07a0374"'
# go mod download vcstest/tagtests@default needs a RepoSum again
go mod download -x -json vcs-test.golang.org/hg/tagtests.hg@default
cp stdout tagtestsdefault.json
stdout '"Version": "v0.0.0-20190509225625-8d0b18b816df"'
stdout '"Version": "v0.2.3-0.20190509225625-8d0b18b816df"'
stdout '"Query": "default"'
stdout '"VCS": "hg"'
stdout '"URL": ".*/hg/tagtests"'
@ -292,10 +292,7 @@ go clean -modcache
go mod download -reuse=tagtestsdefault.json -x -json vcs-test.golang.org/hg/tagtests.hg@default
! stderr 'hg( .*)* pull'
stdout '"Reuse": true'
# NOTE: Strictly speaking this should be v0.2.3-... but we never
# implemented Mercurial support for finding ancestor tags to
# create pseudo-versions.
stdout '"Version": "v0.0.0-20190509225625-8d0b18b816df"'
stdout '"Version": "v0.2.3-0.20190509225625-8d0b18b816df"'
stdout '"Query": "default"'
stdout '"VCS": "hg"'
stdout '"URL": ".*/hg/tagtests"'
@ -310,7 +307,7 @@ go clean -modcache
go mod download -reuse=all.json -x -json vcs-test.golang.org/hg/tagtests.hg@default
! stderr 'hg( .*)* pull'
stdout '"Reuse": true'
stdout '"Version": "v0.0.0-20190509225625-8d0b18b816df"'
stdout '"Version": "v0.2.3-0.20190509225625-8d0b18b816df"'
stdout '"Query": "default"'
stdout '"VCS": "hg"'
stdout '"URL": ".*/hg/tagtests"'

View file

@ -0,0 +1,118 @@
handle git
env GIT_AUTHOR_NAME='Russ Cox'
env GIT_AUTHOR_EMAIL='rsc@golang.org'
env GIT_COMMITTER_NAME=$GIT_AUTHOR_NAME
env GIT_COMMITTER_EMAIL=$GIT_AUTHOR_EMAIL
git init
git branch -M master
at 2018-07-17T12:41:39-04:00
cp x_cf92c7b.go x.go
git add x.go
git commit -m 'initial commit'
at 2018-07-17T12:41:57-04:00
cp x_52853eb.go x.go
git commit -m 'add X' x.go
at 2018-07-17T12:42:07-04:00
cp x_7fff7f3.go x.go
git commit -m 'gofmt' x.go
git tag v1.0.0
at 2018-07-17T12:42:28-04:00
cp x_fa4f5d6.go x.go
git commit -m 'X->XX' x.go
at 2018-07-17T12:42:36-04:00
cp x_d7ae1e4.go x.go
git commit -m 'gofmt' x.go
git tag v2.0.0
at 2018-07-17T12:42:53-04:00
cp x_7303f77.go x.go
git commit -m 'add XXX' x.go
at 2018-07-17T12:47:59-04:00
git checkout v1.0.0
cp x_1abc5ff.go x.go
git commit -m 'comment' x.go
at 2018-07-17T12:48:22-04:00
cp x_731e3b1.go x.go
git commit -m 'prerelease' x.go
git tag v1.1.0-pre
at 2018-07-17T12:48:49-04:00
cp x_fb3c628.go x.go
git commit -m 'working' x.go
at 2018-07-17T12:49:05-04:00
cp x_9f6f860.go x.go
git commit -m 'v1.2.0' x.go
git tag v1.2.0
at 2018-07-17T12:49:42-04:00
cp x_d2d4c3e.go x.go
git commit -m 'more' x.go
git tag morework
git show-ref --tags --heads
cmp stdout .git-refs
-- .git-refs --
7303f77963648d5f1ec5e55eccfad8e14035866c refs/heads/master
d2d4c3ea66230e7ad6fbd8f0ecd8c0f851392364 refs/tags/morework
7fff7f3417faa4a795f9518bc2bef05147a1d6c0 refs/tags/v1.0.0
731e3b12a0272dcafb560b8fa6a4e9ffb20ef5c9 refs/tags/v1.1.0-pre
9f6f860fe5c92cd835fdde2913aca8db9ce63373 refs/tags/v1.2.0
d7ae1e4b368320e7a577fc8a9efc1e78aacac52a refs/tags/v2.0.0
-- x_1abc5ff.go --
package legacytest
// add comment
const X = 1
-- x_52853eb.go --
package legacytest
const X = 1
-- x_7303f77.go --
package legacytest
const XX = 2
const XXX = 3
-- x_731e3b1.go --
package legacytest
// add comment again
const X = 1
-- x_7fff7f3.go --
package legacytest
const X = 1
-- x_9f6f860.go --
package legacytest
// add comment again!!!
const X = 1
-- x_cf92c7b.go --
package legacytest
-- x_d2d4c3e.go --
package legacytest
// add comment hack hack hack
const X = 1
-- x_d7ae1e4.go --
package legacytest
const XX = 2
-- x_fa4f5d6.go --
package legacytest
const XX = 2
-- x_fb3c628.go --
package legacytest
// add comment fixed
const X = 1

View file

@ -0,0 +1,124 @@
handle hg
env user='Russ Cox <rsc@golang.org>'
hg init
env date=2018-07-17T12:41:39-04:00
cp x_cf92c7b.go x.go
hg add x.go
hg commit --user=$user --date=$date -m 'initial commit'
env date=2018-07-17T12:41:57-04:00
cp x_52853eb.go x.go
hg commit --user=$user --date=$date -m 'add X' x.go
env date=2018-07-17T12:42:07-04:00
cp x_7fff7f3.go x.go
hg commit --user=$user --date=$date -m 'gofmt' x.go
hg tag --user=$user --date=$date v1.0.0
env date=2018-07-17T12:42:28-04:00
cp x_fa4f5d6.go x.go
hg commit --user=$user --date=$date -m 'X->XX' x.go
env date=2018-07-17T12:42:36-04:00
cp x_d7ae1e4.go x.go
hg commit --user=$user --date=$date -m 'gofmt' x.go
hg tag --user=$user --date=$date v2.0.0
env date=2018-07-17T12:42:53-04:00
cp x_7303f77.go x.go
hg commit --user=$user --date=$date -m 'add XXX' x.go
env date=2018-07-17T12:47:59-04:00
hg update v1.0.0
cp x_1abc5ff.go x.go
hg commit --user=$user --date=$date -m 'comment' x.go
env date=2018-07-17T12:48:22-04:00
cp x_731e3b1.go x.go
hg commit --user=$user --date=$date -m 'prerelease' x.go
hg tag --user=$user --date=$date v1.1.0-pre
env date=2018-07-17T12:48:49-04:00
cp x_fb3c628.go x.go
hg commit --user=$user --date=$date -m 'working' x.go
env date=2018-07-17T12:49:05-04:00
cp x_9f6f860.go x.go
hg commit --user=$user --date=$date -m 'v1.2.0' x.go
hg tag --user=$user --date=$date v1.2.0
env date=2018-07-17T12:49:42-04:00
cp x_d2d4c3e.go x.go
hg commit --user=$user --date=$date -m 'more' x.go
hg tag --user=$user --date=$date morework
hg log -r ':' --template '{node|short} {desc|strip|firstline}\n'
cmp stdout .hg-log
-- .hg-log --
9dc9138de2e5 initial commit
ee0106da3c7c add X
d6ad170f61d4 gofmt
90c54d4351ee Added tag v1.0.0 for changeset d6ad170f61d4
c6260ab8dc3e X->XX
e64782fcadfd gofmt
d6ad604046f6 Added tag v2.0.0 for changeset e64782fcadfd
663753d3ac63 add XXX
4555a6dd66c0 comment
90da67a9bf0c prerelease
d7c15fbd635d Added tag v1.1.0-pre for changeset 90da67a9bf0c
accb169a3696 working
07462d11385f v1.2.0
ed9a22ebb8a1 Added tag v1.2.0 for changeset 07462d11385f
498b291aa133 more
2840708d1294 Added tag morework for changeset 498b291aa133
-- x_1abc5ff.go --
package legacytest
// add comment
const X = 1
-- x_52853eb.go --
package legacytest
const X = 1
-- x_7303f77.go --
package legacytest
const XX = 2
const XXX = 3
-- x_731e3b1.go --
package legacytest
// add comment again
const X = 1
-- x_7fff7f3.go --
package legacytest
const X = 1
-- x_9f6f860.go --
package legacytest
// add comment again!!!
const X = 1
-- x_cf92c7b.go --
package legacytest
-- x_d2d4c3e.go --
package legacytest
// add comment hack hack hack
const X = 1
-- x_d7ae1e4.go --
package legacytest
const XX = 2
-- x_fa4f5d6.go --
package legacytest
const XX = 2
-- x_fb3c628.go --
package legacytest
// add comment fixed
const X = 1