summaryrefslogtreecommitdiffstats
path: root/pkg/gui/filetree/commit_file_manager.go
blob: 3fd89b63ed1701d6ebd7f6f52901cc8ba19c7ed2 (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
package filetree

import (
	"github.com/jesseduffield/lazygit/pkg/commands/models"
	"github.com/jesseduffield/lazygit/pkg/commands/patch"
	"github.com/jesseduffield/lazygit/pkg/gui/presentation"
	"github.com/sirupsen/logrus"
)

type CommitFileManager struct {
	files          []*models.CommitFile
	tree           *CommitFileNode
	showTree       bool
	log            *logrus.Entry
	collapsedPaths CollapsedPaths
	// parent is the identifier of the parent object e.g. a commit SHA if this commit file is for a commit, or a stash entry ref like 'stash@{1}'
	parent string
}

func (m *CommitFileManager) GetParent() string {
	return m.parent
}

func NewCommitFileManager(files []*models.CommitFile, log *logrus.Entry, showTree bool) *CommitFileManager {
	return &CommitFileManager{
		files:          files,
		log:            log,
		showTree:       showTree,
		collapsedPaths: CollapsedPaths{},
	}
}

func (m *CommitFileManager) ExpandToPath(path string) {
	m.collapsedPaths.ExpandToPath(path)
}

func (m *CommitFileManager) ToggleShowTree() {
	m.showTree = !m.showTree
	m.SetTree()
}

func (m *CommitFileManager) GetItemAtIndex(index int) *CommitFileNode {
	// need to traverse the three depth first until we get to the index.
	return m.tree.GetNodeAtIndex(index+1, m.collapsedPaths) // ignoring root
}

func (m *CommitFileManager) GetIndexForPath(path string) (int, bool) {
	index, found := m.tree.GetIndexForPath(path, m.collapsedPaths)
	return index - 1, found
}

func (m *CommitFileManager) GetAllItems() []*CommitFileNode {
	if m.tree == nil {
		return nil
	}

	return m.tree.Flatten(m.collapsedPaths)[1:] // ignoring root
}

func (m *CommitFileManager) GetItemsLength() int {
	return m.tree.Size(m.collapsedPaths) - 1 // ignoring root
}

func (m *CommitFileManager) GetAllFiles() []*models.CommitFile {
	return m.files
}

func (m *CommitFileManager) SetFiles(files []*models.CommitFile, parent string) {
	m.files = files
	m.parent = parent

	m.SetTree()
}

func (m *CommitFileManager) SetTree() {
	if m.showTree {
		m.tree = BuildTreeFromCommitFiles(m.files)
	} else {
		m.tree = BuildFlatTreeFromCommitFiles(m.files)
	}
}

func (m *CommitFileManager) IsCollapsed(path string) bool {
	return m.collapsedPaths.IsCollapsed(path)
}

func (m *CommitFileManager) ToggleCollapsed(path string) {
	m.collapsedPaths.ToggleCollapsed(path)
}

func (m *CommitFileManager) Render(diffName string, patchManager *patch.PatchManager) []string {
	return renderAux(m.tree, m.collapsedPaths, "", -1, func(n INode, depth int) string {
		castN := n.(*CommitFileNode)

		// This is a little convoluted because we're dealing with either a leaf or a non-leaf.
		// But this code actually applies to both. If it's a leaf, the status will just
		// be whatever status it is, but if it's a non-leaf it will determine its status
		// based on the leaves of that subtree
		var status patch.PatchStatus
		if castN.EveryFile(func(file *models.CommitFile) bool {
			return patchManager.GetFileStatus(file.Name, m.parent) == patch.WHOLE
		}) {
			status = patch.WHOLE
		} else if castN.EveryFile(func(file *models.CommitFile) bool {
			return patchManager.GetFileStatus(file.Name, m.parent) == patch.UNSELECTED
		}) {
			status = patch.UNSELECTED
		} else {
			status = patch.PART
		}

		return presentation.GetCommitFileLine(castN.NameAtDepth(depth), diffName, castN.File, status)
	})
}