diff options
| author | jannfis <jann@mistrust.net> | 2021-01-30 19:21:08 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-01-30 19:21:08 +0100 |
| commit | 84c46306929e276d995a38463f13e383fa57fc45 (patch) | |
| tree | f33e485090c402bcdb83309016e802dee4260196 /ext | |
| parent | c9e4de4b33a180e803ed01d28f37dc85e2a5fba3 (diff) | |
feat: Resolve symrefs to allow them as targetRevision for write back (#151)
Diffstat (limited to 'ext')
| -rw-r--r-- | ext/git/client.go | 22 | ||||
| -rw-r--r-- | ext/git/mocks/Client.go | 23 | ||||
| -rw-r--r-- | ext/git/mocks/Creds.go | 46 |
3 files changed, 90 insertions, 1 deletions
diff --git a/ext/git/client.go b/ext/git/client.go index 097ce9e..1c28491 100644 --- a/ext/git/client.go +++ b/ext/git/client.go @@ -63,6 +63,7 @@ type Client interface { Commit(pathSpec string, message string, signingKey string) error Push(remote string, branch string, force bool) error Add(path string) error + SymRefToBranch(symRef string) (string, error) } // nativeGitClient implements Client interface using git CLI @@ -514,6 +515,12 @@ func (m *nativeGitClient) VerifyCommitSignature(revision string) (string, error) return out, nil } +// Commit perfoms a git commit for the given pathSpec to the currently checked +// out branch. If pathSpec is empty, or the special value "*", all pending +// changes will be commited. If message is not the empty string, it will be +// used as the commit message, otherwise a default commit message will be used. +// If signingKey is not the empty string, commit will be signed with the given +// GPG key. func (m *nativeGitClient) Commit(pathSpec string, message string, signingKey string) error { defaultCommitMsg := "Update parameters" args := []string{"commit"} @@ -555,6 +562,8 @@ func (m *nativeGitClient) Branch(sourceBranch string, targetBranch string) error return nil } +// Push pushes local changes to the remote branch. If force is true, will force +// the remote to accept the push. func (m *nativeGitClient) Push(remote string, branch string, force bool) error { args := []string{"push"} if force { @@ -568,10 +577,23 @@ func (m *nativeGitClient) Push(remote string, branch string, force bool) error { return nil } +// Add adds a path spec to the repository func (m *nativeGitClient) Add(path string) error { return m.runCredentialedCmd("git", "add", path) } +// SymRefToBranch retrieves the branch name a symbolic ref points to +func (m *nativeGitClient) SymRefToBranch(symRef string) (string, error) { + output, err := m.runCmd("symbolic-ref", symRef) + if err != nil { + return "", fmt.Errorf("could not resolve symbolic ref '%s': %v", symRef, err) + } + if a := strings.SplitN(output, "refs/heads/", 2); len(a) == 2 { + return a[1], nil + } + return "", fmt.Errorf("no symbolic ref named '%s' could be found", symRef) +} + // runWrapper runs a custom command with all the semantics of running the Git client func (m *nativeGitClient) runGnuPGWrapper(wrapper string, args ...string) (string, error) { cmd := exec.Command(wrapper, args...) diff --git a/ext/git/mocks/Client.go b/ext/git/mocks/Client.go index a65d9ad..84745f0 100644 --- a/ext/git/mocks/Client.go +++ b/ext/git/mocks/Client.go @@ -1,4 +1,4 @@ -// Code generated by mockery v1.0.0. DO NOT EDIT. +// Code generated by mockery v1.1.2. DO NOT EDIT. package mocks @@ -272,6 +272,27 @@ func (_m *Client) Root() string { return r0 } +// SymRefToBranch provides a mock function with given fields: symRef +func (_m *Client) SymRefToBranch(symRef string) (string, error) { + ret := _m.Called(symRef) + + var r0 string + if rf, ok := ret.Get(0).(func(string) string); ok { + r0 = rf(symRef) + } else { + r0 = ret.Get(0).(string) + } + + var r1 error + if rf, ok := ret.Get(1).(func(string) error); ok { + r1 = rf(symRef) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + // VerifyCommitSignature provides a mock function with given fields: _a0 func (_m *Client) VerifyCommitSignature(_a0 string) (string, error) { ret := _m.Called(_a0) diff --git a/ext/git/mocks/Creds.go b/ext/git/mocks/Creds.go new file mode 100644 index 0000000..205d4df --- /dev/null +++ b/ext/git/mocks/Creds.go @@ -0,0 +1,46 @@ +// Code generated by mockery v1.1.2. DO NOT EDIT. + +package mocks + +import ( + io "io" + + mock "github.com/stretchr/testify/mock" +) + +// Creds is an autogenerated mock type for the Creds type +type Creds struct { + mock.Mock +} + +// Environ provides a mock function with given fields: +func (_m *Creds) Environ() (io.Closer, []string, error) { + ret := _m.Called() + + var r0 io.Closer + if rf, ok := ret.Get(0).(func() io.Closer); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(io.Closer) + } + } + + var r1 []string + if rf, ok := ret.Get(1).(func() []string); ok { + r1 = rf() + } else { + if ret.Get(1) != nil { + r1 = ret.Get(1).([]string) + } + } + + var r2 error + if rf, ok := ret.Get(2).(func() error); ok { + r2 = rf() + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} |
