summaryrefslogtreecommitdiff
path: root/shell.go
blob: d9c7bcd3eccc7136186b8bc1d597b91df581fbcc (plain)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package gitpb

import (
	"bytes"
	"errors"
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"strings"

	"github.com/go-cmd/cmd"
	"go.wit.com/lib/gui/shell"
	"go.wit.com/log"
)

// execute something with the working directory
// set to the FullPath
func (repo *Repo) Run(cmd []string) cmd.Status {
	result := shell.PathRun(repo.FullPath, cmd)
	output := strings.Join(result.Stdout, "\n")
	if result.Error != nil {
		log.Log(WARN, "cmd:", cmd)
		log.Log(WARN, "ouptput:", output)
		log.Log(WARN, "failed with error:", result.Error)
	}
	return result
}

func (repo *Repo) RunEcho(cmd []string) cmd.Status {
	result := shell.PathRunQuiet(repo.FullPath, cmd)
	log.Log(NOW, "cmd:", repo.FullPath, cmd)
	log.Warn(WARN, "cmd.Exit:", result.Exit, "cmd.Error:", result.Error)
	for _, line := range result.Stdout {
		log.Log(NOW, "STDOUT:", line)
	}
	for _, line := range result.Stderr {
		log.Log(NOW, "STDERR:", line)
	}
	return result
}

func (repo *Repo) RunRealtime(cmd []string) cmd.Status {
	return shell.PathRunRealtime(repo.GetFullPath(), cmd)
}

func (repo *Repo) RunRealtimeVerbose(cmd []string) cmd.Status {
	log.Log(NOW, "EXEC: cd", repo.GetFullPath(), ";", cmd)
	return shell.PathRunRealtime(repo.GetFullPath(), cmd)
}

func (repo *Repo) RunQuiet(cmd []string) (*cmd.Status, error) {
	result := shell.PathRunQuiet(repo.FullPath, cmd)
	if result.Error != nil {
		log.Warn(repo.GetGoPath(), cmd, "wow. golang is cool. an os.Error:", result.Error)
		return &result, result.Error
	}
	if result.Exit != 0 {
		// log.Warn(cmd, "failed with", result.Exit, repo.GetGoPath())
		return &result, errors.New(fmt.Sprint(cmd, "failed with", result.Exit))
	}
	return &result, nil
}

func (repo *Repo) RunStrict(cmd []string) (*cmd.Status, error) {
	result, err := repo.RunQuiet(cmd)
	if err != nil {
		log.Warn(cmd, "failed with", result.Exit, repo.GetGoPath())
		return result, errors.New(fmt.Sprint(cmd, "failed with", result.Exit))
	}
	return result, nil
}

func (repo *Repo) Exists(filename string) bool {
	if repo == nil {
		return false
	}
	testf := filepath.Join(repo.FullPath, filename)
	_, err := os.Stat(testf)
	if err != nil {
		return false
	}
	return true
}

func (repo *Repo) IsValidDir() bool {
	if repo == nil {
		return false
	}
	if !repo.IsGitDirectory() {
		return false
	}
	return true
}

func (repo *Repo) ReadFile(fname string) ([]byte, error) {
	fullname := filepath.Join(repo.FullPath, fname)
	return os.ReadFile(fullname)
}

func (repo *Repo) IsGitDirectory() bool {
	gitdir := filepath.Join(repo.FullPath, ".git")
	info, err := os.Stat(gitdir)
	if err != nil {
		return false
	}
	return info.IsDir()
}

func (repo *Repo) IsDirectory() bool {
	info, err := os.Stat(repo.FullPath)
	if err != nil {
		return false
	}
	return info.IsDir()
}

func (repo *Repo) RunAll(all [][]string) bool {
	for _, cmd := range all {
		log.Log(WARN, "doAll() RUNNING: cmd =", cmd)
		r := repo.Run(cmd)
		if r.Error != nil {
			log.Log(WARN, "doAll() err =", r.Error)
			log.Log(WARN, "doAll() out =", r.Stdout)
			return false
		}
	}
	return true
}

