2022-01-08 05:00:36 +02:00
|
|
|
package git_commands
|
2020-09-29 12:03:39 +02:00
|
|
|
|
|
|
|
import (
|
2022-09-13 12:11:03 +02:00
|
|
|
"os"
|
2021-08-03 14:38:03 +02:00
|
|
|
"strconv"
|
2023-03-26 14:04:09 +02:00
|
|
|
"strings"
|
2020-09-29 12:03:39 +02:00
|
|
|
|
|
|
|
"github.com/go-errors/errors"
|
2023-03-26 14:04:09 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/config"
|
2020-09-29 12:03:39 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
|
|
|
)
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
type FileCommands struct {
|
2022-01-18 12:26:21 +02:00
|
|
|
*GitCommon
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
|
2022-01-18 12:26:21 +02:00
|
|
|
func NewFileCommands(gitCommon *GitCommon) *FileCommands {
|
2022-01-02 01:34:33 +02:00
|
|
|
return &FileCommands{
|
2022-01-18 12:26:21 +02:00
|
|
|
GitCommon: gitCommon,
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
// Cat obtains the content of a file
|
|
|
|
func (self *FileCommands) Cat(fileName string) (string, error) {
|
2022-09-13 12:11:03 +02:00
|
|
|
buf, err := os.ReadFile(fileName)
|
2020-09-29 12:03:39 +02:00
|
|
|
if err != nil {
|
2022-01-02 01:34:33 +02:00
|
|
|
return "", nil
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
2022-01-02 01:34:33 +02:00
|
|
|
return string(buf), nil
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
2020-11-24 23:52:00 +02:00
|
|
|
|
2023-03-26 14:04:09 +02:00
|
|
|
func (self *FileCommands) GetEditCmdStrLegacy(filename string, lineNumber int) (string, error) {
|
2022-01-08 05:00:36 +02:00
|
|
|
editor := self.UserConfig.OS.EditCommand
|
2021-05-20 08:44:58 +02:00
|
|
|
|
|
|
|
if editor == "" {
|
2022-01-08 05:00:36 +02:00
|
|
|
editor = self.config.GetCoreEditor()
|
2021-05-20 08:44:58 +02:00
|
|
|
}
|
2021-04-01 11:27:06 +02:00
|
|
|
if editor == "" {
|
2022-01-08 05:00:36 +02:00
|
|
|
editor = self.os.Getenv("GIT_EDITOR")
|
2021-04-01 11:27:06 +02:00
|
|
|
}
|
2020-11-24 23:52:00 +02:00
|
|
|
if editor == "" {
|
2022-01-08 05:00:36 +02:00
|
|
|
editor = self.os.Getenv("VISUAL")
|
2020-11-24 23:52:00 +02:00
|
|
|
}
|
|
|
|
if editor == "" {
|
2022-01-08 05:00:36 +02:00
|
|
|
editor = self.os.Getenv("EDITOR")
|
2020-11-24 23:52:00 +02:00
|
|
|
}
|
|
|
|
if editor == "" {
|
2023-05-21 09:00:29 +02:00
|
|
|
if err := self.cmd.New([]string{"which", "vi"}).DontLog().Run(); err == nil {
|
2020-11-24 23:52:00 +02:00
|
|
|
editor = "vi"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if editor == "" {
|
2021-06-01 08:23:33 +02:00
|
|
|
return "", errors.New("No editor defined in config file, $GIT_EDITOR, $VISUAL, $EDITOR, or git config")
|
2021-04-10 03:40:42 +02:00
|
|
|
}
|
|
|
|
|
2021-08-03 14:38:03 +02:00
|
|
|
templateValues := map[string]string{
|
|
|
|
"editor": editor,
|
2022-01-08 05:00:36 +02:00
|
|
|
"filename": self.cmd.Quote(filename),
|
2021-08-03 14:38:03 +02:00
|
|
|
"line": strconv.Itoa(lineNumber),
|
|
|
|
}
|
|
|
|
|
2022-01-08 05:00:36 +02:00
|
|
|
editCmdTemplate := self.UserConfig.OS.EditCommandTemplate
|
2022-04-22 14:54:03 +02:00
|
|
|
if len(editCmdTemplate) == 0 {
|
2022-02-15 20:34:36 +02:00
|
|
|
switch editor {
|
2022-04-22 14:54:03 +02:00
|
|
|
case "emacs", "nano", "vi", "vim", "nvim":
|
|
|
|
editCmdTemplate = "{{editor}} +{{line}} -- {{filename}}"
|
2022-02-15 20:34:36 +02:00
|
|
|
case "subl":
|
2022-04-22 14:54:03 +02:00
|
|
|
editCmdTemplate = "{{editor}} -- {{filename}}:{{line}}"
|
2022-02-15 20:34:36 +02:00
|
|
|
case "code":
|
2022-04-22 14:54:03 +02:00
|
|
|
editCmdTemplate = "{{editor}} -r --goto -- {{filename}}:{{line}}"
|
|
|
|
default:
|
|
|
|
editCmdTemplate = "{{editor}} -- {{filename}}"
|
2022-02-15 20:34:36 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-04 11:43:34 +02:00
|
|
|
return utils.ResolvePlaceholderString(editCmdTemplate, templateValues), nil
|
2021-04-10 03:40:42 +02:00
|
|
|
}
|
2023-03-26 14:04:09 +02:00
|
|
|
|
|
|
|
func (self *FileCommands) GetEditCmdStr(filename string) (string, bool) {
|
2023-03-28 17:49:55 +02:00
|
|
|
// Legacy support for old config; to be removed at some point
|
|
|
|
if self.UserConfig.OS.Edit == "" && self.UserConfig.OS.EditCommandTemplate != "" {
|
|
|
|
if cmdStr, err := self.GetEditCmdStrLegacy(filename, 1); err == nil {
|
|
|
|
return cmdStr, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-26 14:04:09 +02:00
|
|
|
template, editInTerminal := config.GetEditTemplate(&self.UserConfig.OS, self.guessDefaultEditor)
|
|
|
|
|
|
|
|
templateValues := map[string]string{
|
|
|
|
"filename": self.cmd.Quote(filename),
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdStr := utils.ResolvePlaceholderString(template, templateValues)
|
|
|
|
return cmdStr, editInTerminal
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *FileCommands) GetEditAtLineCmdStr(filename string, lineNumber int) (string, bool) {
|
2023-03-28 17:49:55 +02:00
|
|
|
// Legacy support for old config; to be removed at some point
|
|
|
|
if self.UserConfig.OS.EditAtLine == "" && self.UserConfig.OS.EditCommandTemplate != "" {
|
|
|
|
if cmdStr, err := self.GetEditCmdStrLegacy(filename, lineNumber); err == nil {
|
|
|
|
return cmdStr, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-26 14:04:09 +02:00
|
|
|
template, editInTerminal := config.GetEditAtLineTemplate(&self.UserConfig.OS, self.guessDefaultEditor)
|
|
|
|
|
|
|
|
templateValues := map[string]string{
|
|
|
|
"filename": self.cmd.Quote(filename),
|
|
|
|
"line": strconv.Itoa(lineNumber),
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdStr := utils.ResolvePlaceholderString(template, templateValues)
|
|
|
|
return cmdStr, editInTerminal
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *FileCommands) GetEditAtLineAndWaitCmdStr(filename string, lineNumber int) string {
|
2023-03-28 17:49:55 +02:00
|
|
|
// Legacy support for old config; to be removed at some point
|
|
|
|
if self.UserConfig.OS.EditAtLineAndWait == "" && self.UserConfig.OS.EditCommandTemplate != "" {
|
|
|
|
if cmdStr, err := self.GetEditCmdStrLegacy(filename, lineNumber); err == nil {
|
|
|
|
return cmdStr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-26 14:04:09 +02:00
|
|
|
template := config.GetEditAtLineAndWaitTemplate(&self.UserConfig.OS, self.guessDefaultEditor)
|
|
|
|
|
|
|
|
templateValues := map[string]string{
|
|
|
|
"filename": self.cmd.Quote(filename),
|
|
|
|
"line": strconv.Itoa(lineNumber),
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdStr := utils.ResolvePlaceholderString(template, templateValues)
|
|
|
|
return cmdStr
|
|
|
|
}
|
|
|
|
|
2023-07-17 07:45:10 +02:00
|
|
|
func (self *FileCommands) GetOpenDirInEditorCmdStr(path string) string {
|
|
|
|
template := config.GetOpenDirInEditorTemplate(&self.UserConfig.OS, self.guessDefaultEditor)
|
|
|
|
|
|
|
|
templateValues := map[string]string{
|
|
|
|
"dir": self.cmd.Quote(path),
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdStr := utils.ResolvePlaceholderString(template, templateValues)
|
|
|
|
return cmdStr
|
|
|
|
}
|
|
|
|
|
2023-03-26 14:04:09 +02:00
|
|
|
func (self *FileCommands) guessDefaultEditor() string {
|
|
|
|
// Try to query a few places where editors get configured
|
|
|
|
editor := self.config.GetCoreEditor()
|
|
|
|
if editor == "" {
|
|
|
|
editor = self.os.Getenv("GIT_EDITOR")
|
|
|
|
}
|
|
|
|
if editor == "" {
|
|
|
|
editor = self.os.Getenv("VISUAL")
|
|
|
|
}
|
|
|
|
if editor == "" {
|
|
|
|
editor = self.os.Getenv("EDITOR")
|
|
|
|
}
|
|
|
|
|
|
|
|
if editor != "" {
|
|
|
|
// At this point, it might be more than just the name of the editor;
|
|
|
|
// e.g. it might be "code -w" or "vim -u myvim.rc". So assume that
|
|
|
|
// everything up to the first space is the editor name.
|
|
|
|
editor = strings.Split(editor, " ")[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return editor
|
|
|
|
}
|