1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-28 16:02:01 +03:00

move file and submodule

This commit is contained in:
Jesse Duffield
2020-09-29 18:45:00 +10:00
parent eda4619a4f
commit 8d2af5cc61
11 changed files with 69 additions and 66 deletions

View File

@ -1,60 +0,0 @@
package commands
import (
"strings"
"github.com/jesseduffield/lazygit/pkg/utils"
)
// File : A file from git status
// duplicating this for now
type File struct {
Name string
HasStagedChanges bool
HasUnstagedChanges bool
Tracked bool
Deleted bool
HasMergeConflicts bool
HasInlineMergeConflicts bool
DisplayString string
Type string // one of 'file', 'directory', and 'other'
ShortStatus string // e.g. 'AD', ' A', 'M ', '??'
}
const RENAME_SEPARATOR = " -> "
func (f *File) IsRename() bool {
return strings.Contains(f.Name, RENAME_SEPARATOR)
}
// Names returns an array containing just the filename, or in the case of a rename, the after filename and the before filename
func (f *File) Names() []string {
return strings.Split(f.Name, RENAME_SEPARATOR)
}
// returns true if the file names are the same or if a a file rename includes the filename of the other
func (f *File) Matches(f2 *File) bool {
return utils.StringArraysOverlap(f.Names(), f2.Names())
}
func (f *File) ID() string {
return f.Name
}
func (f *File) Description() string {
return f.Name
}
func (f *File) IsSubmodule(configs []*SubmoduleConfig) bool {
return f.SubmoduleConfig(configs) != nil
}
func (f *File) SubmoduleConfig(configs []*SubmoduleConfig) *SubmoduleConfig {
for _, config := range configs {
if f.Name == config.Name {
return config
}
}
return nil
}

View File

