patch.go

 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
package repo

import (
	"os/exec"
	"path/filepath"
	"senary/common"
)

type PatchHandler struct {
	RepoPath string
}

func NewPatchHandler(config *common.Config, repo string) *PatchHandler {
	return &PatchHandler{
		RepoPath: config.RepoPathMap[repo],
	}
}

func (ph *PatchHandler) CheckApply(patchfile string) ([]byte, error) {
	patchFileAbs, _ := filepath.Abs(patchfile)
	cmd := exec.Command("git", "apply", "--check", patchFileAbs)
	cmd.Dir = ph.RepoPath
	output, err := cmd.CombinedOutput()
	return output, err
}

func (ph *PatchHandler) CheckApplied(patchfile string) ([]byte, error) {
	patchFileAbs, _ := filepath.Abs(patchfile)
	cmd := exec.Command("git", "apply", "--reverse", "--check", patchFileAbs)
	cmd.Dir = ph.RepoPath
	output, err := cmd.CombinedOutput()
	return output, err
}

func (ph *PatchHandler) Apply(patchfile string) error {
	patchFileAbs, _ := filepath.Abs(patchfile)
	cmd := exec.Command("git", "am", "--keep-cr", patchFileAbs)
	cmd.Dir = ph.RepoPath
	_, err := cmd.CombinedOutput()
	return err
}

func (ph *PatchHandler) Rebuild() error {
	cmd := exec.Command("./senary", "build")
	_, err := cmd.CombinedOutput()
	return err
}

func (ph *PatchHandler) Upstream() error {
	cmd := exec.Command("git", "push")
	cmd.Dir = ph.RepoPath
	_, err := cmd.CombinedOutput()
	return err
}