Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions git.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ func getBranches(all_branches bool) []string {

branches_str, _ := oneliner("git", "name-rev", "--name-only", "--exclude=tags/*", "HEAD")
if all_branches {
branches_str, _ = oneliner("git", "branch", "--no-column", "--contains", "HEAD")
branches_str, _ = oneliner("git", "branch", "--no-column", "--points-at", "HEAD")
}

var branches = make([]string, 5)
if branches_str != "" {
// Remove asterisk from branches list
r := regexp.MustCompile("[\\* ] ")
branches_str = r.ReplaceAllString(branches_str, "")
// Remove "(HEAD detached at..." if not on a branch
r = regexp.MustCompile("\\(HEAD detached at .*\\)")
branches_str = r.ReplaceAllString(branches_str, "")
branches = strings.Split(branches_str, "\n")

// Branches list is separated by spaces. Let's put it in an array
Expand All @@ -45,23 +48,26 @@ func getBranches(all_branches bool) []string {
labels = append(labels, tags...)
}

var cleanLabels = []string{}
for key := range labels {
var label = labels[key]
// Remove start of "heads/origin" if exist
r := regexp.MustCompile("^heads\\/origin\\/")
labels[key] = r.ReplaceAllString(labels[key], "")
label = regexp.MustCompile("^heads\\/origin\\/").ReplaceAllString(label, "")

// Remove start of "remotes/origin" if exist
r = regexp.MustCompile("^remotes\\/origin\\/")
labels[key] = r.ReplaceAllString(labels[key], "")
label = regexp.MustCompile("^remotes\\/origin\\/").ReplaceAllString(label, "")

// Replace all "/" with "."
labels[key] = strings.Replace(labels[key], "/", ".", -1)
label = strings.Replace(label, "/", ".", -1)

// Replace all "~" with "."
labels[key] = strings.Replace(labels[key], "~", ".", -1)
}
label = strings.Replace(label, "~", ".", -1)

return labels
if label != "" {
cleanLabels = append(cleanLabels, label)
}
}
return cleanLabels
}

func isDirty() bool {
Expand Down
124 changes: 121 additions & 3 deletions git_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package captain // import "github.com/harbur/captain"

import (
"io/ioutil"
"log"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -16,12 +19,89 @@ func TestGitGetRevisionFullSha(t *testing.T) {

// TODO Fails because it assumes current branch is master
func TestGitGetBranch(t *testing.T) {
// assert.Equal(t, []string{"master"}, getBranches(false), "Git branch should be master")
pwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
defer os.Chdir(pwd)
dir, err := ioutil.TempDir("", "captain_tmp-")
if err != nil {
log.Fatal(err)
}
os.Chdir(dir)
defer os.RemoveAll(dir)
CreateRepoWithCommits()
// when tag checked out
oneliner("git", "checkout", "v1.0")
assert.Equal(
t,
[]string{"b-three", "v1.0", "v1.1"},
getBranches(false),
"Git branch should be those pointing to v1.0")
// when branch checked out
oneliner("git", "checkout", "b-two")
assert.Equal(
t,
[]string{"b-three", "v1.0", "v1.1"}, // returns b-three instead of b-two since it is first alphanumerically
getBranches(false),
"Git branch should be those pointing to b-two")
// when branch with no tags checked out
oneliner("git", "checkout", "b-one")
assert.Equal(
t,
[]string{"b-one"},
getBranches(false),
"Git branch should be those pointing to b-one")
// when tag with no branches checked out
oneliner("git", "checkout", "v3.0")
assert.Equal(
t,
[]string{"undefined", "v3.0"}, // "undefined" is a string returned by git. Tempted to leave as is.
getBranches(false),
"Git branch should be those pointing to v3.0")
}

// TODO Fails because it assumes current branch is master
func TestGitGetBranchAllBranches(t *testing.T) {
// assert.Equal(t, []string{"master"}, getBranches(true), "Git branch should be master")
pwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
defer os.Chdir(pwd)
dir, err := ioutil.TempDir("", "captain_tmp-")
if err != nil {
log.Fatal(err)
}
os.Chdir(dir)
defer os.RemoveAll(dir)
CreateRepoWithCommits()
// when tag checked out
oneliner("git", "checkout", "v1.0")
assert.Equal(
t,
[]string{"b-three", "b-two", "v1.0", "v1.1"},
getBranches(true),
"Git branch should be those pointing to v1.0")
// when branch checked out
oneliner("git", "checkout", "b-two")
assert.Equal(
t,
[]string{"b-three", "b-two", "v1.0", "v1.1"},
getBranches(true),
"Git branch should be those pointing to b-two")
// when branch with no tags checked out
oneliner("git", "checkout", "b-one")
assert.Equal(
t,
[]string{"b-one", "master"},
getBranches(true),
"Git branch should be those pointing to b-one")
// when tag with no branches checked out
oneliner("git", "checkout", "v3.0")
assert.Equal(
t,
[]string{"v3.0"},
getBranches(true),
"Git branch should be those pointing to v3.0")
}

// TODO Fails because vendors/ is not git-ignored.
Expand All @@ -32,3 +112,41 @@ func TestGitIsDirty(t *testing.T) {
func TestGitIsGit(t *testing.T) {
assert.Equal(t, true, isGit(), "There should be a git repository")
}

func CreateRepoWithCommits(){
// create a git repository of the following form
// commit-A => master, b-one
// commit-B => b-two, b-three, v1.0, v1.1
// commit-C => b-four, v2.0
// commit-D => v3.0
oneliner("git", "init")
CommitEmptyFile("a.txt")
oneliner("git", "checkout", "-b", "b-one")
oneliner("git", "checkout", "-b", "b-two")
CommitEmptyFile("b.txt")
oneliner("git", "checkout", "-b", "b-three")
oneliner("git", "tag", "-a", "v1.0", "-m", "1.0")
oneliner("git", "tag", "v1.1")
oneliner("git", "checkout", "-b", "b-four")
CommitEmptyFile("c.txt")
oneliner("git", "tag", "-a", "v2.0", "-m", "2.0")
oneliner("git", "checkout", "-b", "b-five")
CommitEmptyFile("d.txt")
oneliner("git", "tag", "-a", "v3.0", "-m", "3.0")
oneliner("git", "checkout", "v3.0")
oneliner("git", "branch", "-D", "b-five")
}

func CommitEmptyFile(name string){
CreateEmptyFile(name)
oneliner("git", "add", ".")
oneliner("git", "commit", "-m", "wip")
}

func CreateEmptyFile(name string){
emptyFile, err := os.Create(name)
if err != nil {
log.Fatal(err)
}
defer emptyFile.Close()
}