44 "errors"
55 "fmt"
66 "net/http"
7- "os/exec"
87 "regexp"
98
109 "github.com/AlecAivazis/survey/v2"
@@ -13,11 +12,9 @@ import (
1312 "github.com/cli/cli/context"
1413 "github.com/cli/cli/git"
1514 "github.com/cli/cli/internal/ghrepo"
16- "github.com/cli/cli/internal/run"
1715 "github.com/cli/cli/pkg/cmdutil"
1816 "github.com/cli/cli/pkg/iostreams"
1917 "github.com/cli/cli/pkg/prompt"
20- "github.com/cli/safeexec"
2118 "github.com/spf13/cobra"
2219)
2320
@@ -27,6 +24,7 @@ type SyncOptions struct {
2724 BaseRepo func () (ghrepo.Interface , error )
2825 Remotes func () (context.Remotes , error )
2926 CurrentBranch func () (string , error )
27+ Git gitClient
3028 DestArg string
3129 SrcArg string
3230 Branch string
@@ -41,6 +39,7 @@ func NewCmdSync(f *cmdutil.Factory, runF func(*SyncOptions) error) *cobra.Comman
4139 BaseRepo : f .BaseRepo ,
4240 Remotes : f .Remotes ,
4341 CurrentBranch : f .Branch ,
42+ Git : & gitExecuter {gitCommand : git .GitCommand },
4443 }
4544
4645 cmd := & cobra.Command {
@@ -202,12 +201,16 @@ func syncLocalRepo(srcRepo ghrepo.Interface, opts *SyncOptions) error {
202201 }
203202 remote := remotes [0 ]
204203 branch := opts .Branch
204+ git := opts .Git
205205
206- _ = executeCmds ([][]string {{"git" , "fetch" , remote .Name , fmt .Sprintf ("+refs/heads/%s" , branch )}})
206+ err = git .Fetch ([]string {remote .Name , fmt .Sprintf ("+refs/heads/%s" , branch )})
207+ if err != nil {
208+ return err
209+ }
207210
208- hasLocalBranch := git .HasLocalBranch (branch )
211+ hasLocalBranch := git .HasLocalBranch ([] string { branch } )
209212 if hasLocalBranch {
210- fastForward , err := git .IsAncestor (branch , fmt .Sprintf ("%s/%s" , remote .Name , branch ))
213+ fastForward , err := git .IsAncestor ([] string { branch , fmt .Sprintf ("%s/%s" , remote .Name , branch )} )
211214 if err != nil {
212215 return err
213216 }
@@ -217,38 +220,54 @@ func syncLocalRepo(srcRepo ghrepo.Interface, opts *SyncOptions) error {
217220 }
218221 }
219222
220- startBranch , err := opts . CurrentBranch ()
223+ dirtyRepo , err := git . IsDirty ()
221224 if err != nil {
222225 return err
223226 }
224-
225- dirtyRepo , err := git .IsDirty ()
227+ startBranch , err := git .CurrentBranch ()
226228 if err != nil {
227229 return err
228230 }
229231
230- var cmds [][]string
231232 if dirtyRepo {
232- cmds = append (cmds , []string {"git" , "stash" , "push" })
233+ err = git .Stash ([]string {"push" })
234+ if err != nil {
235+ return err
236+ }
233237 }
234238 if startBranch != branch {
235- cmds = append (cmds , []string {"git" , "checkout" , branch })
239+ err = git .Checkout ([]string {branch })
240+ if err != nil {
241+ return err
242+ }
236243 }
237244 if hasLocalBranch {
238245 if opts .Force {
239- cmds = append (cmds , []string {"git" , "reset" , "--hard" , fmt .Sprintf ("refs/remotes/%s/%s" , remote , branch )})
246+ err = git .Reset ([]string {"--hard" , fmt .Sprintf ("refs/remotes/%s/%s" , remote , branch )})
247+ if err != nil {
248+ return err
249+ }
240250 } else {
241- cmds = append (cmds , []string {"git" , "merge" , "--ff-only" , fmt .Sprintf ("refs/remotes/%s/%s" , remote , branch )})
251+ err = git .Merge ([]string {"--ff-only" , fmt .Sprintf ("refs/remotes/%s/%s" , remote , branch )})
252+ if err != nil {
253+ return err
254+ }
242255 }
243256 }
244257 if startBranch != branch {
245- cmds = append (cmds , []string {"git" , "checkout" , startBranch })
258+ err = git .Checkout ([]string {startBranch })
259+ if err != nil {
260+ return err
261+ }
246262 }
247263 if dirtyRepo {
248- cmds = append (cmds , []string {"git" , "stash" , "pop" })
264+ err = git .Stash ([]string {"pop" })
265+ if err != nil {
266+ return err
267+ }
249268 }
250269
251- return executeCmds ( cmds )
270+ return nil
252271}
253272
254273func syncRemoteRepo (client * api.Client , destRepo , srcRepo ghrepo.Interface , opts * SyncOptions ) error {
@@ -270,17 +289,3 @@ func syncRemoteRepo(client *api.Client, destRepo, srcRepo ghrepo.Interface, opts
270289
271290 return err
272291}
273-
274- func executeCmds (cmdQueue [][]string ) error {
275- exe , err := safeexec .LookPath ("git" )
276- if err != nil {
277- return err
278- }
279- for _ , args := range cmdQueue {
280- cmd := exec .Command (exe , args [1 :]... )
281- if err := run .PrepareCmd (cmd ).Run (); err != nil {
282- return err
283- }
284- }
285- return nil
286- }
0 commit comments