forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: making sure the right Run function is called for step git merge (j…
…enkins-x#4336) fixes issue jenkins-x#4334 Signed-off-by: Hardy Ferentschik <hardy@ferentschik.de>
- Loading branch information
1 parent
19da9a4
commit 442ddfc
Showing
6 changed files
with
245 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package git | ||
|
||
import ( | ||
"fmt" | ||
"github.com/jenkins-x/jx/pkg/cmd/opts" | ||
"github.com/jenkins-x/jx/pkg/gits" | ||
"github.com/jenkins-x/jx/pkg/log" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestStepGitMerge(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Step Git Merge Suite") | ||
} | ||
|
||
var _ = Describe("step git merge", func() { | ||
var ( | ||
masterSha string | ||
branchSha string | ||
repoDir string | ||
err error | ||
) | ||
|
||
BeforeEach(func() { | ||
repoDir, err = ioutil.TempDir("", "jenkins-x-git-test-repo-") | ||
if err != nil { | ||
Fail("unable to create test repo dir") | ||
} | ||
log.Logger().Debugf("created temporary git repo under %s", repoDir) | ||
gits.GitCmd(Fail, repoDir, "init") | ||
|
||
gits.WriteFile(Fail, repoDir, "a.txt", "a") | ||
gits.Add(Fail, repoDir) | ||
masterSha = gits.Commit(Fail, repoDir, "a commit") | ||
|
||
gits.Branch(Fail, repoDir, "foo") | ||
gits.WriteFile(Fail, repoDir, "b.txt", "b") | ||
gits.Add(Fail, repoDir) | ||
branchSha = gits.Commit(Fail, repoDir, "b commit") | ||
|
||
gits.Checkout(Fail, repoDir, "master") | ||
}) | ||
|
||
AfterEach(func() { | ||
_ = os.RemoveAll(repoDir) | ||
log.Logger().Debugf("deleted temporary git repo under %s", repoDir) | ||
}) | ||
|
||
Context("with command line options", func() { | ||
It("succeeds", func() { | ||
currentHeadSha := gits.HeadSha(Fail, repoDir) | ||
Expect(currentHeadSha).Should(Equal(masterSha)) | ||
|
||
options := StepGitMergeOptions{ | ||
StepOptions: opts.StepOptions{ | ||
CommonOptions: &opts.CommonOptions{}, | ||
}, | ||
SHAs: []string{branchSha}, | ||
Dir: repoDir, | ||
BaseBranch: "master", | ||
BaseSHA: masterSha, | ||
} | ||
|
||
err := options.Run() | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
currentHeadSha = gits.HeadSha(Fail, repoDir) | ||
Expect(currentHeadSha).Should(Equal(branchSha)) | ||
}) | ||
}) | ||
|
||
Context("with PULL_REFS", func() { | ||
BeforeEach(func() { | ||
err := os.Setenv("PULL_REFS", fmt.Sprintf("master:%s,foo:%s", masterSha, branchSha)) | ||
if err != nil { | ||
Fail("unable to set PULL_REFS") | ||
} | ||
|
||
}) | ||
|
||
AfterEach(func() { | ||
err := os.Unsetenv("PULL_REFS") | ||
if err != nil { | ||
Fail("unable to unset PULL_REFS") | ||
} | ||
}) | ||
|
||
It("succeeds", func() { | ||
currentHeadSha := gits.HeadSha(Fail, repoDir) | ||
Expect(currentHeadSha).Should(Equal(masterSha)) | ||
|
||
options := StepGitMergeOptions{ | ||
StepOptions: opts.StepOptions{ | ||
CommonOptions: &opts.CommonOptions{}, | ||
}, | ||
Dir: repoDir, | ||
} | ||
|
||
err := options.Run() | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
currentHeadSha = gits.HeadSha(Fail, repoDir) | ||
Expect(currentHeadSha).Should(Equal(branchSha)) | ||
}) | ||
}) | ||
|
||
Context("with no options and no PULL_REFS", func() { | ||
It("logs warning", func() { | ||
options := StepGitMergeOptions{ | ||
StepOptions: opts.StepOptions{ | ||
CommonOptions: &opts.CommonOptions{}, | ||
}, | ||
Dir: repoDir, | ||
} | ||
|
||
out := log.CaptureOutput(func() { | ||
err := options.Run() | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
currentHeadSha := gits.HeadSha(Fail, repoDir) | ||
Expect(currentHeadSha).Should(Equal(masterSha)) | ||
}) | ||
|
||
Expect(out).Should(ContainSubstring("no SHAs to merge")) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package gits | ||
|
||
import ( | ||
"github.com/jenkins-x/jx/pkg/log" | ||
"github.com/jenkins-x/jx/pkg/util" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// WriteFile creates a file with the specified name underneath the gitRepo directory adding the specified content. | ||
// The file name can be path as well and intermediate directories are created. | ||
func WriteFile(fail func(string, ...int), repoDir string, name string, contents string) { | ||
path := filepath.Join(repoDir, name) | ||
err := os.MkdirAll(filepath.Dir(path), 0755) | ||
if err != nil { | ||
log.Logger().Error(err.Error()) | ||
fail("unable to create directory") | ||
} | ||
|
||
b := []byte(contents) | ||
err = ioutil.WriteFile(path, b, 0644) | ||
if err != nil { | ||
log.Logger().Error(err.Error()) | ||
fail("unable to write file content") | ||
} | ||
} | ||
|
||
// HeadSha returns the commit SHA of the current HEAD commit within the specified git directory | ||
func HeadSha(fail func(string, ...int), repoDir string) string { | ||
data, err := ioutil.ReadFile(filepath.Join(repoDir, ".git", "HEAD")) | ||
if err != nil { | ||
log.Logger().Error(err.Error()) | ||
fail("unable to read file") | ||
} | ||
|
||
headRef := strings.TrimPrefix(string(data), "ref: ") | ||
headRef = strings.Trim(headRef, "\n") | ||
|
||
return ReadRef(fail, repoDir, headRef) | ||
} | ||
|
||
// ReadRef reads the commit SHA of the specified ref. Needs to be of the form /refs/heads/<name>. | ||
func ReadRef(fail func(string, ...int), repoDir string, name string) string { | ||
data, err := ioutil.ReadFile(filepath.Join(repoDir, ".git", name)) | ||
if err != nil { | ||
log.Logger().Error(err.Error()) | ||
fail("unable to read file") | ||
} | ||
return strings.Trim(string(data), "\n") | ||
} | ||
|
||
// Add adds all unstaged changes to the index. | ||
func Add(fail func(string, ...int), repoDir string) { | ||
GitCmd(fail, repoDir, "add", ".") | ||
} | ||
|
||
// Commit commits all staged changes with the specified commit message. | ||
func Commit(fail func(string, ...int), repoDir string, message string) string { | ||
GitCmd(fail, repoDir, "commit", "-m", message) | ||
return HeadSha(fail, repoDir) | ||
} | ||
|
||
// Tag creates an annotated tag. | ||
func Tag(fail func(string, ...int), repoDir string, tag string, message string) string { | ||
GitCmd(fail, repoDir, "tag", "-a", "-m", message, tag) | ||
return HeadSha(fail, repoDir) | ||
} | ||
|
||
// Checkout switches to the specified branch. | ||
func Checkout(fail func(string, ...int), repoDir string, branch string) { | ||
GitCmd(fail, repoDir, "checkout", branch) | ||
} | ||
|
||
// Branch creates a new branch with the specified name. | ||
func Branch(fail func(string, ...int), repoDir string, branch string) { | ||
GitCmd(fail, repoDir, "checkout", "-b", branch) | ||
} | ||
|
||
// DetachHead puts the repository in a detached head mode. | ||
func DetachHead(fail func(string, ...int), repoDir string) { | ||
head := HeadSha(fail, repoDir) | ||
GitCmd(fail, repoDir, "checkout", head) | ||
} | ||
|
||
// GitCmd runs a git command with arguments in the specified git repository | ||
func GitCmd(fail func(string, ...int), repoDir string, args ...string) { | ||
cmd := util.Command{ | ||
Dir: repoDir, | ||
Name: "git", | ||
Args: args, | ||
} | ||
_, err := cmd.RunWithoutRetry() | ||
if err != nil { | ||
log.Logger().Error(err.Error()) | ||
fail("unable to write file content") | ||
} | ||
} |