Skip to content

Commit 836c62d

Browse files
committed
refactor to have main.go in root
1 parent 9bbfcc5 commit 836c62d

15 files changed

+246
-76
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
args: ./...
3838

3939
- name: Build
40-
run: go build ./...
40+
run: go build .
4141

4242
- name: actions-goveralls
4343
uses: shogo82148/actions-goveralls@v1

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,28 @@ If the match is not exact or the current branch is not the default branch
1515
or a protected branch, it creates a work-in-progress semver string like `v0.1.2-myfeature.123`.
1616

1717
Supports raw git repositories as well as GitLab and GitHub builders.
18+
19+
## Print current version of a git repository
20+
21+
```sh
22+
$ go install github.com/linkdata/gitsemver@latest
23+
$ gitsemver $HOME/myreleasedpackage
24+
v1.2.3
25+
```
26+
27+
## Generate a go package file with version information
28+
29+
```go
30+
//go:generate go run github.com/linkdata/gitsemver@latest -gopackage -out version.gen.go
31+
```
32+
33+
Generates a file called `version.gen.go` with contents like
34+
35+
```go
36+
// Code generated by gitsemver at 2025-02-10 07:47:15 UTC DO NOT EDIT.
37+
// branch "mybranch", build 456
38+
package mypackage
39+
40+
const PkgName = "mypackage"
41+
const PkgVersion = "v1.2.3-mybranch.456"
42+
```

main.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"flag"
6+
"fmt"
7+
"os"
8+
"path"
9+
"syscall"
10+
11+
gitsemver "github.com/linkdata/gitsemver/pkg"
12+
)
13+
14+
func writeOutput(fileName, content string) (err error) {
15+
f := os.Stdout
16+
if len(fileName) > 0 {
17+
fileName = path.Clean(fileName)
18+
if f, err = os.Create(fileName); err != nil /* #nosec G304 */ {
19+
return
20+
}
21+
defer f.Close()
22+
}
23+
_, err = f.WriteString(content)
24+
return
25+
}
26+
27+
var (
28+
flagGit = flag.String("git", "git", "path to Git executable")
29+
flagOut = flag.String("out", "", "write to file instead of stdout (relative paths are relative to repo)")
30+
flagName = flag.String("name", "", "override the Go PkgName, default is to use last portion of module in go.mod")
31+
flagGoPackage = flag.Bool("gopackage", false, "write Go source with PkgName and PkgVersion")
32+
flagNoFetch = flag.Bool("nofetch", false, "don't fetch remote tags")
33+
flagNoNewline = flag.Bool("nonewline", false, "don't print a newline after the output")
34+
)
35+
36+
func mainfn() int {
37+
repoDir := os.ExpandEnv(flag.Arg(0))
38+
if repoDir == "" {
39+
repoDir = "."
40+
}
41+
42+
vs, err := gitsemver.New(*flagGit)
43+
if err == nil {
44+
if repoDir, err = vs.Git.CheckGitRepo(repoDir); err == nil {
45+
if !*flagNoFetch {
46+
err = vs.Git.FetchTags(repoDir)
47+
}
48+
if err == nil {
49+
var vi gitsemver.VersionInfo
50+
if vi, err = vs.GetVersion(repoDir); err == nil {
51+
content := vi.Version
52+
if *flagGoPackage {
53+
content, err = vi.GoPackage(repoDir, *flagName)
54+
}
55+
if err == nil {
56+
outpath := os.ExpandEnv(*flagOut)
57+
if outpath != "" && !path.IsAbs(outpath) {
58+
outpath = path.Join(repoDir, outpath)
59+
}
60+
if !*flagNoNewline {
61+
content += "\n"
62+
}
63+
if err = writeOutput(outpath, content); err == nil {
64+
return 0
65+
}
66+
}
67+
}
68+
}
69+
}
70+
}
71+
72+
retv := 125
73+
fmt.Fprintln(os.Stderr, err.Error())
74+
if e := errors.Unwrap(err); e != nil {
75+
if errno, ok := e.(syscall.Errno); ok {
76+
retv = int(errno)
77+
}
78+
}
79+
return retv
80+
}
81+
82+
var exitFn func(int) = os.Exit
83+
84+
func main() {
85+
flag.Parse()
86+
exitFn(mainfn())
87+
}

main_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"os"
6+
"strings"
7+
"testing"
8+
)
9+
10+
func TestMainFn(t *testing.T) {
11+
flag.Parse()
12+
*flagGoPackage = true
13+
*flagOut = "test.out"
14+
mainfn()
15+
b, err := os.ReadFile("test.out")
16+
if err == nil {
17+
defer os.Remove("test.out")
18+
s := string(b)
19+
if !strings.Contains(s, "package gitsemver") || !strings.Contains(s, "PkgName = \"gitsemver\"") {
20+
t.Error(s)
21+
}
22+
} else {
23+
t.Error(err)
24+
}
25+
}
26+
27+
func TestMainError(t *testing.T) {
28+
flag.Parse()
29+
exitFn = func(i int) {
30+
if i == 0 {
31+
t.Error(i)
32+
}
33+
if i == 125 {
34+
t.Log("didn't get a syscall.Errno")
35+
}
36+
}
37+
*flagOut = "/proc/.nonexistant"
38+
main()
39+
}
File renamed without changes.

