Skip to content

Commit 2311b76

Browse files
committed
add and push new patch tag with -incpatch
1 parent d5405b1 commit 2311b76

6 files changed

Lines changed: 80 additions & 13 deletions

File tree

main.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var (
3131
flagGoPackage = flag.Bool("gopackage", false, "write Go source with PkgName and PkgVersion")
3232
flagNoFetch = flag.Bool("nofetch", false, "don't fetch remote tags")
3333
flagNoNewline = flag.Bool("nonewline", false, "don't print a newline after the output")
34-
flagIncPatch = flag.Bool("incpatch", false, "increment the patch level of the version")
34+
flagIncPatch = flag.Bool("incpatch", false, "increment the patch level and create a new tag")
3535
)
3636

3737
func mainfn() int {
@@ -42,6 +42,7 @@ func mainfn() int {
4242

4343
vs, err := gitsemver.New(*flagGit)
4444
if err == nil {
45+
var createTag string
4546
if repoDir, err = vs.Git.CheckGitRepo(repoDir); err == nil {
4647
if !*flagNoFetch {
4748
err = vs.Git.FetchTags(repoDir)
@@ -50,7 +51,7 @@ func mainfn() int {
5051
var vi gitsemver.VersionInfo
5152
if vi, err = vs.GetVersion(repoDir); err == nil {
5253
if *flagIncPatch {
53-
vi.IncPatch()
54+
createTag = vi.IncPatch()
5455
}
5556
content := vi.Version()
5657
if *flagGoPackage {
@@ -65,12 +66,17 @@ func mainfn() int {
6566
content += "\n"
6667
}
6768
if err = writeOutput(outpath, content); err == nil {
68-
return 0
69+
if err = vs.Git.CreateTag(repoDir, createTag); err == nil {
70+
if err = vs.Git.PushTag(repoDir, createTag); err == nil {
71+
return 0
72+
}
73+
}
6974
}
7075
}
7176
}
7277
}
7378
}
79+
_ = vs.Git.DeleteTag(repoDir, createTag)
7480
}
7581

7682
retv := 125

main_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ func TestMainFn(t *testing.T) {
1111
flag.Parse()
1212
*flagGoPackage = true
1313
*flagOut = "test.out"
14-
*flagIncPatch = true
1514
mainfn()
1615
b, err := os.ReadFile("test.out")
1716
if err == nil {
@@ -26,7 +25,6 @@ func TestMainFn(t *testing.T) {
2625
}
2726

2827
func TestMainError(t *testing.T) {
29-
flag.Parse()
3028
exitFn = func(i int) {
3129
if i == 0 {
3230
t.Error(i)
@@ -35,6 +33,8 @@ func TestMainError(t *testing.T) {
3533
t.Log("didn't get a syscall.Errno")
3634
}
3735
}
36+
flag.Parse()
3837
*flagOut = "/proc/.nonexistant"
38+
*flagIncPatch = true
3939
main()
4040
}

pkg/gitter.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ type Gitter interface {
3030
GetBuild(repo string) string
3131
// FetchTags calls "git fetch --tags"
3232
FetchTags(repo string) error
33+
// CreateTag creates a new lightweight tag. Does nothing if tag is empty.
34+
CreateTag(repo, tag string) error
35+
// DeleteTag deletes the given tag. Does nothing if tag is empty.
36+
DeleteTag(repo, tag string) (err error)
37+
// PushTag pushes the given tag to the origin. Does nothing if tag is empty.
38+
PushTag(repo, tag string) (err error)
3339
}
3440

3541
type DefaultGitter string
@@ -47,12 +53,6 @@ var ErrNotDirectory = errors.New("not a directory")
4753
// Returns nil if it is, else an error.
4854
func checkDir(dir string) (err error) {
4955
_, err = os.ReadDir(dir)
50-
/*var fi os.FileInfo
51-
if fi, err = os.Stat(dir); err == nil {
52-
if !fi.IsDir() {
53-
err = ErrNotDirectory
54-
}
55-
}*/
5656
return
5757
}
5858

@@ -174,3 +174,24 @@ func (dg DefaultGitter) FetchTags(repo string) (err error) {
174174
err = exec.Command(string(dg), "-C", repo, "fetch", "--tags").Run() /* #nosec G204 */
175175
return
176176
}
177+
178+
func (dg DefaultGitter) CreateTag(repo, tag string) (err error) {
179+
if tag != "" {
180+
err = exec.Command(string(dg), "-C", repo, "tag", tag).Run() /* #nosec G204 */
181+
}
182+
return
183+
}
184+
185+
func (dg DefaultGitter) DeleteTag(repo, tag string) (err error) {
186+
if tag != "" {
187+
err = exec.Command(string(dg), "-C", repo, "tag", "-d", tag).Run() /* #nosec G204 */
188+
}
189+
return
190+
}
191+
192+
func (dg DefaultGitter) PushTag(repo, tag string) (err error) {
193+
if tag != "" {
194+
err = exec.Command(string(dg), "-C", repo, "push", "origin", tag).Run() /* #nosec G204 */
195+
}
196+
return
197+
}

pkg/gitter_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,29 @@ func Test_DefaultGitter_FetchTags(t *testing.T) {
193193
}
194194
dg.FetchTags(".")
195195
}
196+
197+
func Test_DefaultGitter_CreateDeleteTag(t *testing.T) {
198+
dg, err := gitsemver.NewDefaultGitter("git")
199+
if err != nil {
200+
t.Error(err)
201+
}
202+
err = dg.CreateTag(".", "test-tag")
203+
if err != nil {
204+
t.Error(err)
205+
}
206+
err = dg.DeleteTag(".", "test-tag")
207+
if err != nil {
208+
t.Error(err)
209+
}
210+
}
211+
212+
func Test_DefaultGitter_PushTag(t *testing.T) {
213+
dg, err := gitsemver.NewDefaultGitter("git")
214+
if err != nil {
215+
t.Error(err)
216+
}
217+
err = dg.PushTag(".", "v1.0.0")
218+
if err != nil {
219+
t.Error(err)
220+
}
221+
}

pkg/mockgitter_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,16 @@ func (mg *MockGitter) FetchTags(repo string) error {
138138
return nil
139139
}
140140

141+
func (mg *MockGitter) CreateTag(repo, tag string) (err error) {
142+
return
143+
}
144+
145+
func (mg *MockGitter) DeleteTag(repo, tag string) (err error) {
146+
return
147+
}
148+
149+
func (mg *MockGitter) PushTag(repo, tag string) (err error) {
150+
return
151+
}
152+
141153
var _ gitsemver.Gitter = &MockGitter{}

pkg/versioninfo.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,17 @@ func (vi *VersionInfo) GoPackage(repo, pkgName string) (retv string, err error)
6666
return
6767
}
6868

69-
// IncPatch increments the patch level of the version.
70-
func (vi *VersionInfo) IncPatch() {
69+
// IncPatch increments the patch level of the version, returning the new tag.
70+
func (vi *VersionInfo) IncPatch() string {
7171
for strings.Count(vi.Tag, ".") < 2 {
7272
vi.Tag += ".0"
7373
}
7474
patchindex := strings.LastIndexByte(vi.Tag, '.') + 1
7575
if patchlevel, err := strconv.Atoi(vi.Tag[patchindex:]); err == nil {
7676
vi.Tag = vi.Tag[:patchindex] + strconv.Itoa(patchlevel+1)
77+
vi.SameTree = true
7778
}
79+
return vi.Tag
7880
}
7981

8082
func CleanBranch(branch string) string {

0 commit comments

Comments
 (0)