1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/commands/git_commands/working_tree.go

372 lines
10 KiB
Go
Raw Normal View History

2022-01-08 05:00:36 +02:00
package git_commands
2022-01-02 01:34:33 +02:00
import (
"fmt"
"os"
"github.com/go-errors/errors"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
)
type WorkingTreeCommands struct {
*GitCommon
2022-01-02 01:34:33 +02:00
submodule *SubmoduleCommands
fileLoader *FileLoader
2022-01-02 01:34:33 +02:00
}
func NewWorkingTreeCommands(
gitCommon *GitCommon,
submodule *SubmoduleCommands,
fileLoader *FileLoader,
2022-01-02 01:34:33 +02:00
) *WorkingTreeCommands {
return &WorkingTreeCommands{
GitCommon: gitCommon,
submodule: submodule,
2022-01-02 01:34:33 +02:00
fileLoader: fileLoader,
}
}
func (self *WorkingTreeCommands) OpenMergeToolCmdObj() oscommands.ICmdObj {
return self.cmd.New(NewGitCmd("mergetool").ToArgv())
2022-01-02 01:34:33 +02:00
}
func (self *WorkingTreeCommands) OpenMergeTool() error {
return self.OpenMergeToolCmdObj().Run()
}
// StageFile stages a file
2022-01-25 16:20:19 +02:00
func (self *WorkingTreeCommands) StageFile(path string) error {
return self.StageFiles([]string{path})
}
func (self *WorkingTreeCommands) StageFiles(paths []string) error {
cmdArgs := NewGitCmd("add").Arg("--").Arg(paths...).ToArgv()
return self.cmd.New(cmdArgs).Run()
2022-01-02 01:34:33 +02:00
}
// StageAll stages all files
func (self *WorkingTreeCommands) StageAll() error {
cmdArgs := NewGitCmd("add").Arg("-A").ToArgv()
return self.cmd.New(cmdArgs).Run()
2022-01-02 01:34:33 +02:00
}
// UnstageAll unstages all files
func (self *WorkingTreeCommands) UnstageAll() error {
return self.cmd.New(NewGitCmd("reset").ToArgv()).Run()
2022-01-02 01:34:33 +02:00
}
// UnStageFile unstages a file
// we accept an array of filenames for the cases where a file has been renamed i.e.
// we accept the current name and the previous name
func (self *WorkingTreeCommands) UnStageFile(fileNames []string, reset bool) error {
for _, name := range fileNames {
var cmdArgs []string
if reset {
cmdArgs = NewGitCmd("reset").Arg("HEAD", "--", name).ToArgv()
} else {
cmdArgs = NewGitCmd("rm").Arg("--cached", "--force", "--", name).ToArgv()
}
err := self.cmd.New(cmdArgs).Run()
2022-01-02 01:34:33 +02:00
if err != nil {
return err
}
}
return nil
}
2022-01-08 05:00:36 +02:00
func (self *WorkingTreeCommands) BeforeAndAfterFileForRename(file *models.File) (*models.File, *models.File, error) {
2022-01-02 01:34:33 +02:00
if !file.IsRename() {
return nil, nil, errors.New("Expected renamed file")
}
// we've got a file that represents a rename from one file to another. Here we will refetch
// all files, passing the --no-renames flag and then recursively call the function
// again for the before file and after file.
filesWithoutRenames := self.fileLoader.GetStatusFiles(GetStatusFileOptions{NoRenames: true})
2022-01-02 01:34:33 +02:00
var beforeFile *models.File
var afterFile *models.File
for _, f := range filesWithoutRenames {
if f.Name == file.PreviousName {
beforeFile = f
}
if f.Name == file.Name {
afterFile = f
}
}
if beforeFile == nil || afterFile == nil {
return nil, nil, errors.New("Could not find deleted file or new file for file rename")
}
if beforeFile.IsRename() || afterFile.IsRename() {
// probably won't happen but we want to ensure we don't get an infinite loop
return nil, nil, errors.New("Nested rename found")
}
return beforeFile, afterFile, nil
}
// DiscardAllFileChanges directly
2022-01-08 05:00:36 +02:00
func (self *WorkingTreeCommands) DiscardAllFileChanges(file *models.File) error {
2022-01-02 01:34:33 +02:00
if file.IsRename() {
2022-01-08 05:00:36 +02:00
beforeFile, afterFile, err := self.BeforeAndAfterFileForRename(file)
2022-01-02 01:34:33 +02:00
if err != nil {
return err
}
2022-01-08 05:00:36 +02:00
if err := self.DiscardAllFileChanges(beforeFile); err != nil {
2022-01-02 01:34:33 +02:00
return err
}
2022-01-08 05:00:36 +02:00
if err := self.DiscardAllFileChanges(afterFile); err != nil {
2022-01-02 01:34:33 +02:00
return err
}
return nil
}
if file.ShortStatus == "AA" {
if err := self.cmd.New(
NewGitCmd("checkout").Arg("--ours", "--", file.Name).ToArgv(),
).Run(); err != nil {
2022-01-02 01:34:33 +02:00
return err
}
if err := self.cmd.New(
NewGitCmd("add").Arg("--", file.Name).ToArgv(),
).Run(); err != nil {
2022-01-02 01:34:33 +02:00
return err
}
return nil
}
if file.ShortStatus == "DU" {
return self.cmd.New(
NewGitCmd("rm").Arg("--", file.Name).ToArgv(),
).Run()
2022-01-02 01:34:33 +02:00
}
// if the file isn't tracked, we assume you want to delete it
if file.HasStagedChanges || file.HasMergeConflicts {
if err := self.cmd.New(
NewGitCmd("reset").Arg("--", file.Name).ToArgv(),
).Run(); err != nil {
2022-01-02 01:34:33 +02:00
return err
}
}
if file.ShortStatus == "DD" || file.ShortStatus == "AU" {
return nil
}
if file.Added {
2022-01-08 05:00:36 +02:00
return self.os.RemoveFile(file.Name)
2022-01-02 01:34:33 +02:00
}
2022-01-08 05:00:36 +02:00
return self.DiscardUnstagedFileChanges(file)
2022-01-02 01:34:33 +02:00
}
2022-01-21 15:13:51 +02:00
type IFileNode interface {
ForEachFile(cb func(*models.File) error) error
GetFilePathsMatching(test func(*models.File) bool) []string
GetPath() string
}
func (self *WorkingTreeCommands) DiscardAllDirChanges(node IFileNode) error {
2022-01-02 01:34:33 +02:00
// this could be more efficient but we would need to handle all the edge cases
2022-01-08 05:00:36 +02:00
return node.ForEachFile(self.DiscardAllFileChanges)
2022-01-02 01:34:33 +02:00
}
2022-01-21 15:13:51 +02:00
func (self *WorkingTreeCommands) DiscardUnstagedDirChanges(node IFileNode) error {
2022-01-08 05:00:36 +02:00
if err := self.RemoveUntrackedDirFiles(node); err != nil {
2022-01-02 01:34:33 +02:00
return err
}
cmdArgs := NewGitCmd("checkout").Arg("--", node.GetPath()).ToArgv()
if err := self.cmd.New(cmdArgs).Run(); err != nil {
2022-01-02 01:34:33 +02:00
return err
}
return nil
}
2022-01-21 15:13:51 +02:00
func (self *WorkingTreeCommands) RemoveUntrackedDirFiles(node IFileNode) error {
untrackedFilePaths := node.GetFilePathsMatching(
func(file *models.File) bool { return !file.GetIsTracked() },
2022-01-02 01:34:33 +02:00
)
for _, path := range untrackedFilePaths {
err := os.Remove(path)
if err != nil {
return err
}
}
return nil
}
// DiscardUnstagedFileChanges directly
2022-01-08 05:00:36 +02:00
func (self *WorkingTreeCommands) DiscardUnstagedFileChanges(file *models.File) error {
cmdArgs := NewGitCmd("checkout").Arg("--", file.Name).ToArgv()
return self.cmd.New(cmdArgs).Run()
2022-01-02 01:34:33 +02:00
}
// Ignore adds a file to the gitignore for the repo
2022-01-08 05:00:36 +02:00
func (self *WorkingTreeCommands) Ignore(filename string) error {
return self.os.AppendLineToFile(".gitignore", filename)
2022-01-02 01:34:33 +02:00
}
// Exclude adds a file to the .git/info/exclude for the repo
func (self *WorkingTreeCommands) Exclude(filename string) error {
return self.os.AppendLineToFile(".git/info/exclude", filename)
}
2022-01-02 01:34:33 +02:00
// WorktreeFileDiff returns the diff of a file
2022-01-08 05:00:36 +02:00
func (self *WorkingTreeCommands) WorktreeFileDiff(file *models.File, plain bool, cached bool, ignoreWhitespace bool) string {
2022-01-02 01:34:33 +02:00
// for now we assume an error means the file was deleted
2022-01-08 05:00:36 +02:00
s, _ := self.WorktreeFileDiffCmdObj(file, plain, cached, ignoreWhitespace).RunWithOutput()
2022-01-02 01:34:33 +02:00
return s
}
2022-01-08 05:00:36 +02:00
func (self *WorkingTreeCommands) WorktreeFileDiffCmdObj(node models.IFile, plain bool, cached bool, ignoreWhitespace bool) oscommands.ICmdObj {
colorArg := self.UserConfig.Git.Paging.ColorArg
2022-01-02 01:34:33 +02:00
if plain {
colorArg = "never"
}
contextSize := self.UserConfig.Git.DiffContextSize
prevPath := node.GetPreviousPath()
noIndex := !node.GetIsTracked() && !node.GetHasStagedChanges() && !cached && node.GetIsFile()
cmdArgs := NewGitCmd("diff").
Arg("--submodule").
Arg("--no-ext-diff").
Arg(fmt.Sprintf("--unified=%d", contextSize)).
Arg(fmt.Sprintf("--color=%s", colorArg)).
ArgIf(ignoreWhitespace, "--ignore-all-space").
ArgIf(cached, "--cached").
ArgIf(noIndex, "--no-index").
Arg("--").
ArgIf(noIndex, "/dev/null").
Arg(node.GetPath()).
ArgIf(prevPath != "", prevPath).
ToArgv()
2022-01-02 01:34:33 +02:00
return self.cmd.New(cmdArgs).DontLog()
2022-01-02 01:34:33 +02:00
}
// ShowFileDiff get the diff of specified from and to. Typically this will be used for a single commit so it'll be 123abc^..123abc
// but when we're in diff mode it could be any 'from' to any 'to'. The reverse flag is also here thanks to diff mode.
func (self *WorkingTreeCommands) ShowFileDiff(from string, to string, reverse bool, fileName string, plain bool,
ignoreWhitespace bool,
) (string, error) {
return self.ShowFileDiffCmdObj(from, to, reverse, fileName, plain, ignoreWhitespace).RunWithOutput()
2022-01-02 01:34:33 +02:00
}
func (self *WorkingTreeCommands) ShowFileDiffCmdObj(from string, to string, reverse bool, fileName string, plain bool,
ignoreWhitespace bool,
) oscommands.ICmdObj {
2022-01-08 05:00:36 +02:00
contextSize := self.UserConfig.Git.DiffContextSize
colorArg := self.UserConfig.Git.Paging.ColorArg
2022-01-02 01:34:33 +02:00
if plain {
colorArg = "never"
}
cmdArgs := NewGitCmd("diff").
Arg("--submodule").
Arg("--no-ext-diff").
Arg(fmt.Sprintf("--unified=%d", contextSize)).
Arg("--no-renames").
Arg(fmt.Sprintf("--color=%s", colorArg)).
Arg(from).
Arg(to).
ArgIf(reverse, "-R").
ArgIf(ignoreWhitespace, "--ignore-all-space").
Arg("--").
Arg(fileName).
ToArgv()
return self.cmd.New(cmdArgs).DontLog()
2022-01-02 01:34:33 +02:00
}
// CheckoutFile checks out the file for the given commit
2022-01-08 05:00:36 +02:00
func (self *WorkingTreeCommands) CheckoutFile(commitSha, fileName string) error {
cmdArgs := NewGitCmd("checkout").Arg(commitSha, "--", fileName).
ToArgv()
return self.cmd.New(cmdArgs).Run()
2022-01-02 01:34:33 +02:00
}
// DiscardAnyUnstagedFileChanges discards any unstaged file changes via `git checkout -- .`
2022-01-08 05:00:36 +02:00
func (self *WorkingTreeCommands) DiscardAnyUnstagedFileChanges() error {
cmdArgs := NewGitCmd("checkout").Arg("--", ".").
ToArgv()
return self.cmd.New(cmdArgs).Run()
2022-01-02 01:34:33 +02:00
}
// RemoveTrackedFiles will delete the given file(s) even if they are currently tracked
2022-01-08 05:00:36 +02:00
func (self *WorkingTreeCommands) RemoveTrackedFiles(name string) error {
cmdArgs := NewGitCmd("rm").Arg("-r", "--cached", "--", name).
ToArgv()
return self.cmd.New(cmdArgs).Run()
2022-01-02 01:34:33 +02:00
}
// RemoveUntrackedFiles runs `git clean -fd`
2022-01-08 05:00:36 +02:00
func (self *WorkingTreeCommands) RemoveUntrackedFiles() error {
cmdArgs := NewGitCmd("clean").Arg("-fd").ToArgv()
return self.cmd.New(cmdArgs).Run()
2022-01-02 01:34:33 +02:00
}
// ResetAndClean removes all unstaged changes and removes all untracked files
2022-01-08 05:00:36 +02:00
func (self *WorkingTreeCommands) ResetAndClean() error {
submoduleConfigs, err := self.submodule.GetConfigs()
2022-01-02 01:34:33 +02:00
if err != nil {
return err
}
if len(submoduleConfigs) > 0 {
2022-01-08 05:00:36 +02:00
if err := self.submodule.ResetSubmodules(submoduleConfigs); err != nil {
2022-01-02 01:34:33 +02:00
return err
}
}
2022-01-08 05:00:36 +02:00
if err := self.ResetHard("HEAD"); err != nil {
2022-01-02 01:34:33 +02:00
return err
}
2022-01-08 05:00:36 +02:00
return self.RemoveUntrackedFiles()
2022-01-02 01:34:33 +02:00
}
// ResetHardHead runs `git reset --hard`
func (self *WorkingTreeCommands) ResetHard(ref string) error {
cmdArgs := NewGitCmd("reset").Arg("--hard", ref).
ToArgv()
return self.cmd.New(cmdArgs).Run()
2022-01-02 01:34:33 +02:00
}
// ResetSoft runs `git reset --soft HEAD`
func (self *WorkingTreeCommands) ResetSoft(ref string) error {
cmdArgs := NewGitCmd("reset").Arg("--soft", ref).
ToArgv()
2022-01-02 01:34:33 +02:00
return self.cmd.New(cmdArgs).Run()
2022-01-02 01:34:33 +02:00
}
2022-01-08 07:02:56 +02:00
func (self *WorkingTreeCommands) ResetMixed(ref string) error {
cmdArgs := NewGitCmd("reset").Arg("--mixed", ref).
ToArgv()
2022-01-08 07:02:56 +02:00
return self.cmd.New(cmdArgs).Run()
2022-01-08 07:02:56 +02:00
}