summaryrefslogtreecommitdiff
path: root/branches.go
blob: 4c24872a065818ac37ed2c453b3d7ed8ae1ae611 (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
package gitpb

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

	"github.com/go-cmd/cmd"
)

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

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.RunQuiet(cmd)
	if err != nil {
		return "", err
	}
	if len(r.Stdout) == 0 {
		return "", errors.New("git describe was empty")
	}
	return r.Stdout[0], nil
}

// lookup a hash from a tag with 'git rev-list'
func (repo *Repo) GetTagHash(t string) string {
	// git rev-list -n 1 v0.0.66
	cmd := []string{"git", "rev-list", "-n", "1", t}
	result, _ := repo.RunStrict(cmd)
	// log.Info("getLastTagVersion()", result.Stdout)

	if len(result.Stdout) == 0 {
		// log.Log(WARN, "no gitpb.LastTag() repo is broken. ignore this.", repo.GetGoPath())
		return ""
	}

	return result.Stdout[0]
}

// deletes the devel local branch if it is a subset of the remote devel branch
func (repo *Repo) DeleteLocalDevelBranch() error {
	branch := repo.GetDevelBranchName()
	remote := filepath.Join("origin", branch)

	if !repo.IsDevelRemote() {
		return fmt.Errorf("no remote branch")
	}

	b1 := repo.CountDiffObjects(branch, remote) // should be zero
	if b1 == 0 {
		cmd := []string{"git", "branch", "-D", repo.GetDevelBranchName()}
		// log.Info("DEVEL IS IN REMOTE", repo.GetGoPath(), cmd)
		_, err := repo.RunVerboseOnError(cmd)
		return err
	} else {
		return fmt.Errorf("local branch has patches not in remote")
	}
}

// makes a local branch based off of the master branch
// (unless a remote devel branch exists. then it uses that)
func (repo *Repo) MakeLocalDevelBranch() (*cmd.Status, error) {
	branch := repo.GetDevelBranchName()
	if branch == "" {
		// hard coded default
		branch = "devel"
	}

	if repo.Exists(filepath.Join(".git/refs/heads", branch)) {
		// local devel branch already exists
		return nil, nil
	}

	if repo.Exists(filepath.Join(".git/refs/remotes/origin", branch)) {
		// remote devel branch exists, but local does not
		cmd := []string{"git", "checkout", branch}
		return repo.RunVerboseOnError(cmd)
	}

	master := repo.GetMasterBranchName()
	cmd := []string{"git", "branch", branch, master}
	repo.RunVerboseOnError(cmd)
	cmd = []string{"git", "checkout", branch}
	return repo.RunVerboseOnError(cmd)
}