-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolveconflict.go
69 lines (61 loc) · 1.49 KB
/
resolveconflict.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package dx
import (
"fmt"
"log/slog"
"slices"
"strings"
"github.com/kitimark/dx/pkg/conflictresolver"
"github.com/kitimark/dx/pkg/exec"
"github.com/spf13/cobra"
)
func NewResolveConflictCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "resolve-conflict",
Aliases: []string{"resolve"},
RunE: cmdResolveConflictRun,
}
return cmd
}
func cmdResolveConflictRun(cmd *cobra.Command, _ []string) error {
conflictedFiles, err := getConflictedFiles()
if err != nil {
return err
}
for _, r := range conflictresolver.ConflictResolvers {
if r.Detect(conflictedFiles) {
slog.Info(fmt.Sprintf("detect %s conflict, trying to resolve", r.Name()))
err = r.Resolve(conflictedFiles)
if err != nil {
cmd.SilenceUsage = true
return err
}
}
}
return nil
}
var gitXYConflictedStatuses = []string{"AA", "UU"}
// getConflictedFiles return list of conflict files that parsed from `git status --short`
//
// ### Example output of `git status --short`
//
// UU go.mod
// AA go.sum
// UU main.go
//
// ### Output notation
//
// ref: https://git-scm.com/docs/git-status#_short_format
func getConflictedFiles() ([]string, error) {
out, err := exec.OutputErr("git", "status", "--short")
if err != nil {
return nil, err
}
var conflictedFiles []string
for _, line := range strings.Split(out, "\n") {
content := strings.Split(line, " ")
if slices.Contains(gitXYConflictedStatuses, content[0]) {
conflictedFiles = append(conflictedFiles, content[1])
}
}
return conflictedFiles, nil
}