aboutsummaryrefslogtreecommitdiffstats
path: root/config/branch.go
blob: a8fe0b5abb6afe29d5532dace247b2dfaac8cc2e (plain) (blame)
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
package config

import (
	"errors"
	"strings"

	"github.com/go-git/go-git/v5/plumbing"
	format "github.com/go-git/go-git/v5/plumbing/format/config"
)

var (
	errBranchEmptyName     = errors.New("branch config: empty name")
	errBranchInvalidMerge  = errors.New("branch config: invalid merge")
	errBranchInvalidRebase = errors.New("branch config: rebase must be one of 'true' or 'interactive'")
)

// Branch contains information on the
// local branches and which remote to track
type Branch struct {
	// Name of branch
	Name string
	// Remote name of remote to track
	Remote string
	// Merge is the local refspec for the branch
	Merge plumbing.ReferenceName
	// Rebase instead of merge when pulling. Valid values are
	// "true" and "interactive".  "false" is undocumented and
	// typically represented by the non-existence of this field
	Rebase string
	// Description explains what the branch is for.
	// Multi-line explanations may be used.
	//
	// Original git command to edit:
	//	git branch --edit-description
	Description string

	raw *format.Subsection
}

// Validate validates fields of branch
func (b *Branch) Validate() error {
	if b.Name == "" {
		return errBranchEmptyName
	}

	if b.Merge != "" && !b.Merge.IsBranch() {
		return errBranchInvalidMerge
	}

	if b.Rebase != "" &&
		b.Rebase != "true" &&
		b.Rebase != "interactive" &&
		b.Rebase != "false" {
		return errBranchInvalidRebase
	}

	return nil
}

func (b *Branch) marshal() *format.Subsection {
	if b.raw == nil {
		b.raw = &format.Subsection{}
	}

	b.raw.Name = b.Name

	if b.Remote == "" {
		b.raw.RemoveOption(remoteSection)
	} else {
		b.raw.SetOption(remoteSection, b.Remote)
	}

	if b.Merge == "" {
		b.raw.RemoveOption(mergeKey)
	} else {
		b.raw.SetOption(mergeKey, string(b.Merge))
	}

	if b.Rebase == "" {
		b.raw.RemoveOption(rebaseKey)
	} else {
		b.raw.SetOption(rebaseKey, b.Rebase)
	}

	if b.Description == "" {
		b.raw.RemoveOption(descriptionKey)
	} else {
		desc := quoteDescription(b.Description)
		b.raw.SetOption(descriptionKey, desc)
	}

	return b.raw
}

func quoteDescription(desc string) string {
	return strings.ReplaceAll(desc, "\n", `\n`)
}

func (b *Branch) unmarshal(s *format.Subsection) error {
	b.raw = s

	b.Name = b.raw.Name
	b.Remote = b.raw.Options.Get(remoteSection)
	b.Merge = plumbing.ReferenceName(b.raw.Options.Get(mergeKey))
	b.Rebase = b.raw.Options.Get(rebaseKey)
	b.Description = unquoteDescription(b.raw.Options.Get(descriptionKey))

	return b.Validate()
}

func unquoteDescription(desc string) string {
	return strings.ReplaceAll(desc, `\n`, "\n")
}