func (repo *Repo) RunStrictAll(all [][]string) (*cmd.Status, error) {
	for _, cmd := range all {
		log.Log(WARN, "doAll() RUNNING: cmd =", cmd)
		if result, err := repo.RunStrict(cmd); err != nil {
			return result, err
		}
	}
	return nil, nil
}

func (repo *Repo) RunVerbose(cmd []string) error {
	// log.Info("EXEC Running:", repo.GetGoPath(), cmd)
	err := shell.PathExecVerbose(repo.GetFullPath(), cmd)
	if err != nil {
		log.Info("Error", cmd, err)
		return err
	}
	return nil
}

func (repo *Repo) RunVerboseOnError(cmd []string) (*cmd.Status, error) {
	r, err := repo.RunStrict(cmd)
	if err == nil {
		return r, err
	}
	log.Info("Run Error:", repo.GetGoPath(), cmd, err)
	for _, line := range r.Stdout {
		log.Info(line)
	}
	for _, line := range r.Stderr {
		log.Info(line)
	}
	return r, err
}

// only safe to run len() on STDOUT
// DO NOT TRY TO PARSE THIS EXCEPT HASH AS FIRST VALUE
// Intended to be human readable
func (repo *Repo) ConstructGitDiffLog(branch1, branch2 string) []string {
	var cmd []string
	cmd = append(cmd, "git")
	cmd = append(cmd, "log")
	cmd = append(cmd, "--format=\"%H %ae %as %s\"")
	cmd = append(cmd, branch1)
	cmd = append(cmd, "--not")
	cmd = append(cmd, branch2)
	return cmd
}

// count all objects only in branch1
func (repo *Repo) CountDiffObjects(branch1, branch2 string) int {
	cmd := repo.ConstructGitDiffLog(branch1, branch2)
	r, err := repo.RunVerboseOnError(cmd)
	if err != nil {
		return -1
	}
	// log.Info("countDiffObjects()", cmd, len(r.Stdout), strings.Join(r.Stdout, " "))
	return len(r.Stdout)
}

func (repo *Repo) RunPipe(cmd1 []string, cmd2 []string) cmd.Status {
	var s cmd.Status
	var arg0 string
	var args []string
	if len(cmd1) == 0 {
		s.Error = errors.New("Error: Command slice is empty.")
		return s
	}
	if len(cmd1) == 1 {
		// Pass the first element as the command, and the rest as variadic arguments
		arg0 = cmd1[0]
	} else {
		arg0 = cmd1[0]
		args = cmd1[1:]
	}
	cmdShow := exec.Command(arg0, args...)
	cmdShow.Dir = repo.GetFullPath()

	if len(cmd2) == 0 {
		s.Error = errors.New("Error: Command slice is empty.")
		return s
	}
	if len(cmd2) == 1 {
		// Pass the first element as the command, and the rest as variadic arguments
		arg0 = cmd2[0]
	} else {
		arg0 = cmd2[0]
		args = cmd2[1:]
	}
	// 2. Create the command to calculate the patch-id from stdin.
	cmdPipeID := exec.Command(arg0, args...)
	cmdPipeID.Dir = repo.GetFullPath()

	// 3. Connect the output of "git show" to the input of "git patch-id".
	//    This is the Go equivalent of the shell pipe `|`.
	pipe, err := cmdShow.StdoutPipe()
	if err != nil {
		s.Error = fmt.Errorf("failed to create pipe: %w", err)
		return s
	}
	cmdPipeID.Stdin = pipe

	// 4. We need a buffer to capture the final output from git patch-id.
	var output bytes.Buffer
	cmdPipeID.Stdout = &output

	// 5. Start the reading command (patch-id) first.
	if err := cmdPipeID.Start(); err != nil {
		s.Error = fmt.Errorf("failed to start git-patch-id: %w", err)
		return s
	}

	// 6. Run the writing command (show). This will block until it's done.
	if err := cmdShow.Run(); err != nil {
		s.Error = fmt.Errorf("failed to run git-show: %w", err)
		return s
	}

	// 7. Wait for the reading command to finish.
	if err := cmdPipeID.Wait(); err != nil {
		s.Error = fmt.Errorf("failed to wait for git-patch-id: %w", err)
		return s
	}

	return s
}