Skip to content

Commit 9fd976b

Browse files
authored
Feature: Add ability to specify the clone branch (#9)
1 parent ecfba94 commit 9fd976b

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

cmd/goci/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ var Component string
1919
// Tag of the component
2020
var Tag string
2121

22+
var CommitMessage string
23+
2224
// Changed file display depth(dir1/dir2/.../dir*depth*)
2325
var Depth = 0
2426

@@ -29,6 +31,7 @@ func main() {
2931
cli.StringFlag("path", "Path to the versions folder (e.g. opendax/2-6/versions.yaml)", &Path)
3032
cli.StringFlag("component", "Name of the component to update", &Component)
3133
cli.StringFlag("tag", "Tag to insert into the versions file", &Tag)
34+
cli.StringFlag("message", "Commit message", &CommitMessage)
3235
cli.AddCommand(cmdVersions)
3336

3437
cmdChanges := kli.NewCommand("changes", "List files changed in the last commit").Action(actionChanges)

cmd/goci/versions.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ func actionVersions() error {
2323
// Path to versions file
2424
if Path == "" {
2525
Path = fmt.Sprintf("%s/opendax/%s/versions.yaml", tmp, cnf.Branch)
26+
} else {
27+
Path = strings.Join([]string{tmp, Path}, "/")
2628
}
2729
// Remove existing git folder
2830
if err := os.RemoveAll(tmp); err != nil {
@@ -75,8 +77,12 @@ func actionVersions() error {
7577
v.Save()
7678

7779
// Commit & Push to global OpenDAX versions
78-
fmt.Println("Committing & Pushing to global OpenDAX versions")
79-
hash, err := git.Update(repo, &auth, fmt.Sprintf("%s: Update %s version to %s", cnf.Branch, Component, Tag))
80+
fmt.Println("Committing & Pushing to remote repository")
81+
if CommitMessage == "" {
82+
CommitMessage = fmt.Sprintf("%s: Update %s version to %s", cnf.Branch, Component, Tag)
83+
}
84+
85+
hash, err := git.Update(repo, &auth, CommitMessage)
8086
if err == nil {
8187
fmt.Printf("Pushed with commit hash: %s\n", hash)
8288
}

git/git.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
package git
22

33
import (
4+
"fmt"
45
"os"
56

67
git "github.com/go-git/go-git/v5"
8+
"github.com/go-git/go-git/v5/plumbing"
79
"github.com/go-git/go-git/v5/plumbing/transport"
810
"github.com/go-git/go-git/v5/plumbing/transport/http"
911
)
1012

1113
// Config for the git repository
1214
type Config struct {
13-
Username string `env:"GIT_USERNAME" env-default:"kite-bot" env-description:"Git username"`
14-
Email string `env:"GIT_EMAIL" env-default:"[email protected]" env-description:"Git user email"`
15-
Token string `env:"GIT_TOKEN" env-description:"Git access token"`
16-
URL string `env:"GIT_URL" env-default:"https://github.com/openware/versions.git" env-description:"Git repository url"`
17-
Branch string `env:"DRONE_BRANCH" env-default:"2-6-stable" env-description:"drone target branch"`
18-
Repo string `env:"DRONE_REPO_NAME" env-description:"component repo name"`
15+
Username string `env:"GIT_USERNAME" env-default:"kite-bot" env-description:"Git username"`
16+
Email string `env:"GIT_EMAIL" env-default:"[email protected]" env-description:"Git user email"`
17+
Token string `env:"GIT_TOKEN" env-description:"Git access token"`
18+
URL string `env:"GIT_URL" env-default:"https://github.com/openware/versions.git" env-description:"Git repository url"`
19+
CloneBranch string `env:"CLONE_BRANCH" env-description:"target clone branch"`
20+
Branch string `env:"DRONE_BRANCH" env-default:"2-6-stable" env-description:"drone target branch"`
21+
Repo string `env:"DRONE_REPO_NAME" env-description:"component repo name"`
1922
}
2023

2124
// Auth to describe auth method
@@ -45,11 +48,18 @@ func (a *AuthToken) Method() transport.AuthMethod {
4548

4649
// Clone repository with config
4750
func Clone(cnf *Config, auth Auth, outDir string) (*git.Repository, error) {
48-
repo, err := git.PlainClone(outDir, false, &git.CloneOptions{
51+
cloneOptions := &git.CloneOptions{
4952
Auth: auth.Method(),
5053
URL: cnf.URL,
5154
Progress: os.Stdout,
52-
})
55+
}
56+
57+
if cnf.CloneBranch != "" {
58+
cloneOptions.ReferenceName = plumbing.NewBranchReferenceName(cnf.CloneBranch)
59+
fmt.Printf("Cloning %s branch\n", cnf.CloneBranch)
60+
}
61+
62+
repo, err := git.PlainClone(outDir, false, cloneOptions)
5363
if err != nil {
5464
return nil, err
5565
}

0 commit comments

Comments
 (0)