summaryrefslogtreecommitdiff
path: root/pkg/gui/sideviews.go
blob: 21ce55ed8d8fd4d9781ed97a42e07d109148a7ef (about) (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package gui

import (
	"fmt"

	"github.com/isacikgoz/gitbatch/pkg/git"
	"github.com/jroimartin/gocui"
)

var (
	confirmationViewFeature = viewFeature{Name: "confirmation", Title: " Confirmation "}
	sideViews               = []viewFeature{remoteViewFeature, remoteBranchViewFeature, branchViewFeature, commitViewFeature}
)

// refreshes the side views of the application for given git.RepoEntity struct
func (gui *Gui) refreshSideViews(entity *git.RepoEntity) error {
	var err error
	if err = gui.updateRemotes(entity); err != nil {
		return err
	}
	if err = gui.updateBranch(entity); err != nil {
		return err
	}
	if err = gui.updateRemoteBranches(entity); err != nil {
		return err
	}
	if err = gui.updateCommits(entity); err != nil {
		return err
	}
	return err
}

// updates the remotesview for given entity
func (gui *Gui) updateRemotes(entity *git.RepoEntity) error {
	var err error
	out, err := gui.g.View(remoteViewFeature.Name)
	if err != nil {
		return err
	}
	out.Clear()

	currentindex := 0
	totalRemotes := len(entity.Remotes)
	if totalRemotes > 0 {
		for i, r := range entity.Remotes {
			// TODO: maybe the text styling can be moved to textstyle.go file
			_, shortURL := trimRemoteURL(r.URL[0])
			suffix := shortURL
			if r.Name == entity.Remote.Name {
				currentindex = i
				fmt.Fprintln(out, selectionIndicator+r.Name+": "+suffix)
				continue
			}
			fmt.Fprintln(out, tab+r.Name+": "+suffix)
		}
		if err = gui.smartAnchorRelativeToLine(out, currentindex, totalRemotes); err != nil {
			return err
		}
	}
	return nil
}

// updates the remotebranchview for given entity
func (gui *Gui) updateRemoteBranches(entity *git.RepoEntity) error {
	var err error
	out, err := gui.g.View(remoteBranchViewFeature.Name)
	if err != nil {
		return err
	}
	out.Clear()
	currentindex := 0
	trb := len(entity.Remote.Branches)
	if trb > 0 {
		for i, r := range entity.Remote.Branches {
			rName := r.Name
			if r.Deleted {
				rName = rName + ws + dirty
			}
			if r.Name == entity.Remote.Branch.Name {
				currentindex = i
				fmt.Fprintln(out, selectionIndicator+rName)
				continue
			}
			fmt.Fprintln(out, tab+rName)
		}
		if err = gui.smartAnchorRelativeToLine(out, currentindex, trb); err != nil {
			return err
		}
	}
	return nil
}

// updates the branchview for given entity
func (gui *Gui) updateBranch(entity *git.RepoEntity) error {
	var err error
	out, err := gui.g.View(branchViewFeature.Name)
	if err != nil {
		return err
	}
	out.Clear()

	currentindex := 0
	totalbranches := len(entity.Branches)
	for i, b := range entity.Branches {
		if b.Name == entity.Branch.Name {
			currentindex = i
			fmt.Fprintln(out, selectionIndicator+b.Name)
			continue
		}
		fmt.Fprintln(out, tab+b.Name)
	}
	err = gui.smartAnchorRelativeToLine(out, currentindex, totalbranches)
	return err
}

// updates the commitsview for given entity
func (gui *Gui) updateCommits(entity *git.RepoEntity) error {
	var err error
	out, err := gui.g.View(commitViewFeature.Name)
	if err != nil {
		return err
	}
	out.Clear()

	currentindex := 0
	totalcommits := len(entity.Commits)
	for i, c := range entity.Commits {
		var body string
		if c.CommitType == git.EvenCommit {
			body = cyan.Sprint(c.Hash[:hashLength]) + " " + c.Message
		} else if c.CommitType == git.LocalCommit {
			body = blue.Sprint(c.Hash[:hashLength]) + " " + c.Message
		} else {
			body = yellow.Sprint(c.Hash[:hashLength]) + " " + c.Message
		}
		if c.Hash == entity.Commit.Hash {
			currentindex = i
			fmt.Fprintln(out, selectionIndicator+body)
			continue
		}
		fmt.Fprintln(out, tab+body)
	}
	if err = gui.smartAnchorRelativeToLine(out, currentindex, totalcommits); err != nil {
		return err
	}
	return err
}

func (gui *Gui) sideViewsNextItem(g *gocui.Gui, v *gocui.View) error {
	var err error
	entity := gui.getSelectedRepository()
	switch viewName := v.Name(); viewName {
	case remoteBranchViewFeature.Name:
		if err = entity.Remote.NextRemoteBranch(); err != nil {
			return err
		}
		err = gui.updateRemoteBranches(entity)
	case remoteViewFeature.Name:
		if err = entity.NextRemote(); err != nil {
			return err
		}
		err = gui.remoteChangeFollowUp(entity)
	case branchViewFeature.Name:
		if err = entity.Checkout(entity.NextBranch()); err != nil {
			err = gui.openErrorView(g, err.Error(),
				"You should manually resolve this issue",
				branchViewFeature.Name)
			return err
		}
		// err = gui.checkoutFollowUp(entity)
	case commitViewFeature.Name:
		entity.NextCommit()

		err = gui.updateCommits(entity)
	}
	return err
}

func (gui *Gui) sideViewsPreviousItem(g *gocui.Gui, v *gocui.View) error {
	var err error
	entity := gui.getSelectedRepository()
	switch viewName := v.Name(); viewName {
	case remoteBranchViewFeature.Name:
		if err = entity.Remote.PreviousRemoteBranch(); err != nil {
			return err
		}
		err = gui.updateRemoteBranches(entity)
	case remoteViewFeature.Name:
		if err = entity.PreviousRemote(); err != nil {
			return err
		}
		err = gui.remoteChangeFollowUp(entity)
	case branchViewFeature.Name:
		if err = entity.Checkout(entity.PreviousBranch()); err != nil {
			err = gui.openErrorView(g, err.Error(),
				"You should manually resolve this issue",
				branchViewFeature.Name)
			return err
		}
		// err = gui.checkoutFollowUp(entity)
	case commitViewFeature.Name:
		entity.PreviousCommit()

		err = gui.updateCommits(entity)
	}
	return err
}

// basically does fetch --prune
func (gui *Gui) syncRemoteBranch(g *gocui.Gui, v *gocui.View) error {
	var err error
	entity := gui.getSelectedRepository()
	if err = git.Fetch(entity, git.FetchOptions{
		RemoteName: entity.Remote.Name,
		Prune:      true,
	}); err != nil {
		return err
	}
	vr, err := g.View(remoteViewFeature.Name)
	if err != nil {
		return err
	}
	// have no idea why this works..
	// some time need to fix, movement aint bad huh?
	gui.sideViewsNextItem(g, vr)
	gui.sideViewsPreviousItem(g, vr)
	err = gui.updateRemoteBranches(entity)
	return err
}

// basically does fetch --prune
func (gui *Gui) setUpstreamToBranch(g *gocui.Gui, v *gocui.View) error {
	maxX, maxY := g.Size()

	entity := gui.getSelectedRepository()
	v, err := g.SetView(confirmationViewFeature.Name, maxX/2-30, maxY/2-2, maxX/2+30, maxY/2+2)
	if err != nil {
		if err != gocui.ErrUnknownView {
			return err
		}
		fmt.Fprintln(v, "branch."+entity.Branch.Name+"."+"remote"+"="+entity.Remote.Name)
		fmt.Fprintln(v, "branch."+entity.Branch.Name+"."+"merge"+"="+entity.Branch.Reference.Name().String())
	}
	return gui.focusToView(confirmationViewFeature.Name)
}

// basically does fetch --prune
func (gui *Gui) confirmSetUpstreamToBranch(g *gocui.Gui, v *gocui.View) error {
	var err error
	entity := gui.getSelectedRepository()
	if err = git.AddConfig(entity, git.ConfigOptions{
		Section: "branch." + entity.Branch.Name,
		Option:  "remote",
		Site:    git.ConfigSiteLocal,
	}, entity.Remote.Name); err != nil {
		return err
	}
	if err = git.AddConfig(entity, git.ConfigOptions{
		Section: "branch." + entity.Branch.Name,
		Option:  "merge",
		Site:    git.ConfigSiteLocal,
	}, entity.Branch.Reference.Name().String()); err != nil {
		return err
	}
	entity.Refresh()
	return gui.closeConfirmationView(g, v)
}

func (gui *Gui) closeConfirmationView(g *gocui.Gui, v *gocui.View) error {
	if err := g.DeleteView(v.Name()); err != nil {
		return err
	}
	return gui.closeViewCleanup(branchViewFeature.Name)
}

// after checkout a remote some refreshments needed
func (gui *Gui) remoteChangeFollowUp(entity *git.RepoEntity) (err error) {
	if err = gui.updateRemotes(entity); err != nil {
		return err
	}
	err = gui.updateRemoteBranches(entity)
	return err
}