forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdif.go
More file actions
159 lines (143 loc) · 3.42 KB
/
dif.go
File metadata and controls
159 lines (143 loc) · 3.42 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package dif
import (
"errors"
"net/http"
"os"
"os/exec"
"path/filepath"
"sort"
"github.com/AlecAivazis/survey/v2"
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/pkg/cmd/pr/shared"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/cli/cli/v2/pkg/prompt"
"github.com/spf13/cobra"
)
type Cache interface {
Exists(string) bool
Create(string, []byte) error
}
type cache struct{}
func (cache) Exists(path string) bool {
if _, err := os.Stat(path); err != nil {
return false
}
return true
}
func (cache) Create(path string, content []byte) error {
err := os.MkdirAll(filepath.Dir(path), 0755)
if err != nil {
return err
}
out, err := os.Create(path)
if err != nil {
return err
}
defer out.Close()
_, err = out.Write(content)
return err
}
type DifOptions struct {
HttpClient func() (*http.Client, error)
IO *iostreams.IOStreams
Cache Cache
Finder shared.PRFinder
SelectorArg string
}
func NewCmdDif(f *cmdutil.Factory, runF func(*DifOptions) error) *cobra.Command {
opts := &DifOptions{
IO: f.IOStreams,
HttpClient: f.HttpClient,
Cache: cache{},
}
cmd := &cobra.Command{
Use: "dif [<number> | <url> | <branch>]",
Short: "View changes in a pull request",
Long: heredoc.Doc(`
View changes in a pull request.
Without an argument, the pull request that belongs to the current branch
is selected.
`),
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts.Finder = shared.NewFinder(f)
if repoOverride, _ := cmd.Flags().GetString("repo"); repoOverride != "" && len(args) == 0 {
return &cmdutil.FlagError{Err: errors.New("argument required when using the --repo flag")}
}
if len(args) > 0 {
opts.SelectorArg = args[0]
}
if runF != nil {
return runF(opts)
}
return difRun(opts)
},
}
return cmd
}
func difRun(opts *DifOptions) error {
if !opts.IO.CanPrompt() {
return errors.New("must be run interactively")
}
findOptions := shared.FindOptions{
Selector: opts.SelectorArg,
Fields: []string{"number", "files", "baseRefOid", "headRefOid"},
}
pr, repo, err := opts.Finder.Find(findOptions)
if err != nil {
return err
}
client, err := opts.HttpClient()
if err != nil {
return err
}
candidates := []string{}
for _, file := range pr.Files.Nodes {
candidates = append(candidates, file.Path)
}
sort.Strings(candidates)
for {
var filename string
err = prompt.SurveyAskOne(&survey.Select{
Message: "View which file diff?",
Options: candidates,
}, &filename)
baseFile, err := getFileContent(opts.Cache, client, repo, filename, pr.BaseRefOid)
if err != nil {
return err
}
headFile, err := getFileContent(opts.Cache, client, repo, filename, pr.HeadRefOid)
if err != nil {
return err
}
diffCommand := exec.Command("nvim", "-d", "-R", baseFile, headFile)
diffCommand.Stderr = opts.IO.ErrOut
diffCommand.Stdout = opts.IO.Out
diffCommand.Stdin = opts.IO.In
err = diffCommand.Run()
if err != nil {
return err
}
choice := ""
err = prompt.SurveyAskOne(&survey.Select{
Message: "What next?",
Options: []string{
"View another file diff",
"Cancel",
},
}, &choice)
if err != nil {
return err
}
switch choice {
case "View another file diff":
continue
default:
return cmdutil.CancelError
}
}
}
func storeFileContent(name string, content []byte) (string, error) {
return "", nil
}