summaryrefslogtreecommitdiff
path: root/common.go
blob: 1bf6c6be77128ebf1b97a3adda716cf27bfed69b (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
package gitpb

import (
	"os"

	"go.wit.com/lib/gui/shell"
	"go.wit.com/log"
)

func (repo *Repo) SetReadOnly(b bool) {
	repo.ReadOnly = b
}

func (repo *Repo) SetTargetVersion(target string) {
	repo.TargetVersion = target
}

func (repo *Repo) SetMasterBranchName(s string) {
	repo.MasterBranchName = s
}

func (repo *Repo) GetGoPath() string {
	if repo.GoInfo == nil {
		return repo.Namespace
	}
	if repo.GoInfo.GoPath == "" {
		return repo.Namespace
	}
	return repo.GoInfo.GoPath
}

func (repo *Repo) GetGoPrimitive() bool {
	if repo.GoInfo == nil {
		return false
	}
	return repo.GoInfo.GoPrimitive
}

func (repo *Repo) SetGoPrimitive(b bool) {
	if repo.GoInfo == nil {
		repo.GoInfo = new(GoInfo)
	}
	repo.GoInfo.GoPrimitive = b
}

func (repo *Repo) IsUserBranch() bool {
	if repo.GetCurrentBranchName() == repo.GetUserBranchName() {
		return true
	}
	return false
}

func (repo *Repo) IsMasterBranch() bool {
	if repo.GetCurrentBranchName() == repo.GetMasterBranchName() {
		return true
	}
	return false
}

func (repo *Repo) IsDevelBranch() bool {
	if repo.GetCurrentBranchName() == repo.GetDevelBranchName() {
		return true
	}
	return false
}

func (repo *Repo) GitCommit() error {
	if !repo.CheckDirty() {
		// repo is not dirty. nothing to do!
		return nil
	}
	os.Chdir(repo.GetFullPath())

	os.Setenv("LESS", "-XR")
	if err := shell.Exec([]string{"git", "diff"}); err != nil {
		return err
	}

	if err := shell.ExecCheck([]string{"git", "add", "--all"}); err != nil {
		return err
	}

	if err := shell.ExecCheck([]string{"git", "commit", "--all"}); err != nil {
		shell.ExecCheck([]string{"git", "reset"})
		return err
	}
	log.Info("git commit ok.")
	return nil
}