Skip to content

Commit

Permalink
Added support for alternate git remotes to support git forks of go pr…
Browse files Browse the repository at this point in the history
…ojects.
  • Loading branch information
cbegin committed Feb 7, 2018
1 parent ca34aee commit 4b39ca0
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 14 deletions.
9 changes: 7 additions & 2 deletions commands/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ var ReleaseCommand = cli.Command{
Name: "branch,b",
Usage: "Branch name to release. Default 'master'.",
},
cli.StringFlag{
Name: "remote,r",
Usage: "Remote name to verify repository state against. Default 'origin'.",
},
},
}

Expand All @@ -29,11 +33,12 @@ func release(c *cli.Context) error {
}

branch := c.String("branch")
remote := c.String("remote")

//TODO: Make this configurable
var vcsTool vcstool.VCSTool = &vcstool.GitVCSTool{}
if os.Getenv("TESTRELEASE") == "" {
if err := vcsTool.VerifyRepoState(project, branch); err != nil {
if err := vcsTool.VerifyRepoState(project, remote, branch); err != nil {
return err
}
}
Expand All @@ -42,7 +47,7 @@ func release(c *cli.Context) error {
}
if os.Getenv("TESTRELEASE") == "" {
tagName := fmt.Sprintf("v%s", project.Version)
if err := vcsTool.Tag(project, tagName); err != nil {
if err := vcsTool.Tag(project, remote, tagName); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion project.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: graven
version: 1.0.0
version: 1.0.1
go_version: ">=1.9.1"
artifacts:
- classifier: darwin
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
26 changes: 18 additions & 8 deletions vcstool/git_vcstool.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,38 @@ type GitVCSTool struct{}

type Validator func(stdout, stderr string) error

func (g *GitVCSTool) Tag(project *domain.Project, tagName string) error {
func (g *GitVCSTool) Tag(project *domain.Project, remote, tagName string) error {
remoteName := "origin"

if remote != "" {
remoteName = remote
}

if sout, serr, err := util.RunCommand(project.ProjectPath(), nil, "git", "tag", tagName); err != nil {
fmt.Printf("Tagging %v\n%v\n", sout, serr)
return err
}

if sout, serr, err := util.RunCommand(project.ProjectPath(), nil, "git", "push", "--tags"); err != nil {
if sout, serr, err := util.RunCommand(project.ProjectPath(), nil, "git", "push", "--tags", remoteName); err != nil {
fmt.Printf("PushTags %v\n%v\n", sout, serr)
return err
}
return nil
}

func (g *GitVCSTool) VerifyRepoState(project *domain.Project, branch string) error {
func (g *GitVCSTool) VerifyRepoState(project *domain.Project, remote, branch string) error {
remoteName := "origin"
branchName := "master"

if remote != "" {
remoteName = remote
}

if branch != "" {
branchName = branch
}

// Check if on expected branch (e.g. master)
fmt.Println("Check if on expected branch (e.g. master)")
if err := verifyGitState(func(stdout, stderr string) error {
actualBranch := strings.TrimSpace(stdout)
if actualBranch != branchName {
Expand All @@ -44,7 +54,7 @@ func (g *GitVCSTool) VerifyRepoState(project *domain.Project, branch string) err
return err
}

// Ensure no uncommitted changes
fmt.Println("Check for uncommitted changes")
if err := verifyGitState(func(stdout, stderr string) error {
if strings.TrimSpace(stdout) != "" || strings.TrimSpace(stderr) != "" {
return fmt.Errorf("Cannot release with uncommitted changes.")
Expand All @@ -54,7 +64,7 @@ func (g *GitVCSTool) VerifyRepoState(project *domain.Project, branch string) err
return err
}

// Check if changes exist on server
fmt.Println("Check if changes exist on server")
if err := verifyGitState(func(stdout, stderr string) error {
lineCount := len(strings.Split(strings.TrimSpace(stderr), "\n"))
if lineCount > 2 {
Expand All @@ -65,7 +75,7 @@ func (g *GitVCSTool) VerifyRepoState(project *domain.Project, branch string) err
return err
}

// Check if local changes are pushed
fmt.Println("Check if local changes are pushed")
if err := verifyGitState(func(stdout, stderr string) error {
parts := strings.Split(strings.TrimSpace(stdout), "\n")
if strings.TrimSpace(parts[0]) != strings.TrimSpace(parts[1]) {
Expand All @@ -82,7 +92,7 @@ func (g *GitVCSTool) VerifyRepoState(project *domain.Project, branch string) err
func verifyGitState(validator Validator, project *domain.Project, args ...string) error {
sout, serr, err := util.RunCommand(project.ProjectPath(), nil, "git", args...)
if err != nil {
return fmt.Errorf("ERROR running Git command: %v\n", err)
return fmt.Errorf("ERROR running Git command:\n%v\n%v\n", serr, err)
}
return validator(sout, serr)
}
4 changes: 2 additions & 2 deletions vcstool/vsctool.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import (
)

type VCSTool interface {
VerifyRepoState(project *domain.Project, branch string) error
Tag(project *domain.Project, tagname string) error
VerifyRepoState(project *domain.Project, remote, branch string) error
Tag(project *domain.Project, remote, tagname string) error
}
2 changes: 1 addition & 1 deletion version/version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// graven - This file was generated. It will be overwritten. Do not modify.
package version

var Version = "1.0.0"
var Version = "1.0.1"

0 comments on commit 4b39ca0

Please sign in to comment.