2020-09-29 10:45:00 +02:00
|
|
|
package models
|
2018-09-17 13:02:30 +02:00
|
|
|
|
2020-08-07 10:27:18 +02:00
|
|
|
import (
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
2022-07-31 05:52:56 +02:00
|
|
|
"github.com/samber/lo"
|
2020-08-07 10:27:18 +02:00
|
|
|
)
|
2020-08-07 09:52:17 +02:00
|
|
|
|
2018-09-17 13:02:30 +02:00
|
|
|
// File : A file from git status
|
|
|
|
// duplicating this for now
|
|
|
|
type File struct {
|
2019-03-03 06:55:19 +02:00
|
|
|
Name string
|
2021-03-20 23:41:06 +02:00
|
|
|
PreviousName string
|
2019-03-03 06:55:19 +02:00
|
|
|
HasStagedChanges bool
|
|
|
|
HasUnstagedChanges bool
|
|
|
|
Tracked bool
|
2021-03-20 03:07:11 +02:00
|
|
|
Added bool
|
2019-03-03 06:55:19 +02:00
|
|
|
Deleted bool
|
|
|
|
HasMergeConflicts bool
|
|
|
|
HasInlineMergeConflicts bool
|
|
|
|
DisplayString string
|
2019-05-30 14:45:56 +02:00
|
|
|
ShortStatus string // e.g. 'AD', ' A', 'M ', '??'
|
2023-07-17 07:22:14 +02:00
|
|
|
|
|
|
|
// If true, this must be a worktree folder
|
|
|
|
IsWorktree bool
|
2018-09-17 13:02:30 +02:00
|
|
|
}
|
2020-08-07 09:52:17 +02:00
|
|
|
|
2021-03-21 06:58:15 +02:00
|
|
|
// sometimes we need to deal with either a node (which contains a file) or an actual file
|
2021-03-31 14:26:53 +02:00
|
|
|
type IFile interface {
|
2021-03-21 06:58:15 +02:00
|
|
|
GetHasUnstagedChanges() bool
|
|
|
|
GetHasStagedChanges() bool
|
|
|
|
GetIsTracked() bool
|
|
|
|
GetPath() string
|
2022-04-05 16:35:41 +02:00
|
|
|
GetPreviousPath() string
|
2022-11-11 03:16:38 +02:00
|
|
|
GetIsFile() bool
|
2021-03-21 06:58:15 +02:00
|
|
|
}
|
|
|
|
|
2020-08-07 09:52:17 +02:00
|
|
|
func (f *File) IsRename() bool {
|
2021-03-20 23:41:06 +02:00
|
|
|
return f.PreviousName != ""
|
2020-08-07 10:27:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2021-03-20 23:41:06 +02:00
|
|
|
result := []string{f.Name}
|
|
|
|
if f.PreviousName != "" {
|
|
|
|
result = append(result, f.PreviousName)
|
|
|
|
}
|
|
|
|
return result
|
2020-08-07 10:27:18 +02:00
|
|
|
}
|
|
|
|
|
2022-08-30 07:23:37 +02:00
|
|
|
// returns true if the file names are the same or if a file rename includes the filename of the other
|
2020-08-07 10:27:18 +02:00
|
|
|
func (f *File) Matches(f2 *File) bool {
|
|
|
|
return utils.StringArraysOverlap(f.Names(), f2.Names())
|
2020-08-07 09:52:17 +02:00
|
|
|
}
|
2020-08-22 00:49:02 +02:00
|
|
|
|
|
|
|
func (f *File) ID() string {
|
|
|
|
return f.Name
|
|
|
|
}
|
2020-08-22 02:14:53 +02:00
|
|
|
|
|
|
|
func (f *File) Description() string {
|
|
|
|
return f.Name
|
|
|
|
}
|
2020-09-28 01:14:32 +02:00
|
|
|
|
|
|
|
func (f *File) IsSubmodule(configs []*SubmoduleConfig) bool {
|
|
|
|
return f.SubmoduleConfig(configs) != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) SubmoduleConfig(configs []*SubmoduleConfig) *SubmoduleConfig {
|
|
|
|
for _, config := range configs {
|
2021-08-13 03:41:35 +02:00
|
|
|
if f.Name == config.Path {
|
2020-09-28 01:14:32 +02:00
|
|
|
return config
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-03-14 09:46:22 +02:00
|
|
|
|
|
|
|
func (f *File) GetHasUnstagedChanges() bool {
|
|
|
|
return f.HasUnstagedChanges
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) GetHasStagedChanges() bool {
|
|
|
|
return f.HasStagedChanges
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) GetIsTracked() bool {
|
|
|
|
return f.Tracked
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) GetPath() string {
|
2021-03-20 23:41:06 +02:00
|
|
|
// TODO: remove concept of name; just use path
|
|
|
|
return f.Name
|
2021-03-14 09:46:22 +02:00
|
|
|
}
|
2022-04-05 16:35:41 +02:00
|
|
|
|
|
|
|
func (f *File) GetPreviousPath() string {
|
|
|
|
return f.PreviousName
|
|
|
|
}
|
2022-07-31 05:52:56 +02:00
|
|
|
|
2022-11-11 03:16:38 +02:00
|
|
|
func (f *File) GetIsFile() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-07-31 05:52:56 +02:00
|
|
|
type StatusFields struct {
|
|
|
|
HasStagedChanges bool
|
|
|
|
HasUnstagedChanges bool
|
|
|
|
Tracked bool
|
|
|
|
Deleted bool
|
|
|
|
Added bool
|
|
|
|
HasMergeConflicts bool
|
|
|
|
HasInlineMergeConflicts bool
|
|
|
|
ShortStatus string
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetStatusFields(file *File, shortStatus string) {
|
|
|
|
derived := deriveStatusFields(shortStatus)
|
|
|
|
|
|
|
|
file.HasStagedChanges = derived.HasStagedChanges
|
|
|
|
file.HasUnstagedChanges = derived.HasUnstagedChanges
|
|
|
|
file.Tracked = derived.Tracked
|
|
|
|
file.Deleted = derived.Deleted
|
|
|
|
file.Added = derived.Added
|
|
|
|
file.HasMergeConflicts = derived.HasMergeConflicts
|
|
|
|
file.HasInlineMergeConflicts = derived.HasInlineMergeConflicts
|
|
|
|
file.ShortStatus = derived.ShortStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
// shortStatus is something like '??' or 'A '
|
|
|
|
func deriveStatusFields(shortStatus string) StatusFields {
|
|
|
|
stagedChange := shortStatus[0:1]
|
|
|
|
unstagedChange := shortStatus[1:2]
|
2022-07-31 08:11:39 +02:00
|
|
|
tracked := !lo.Contains([]string{"??", "A ", "AM"}, shortStatus)
|
|
|
|
hasStagedChanges := !lo.Contains([]string{" ", "U", "?"}, stagedChange)
|
2022-07-31 05:52:56 +02:00
|
|
|
hasInlineMergeConflicts := lo.Contains([]string{"UU", "AA"}, shortStatus)
|
|
|
|
hasMergeConflicts := hasInlineMergeConflicts || lo.Contains([]string{"DD", "AU", "UA", "UD", "DU"}, shortStatus)
|
|
|
|
|
|
|
|
return StatusFields{
|
2022-07-31 08:11:39 +02:00
|
|
|
HasStagedChanges: hasStagedChanges,
|
2022-07-31 05:52:56 +02:00
|
|
|
HasUnstagedChanges: unstagedChange != " ",
|
2022-07-31 08:11:39 +02:00
|
|
|
Tracked: tracked,
|
2022-07-31 05:52:56 +02:00
|
|
|
Deleted: unstagedChange == "D" || stagedChange == "D",
|
2022-07-31 08:11:39 +02:00
|
|
|
Added: unstagedChange == "A" || !tracked,
|
2022-07-31 05:52:56 +02:00
|
|
|
HasMergeConflicts: hasMergeConflicts,
|
|
|
|
HasInlineMergeConflicts: hasInlineMergeConflicts,
|
|
|
|
ShortStatus: shortStatus,
|
|
|
|
}
|
|
|
|
}
|