In buildconfig, detect current branch and it's remote url as defaults.

This commit is contained in:
Ratnadeep Debnath
2016-12-28 17:00:33 +05:30
parent 875c7d95dc
commit a1797a8d07
3 changed files with 44 additions and 8 deletions
+41 -5
View File
@@ -100,6 +100,29 @@ func getGitRemote(composeFileDir string, remote string) (string, error) {
return url, nil
}
// getGitCurrentBranch gets current git branch name for the current git repo
func getGitCurrentBranch(composeFileDir string) (string, error) {
cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
cmd.Dir = composeFileDir
out, err := cmd.Output()
if err != nil {
return "", err
}
return strings.TrimRight(string(out), "\n"), nil
}
// getGitRemoteForBranch gets git remote for a branch
func getGitRemoteForBranch(composeFileDir string, branch string) (string, error) {
cmd := exec.Command("sh", "-c", fmt.Sprintf("git branch -r | grep %s", branch))
cmd.Dir = composeFileDir
out, err := cmd.Output()
if err != nil {
return "", err
}
return strings.Split(strings.Trim(string(out), "\n "), "/")[0], nil
}
// getComposeFileDir returns compose file directory
func getComposeFileDir(inputFile string) (string, error) {
if strings.Index(inputFile, "/") != 0 {
@@ -302,6 +325,7 @@ func (o *OpenShift) Transform(komposeObject kobject.KomposeObject, opt kobject.C
var composeFileDir string
hasBuild := false
buildRepo := opt.BuildRepo
buildBranch := opt.BuildBranch
for name, service := range komposeObject.ServiceConfigs {
var objects []runtime.Object
@@ -326,12 +350,24 @@ func (o *OpenShift) Transform(komposeObject kobject.KomposeObject, opt kobject.C
logrus.Warningf("Error in detecting compose file's directory.")
continue
}
if !hasGitBinary() && (buildRepo == "" || buildBranch == "") {
logrus.Fatalf("Git is not installed! Please install Git to create buildconfig, else supply source repository to use for build using '--build-repo' option.")
}
if buildBranch == "" {
buildBranch, err = getGitCurrentBranch(composeFileDir)
if err != nil {
logrus.Fatalf("Buildconfig cannot be created because current git branch couldn't be detected.")
}
}
if opt.BuildRepo == "" {
if hasGitBinary() {
buildRepo, err = getGitRemote(composeFileDir, "origin")
if err != nil {
logrus.Fatalf("Buildconfig cannot be created because git remote origin repo couldn't be detected.")
}
var buildRemote string
buildRemote, err = getGitRemoteForBranch(composeFileDir, buildBranch)
if err != nil {
logrus.Fatalf("Buildconfig cannot be created because remote for current git branch couldn't be detected.")
}
buildRepo, err = getGitRemote(composeFileDir, buildRemote)
if err != nil {
logrus.Fatalf("Buildconfig cannot be created because git remote origin repo couldn't be detected.")
}
}
hasBuild = true