github.com/LGUG2Z/story@v0.4.1/git/merge.go (about) 1 package git 2 3 import ( 4 "fmt" 5 "os/exec" 6 "strings" 7 ) 8 9 type FetchOpts struct { 10 Branch string 11 Remote string 12 Project string 13 } 14 15 func Fetch(opts FetchOpts) (string, error) { 16 var args []string 17 args = append(args, "fetch", opts.Remote, fmt.Sprintf("%s:%s", opts.Branch, opts.Branch)) 18 19 command := exec.Command("git", args...) 20 if opts.Project != "" { 21 command.Dir = opts.Project 22 } 23 24 combinedOutput, err := command.CombinedOutput() 25 if err != nil { 26 return "", fmt.Errorf("%s: %s", err, combinedOutput) 27 } 28 29 return strings.TrimSpace(string(combinedOutput)), nil 30 } 31 32 type MergeOpts struct { 33 SourceBranch string 34 DestinationBranch string 35 Project string 36 Squash bool 37 } 38 39 func Merge(opts MergeOpts) (string, error) { 40 var args []string 41 args = append(args, "merge", opts.SourceBranch) 42 if opts.Squash { 43 args = append(args, "--squash") 44 } 45 46 args = append(args, opts.SourceBranch) 47 48 command := exec.Command("git", args...) 49 if opts.Project != "" { 50 command.Dir = opts.Project 51 } 52 53 combinedOutput, err := command.CombinedOutput() 54 if err != nil { 55 return "", fmt.Errorf("%s: %s", err, combinedOutput) 56 } 57 58 return strings.TrimSpace(string(combinedOutput)), nil 59 }