summaryrefslogtreecommitdiff
path: root/pkg/gui/branchview.go
blob: f69a2120b565d78c818125051257984f4e79f899 (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
package gui

import (
	"fmt"

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

// updates the branchview for given entity
func (gui *Gui) updateBranch(g *gocui.Gui, entity *git.RepoEntity) error {
	var err error
	out, err := 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
}

// iteration handler for the branchview
func (gui *Gui) nextBranch(g *gocui.Gui, v *gocui.View) error {
	var err error
	entity := gui.getSelectedRepository()
	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(g, entity)
	return err
}

// iteration handler for the branchview
func (gui *Gui) previousBranch(g *gocui.Gui, v *gocui.View) error {
	var err error
	entity := gui.getSelectedRepository()
	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(g, entity)
	return err
}

// after checkout a branch some refreshments needed
func (gui *Gui) checkoutFollowUp(g *gocui.Gui, entity *git.RepoEntity) (err error) {
	if err = gui.updateBranch(g, entity); err != nil {
		return err
	}
	if err = gui.updateCommits(g, entity); err != nil {
		return err
	}
	if err = gui.updateRemoteBranches(g, entity); err != nil {
		return err
	}
	err = gui.refreshMain(g)
	return err
}