Rework git client interface
This commit is contained in:
parent
00d67e3e5a
commit
2c4a662660
3 changed files with 49 additions and 43 deletions
|
|
@ -9,14 +9,15 @@ import (
|
|||
|
||||
type gitClient interface {
|
||||
BranchRemote(string) (string, error)
|
||||
Checkout([]string) error
|
||||
CheckoutLocal(string) error
|
||||
CheckoutRemote(string, string) error
|
||||
CurrentBranch() (string, error)
|
||||
Fetch([]string) error
|
||||
HasLocalBranch([]string) bool
|
||||
IsAncestor([]string) (bool, error)
|
||||
Fetch(string, string) error
|
||||
HasLocalBranch(string) bool
|
||||
IsAncestor(string, string) (bool, error)
|
||||
IsDirty() (bool, error)
|
||||
Merge([]string) error
|
||||
Reset([]string) error
|
||||
MergeFastForward(string) error
|
||||
ResetHard(string) error
|
||||
}
|
||||
|
||||
type gitExecuter struct{}
|
||||
|
|
@ -35,8 +36,17 @@ func (g *gitExecuter) BranchRemote(branch string) (string, error) {
|
|||
return parts[0], nil
|
||||
}
|
||||
|
||||
func (g *gitExecuter) Checkout(args []string) error {
|
||||
args = append([]string{"checkout"}, args...)
|
||||
func (g *gitExecuter) CheckoutLocal(branch string) error {
|
||||
args := []string{"checkout", branch}
|
||||
cmd, err := git.GitCommand(args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
func (g *gitExecuter) CheckoutRemote(remote, branch string) error {
|
||||
args := []string{"checkout", "--track", fmt.Sprintf("%s/%s", remote, branch)}
|
||||
cmd, err := git.GitCommand(args...)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -48,8 +58,8 @@ func (g *gitExecuter) CurrentBranch() (string, error) {
|
|||
return git.CurrentBranch()
|
||||
}
|
||||
|
||||
func (g *gitExecuter) Fetch(args []string) error {
|
||||
args = append([]string{"fetch"}, args...)
|
||||
func (g *gitExecuter) Fetch(remote, ref string) error {
|
||||
args := []string{"fetch", remote, ref}
|
||||
cmd, err := git.GitCommand(args...)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -57,12 +67,12 @@ func (g *gitExecuter) Fetch(args []string) error {
|
|||
return cmd.Run()
|
||||
}
|
||||
|
||||
func (g *gitExecuter) HasLocalBranch(args []string) bool {
|
||||
return git.HasLocalBranch(args[0])
|
||||
func (g *gitExecuter) HasLocalBranch(branch string) bool {
|
||||
return git.HasLocalBranch(branch)
|
||||
}
|
||||
|
||||
func (g *gitExecuter) IsAncestor(args []string) (bool, error) {
|
||||
args = append([]string{"merge-base", "--is-ancestor"}, args...)
|
||||
func (g *gitExecuter) IsAncestor(ancestor, progeny string) (bool, error) {
|
||||
args := []string{"merge-base", "--is-ancestor", ancestor, progeny}
|
||||
cmd, err := git.GitCommand(args...)
|
||||
if err != nil {
|
||||
return false, err
|
||||
|
|
@ -86,8 +96,8 @@ func (g *gitExecuter) IsDirty() (bool, error) {
|
|||
return false, nil
|
||||
}
|
||||
|
||||
func (g *gitExecuter) Merge(args []string) error {
|
||||
args = append([]string{"merge"}, args...)
|
||||
func (g *gitExecuter) MergeFastForward(ref string) error {
|
||||
args := []string{"merge", "--ff-only", ref}
|
||||
cmd, err := git.GitCommand(args...)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -95,17 +105,8 @@ func (g *gitExecuter) Merge(args []string) error {
|
|||
return cmd.Run()
|
||||
}
|
||||
|
||||
func (g *gitExecuter) Reset(args []string) error {
|
||||
args = append([]string{"reset"}, args...)
|
||||
cmd, err := git.GitCommand(args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
func (g *gitExecuter) Stash(args []string) error {
|
||||
args = append([]string{"stash"}, args...)
|
||||
func (g *gitExecuter) ResetHard(ref string) error {
|
||||
args := []string{"reset", "--hard", ref}
|
||||
cmd, err := git.GitCommand(args...)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -13,28 +13,33 @@ func (g *mockGitClient) BranchRemote(a string) (string, error) {
|
|||
return args.String(0), args.Error(1)
|
||||
}
|
||||
|
||||
func (g *mockGitClient) Checkout(a []string) error {
|
||||
func (g *mockGitClient) CheckoutLocal(a string) error {
|
||||
args := g.Called(a)
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
func (g *mockGitClient) CheckoutRemote(a, b string) error {
|
||||
args := g.Called(a, b)
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
func (g *mockGitClient) CurrentBranch() (string, error) {
|
||||
args := g.Called()
|
||||
return args.String(0), args.Error(1)
|
||||
}
|
||||
|
||||
func (g *mockGitClient) Fetch(a []string) error {
|
||||
args := g.Called(a)
|
||||
func (g *mockGitClient) Fetch(a, b string) error {
|
||||
args := g.Called(a, b)
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
func (g *mockGitClient) HasLocalBranch(a []string) bool {
|
||||
func (g *mockGitClient) HasLocalBranch(a string) bool {
|
||||
args := g.Called(a)
|
||||
return args.Bool(0)
|
||||
}
|
||||
|
||||
func (g *mockGitClient) IsAncestor(a []string) (bool, error) {
|
||||
args := g.Called(a)
|
||||
func (g *mockGitClient) IsAncestor(a, b string) (bool, error) {
|
||||
args := g.Called(a, b)
|
||||
return args.Bool(0), args.Error(1)
|
||||
}
|
||||
|
||||
|
|
@ -43,12 +48,12 @@ func (g *mockGitClient) IsDirty() (bool, error) {
|
|||
return args.Bool(0), args.Error(1)
|
||||
}
|
||||
|
||||
func (g *mockGitClient) Merge(a []string) error {
|
||||
func (g *mockGitClient) MergeFastForward(a string) error {
|
||||
args := g.Called(a)
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
func (g *mockGitClient) Reset(a []string) error {
|
||||
func (g *mockGitClient) ResetHard(a string) error {
|
||||
args := g.Called(a)
|
||||
return args.Error(0)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -239,11 +239,11 @@ func executeLocalRepoSync(srcRepo ghrepo.Interface, remote string, opts *SyncOpt
|
|||
git := opts.Git
|
||||
branch := opts.Branch
|
||||
|
||||
if err := git.Fetch([]string{remote, fmt.Sprintf("refs/heads/%s", branch)}); err != nil {
|
||||
if err := git.Fetch(remote, fmt.Sprintf("refs/heads/%s", branch)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hasLocalBranch := git.HasLocalBranch([]string{branch})
|
||||
hasLocalBranch := git.HasLocalBranch(branch)
|
||||
if hasLocalBranch {
|
||||
branchRemote, err := git.BranchRemote(branch)
|
||||
if err != nil {
|
||||
|
|
@ -253,7 +253,7 @@ func executeLocalRepoSync(srcRepo ghrepo.Interface, remote string, opts *SyncOpt
|
|||
return mismatchRemotesError
|
||||
}
|
||||
|
||||
fastForward, err := git.IsAncestor([]string{branch, fmt.Sprintf("%s/%s", remote, branch)})
|
||||
fastForward, err := git.IsAncestor(branch, fmt.Sprintf("%s/%s", remote, branch))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -269,28 +269,28 @@ func executeLocalRepoSync(srcRepo ghrepo.Interface, remote string, opts *SyncOpt
|
|||
}
|
||||
if startBranch != branch {
|
||||
if hasLocalBranch {
|
||||
if err := git.Checkout([]string{branch}); err != nil {
|
||||
if err := git.CheckoutLocal(branch); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err := git.Checkout([]string{"--track", fmt.Sprintf("%s/%s", remote, branch)}); err != nil {
|
||||
if err := git.CheckoutRemote(remote, branch); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
if hasLocalBranch {
|
||||
if opts.Force {
|
||||
if err := git.Reset([]string{"--hard", fmt.Sprintf("refs/remotes/%s/%s", remote, branch)}); err != nil {
|
||||
if err := git.ResetHard(fmt.Sprintf("refs/remotes/%s/%s", remote, branch)); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err := git.Merge([]string{"--ff-only", fmt.Sprintf("refs/remotes/%s/%s", remote, branch)}); err != nil {
|
||||
if err := git.MergeFastForward(fmt.Sprintf("refs/remotes/%s/%s", remote, branch)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
if startBranch != branch {
|
||||
if err := git.Checkout([]string{startBranch}); err != nil {
|
||||
if err := git.CheckoutLocal(startBranch); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue