mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-02-09 13:47:11 +02:00
Merge branch 'master' of git://github.com/jesseduffield/lazygit into feature/commit-amend
This commit is contained in:
commit
b6b21bc98e
@ -504,26 +504,22 @@ func (c *GitCommand) Ignore(filename string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Show shows the diff of a commit
|
// Show shows the diff of a commit
|
||||||
func (c *GitCommand) Show(sha string) string {
|
func (c *GitCommand) Show(sha string) (string, error) {
|
||||||
result, err := c.OSCommand.RunCommandWithOutput("git show --color " + sha)
|
return c.OSCommand.RunCommandWithOutput(fmt.Sprintf("git show --color %s", sha))
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Diff returns the diff of a file
|
// Diff returns the diff of a file
|
||||||
func (c *GitCommand) Diff(file *File) string {
|
func (c *GitCommand) Diff(file *File) string {
|
||||||
cachedArg := ""
|
cachedArg := ""
|
||||||
|
trackedArg := "--"
|
||||||
fileName := c.OSCommand.Quote(file.Name)
|
fileName := c.OSCommand.Quote(file.Name)
|
||||||
if file.HasStagedChanges && !file.HasUnstagedChanges {
|
if file.HasStagedChanges && !file.HasUnstagedChanges {
|
||||||
cachedArg = "--cached"
|
cachedArg = "--cached"
|
||||||
}
|
}
|
||||||
trackedArg := "--"
|
|
||||||
if !file.Tracked && !file.HasStagedChanges {
|
if !file.Tracked && !file.HasStagedChanges {
|
||||||
trackedArg = "--no-index /dev/null"
|
trackedArg = "--no-index /dev/null"
|
||||||
}
|
}
|
||||||
command := fmt.Sprintf("%s %s %s %s", "git diff --color ", cachedArg, trackedArg, fileName)
|
command := fmt.Sprintf("git diff --color %s %s %s", cachedArg, trackedArg, fileName)
|
||||||
|
|
||||||
// for now we assume an error means the file was deleted
|
// for now we assume an error means the file was deleted
|
||||||
s, _ := c.OSCommand.RunCommandWithOutput(command)
|
s, _ := c.OSCommand.RunCommandWithOutput(command)
|
||||||
|
@ -9,7 +9,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jesseduffield/lazygit/pkg/i18n"
|
"github.com/jesseduffield/lazygit/pkg/i18n"
|
||||||
"github.com/jesseduffield/lazygit/pkg/test"
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
gogit "gopkg.in/src-d/go-git.v4"
|
gogit "gopkg.in/src-d/go-git.v4"
|
||||||
@ -1421,6 +1420,19 @@ func TestGitCommandRemoveFile(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGitCommandShow(t *testing.T) {
|
||||||
|
gitCmd := newDummyGitCommand()
|
||||||
|
gitCmd.OSCommand.command = func(cmd string, args ...string) *exec.Cmd {
|
||||||
|
assert.EqualValues(t, "git", cmd)
|
||||||
|
assert.EqualValues(t, []string{"show", "--color", "456abcde"}, args)
|
||||||
|
|
||||||
|
return exec.Command("echo")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := gitCmd.Show("456abcde")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
func TestGitCommandCheckout(t *testing.T) {
|
func TestGitCommandCheckout(t *testing.T) {
|
||||||
type scenario struct {
|
type scenario struct {
|
||||||
testName string
|
testName string
|
||||||
@ -1553,97 +1565,106 @@ func TestGitCommandGetCommits(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGitCommandDiff(t *testing.T) {
|
func TestGitCommandGetLog(t *testing.T) {
|
||||||
gitCommand := newDummyGitCommand()
|
type scenario struct {
|
||||||
assert.NoError(t, test.GenerateRepo("lots_of_diffs.sh"))
|
testName string
|
||||||
|
command func(string, ...string) *exec.Cmd
|
||||||
|
test func(string)
|
||||||
|
}
|
||||||
|
|
||||||
files := []*File{
|
scenarios := []scenario{
|
||||||
{
|
{
|
||||||
Name: "deleted_staged",
|
"Retrieves logs",
|
||||||
HasStagedChanges: false,
|
func(cmd string, args ...string) *exec.Cmd {
|
||||||
HasUnstagedChanges: true,
|
assert.EqualValues(t, "git", cmd)
|
||||||
Tracked: true,
|
assert.EqualValues(t, []string{"log", "--oneline", "-30"}, args)
|
||||||
Deleted: true,
|
|
||||||
HasMergeConflicts: false,
|
return exec.Command("echo", "6f0b32f commands/git : add GetCommits tests refactor\n9d9d775 circle : remove new line")
|
||||||
DisplayString: " D deleted_staged",
|
},
|
||||||
|
func(output string) {
|
||||||
|
assert.EqualValues(t, "6f0b32f commands/git : add GetCommits tests refactor\n9d9d775 circle : remove new line\n", output)
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "file with space staged",
|
"An error occurred when retrieving logs",
|
||||||
HasStagedChanges: true,
|
func(cmd string, args ...string) *exec.Cmd {
|
||||||
HasUnstagedChanges: false,
|
assert.EqualValues(t, "git", cmd)
|
||||||
Tracked: false,
|
assert.EqualValues(t, []string{"log", "--oneline", "-30"}, args)
|
||||||
Deleted: false,
|
return exec.Command("test")
|
||||||
HasMergeConflicts: false,
|
},
|
||||||
DisplayString: "A \"file with space staged\"",
|
func(output string) {
|
||||||
},
|
assert.Empty(t, output)
|
||||||
{
|
},
|
||||||
Name: "file with space unstaged",
|
|
||||||
HasStagedChanges: false,
|
|
||||||
HasUnstagedChanges: true,
|
|
||||||
Tracked: false,
|
|
||||||
Deleted: false,
|
|
||||||
HasMergeConflicts: false,
|
|
||||||
DisplayString: "?? file with space unstaged",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "modified_unstaged",
|
|
||||||
HasStagedChanges: true,
|
|
||||||
HasUnstagedChanges: false,
|
|
||||||
Tracked: true,
|
|
||||||
Deleted: false,
|
|
||||||
HasMergeConflicts: false,
|
|
||||||
DisplayString: "M modified_unstaged",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "modified_staged",
|
|
||||||
HasStagedChanges: false,
|
|
||||||
HasUnstagedChanges: true,
|
|
||||||
Tracked: true,
|
|
||||||
Deleted: false,
|
|
||||||
HasMergeConflicts: false,
|
|
||||||
DisplayString: " M modified_staged",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "renamed_before -> renamed_after",
|
|
||||||
HasStagedChanges: true,
|
|
||||||
HasUnstagedChanges: false,
|
|
||||||
Tracked: true,
|
|
||||||
Deleted: false,
|
|
||||||
HasMergeConflicts: false,
|
|
||||||
DisplayString: "R renamed_before -> renamed_after",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "untracked_unstaged",
|
|
||||||
HasStagedChanges: false,
|
|
||||||
HasUnstagedChanges: true,
|
|
||||||
Tracked: false,
|
|
||||||
Deleted: false,
|
|
||||||
HasMergeConflicts: false,
|
|
||||||
DisplayString: "?? untracked_unstaged",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "untracked_staged",
|
|
||||||
HasStagedChanges: true,
|
|
||||||
HasUnstagedChanges: false,
|
|
||||||
Tracked: false,
|
|
||||||
Deleted: false,
|
|
||||||
HasMergeConflicts: false,
|
|
||||||
DisplayString: "A untracked_staged",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "master",
|
|
||||||
HasStagedChanges: false,
|
|
||||||
HasUnstagedChanges: true,
|
|
||||||
Tracked: false,
|
|
||||||
Deleted: false,
|
|
||||||
HasMergeConflicts: false,
|
|
||||||
DisplayString: "?? master",
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, file := range files {
|
for _, s := range scenarios {
|
||||||
t.Run(file.Name, func(t *testing.T) {
|
t.Run(s.testName, func(t *testing.T) {
|
||||||
assert.NotContains(t, gitCommand.Diff(file), "error")
|
gitCmd := newDummyGitCommand()
|
||||||
|
gitCmd.OSCommand.command = s.command
|
||||||
|
s.test(gitCmd.GetLog())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGitCommandDiff(t *testing.T) {
|
||||||
|
type scenario struct {
|
||||||
|
testName string
|
||||||
|
command func(string, ...string) *exec.Cmd
|
||||||
|
file *File
|
||||||
|
}
|
||||||
|
|
||||||
|
scenarios := []scenario{
|
||||||
|
{
|
||||||
|
"Default case",
|
||||||
|
func(cmd string, args ...string) *exec.Cmd {
|
||||||
|
assert.EqualValues(t, "git", cmd)
|
||||||
|
assert.EqualValues(t, []string{"diff", "--color", "--", "test.txt"}, args)
|
||||||
|
|
||||||
|
return exec.Command("echo")
|
||||||
|
},
|
||||||
|
&File{
|
||||||
|
Name: "test.txt",
|
||||||
|
HasStagedChanges: false,
|
||||||
|
Tracked: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"All changes staged",
|
||||||
|
func(cmd string, args ...string) *exec.Cmd {
|
||||||
|
assert.EqualValues(t, "git", cmd)
|
||||||
|
assert.EqualValues(t, []string{"diff", "--color", "--cached", "--", "test.txt"}, args)
|
||||||
|
|
||||||
|
return exec.Command("echo")
|
||||||
|
},
|
||||||
|
&File{
|
||||||
|
Name: "test.txt",
|
||||||
|
HasStagedChanges: true,
|
||||||
|
HasUnstagedChanges: false,
|
||||||
|
Tracked: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"File not tracked and file has no staged changes",
|
||||||
|
func(cmd string, args ...string) *exec.Cmd {
|
||||||
|
assert.EqualValues(t, "git", cmd)
|
||||||
|
assert.EqualValues(t, []string{"diff", "--color", "--no-index", "/dev/null", "test.txt"}, args)
|
||||||
|
|
||||||
|
return exec.Command("echo")
|
||||||
|
},
|
||||||
|
&File{
|
||||||
|
Name: "test.txt",
|
||||||
|
HasStagedChanges: false,
|
||||||
|
Tracked: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, s := range scenarios {
|
||||||
|
t.Run(s.testName, func(t *testing.T) {
|
||||||
|
gitCmd := newDummyGitCommand()
|
||||||
|
gitCmd.OSCommand.command = s.command
|
||||||
|
gitCmd.Diff(s.file)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -68,7 +68,10 @@ func (gui *Gui) handleCommitSelect(g *gocui.Gui, v *gocui.View) error {
|
|||||||
}
|
}
|
||||||
return gui.renderString(g, "main", gui.Tr.SLocalize("NoCommitsThisBranch"))
|
return gui.renderString(g, "main", gui.Tr.SLocalize("NoCommitsThisBranch"))
|
||||||
}
|
}
|
||||||
commitText := gui.GitCommand.Show(commit.Sha)
|
commitText, err := gui.GitCommand.Show(commit.Sha)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return gui.renderString(g, "main", commitText)
|
return gui.renderString(g, "main", commitText)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,35 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
set -ex; rm -rf repo; mkdir repo; cd repo
|
|
||||||
|
|
||||||
git init
|
|
||||||
git config user.email "test@example.com"
|
|
||||||
git config user.name "Lazygit Tester"
|
|
||||||
|
|
||||||
|
|
||||||
echo "deleted" > deleted_staged
|
|
||||||
echo "deleted_unstaged" > deleted_unstaged
|
|
||||||
echo "modified_staged" > modified_staged
|
|
||||||
echo "modified_unstaged" > modified_unstaged
|
|
||||||
echo "renamed" > renamed_before
|
|
||||||
|
|
||||||
git add .
|
|
||||||
git commit -m "files to delete"
|
|
||||||
rm deleted_staged
|
|
||||||
rm deleted_unstaged
|
|
||||||
|
|
||||||
rm renamed_before
|
|
||||||
echo "renamed" > renamed_after
|
|
||||||
echo "more" >> modified_staged
|
|
||||||
echo "more" >> modified_unstaged
|
|
||||||
echo "untracked_staged" > untracked_staged
|
|
||||||
echo "untracked_unstaged" > untracked_unstaged
|
|
||||||
echo "blah" > "file with space staged"
|
|
||||||
echo "blah" > "file with space unstaged"
|
|
||||||
echo "same name as branch" > master
|
|
||||||
|
|
||||||
git add deleted_staged
|
|
||||||
git add modified_staged
|
|
||||||
git add untracked_staged
|
|
||||||
git add "file with space staged"
|
|
||||||
git add renamed_before
|
|
||||||
git add renamed_after
|
|
Loading…
x
Reference in New Issue
Block a user