summaryrefslogtreecommitdiffstats
path: root/pkg/git/patch_parser.go
blob: 1dbacd01ce6da0512c52fe15ecdd954504d35cac (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
package git

import (
	"strings"

	"github.com/sirupsen/logrus"
)

type PatchParser struct {
	Log *logrus.Entry
}

// NewPatchParser builds a new branch list builder
func NewPatchParser(log *logrus.Entry) (*PatchParser, error) {
	return &PatchParser{
		Log: log,
	}, nil
}

func (p *PatchParser) ParsePatch(patch string) ([]int, []int, error) {
	lines := strings.Split(patch, "\n")
	hunkStarts := []int{}
	stageableLines := []int{}
	pastHeader := false
	for index, line := range lines {
		if strings.HasPrefix(line, "@@") {
			pastHeader = true
			hunkStarts = append(hunkStarts, index)
		}
		if pastHeader && (strings.HasPrefix(line, "-") || strings.HasPrefix(line, "+")) {
			stageableLines = append(stageableLines, index)
		}
	}
	p.Log.WithField("staging", "staging").Info(stageableLines)
	return hunkStarts, stageableLines, nil
}