summaryrefslogtreecommitdiffstats
path: root/pkg/commands/git_commands/file.go
blob: 9401e6d547f9cc9971b71e6a4158363719334ab9 (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
package git_commands

import (
	"os"
	"strconv"
	"strings"

	"github.com/go-errors/errors"
	"github.com/jesseduffield/lazygit/pkg/config"
	"github.com/jesseduffield/lazygit/pkg/utils"
	"github.com/samber/lo"
)

type FileCommands struct {
	*GitCommon
}

func NewFileCommands(gitCommon *GitCommon) *FileCommands {
	return &FileCommands{
		GitCommon: gitCommon,
	}
}

// Cat obtains the content of a file
func (self *FileCommands) Cat(fileName string) (string, error) {
	buf, err := os.ReadFile(fileName)
	if err != nil {
		return "", nil
	}
	return string(buf), nil
}

func (self *FileCommands) GetEditCmdStrLegacy(filename string, lineNumber int) (string, error) {
	editor := self.UserConfig.OS.EditCommand

	if editor == "" {
		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 == "" {
		if err := self.cmd.New([]string{"which", "vi"}).DontLog().Run(); err == nil {
			editor = "vi"
		}
	}
	if editor == "" {
		return "", errors.New("No editor defined in config file, $GIT_EDITOR, $VISUAL, $EDITOR, or git config")
	}

	templateValues := map[string]string{
		"editor":   editor,
		"filename": self.cmd.Quote(filename),
		"line":     strconv.Itoa(lineNumber),
	}

	editCmdTemplate := self.UserConfig.OS.EditCommandTemplate
	if len(editCmdTemplate) == 0 {
		switch editor {
		case "emacs", "nano", "vi", "vim", "nvim":
			editCmdTemplate = "{{editor}} +{{line}} -- {{filename}}"
		case "subl":
			editCmdTemplate = "{{editor}} -- {{filename}}:{{line}}"
		case "code":
			editCmdTemplate = "{{editor}} -r --goto -- {{filename}}:{{line}}"
		default:
			editCmdTemplate = "{{editor}} -- {{filename}}"
		}
	}
	return utils.ResolvePlaceholderString(editCmdTemplate, templateValues), nil
}

func (self *FileCommands) GetEditCmdStr(filenames []string) (string, bool) {
	// Legacy support for old config; to be removed at some point
	if self.UserConfig.OS.Edit == "" && self.UserConfig.OS.EditCommandTemplate != "" {
		// If multiple files are selected, we'll simply edit just the first one.
		// It's not worth fixing this for the legacy support.
		if cmdStr, err := self.GetEditCmdStrLegacy(filenames[0], 1); err == nil {
			return cmdStr, true
		}
	}

	template, suspend := config.GetEditTemplate(&self.UserConfig.OS, self.guessDefaultEditor)
	quotedFilenames := lo.Map(filenames, func(filename string, _ int) string { return self.cmd.Quote(filename) })

	templateValues := map[string]string{
		"filename": strings.Join(quotedFilenames, " "),
	}

	cmdStr := utils.ResolvePlaceholderString(template, templateValues)
	return cmdStr, suspend
}

func (self *FileCommands) GetEditAtLineCmdStr(filename string, lineNumber int) (string, bool) {
	// 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
		}
	}

	template, suspend := 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, suspend
}

func (self *FileCommands) GetEditAtLineAndWaitCmdStr(filename string, lineNumber int) string {
	// 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
		}
	}

	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
}

func (self *FileCommands) GetOpenDirInEditorCmdStr(path string) (string, bool) {
	template, suspend := config.GetOpenDirInEditorTemplate(&self.UserConfig.OS, self.guessDefaultEditor)

	templateValues := map[string]string{
		"dir": self.cmd.Quote(path),
	}

	cmdStr := utils.ResolvePlaceholderString(template, templateValues)
	return cmdStr, suspend
}

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
}