summaryrefslogtreecommitdiffstats
path: root/pkg/gui/mergeconflicts/state.go
blob: d84f05545dd26d377092787515489e48a4c89532 (plain)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package mergeconflicts

import (
	"sync"

	"github.com/jesseduffield/lazygit/pkg/utils"
)

type State struct {
	sync.Mutex

	// path of the file with the conflicts
	path string

	// This is a stack of the file content. It is used to undo changes.
	// The last item is the current file content.
	contents []string

	conflicts []*mergeConflict
	// this is the index of the above `conflicts` field which is currently selected
	conflictIndex int

	// this is the index of the selected conflict's available selections slice e.g. [TOP, MIDDLE, BOTTOM]
	// We use this to know which hunk of the conflict is selected.
	selectionIndex int
}

func NewState() *State {
	return &State{
		Mutex:          sync.Mutex{},
		conflictIndex:  0,
		selectionIndex: 0,
		conflicts:      []*mergeConflict{},
		contents:       []string{},
	}
}

func (s *State) setConflictIndex(index int) {
	if len(s.conflicts) == 0 {
		s.conflictIndex = 0
	} else {
		s.conflictIndex = clamp(index, 0, len(s.conflicts)-1)
	}
	s.setSelectionIndex(s.selectionIndex)
}

func (s *State) setSelectionIndex(index int) {
	if selections := s.availableSelections(); len(selections) != 0 {
		s.selectionIndex = clamp(index, 0, len(selections)-1)
	}
}

func (s *State) SelectNextConflictHunk() {
	s.setSelectionIndex(s.selectionIndex + 1)
}

func (s *State) SelectPrevConflictHunk() {
	s.setSelectionIndex(s.selectionIndex - 1)
}

func (s *State) SelectNextConflict() {
	s.setConflictIndex(s.conflictIndex + 1)
}

func (s *State) SelectPrevConflict() {
	s.setConflictIndex(s.conflictIndex - 1)
}

func (s *State) currentConflict() *mergeConflict {
	if len(s.conflicts) == 0 {
		return nil
	}

	return s.conflicts[s.conflictIndex]
}

// this is for starting a new merge conflict session
func (s *State) SetContent(content string, path string) {
	if content == s.GetContent() && path == s.path {
		return
	}

	s.path = path
	s.contents = []string{}
	s.PushContent(content)
}

// this is for when you've resolved a conflict. This allows you to undo to a previous
// state
func (s *State) PushContent(content string) {
	s.contents = append(s.contents, content)
	s.setConflicts(findConflicts(content))
}

func (s *State) GetContent() string {
	if len(s.contents) == 0 {
		return ""
	}

	return s.contents[len(s.contents)-1]
}

func (s *State) GetPath() string {
	return s.path
}

func (s *State) Undo() bool {
	if len(s.contents) <= 1 {
		return false
	}

	s.contents = s.contents[:len(s.contents)-1]

	newContent := s.GetContent()
	// We could be storing the old conflicts and selected index on a stack too.
	s.setConflicts(findConflicts(newContent))

	return true
}

func (s *State) setConflicts(conflicts []*mergeConflict) {
	s.conflicts = conflicts
	s.setConflictIndex(s.conflictIndex)
}

func (s *State) NoConflicts() bool {
	return len(s.conflicts) == 0
}

func (s *State) Selection() Selection {
	if selections := s.availableSelections(); len(selections) > 0 {
		return selections[s.selectionIndex]
	}
	return TOP
}

func (s *State) availableSelections() []Selection {
	if conflict := s.currentConflict(); conflict != nil {
		return availableSelections(conflict)
	}
	return nil
}

func (s *State) AllConflictsResolved() bool {
	return len(s.conflicts) == 0
}

func (s *State) Reset() {
	s.contents = []string{}
	s.path = ""
}

func (s *State) Active() bool {
	return s.path != ""
}

func (s *State) GetConflictMiddle() int {
	currentConflict := s.currentConflict()

	if currentConflict == nil {
		return 0
	}

	return currentConflict.target
}

func (s *State) ContentAfterConflictResolve(selection Selection) (bool, string, error) {
	conflict := s.currentConflict()
	if conflict == nil {
		return false, "", nil
	}

	content := ""
	err := utils.ForEachLineInFile(s.path, func(line string, i int) {
		if selection.isIndexToKeep(conflict, i) {
			content += line
		}
	})

	if err != nil {
		return false, "", err
	}

	return true, content, nil
}

func clamp(x int, min int, max int) int {
	if x < min {
		return min
	} else if x > max {
		return max
	}
	return x
}