Debian Codegraph @

internal/command/merge.go

raw symbols SCIP
1 package command
2
3 import (
4 "regexp"
5
6 gerr "github.com/isacikgoz/gitbatch/internal/errors"
7 "github.com/isacikgoz/gitbatch/internal/git"
8 )
9
10 // MergeOptions defines the rules of a merge operation
11 type MergeOptions struct {
12 // Name of the branch to merge with.
13 BranchName string
14 // Be verbose.
15 Verbose bool
16 // With true do not show a diffstat at the end of the merge.
17 NoStat bool
18 // Mode is the command mode
19 CommandMode Mode
20 }
21
22 // Merge incorporates changes from the named commits or branches into the
23 // current branch
24 func Merge(r *git.Repository, options *MergeOptions) error {
25
26 args := make([]string, 0)
27 args = append(args, "merge")
28 if len(options.BranchName) > 0 {
29 args = append(args, options.BranchName)
30 }
31 if options.Verbose {
32 args = append(args, "-v")
33 }
34 if options.NoStat {
35 args = append(args, "-n")
36 }
37
38 ref, _ := r.Repo.Head()
39 if out, err := Run(r.AbsPath, "git", args); err != nil {
40 return gerr.ParseGitError(out, err)
41 }
42
43 newref, _ := r.Repo.Head()
44 r.SetWorkStatus(git.Success)
45 msg, err := getMergeMessage(r, ref.Hash().String(), newref.Hash().String())
46 if err != nil {
47 msg = "couldn't get stat"
48 }
49 r.State.Message = msg
50 return r.Refresh()
51 }
52
53 func getMergeMessage(r *git.Repository, ref1, ref2 string) (string, error) {
54 var msg string
55 if ref1 == ref2 {
56 msg = "already up-to-date"
57 } else {
58 out, err := DiffStatRefs(r, ref1, ref2)
59 if err != nil {
60 return "", err
61 }
62 re := regexp.MustCompile(`\r?\n`)
63 lines := re.Split(out, -1)
64 last := lines[len(lines)-1]
65 if len(last) > 0 {
66 msg = lines[len(lines)-1][1:]
67 }
68 }
69 return msg, nil
70 }