@ -272,7 +272,7 @@ func (c *GitCommand) GetConfigValue(key string) string {
return strings.TrimSpace(output)
}
func (c *GitCommand) GetStatusFiles(opts GetStatusFileOptions) []*File {
func (c *GitCommand) GetStatusFiles(opts GetStatusFileOptions) []*models.File {
// check if config wants us ignoring untracked files
untrackedFilesSetting := c.GetConfigValue("status.showUntrackedFiles")
@ -286,7 +286,7 @@ func (c *GitCommand) GetStatusFiles(opts GetStatusFileOptions) []*File {
c.Log.Error(err)
}
statusStrings := utils.SplitLines(statusOutput)
files := []*File{}
files := []*models.File{}
for _, statusString := range statusStrings {
if strings.HasPrefix(statusString, "warning") {
@ -302,7 +302,7 @@ func (c *GitCommand) GetStatusFiles(opts GetStatusFileOptions) []*File {
hasMergeConflicts := utils.IncludesString([]string{"DD", "AA", "UU", "AU", "UA", "UD", "DU"}, change)
hasInlineMergeConflicts := utils.IncludesString([]string{"UU", "AA"}, change)
file := &File{
file := &models.File{
Name: filename,
DisplayString: statusString,
HasStagedChanges: !hasNoStagedChanges,
@ -331,7 +331,7 @@ func (c *GitCommand) StashSave(message string) error {
}
// MergeStatusFiles merge status files
func (c *GitCommand) MergeStatusFiles(oldFiles, newFiles []*File, selectedFile *File) []*File {
func (c *GitCommand) MergeStatusFiles(oldFiles, newFiles []*models.File, selectedFile *models.File) []*models.File {
if len(oldFiles) == 0 {
return newFiles
}
@ -339,7 +339,7 @@ func (c *GitCommand) MergeStatusFiles(oldFiles, newFiles []*File, selectedFile *
appendedIndexes := []int{}
// retain position of files we already could see
result := []*File{}
result := []*models.File{}
for _, oldFile := range oldFiles {
for newIndex, newFile := range newFiles {
if includesInt(appendedIndexes, newIndex) {
@ -679,7 +679,7 @@ func (c *GitCommand) RebaseMode() (string, error) {
}
}
func (c *GitCommand) BeforeAndAfterFileForRename(file *File) (*File, *File, error) {
func (c *GitCommand) BeforeAndAfterFileForRename(file *models.File) (*models.File, *models.File, error) {
if !file.IsRename() {
return nil, nil, errors.New("Expected renamed file")
@ -692,8 +692,8 @@ func (c *GitCommand) BeforeAndAfterFileForRename(file *File) (*File, *File, erro
split := strings.Split(file.Name, " -> ")
filesWithoutRenames := c.GetStatusFiles(GetStatusFileOptions{NoRenames: true})
var beforeFile *File
var afterFile *File
var beforeFile *models.File
var afterFile *models.File
for _, f := range filesWithoutRenames {
if f.Name == split[0] {
beforeFile = f
@ -716,7 +716,7 @@ func (c *GitCommand) BeforeAndAfterFileForRename(file *File) (*File, *File, erro
}
// DiscardAllFileChanges directly
func (c *GitCommand) DiscardAllFileChanges(file *File) error {
func (c *GitCommand) DiscardAllFileChanges(file *models.File) error {
if file.IsRename() {
beforeFile, afterFile, err := c.BeforeAndAfterFileForRename(file)
if err != nil {
@ -749,7 +749,7 @@ func (c *GitCommand) DiscardAllFileChanges(file *File) error {
}
// DiscardUnstagedFileChanges directly
func (c *GitCommand) DiscardUnstagedFileChanges(file *File) error {
func (c *GitCommand) DiscardUnstagedFileChanges(file *models.File) error {
quotedFileName := c.OSCommand.Quote(file.Name)
return c.OSCommand.RunCommand("git checkout -- %s", quotedFileName)
}
@ -824,13 +824,13 @@ func (c *GitCommand) CheckRemoteBranchExists(branch *models.Branch) bool {
}
// WorktreeFileDiff returns the diff of a file
func (c *GitCommand) WorktreeFileDiff(file *File, plain bool, cached bool) string {
func (c *GitCommand) WorktreeFileDiff(file *models.File, plain bool, cached bool) string {
// for now we assume an error means the file was deleted
s, _ := c.OSCommand.RunCommandWithOutput(c.WorktreeFileDiffCmdStr(file, plain, cached))
return s
}
func (c *GitCommand) WorktreeFileDiffCmdStr(file *File, plain bool, cached bool) string {
func (c *GitCommand) WorktreeFileDiffCmdStr(file *models.File, plain bool, cached bool) string {
cachedArg := ""
trackedArg := "--"
colorArg := c.colorArg()

View File

@ -311,7 +311,7 @@ func TestGitCommandGetStatusFiles(t *testing.T) {
type scenario struct {
testName string
command func(string, ...string) *exec.Cmd
test func([]*File)
test func([]*models.File)
}
scenarios := []scenario{
@ -320,7 +320,7 @@ func TestGitCommandGetStatusFiles(t *testing.T) {
func(cmd string, args ...string) *exec.Cmd {
return exec.Command("echo")
},
func(files []*File) {
func(files []*models.File) {
assert.Len(t, files, 0)
},
},
@ -332,10 +332,10 @@ func TestGitCommandGetStatusFiles(t *testing.T) {
"MM file1.txt\nA file3.txt\nAM file2.txt\n?? file4.txt\nUU file5.txt",
)
},
func(files []*File) {
func(files []*models.File) {
assert.Len(t, files, 5)
expected := []*File{
expected := []*models.File{
{
Name: "file1.txt",
HasStagedChanges: true,
@ -457,22 +457,22 @@ func TestGitCommandCommitAmend(t *testing.T) {
func TestGitCommandMergeStatusFiles(t *testing.T) {
type scenario struct {
testName string
oldFiles []*File
newFiles []*File
test func([]*File)
oldFiles []*models.File
newFiles []*models.File
test func([]*models.File)
}
scenarios := []scenario{
{
"Old file and new file are the same",
[]*File{},
[]*File{
[]*models.File{},
[]*models.File{
{
Name: "new_file.txt",
},
},
func(files []*File) {
expected := []*File{
func(files []*models.File) {
expected := []*models.File{
{
Name: "new_file.txt",
},
@ -484,7 +484,7 @@ func TestGitCommandMergeStatusFiles(t *testing.T) {
},
{
"Several files to merge, with some identical",
[]*File{
[]*models.File{
{
Name: "new_file1.txt",
},
@ -495,7 +495,7 @@ func TestGitCommandMergeStatusFiles(t *testing.T) {
Name: "new_file3.txt",
},
},
[]*File{
[]*models.File{
{
Name: "new_file4.txt",
},
@ -506,8 +506,8 @@ func TestGitCommandMergeStatusFiles(t *testing.T) {
Name: "new_file1.txt",
},
},
func(files []*File) {
expected := []*File{
func(files []*models.File) {
expected := []*models.File{
{
Name: "new_file1.txt",
},
@ -1099,7 +1099,7 @@ func TestGitCommandDiscardAllFileChanges(t *testing.T) {
testName string
command func() (func(string, ...string) *exec.Cmd, *[][]string)
test func(*[][]string, error)
file *File
file *models.File
removeFile func(string) error
}
@ -1121,7 +1121,7 @@ func TestGitCommandDiscardAllFileChanges(t *testing.T) {
{"reset", "--", "test"},
})
},
&File{
&models.File{
Name: "test",
HasStagedChanges: true,
},
@ -1144,7 +1144,7 @@ func TestGitCommandDiscardAllFileChanges(t *testing.T) {
assert.EqualError(t, err, "an error occurred when removing file")
assert.Len(t, *cmdsCalled, 0)
},
&File{
&models.File{
Name: "test",
Tracked: false,
},
@ -1169,7 +1169,7 @@ func TestGitCommandDiscardAllFileChanges(t *testing.T) {
{"checkout", "--", "test"},
})
},
&File{
&models.File{
Name: "test",
Tracked: true,
HasStagedChanges: false,
@ -1195,7 +1195,7 @@ func TestGitCommandDiscardAllFileChanges(t *testing.T) {
{"checkout", "--", "test"},
})
},
&File{
&models.File{
Name: "test",
Tracked: true,
HasStagedChanges: false,
@ -1222,7 +1222,7 @@ func TestGitCommandDiscardAllFileChanges(t *testing.T) {
{"checkout", "--", "test"},
})
},
&File{
&models.File{
Name: "test",
Tracked: true,
HasStagedChanges: true,
@ -1249,7 +1249,7 @@ func TestGitCommandDiscardAllFileChanges(t *testing.T) {
{"checkout", "--", "test"},
})
},
&File{
&models.File{
Name: "test",
Tracked: true,
HasMergeConflicts: true,
@ -1275,7 +1275,7 @@ func TestGitCommandDiscardAllFileChanges(t *testing.T) {
{"reset", "--", "test"},
})
},
&File{
&models.File{
Name: "test",
Tracked: false,
HasStagedChanges: true,
@ -1299,7 +1299,7 @@ func TestGitCommandDiscardAllFileChanges(t *testing.T) {
assert.NoError(t, err)
assert.Len(t, *cmdsCalled, 0)
},
&File{
&models.File{
Name: "test",
Tracked: false,
HasStagedChanges: false,
@ -1386,7 +1386,7 @@ func TestGitCommandDiff(t *testing.T) {
type scenario struct {
testName string
command func(string, ...string) *exec.Cmd
file *File
file *models.File
plain bool
cached bool
}
@ -1400,7 +1400,7 @@ func TestGitCommandDiff(t *testing.T) {
return exec.Command("echo")
},
&File{
&models.File{
Name: "test.txt",
HasStagedChanges: false,
Tracked: true,
@ -1416,7 +1416,7 @@ func TestGitCommandDiff(t *testing.T) {
return exec.Command("echo")
},
&File{
&models.File{
Name: "test.txt",
HasStagedChanges: false,
Tracked: true,
@ -1432,7 +1432,7 @@ func TestGitCommandDiff(t *testing.T) {
return exec.Command("echo")
},
&File{
&models.File{
Name: "test.txt",
HasStagedChanges: false,
Tracked: true,
@ -1448,7 +1448,7 @@ func TestGitCommandDiff(t *testing.T) {
return exec.Command("echo")
},
&File{
&models.File{
Name: "test.txt",
HasStagedChanges: false,
Tracked: false,
@ -1806,7 +1806,7 @@ func TestGitCommandDiscardOldFileChanges(t *testing.T) {
func TestGitCommandDiscardUnstagedFileChanges(t *testing.T) {
type scenario struct {
testName string
file *File
file *models.File
command func(string, ...string) *exec.Cmd
test func(error)
}
@ -1814,7 +1814,7 @@ func TestGitCommandDiscardUnstagedFileChanges(t *testing.T) {
scenarios := []scenario{
{
"valid case",
&File{Name: "test.txt"},
&models.File{Name: "test.txt"},
test.CreateMockCommand(t, []*test.CommandSwapper{
{
Expect: `git checkout -- "test.txt"`,

View File

@ -1,7 +0,0 @@
package commands
type SubmoduleConfig struct {
Name string
Path string
Url string
}

View File

@ -4,6 +4,8 @@ import (
"bufio"
"os"
"regexp"
"github.com/jesseduffield/lazygit/pkg/models"
)
// .gitmodules looks like this:
@ -11,7 +13,7 @@ import (
// path = blah/mysubmodule
// url = git@github.com:subbo.git
func (c *GitCommand) GetSubmoduleConfigs() ([]*SubmoduleConfig, error) {
func (c *GitCommand) GetSubmoduleConfigs() ([]*models.SubmoduleConfig, error) {
file, err := os.Open(".gitmodules")
if err != nil {
if os.IsNotExist(err) {
@ -34,12 +36,12 @@ func (c *GitCommand) GetSubmoduleConfigs() ([]*SubmoduleConfig, error) {
}
}
configs := []*SubmoduleConfig{}
configs := []*models.SubmoduleConfig{}
for scanner.Scan() {
line := scanner.Text()
if name, ok := firstMatch(line, `\[submodule "(.*)"\]`); ok {
configs = append(configs, &SubmoduleConfig{Name: name})
configs = append(configs, &models.SubmoduleConfig{Name: name})
continue
}
@ -57,7 +59,7 @@ func (c *GitCommand) GetSubmoduleConfigs() ([]*SubmoduleConfig, error) {
return configs, nil
}
func (c *GitCommand) SubmoduleStash(config *SubmoduleConfig) error {
func (c *GitCommand) SubmoduleStash(config *models.SubmoduleConfig) error {
// if the path does not exist then it hasn't yet been initialized so we'll swallow the error
// because the intention here is to have no dirty worktree state
if _, err := os.Stat(config.Path); os.IsNotExist(err) {
@ -68,7 +70,7 @@ func (c *GitCommand) SubmoduleStash(config *SubmoduleConfig) error {
return c.OSCommand.RunCommand("git -C %s stash --include-untracked", config.Path)
}
func (c *GitCommand) SubmoduleReset(config *SubmoduleConfig) error {
func (c *GitCommand) SubmoduleReset(config *models.SubmoduleConfig) error {
return c.OSCommand.RunCommand("git submodule update --force %s", config.Name)
}