Skip to content

Commit 3e727a9

Browse files
committed
cmd/go/internal/vcs: include Subversion VCS build information
The existing implementation lacks the Status function for retrieving VCS build information for Subversion. As a consequence, binaries aren't stamped with the Revision, CommitTime and Uncommitted information from SVN repositories. This change provides the svnStatus function and retrieves the information by running svn info and svn status commands.
1 parent 352dd2d commit 3e727a9

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

src/cmd/go/internal/vcs/vcs.go

+30
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ var vcsSvn = &Cmd{
498498
Scheme: []string{"https", "http", "svn", "svn+ssh"},
499499
PingCmd: "info -- {scheme}://{repo}",
500500
RemoteRepo: svnRemoteRepo,
501+
Status: svnStatus,
501502
}
502503

503504
func svnRemoteRepo(vcsSvn *Cmd, rootDir string) (remoteRepo string, err error) {
@@ -530,6 +531,35 @@ func svnRemoteRepo(vcsSvn *Cmd, rootDir string) (remoteRepo string, err error) {
530531
return strings.TrimSpace(out), nil
531532
}
532533

534+
func svnStatus(vcsSvn *Cmd, rootDir string) (Status, error) {
535+
out, err := vcsSvn.runOutputVerboseOnly(rootDir, "info --show-item last-changed-revision")
536+
if err != nil {
537+
return Status{}, err
538+
}
539+
rev := strings.TrimSpace(string(out))
540+
541+
out, err = vcsSvn.runOutputVerboseOnly(rootDir, "info --show-item last-changed-date")
542+
if err != nil {
543+
return Status{}, err
544+
}
545+
commitTime, err := time.Parse(time.RFC3339, strings.TrimSpace(string(out)))
546+
if err != nil {
547+
return Status{}, fmt.Errorf("unable to parse output of svn info: %v", err)
548+
}
549+
550+
out, err = vcsSvn.runOutputVerboseOnly(rootDir, "status")
551+
if err != nil {
552+
return Status{}, err
553+
}
554+
uncommitted := len(out) > 0
555+
556+
return Status{
557+
Revision: rev,
558+
CommitTime: commitTime,
559+
Uncommitted: uncommitted,
560+
}, nil
561+
}
562+
533563
// fossilRepoName is the name go get associates with a fossil repository. In the
534564
// real world the file can be named anything.
535565
const fossilRepoName = ".fossil"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# This test checks that VCS information is stamped into Go binaries by default,
2+
# controlled with -buildvcs. This test focuses on Subversion specifics.
3+
# The Git test covers common functionality.
4+
5+
[!exec:svn] skip
6+
[!exec:svnadmin] skip
7+
[short] skip
8+
env GOBIN=$WORK/gopath/bin
9+
env oldpath=$PATH
10+
cd repo/a
11+
12+
# If there's no local repository, there's no VCS info.
13+
go install
14+
go version -m $GOBIN/a$GOEXE
15+
! stdout vcs.revision
16+
stdout '\s+mod\s+example.com/a\s+\(devel\)'
17+
rm $GOBIN/a$GOEXE
18+
19+
# If there is a repository, but it can't be used for some reason,
20+
# there should be an error. It should hint about -buildvcs=false.
21+
cd ..
22+
mkdir .svn
23+
env PATH=$WORK${/}fakebin${:}$oldpath
24+
chmod 0755 $WORK/fakebin/svn
25+
! exec svn help
26+
cd a
27+
! go install
28+
stderr '^error obtaining VCS status: exit status 1\n\tUse -buildvcs=false to disable VCS stamping.$'
29+
rm $GOBIN/a$GOEXE
30+
cd ..
31+
env PATH=$oldpath
32+
rm .svn
33+
34+
# Untagged repo.
35+
exec svnadmin create repo
36+
exec svn checkout file://$PWD/repo workingDir
37+
cd workingDir
38+
cp ../a/a.go .
39+
cp ../a/go.mod .
40+
cp ../README .
41+
exec svn status
42+
exec svn add a.go go.mod README
43+
exec svn commit -m 'initial commit'
44+
exec svn update
45+
go install
46+
go version -m $GOBIN/a$GOEXE
47+
stdout '^\tbuild\tvcs.revision=1$'
48+
stdout '^\tbuild\tvcs.modified=false$'
49+
stdout '^\tmod\texample.com/a\tv0.0.0-\d+-\d+\t+'
50+
rm $GOBIN/a$GOEXE
51+
52+
# Building with -buildvcs=false suppresses the info.
53+
go install -buildvcs=false
54+
go version -m $GOBIN/a$GOEXE
55+
! stdout vcs.revision
56+
stdout '\s+mod\s+example.com/a\s+\(devel\)'
57+
rm $GOBIN/a$GOEXE
58+
59+
# An untracked file is shown as uncommitted, even if it isn't part of the build.
60+
cp ../../outside/empty.txt extra.txt
61+
go install
62+
go version -m $GOBIN/a$GOEXE
63+
stdout '^\tbuild\tvcs.modified=true$'
64+
stdout '\s+mod\s+example.com/a\s+v0.0.0-\d+-\d+\+dirty\s+'
65+
rm extra.txt
66+
rm $GOBIN/a$GOEXE
67+
68+
# An edited file is shown as uncommitted, even if it isn't part of the build.
69+
cp ../../outside/empty.txt README
70+
go install
71+
go version -m $GOBIN/a$GOEXE
72+
stdout '^\tbuild\tvcs.modified=true$'
73+
stdout '\s+mod\s+example.com/a\s+v0.0.0-\d+-\d+\+dirty\s+'
74+
exec svn revert README
75+
rm $GOBIN/a$GOEXE
76+
77+
-- $WORK/fakebin/svn --
78+
#!/bin/sh
79+
exit 1
80+
-- $WORK/fakebin/svn.bat --
81+
exit 1
82+
-- repo/README --
83+
Far out in the uncharted backwaters of the unfashionable end of the western
84+
spiral arm of the Galaxy lies a small, unregarded yellow sun.
85+
-- repo/a/go.mod --
86+
module example.com/a
87+
88+
go 1.18
89+
-- repo/a/a.go --
90+
package main
91+
92+
func main() {}
93+
94+
-- outside/empty.txt --

0 commit comments

Comments
 (0)