Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/commands/git_commands/rebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ func (self *RebaseCommands) DiscardOldFileChanges(commits []*models.Commit, comm
if err := self.workingTree.StageFile(filePath); err != nil {
return err
}
} else if err := self.workingTree.CheckoutFile("HEAD^", filePath); err != nil {
} else if err := self.workingTree.CheckoutFile("HEAD^", filePath, true); err != nil {
return err
}
}
Expand Down
17 changes: 9 additions & 8 deletions pkg/commands/git_commands/working_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ func (self *WorkingTreeCommands) BeforeAndAfterFileForRename(file *models.File)
return beforeFile, afterFile, nil
}

func newCheckoutCommand() *GitCommandBuilder {
return NewGitCmd("checkout").Config(fmt.Sprintf("core.hooksPath=%s", os.DevNull))
func newCheckoutCommand(skipHooks bool) *GitCommandBuilder {
return NewGitCmd("checkout").
ConfigIf(skipHooks, fmt.Sprintf("core.hooksPath=%s", os.DevNull))
}

// DiscardAllFileChanges directly
Expand All @@ -134,7 +135,7 @@ func (self *WorkingTreeCommands) DiscardAllFileChanges(file *models.File) error

if file.ShortStatus == "AA" {
if err := self.cmd.New(
newCheckoutCommand().Arg("--ours", "--", file.Name).ToArgv(),
newCheckoutCommand(true).Arg("--ours", "--", file.Name).ToArgv(),
).Run(); err != nil {
return err
}
Expand Down Expand Up @@ -193,7 +194,7 @@ func (self *WorkingTreeCommands) DiscardUnstagedDirChanges(node IFileNode) error
return err
}

cmdArgs := newCheckoutCommand().Arg("--", node.GetPath()).ToArgv()
cmdArgs := newCheckoutCommand(true).Arg("--", node.GetPath()).ToArgv()
if err := self.cmd.New(cmdArgs).Run(); err != nil {
return err
}
Expand Down Expand Up @@ -226,7 +227,7 @@ func (self *WorkingTreeCommands) RemoveUntrackedDirFiles(node IFileNode) error {
}

func (self *WorkingTreeCommands) DiscardUnstagedFileChanges(file *models.File) error {
cmdArgs := newCheckoutCommand().Arg("--", file.Name).ToArgv()
cmdArgs := newCheckoutCommand(true).Arg("--", file.Name).ToArgv()
return self.cmd.New(cmdArgs).Run()
}

Expand Down Expand Up @@ -318,16 +319,16 @@ func (self *WorkingTreeCommands) ShowFileDiffCmdObj(from string, to string, reve
}

// CheckoutFile checks out the file for the given commit
func (self *WorkingTreeCommands) CheckoutFile(commitHash, fileName string) error {
cmdArgs := newCheckoutCommand().Arg(commitHash, "--", fileName).
func (self *WorkingTreeCommands) CheckoutFile(commitHash, fileName string, skipHooks bool) error {
cmdArgs := newCheckoutCommand(skipHooks).Arg(commitHash, "--", fileName).
ToArgv()

return self.cmd.New(cmdArgs).Run()
}

// DiscardAnyUnstagedFileChanges discards any unstaged file changes via `git checkout -- .`
func (self *WorkingTreeCommands) DiscardAnyUnstagedFileChanges() error {
cmdArgs := newCheckoutCommand().Arg("--", ".").
cmdArgs := newCheckoutCommand(true).Arg("--", ".").
ToArgv()

return self.cmd.New(cmdArgs).Run()
Expand Down
16 changes: 15 additions & 1 deletion pkg/commands/git_commands/working_tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ func TestWorkingTreeCheckoutFile(t *testing.T) {
testName string
commitHash string
fileName string
skipHooks bool
runner *oscommands.FakeCmdObjRunner
test func(error)
}
Expand All @@ -431,16 +432,29 @@ func TestWorkingTreeCheckoutFile(t *testing.T) {
testName: "typical case",
commitHash: "11af912",
fileName: "test999.txt",
skipHooks: true,
runner: oscommands.NewFakeRunner(t).
ExpectGitArgs([]string{"-c", disableHooksFlag, "checkout", "11af912", "--", "test999.txt"}, "", nil),
test: func(err error) {
assert.NoError(t, err)
},
},
{
testName: "typical case",
commitHash: "11af912",
fileName: "test999.txt",
skipHooks: false,
runner: oscommands.NewFakeRunner(t).
ExpectGitArgs([]string{"checkout", "11af912", "--", "test999.txt"}, "", nil),
test: func(err error) {
assert.NoError(t, err)
},
},
{
testName: "returns error if there is one",
commitHash: "11af912",
fileName: "test999.txt",
skipHooks: true,
runner: oscommands.NewFakeRunner(t).
ExpectGitArgs([]string{"-c", disableHooksFlag, "checkout", "11af912", "--", "test999.txt"}, "", errors.New("error")),
test: func(err error) {
Expand All @@ -453,7 +467,7 @@ func TestWorkingTreeCheckoutFile(t *testing.T) {
t.Run(s.testName, func(t *testing.T) {
instance := buildWorkingTreeCommands(commonDeps{runner: s.runner})

s.test(instance.CheckoutFile(s.commitHash, s.fileName))
s.test(instance.CheckoutFile(s.commitHash, s.fileName, s.skipHooks))
s.runner.CheckForMissingCalls()
})
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/controllers/commits_files_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func (self *CommitFilesController) openCopyMenu() error {

func (self *CommitFilesController) checkout(node *filetree.CommitFileNode) error {
self.c.LogAction(self.c.Tr.Actions.CheckoutFile)
if err := self.c.Git().WorkingTree.CheckoutFile(self.context().GetRef().RefName(), node.GetPath()); err != nil {
if err := self.c.Git().WorkingTree.CheckoutFile(self.context().GetRef().RefName(), node.GetPath(), false); err != nil {
return err
}

Expand Down
55 changes: 55 additions & 0 deletions pkg/integration/tests/commit/checkout_file_from_commit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package commit

import (
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)

var postCheckoutHook = `#!/bin/bash

echo "post-checkout hook called" > hook-result
`

var CheckoutFileFromCommit = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Checkout an individual file from a commit",
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(config *config.AppConfig) {},
SetupRepo: func(shell *Shell) {
shell.CreateFile(".git/hooks/post-checkout", postCheckoutHook)
shell.MakeExecutable(".git/hooks/post-checkout")

shell.CreateFileAndAdd("file", "one\n")
shell.Commit("one")
shell.UpdateFileAndAdd("file", "one\ntwo\n")
shell.Commit("two")
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
t.Views().Commits().
Focus().
Lines(
Contains("two").IsSelected(),
Contains("one"),
).
SelectNextItem().
PressEnter()

t.Views().CommitFiles().
IsFocused().
Lines(
Contains("file").IsSelected(),
).
Press(keys.CommitFiles.CheckoutCommitFile)

t.Views().Files().
Focus().
Lines(
Contains("M file"),
Contains("?? hook-result"),
)

t.FileSystem().FileContent("file", Equals("one\n"))
t.FileSystem().FileContent("hook-result", Equals("post-checkout hook called\n"))
},
},
)
1 change: 1 addition & 0 deletions pkg/integration/tests/test_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ var tests = []*components.IntegrationTest{
commit.AmendWhenThereAreConflictsAndContinue,
commit.AutoWrapMessage,
commit.Checkout,
commit.CheckoutFileFromCommit,
commit.Commit,
commit.CommitMultiline,
commit.CommitSwitchToEditor,
Expand Down