summaryrefslogtreecommitdiff
path: root/branches.go
blob: ec86c20408f329bbc8d09271312b1f00cc46fdaa (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package gitpb

import (
	"errors"
	"os"
	"path/filepath"
	"strings"

	"go.wit.com/log"
)

// returns true if 'git pull' will work
func (repo *Repo) IsBranchRemote(branchname string) bool {
	if branchname == "" {
		return false
	}
	if repo.Exists(filepath.Join(".git/refs/remotes/origin", branchname)) {
		// todo: actually use .git/config
		return true
	}
	return false
}

// returns true if 'git pull' will work
func (repo *Repo) ExistsUserBranchRemote() bool {
	branchname := repo.GetUserBranchName()
	if repo.IsBranchRemote(branchname) {
		return true
	}
	return false
}

// returns true if the user branch exists
func (repo *Repo) ExistsUserBranch() bool {
	if repo.GetUserBranchName() == "" {
		return false
	}
	branchname := repo.GetUserBranchName()
	if repo.Exists(filepath.Join(".git/refs/heads", branchname)) {
		// todo: actually use .git/config
		return true
	}
	return false
}

// returns true if the devel branch exists
func (repo *Repo) ExistsDevelBranch() bool {
	if repo.GetDevelBranchName() == "" {
		return false
	}
	branchname := repo.GetDevelBranchName()
	if repo.Exists(filepath.Join(".git/refs/heads", branchname)) {
		// todo: actually use .git/config
		return true
	}
	return false
}

func (repo *Repo) GetBranchHash(branchname string) string {
	if branchname == "" {
		return ""
	}
	if repo.Exists(filepath.Join(".git/refs/remotes", branchname)) {
		return readRefHash(filepath.Join(repo.FullPath, ".git/refs/remotes", branchname))
	}
	if repo.Exists(filepath.Join(".git/refs/heads", branchname)) {
		return readRefHash(filepath.Join(repo.FullPath, ".git/refs/heads", branchname))
	}
	return ""
}

func (repo *Repo) GetBranchVersion(branchname string) string {
	if branchname == repo.GetUserBranchName() {
		return "user"
	}
	if branchname == repo.GetDevelBranchName() {
		return "devel"
	}
	if branchname == repo.GetMasterBranchName() {
		return "master"
	}
	base, branchname := filepath.Split(branchname)
	if base != "" {
		if branchname == repo.GetUserBranchName() {
			return "remote user"
		}
		if branchname == repo.GetDevelBranchName() {
			return "remote devel"
		}
		if branchname == repo.GetMasterBranchName() {
			return "remote master"
		}
	}
	return ""
}

func readRefHash(filename string) string {
	data, _ := os.ReadFile(filename)
	return string(data)
}

func (repo *Repo) GetLocalBranches() []string {
	return ListFiles(filepath.Join(repo.GetFullPath(), "/.git/refs/heads"))
}

func (repo *Repo) GetRemoteBranches() []string {
	remotes := ListFiles(filepath.Join(repo.GetFullPath(), "/.git/refs/remotes"))
	return remotes
}

// git describe --tags e548b0fb6d0d14cdfb693850d592419f247dc2b1
// v0.22.61-15-gbab84d7
func (repo *Repo) GetHashName(h string) (string, error) {
	h = strings.TrimSpace(h)
	log.Info("GetHashName() is looking for", repo.GetGoPath(), h)
	cmd := []string{"git", "describe", "--tags", h}
	r, err := repo.RunStrictNew(cmd)
	if err != nil {
		return "", err
	}
	if len(r.Stdout) == 0 {
		return "", errors.New("git describe was empty")
	}
	return r.Stdout[0], nil
}