File: file.go

package info (click to toggle)
lazygit 0.50.0%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,808 kB
  • sloc: sh: 128; makefile: 76
file content (164 lines) | stat: -rw-r--r-- 4,436 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package models

import (
	"github.com/jesseduffield/lazygit/pkg/i18n"
	"github.com/jesseduffield/lazygit/pkg/utils"
	"github.com/samber/lo"
)

// File : A file from git status
// duplicating this for now
type File struct {
	Path                    string
	PreviousPath            string
	HasStagedChanges        bool
	HasUnstagedChanges      bool
	Tracked                 bool
	Added                   bool
	Deleted                 bool
	HasMergeConflicts       bool
	HasInlineMergeConflicts bool
	DisplayString           string
	ShortStatus             string // e.g. 'AD', ' A', 'M ', '??'
	LinesDeleted            int
	LinesAdded              int

	// If true, this must be a worktree folder
	IsWorktree bool
}

// sometimes we need to deal with either a node (which contains a file) or an actual file
type IFile interface {
	GetHasUnstagedChanges() bool
	GetHasStagedChanges() bool
	GetIsTracked() bool
	GetPath() string
	GetPreviousPath() string
	GetIsFile() bool
}

func (f *File) IsRename() bool {
	return f.PreviousPath != ""
}

// 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 {
	result := []string{f.Path}
	if f.PreviousPath != "" {
		result = append(result, f.PreviousPath)
	}
	return result
}

// returns true if the file names are the same or if 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.Path
}

func (f *File) Description() string {
	return f.Path
}

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.Path == config.Path {
			return config
		}
	}

	return nil
}

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 {
	// TODO: remove concept of name; just use path
	return f.Path
}

func (f *File) GetPreviousPath() string {
	return f.PreviousPath
}

func (f *File) GetIsFile() bool {
	return true
}

func (f *File) GetMergeStateDescription(tr *i18n.TranslationSet) string {
	m := map[string]string{
		"DD": tr.MergeConflictDescription_DD,
		"AU": tr.MergeConflictDescription_AU,
		"UA": tr.MergeConflictDescription_UA,
		"DU": tr.MergeConflictDescription_DU,
		"UD": tr.MergeConflictDescription_UD,
	}

	if description, ok := m[f.ShortStatus]; ok {
		return description
	}

	panic("should only be called if there's a merge conflict")
}

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]
	tracked := !lo.Contains([]string{"??", "A ", "AM"}, shortStatus)
	hasStagedChanges := !lo.Contains([]string{" ", "U", "?"}, stagedChange)
	hasInlineMergeConflicts := lo.Contains([]string{"UU", "AA"}, shortStatus)
	hasMergeConflicts := hasInlineMergeConflicts || lo.Contains([]string{"DD", "AU", "UA", "UD", "DU"}, shortStatus)

	return StatusFields{
		HasStagedChanges:        hasStagedChanges,
		HasUnstagedChanges:      unstagedChange != " ",
		Tracked:                 tracked,
		Deleted:                 unstagedChange == "D" || stagedChange == "D",
		Added:                   unstagedChange == "A" || !tracked,
		HasMergeConflicts:       hasMergeConflicts,
		HasInlineMergeConflicts: hasInlineMergeConflicts,
		ShortStatus:             shortStatus,
	}
}