Skip to content

Commit 511efc6

Browse files
committed
add commit and push
1 parent 9080580 commit 511efc6

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

commands/release.go

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import (
55
"os"
66

77
"github.com/barelyhuman/commitlog/pkg"
8+
"github.com/go-git/go-git/v5"
9+
"github.com/go-git/go-git/v5/config"
10+
"github.com/go-git/go-git/v5/plumbing"
811
"github.com/urfave/cli/v2"
912
)
1013

@@ -46,13 +49,48 @@ func Release(c *cli.Context) (err error) {
4649
return err
4750
}
4851

49-
// TODO:
50-
// add commit
51-
// add tagging
52-
// add push
53-
// methods to the `releaser`
54-
5552
err = os.WriteFile(".commitlog.release", []byte(releaser.String()), os.ModePerm)
53+
if err != nil {
54+
return
55+
}
56+
57+
openRepo, err := git.PlainOpen(c.String("path"))
58+
if err != nil {
59+
return err
60+
}
61+
62+
var commitHash plumbing.Hash
63+
wt, err := openRepo.Worktree()
64+
if err != nil {
65+
return err
66+
}
67+
68+
if c.Bool("commit") {
69+
wt.Add(".commitlog.release")
70+
commitHash, err = wt.Commit("chore: version"+releaser.String(), &git.CommitOptions{})
71+
if err != nil {
72+
return err
73+
}
74+
75+
_, err = openRepo.CreateTag(releaser.String(), commitHash, &git.CreateTagOptions{})
76+
if err != nil {
77+
err = fmt.Errorf("looks like there was error while creating a tag for the version commit, please try again or create a tag manually: %v", err)
78+
return err
79+
}
80+
}
81+
82+
if c.Bool("push") {
83+
_, err := wt.Status()
84+
if err != nil {
85+
return err
86+
}
87+
88+
openRepo.Push(&git.PushOptions{
89+
RemoteName: "origin",
90+
Progress: os.Stdout,
91+
RefSpecs: []config.RefSpec{config.RefSpec("refs/tags/*:refs/tags/*")},
92+
})
93+
}
5694

5795
return err
5896
}

main.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ func main() {
8484
Name: "init",
8585
Usage: "initialise commitlog release",
8686
},
87+
&cli.StringFlag{
88+
Name: "path",
89+
Value: ".",
90+
Aliases: []string{"p"},
91+
Usage: "root with the '.git' folder `PATH`",
92+
},
8793
&cli.BoolFlag{
8894
Name: "pre-release",
8995
Usage: "create a pre-release version. will default to patch increment unless" +
@@ -109,12 +115,12 @@ func main() {
109115
&cli.BoolFlag{
110116
Name: "commit",
111117
Value: false,
112-
Usage: "if true will create a commit, of the changed version",
118+
Usage: "if true will create a commit and tag, of the changed version",
113119
},
114120
&cli.BoolFlag{
115-
Name: "tag",
121+
Name: "push",
116122
Value: false,
117-
Usage: "if true will create a tag, with the given version",
123+
Usage: "if true will create push the created release commit + tag on origin",
118124
},
119125
},
120126
},

0 commit comments

Comments
 (0)