summaryrefslogtreecommitdiffstats
path: root/pkg/gui/staging_panel.go
blob: be207c2fb7aa29de6f373776f27749c7f3ab5cd1 (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
package gui

import (
	"errors"
	"io/ioutil"

	"github.com/davecgh/go-spew/spew"

	"github.com/jesseduffield/gocui"
	"github.com/jesseduffield/lazygit/pkg/git"
)

func (gui *Gui) refreshStagingPanel() error {
	// get the currently selected file. Get the diff of that file directly, not
	// using any custom diff tools.
	// parse the file to find out where the chunks and unstaged changes are

	file, err := gui.getSelectedFile(gui.g)
	if err != nil {
		if err != gui.Errors.ErrNoFiles {
			return err
		}
		return gui.handleStagingEscape(gui.g, nil)
	}

	if !file.HasUnstagedChanges {
		return gui.handleStagingEscape(gui.g, nil)
	}

	// note for custom diffs, we'll need to send a flag here saying not to use the custom diff
	diff := gui.GitCommand.Diff(file, true)
	colorDiff := gui.GitCommand.Diff(file, false)

	gui.Log.WithField("staging", "staging").Info("DIFF IS:")
	gui.Log.WithField("staging", "staging").Info(spew.Sdump(diff))
	gui.Log.WithField("staging", "staging").Info("hello")

	if len(diff) < 2 {
		return gui.handleStagingEscape(gui.g, nil)
	}

	// parse the diff and store the line numbers of hunks and stageable lines
	// TODO: maybe instantiate this at application start
	p, err := git.NewPatchParser(gui.Log)
	if err != nil {
		return nil
	}
	hunkStarts, stageableLines, err := p.ParsePatch(diff)
	if err != nil {
		return nil
	}

	var currentLineIndex int
	if gui.State.StagingState != nil {
		end := len(stageableLines) - 1
		if end < gui.State.StagingState.CurrentLineIndex {
			currentLineIndex = end
		} else {
			currentLineIndex = gui.State.StagingState.CurrentLineIndex
		}
	} else {
		currentLineIndex = 0
	}

	gui.State.StagingState = &stagingState{
		StageableLines:   stageableLines,
		HunkStarts:       hunkStarts,
		CurrentLineIndex: currentLineIndex,
		Diff:             diff,
	}

	if len(stageableLines) == 0 {
		return errors.New("No lines to stage")
	}

	stagingView := gui.getStagingView(gui.g)
	stagingView.SetCursor(0, stageableLines[currentLineIndex])
	stagingView.SetOrigin(0, 0)
	return gui.renderString(gui.g, "staging", colorDiff)
}

func (gui *Gui) handleStagingEscape(g *gocui.Gui, v *gocui.View) error {
	if _, err := gui.g.SetViewOnBottom("staging"); err != nil {
		return err
	}

	return gui.switchFocus(gui.g, nil, gui.getFilesView(gui.g))
}

// nextNumber returns the next index, cycling if we reach the end
func nextIndex(numbers []int, currentNumber int) int {
	for index, number := range numbers {
		if number > currentNumber {
			return index
		}
	}
	return 0
}

// prevNumber returns the next number, cycling if we reach the end
func prevIndex(numbers []int, currentNumber int) int {
	end := len(numbers) - 1
	for i := end; i >= 0; i -= 1 {
		if numbers[i] < currentNumber {
			return i
		}
	}
	return end
}

func (gui *Gui) handleStagingKeyUp(g *gocui.Gui, v *gocui.View) error {
	return gui.handleCycleLine(true)
}

func (gui *Gui) handleStagingKeyDown(g *gocui.Gui, v *gocui.View) error {
	return gui.handleCycleLine(false)
}

func (gui *Gui) handleCycleLine(up bool) error {
	state := gui.State.StagingState
	lineNumbers := state.StageableLines
	currentLine := lineNumbers[state.CurrentLineIndex]
	var newIndex int
	if up {
		newIndex = prevIndex(lineNumbers, currentLine)
	} else {
		newIndex = nextIndex(lineNumbers, currentLine)
	}

	state.CurrentLineIndex = newIndex
	stagingView := gui.getStagingView(gui.g)
	stagingView.SetCursor(0, lineNumbers[newIndex])
	stagingView.SetOrigin(0, 0)
	return nil
}

func (gui *Gui) handleStageLine(g *gocui.Gui, v *gocui.View) error {
	state := gui.State.StagingState
	p, err := git.NewPatchModifier(gui.Log)
	if err != nil {
		return err
	}

	currentLine := state.StageableLines[state.CurrentLineIndex]
	patch, err := p.ModifyPatch(state.Diff, currentLine)
	if err != nil {
		return err
	}

	// for logging purposes
	ioutil.WriteFile("patch.diff", []byte(patch), 0600)

	// apply the patch then refresh this panel
	// create a new temp file with the patch, then call git apply with that patch
	_, err = gui.GitCommand.ApplyPatch(patch)
	if err != nil {
		panic(err)
	}

	gui.refreshStagingPanel()
	gui.refreshFiles(gui.g)
	return nil
}