2022-08-12 01:19:39 +02:00
|
|
|
package components
|
2022-08-07 14:09:39 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/secureexec"
|
|
|
|
"github.com/mgutz/str"
|
|
|
|
)
|
|
|
|
|
2022-08-09 13:27:12 +02:00
|
|
|
// this is for running shell commands, mostly for the sake of setting up the repo
|
|
|
|
// but you can also run the commands from within lazygit to emulate things happening
|
|
|
|
// in the background.
|
|
|
|
type Shell struct{}
|
2022-08-07 14:09:39 +02:00
|
|
|
|
2022-08-09 13:27:12 +02:00
|
|
|
func NewShell() *Shell {
|
|
|
|
return &Shell{}
|
|
|
|
}
|
2022-08-07 14:09:39 +02:00
|
|
|
|
2022-08-09 13:27:12 +02:00
|
|
|
func (s *Shell) RunCommand(cmdStr string) *Shell {
|
2022-08-07 14:09:39 +02:00
|
|
|
args := str.ToArgv(cmdStr)
|
|
|
|
cmd := secureexec.Command(args[0], args[1:]...)
|
|
|
|
cmd.Env = os.Environ()
|
|
|
|
|
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("error running command: %s\n%s", cmdStr, string(output)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2022-08-09 13:27:12 +02:00
|
|
|
func (s *Shell) CreateFile(path string, content string) *Shell {
|
2022-08-07 14:09:39 +02:00
|
|
|
err := ioutil.WriteFile(path, []byte(content), 0o644)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("error creating file: %s\n%s", path, err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2022-08-09 13:27:12 +02:00
|
|
|
func (s *Shell) NewBranch(name string) *Shell {
|
2022-08-07 14:09:39 +02:00
|
|
|
return s.RunCommand("git checkout -b " + name)
|
|
|
|
}
|
|
|
|
|
2022-08-22 11:52:05 +02:00
|
|
|
func (s *Shell) Checkout(name string) *Shell {
|
|
|
|
return s.RunCommand("git checkout " + name)
|
|
|
|
}
|
|
|
|
|
2022-08-09 13:27:12 +02:00
|
|
|
func (s *Shell) GitAdd(path string) *Shell {
|
2022-08-08 13:32:58 +02:00
|
|
|
return s.RunCommand(fmt.Sprintf("git add \"%s\"", path))
|
|
|
|
}
|
|
|
|
|
2022-08-09 13:27:12 +02:00
|
|
|
func (s *Shell) GitAddAll() *Shell {
|
2022-08-07 14:09:39 +02:00
|
|
|
return s.RunCommand("git add -A")
|
|
|
|
}
|
|
|
|
|
2022-08-09 13:27:12 +02:00
|
|
|
func (s *Shell) Commit(message string) *Shell {
|
2022-08-07 14:09:39 +02:00
|
|
|
return s.RunCommand(fmt.Sprintf("git commit -m \"%s\"", message))
|
|
|
|
}
|
|
|
|
|
2022-08-09 13:27:12 +02:00
|
|
|
func (s *Shell) EmptyCommit(message string) *Shell {
|
2022-08-07 14:09:39 +02:00
|
|
|
return s.RunCommand(fmt.Sprintf("git commit --allow-empty -m \"%s\"", message))
|
|
|
|
}
|
2022-08-08 13:32:58 +02:00
|
|
|
|
2022-08-09 13:27:12 +02:00
|
|
|
// convenience method for creating a file and adding it
|
|
|
|
func (s *Shell) CreateFileAndAdd(fileName string, fileContents string) *Shell {
|
2022-08-08 13:32:58 +02:00
|
|
|
return s.
|
|
|
|
CreateFile(fileName, fileContents).
|
|
|
|
GitAdd(fileName)
|
|
|
|
}
|
|
|
|
|
2022-08-09 13:27:12 +02:00
|
|
|
// creates commits 01, 02, 03, ..., n with a new file in each
|
|
|
|
// The reason for padding with zeroes is so that it's easier to do string
|
|
|
|
// matches on the commit messages when there are many of them
|
|
|
|
func (s *Shell) CreateNCommits(n int) *Shell {
|
2022-08-08 13:32:58 +02:00
|
|
|
for i := 1; i <= n; i++ {
|
|
|
|
s.CreateFileAndAdd(
|
|
|
|
fmt.Sprintf("file%02d.txt", i),
|
|
|
|
fmt.Sprintf("file%02d content", i),
|
|
|
|
).
|
|
|
|
Commit(fmt.Sprintf("commit %02d", i))
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|