environment_test.go renamed to pkg/environment_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"os"
55
"testing"
66

7-
"github.com/linkdata/gitsemver"
7+
gitsemver "github.com/linkdata/gitsemver/pkg"
88
)
99

1010
func Test_OsEnvironment_Getenv(t *testing.T) {
File renamed without changes.

gitsemver_test.go renamed to pkg/gitsemver_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package gitsemver_test
33
import (
44
"testing"
55

6-
"github.com/linkdata/gitsemver"
6+
gitsemver "github.com/linkdata/gitsemver/pkg"
77
)
88

99
func Test_NewVersionStringer_SucceedsNormally(t *testing.T) {

gitter.go renamed to pkg/gitter.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package gitsemver
22

33
import (
44
"errors"
5-
"fmt"
65
"os"
76
"os/exec"
87
"path"
@@ -42,41 +41,45 @@ func NewDefaultGitter(gitBin string) (gitter Gitter, err error) {
4241
return
4342
}
4443

44+
var ErrNotDirectory = errors.New("not a directory")
45+
4546
// checkDir checks that the given path is accessible and is a directory.
4647
// Returns nil if it is, else an error.
4748
func checkDir(dir string) (err error) {
48-
var fi os.FileInfo
49+
_, err = os.ReadDir(dir)
50+
/*var fi os.FileInfo
4951
if fi, err = os.Stat(dir); err == nil {
5052
if !fi.IsDir() {
51-
err = fmt.Errorf("'%s' is not a directory", dir)
53+
err = ErrNotDirectory
5254
}
53-
}
55+
}*/
5456
return
5557
}
5658

5759
// dirOrParentHasGitSubdir returns the name of a directory containing
5860
// a '.git' subdirectory or an empty string. It searches starting from
5961
// the given directory and looks in that and it's parents.
60-
func dirOrParentHasGitSubdir(dir string) string {
61-
if dir != "/" && dir != "." {
62-
if checkDir(path.Join(dir, ".git")) == nil {
63-
return dir
62+
func dirOrParentHasGitSubdir(s string) (dir string, err error) {
63+
if err = checkDir(path.Join(s, ".git")); err != nil {
64+
s = path.Dir(s)
65+
if s != "/" {
66+
if s, e := dirOrParentHasGitSubdir(s); e == nil {
67+
return s, nil
68+
}
6469
}
65-
return dirOrParentHasGitSubdir(path.Dir(dir))
70+
} else {
71+
dir = s
6672
}
67-
return ""
73+
return
6874
}
6975

7076
// CheckGitRepo checks that the given directory is part of a git repository,
7177
// meaning that it or one of it's parent directories has a '.git' subdirectory.
7278
// If it is, it returns the absolute path of the git repo and a nil error.
7379
func (dg DefaultGitter) CheckGitRepo(dir string) (repo string, err error) {
7480
if dir, err = filepath.Abs(dir); err == nil {
75-
if err = checkDir(dir); err == nil {
76-
if repo = dirOrParentHasGitSubdir(dir); repo == "" {
77-
err = errors.New("can't find .git directory")
78-
repo = dir
79-
}
81+
if repo, err = dirOrParentHasGitSubdir(dir); err != nil {
82+
repo = dir
8083
}
8184
}
8285
return

gitter_test.go renamed to pkg/gitter_test.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"slices"
66
"testing"
77

8-
"github.com/linkdata/gitsemver"
8+
gitsemver "github.com/linkdata/gitsemver/pkg"
99
)
1010

1111
func Test_NewDefaultGitter_SucceedsNormally(t *testing.T) {
@@ -37,7 +37,7 @@ func Test_CheckGitRepo_SucceedsForSubdir(t *testing.T) {
3737
if err != nil {
3838
t.Error(err)
3939
}
40-
repo, err := dg.CheckGitRepo("./subdir")
40+
repo, err := dg.CheckGitRepo("../pkg/subdir")
4141
if err != nil {
4242
t.Error(err)
4343
}
@@ -46,7 +46,7 @@ func Test_CheckGitRepo_SucceedsForSubdir(t *testing.T) {
4646
}
4747
}
4848

49-
func Test_CheckGitRepo_FailsForRootAndFile(t *testing.T) {
49+
func Test_CheckGitRepo_FailsForRoot(t *testing.T) {
5050
dg, err := gitsemver.NewDefaultGitter("git")
5151
if err != nil {
5252
t.Error(err)
@@ -58,13 +58,6 @@ func Test_CheckGitRepo_FailsForRootAndFile(t *testing.T) {
5858
if repo != "/" {
5959
t.Error(repo)
6060
}
61-
repo, err = dg.CheckGitRepo("./subdir/empty.txt")
62-
if err == nil {
63-
t.Error("no error")
64-
}
65-
if repo != "" {
66-
t.Error(repo)
67-
}
6861
}
6962

7063
func Test_CheckGitRepo_IgnoresFileNamedGit(t *testing.T) {

0 commit comments

Comments
 (0)