summaryrefslogtreecommitdiff
path: root/gitTag.query.go
blob: 8c288e408aa1b70672d882a271da798d93090391 (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
126
127
128
129
130
131
132
133
134
package gitpb

// runs git, parses output
// types faster than you can

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

	"go.wit.com/log"
)

func (repo *Repo) GetLastTag() string {
	cmd := []string{"git", "rev-list", "--tags", "--max-count=1"}
	result := repo.RunQuiet(cmd)
	log.Info("getLastTagVersion()", result.Stdout)

	if len(result.Stdout) != 1 {
		log.Log(GITPBWARN, "git LastTag() error:", result.Stdout)
		return ""
	}

	hash := result.Stdout[0]

	cmd = []string{"git", "describe", "--tags", "--always", hash}
	result = repo.RunQuiet(cmd)

	if len(result.Stdout) != 1 {
		log.Log(GITPBWARN, "git LastTag() error:", result.Stdout)
		return ""
	}

	return result.Stdout[0]
}

func (repo *Repo) GitMasterVersion() string {
	v, err := repo.gitVersionByName("master")
	/*
		count := repo.LenGitTags()
		log.Info(repo.GoPath, "tag count", count)
		repo.UpdateGitTags()
		count = repo.LenGitTags()
		log.Info(repo.GoPath, "tag count", count)
	*/

	if err == nil {
		return v
	} else {
		log.Log(GITPBWARN, "gitpb.GitMasterVersion() error:", err)
		return ""
	}
}

func (repo *Repo) GitDevelVersion() string {
	v, err := repo.gitVersionByName("devel")
	if err == nil {
		return v
	} else {
		log.Log(GITPBWARN, "gitpb.GitDevelVersion() error:", err)
		return ""
	}
}

func (repo *Repo) GitUserVersion() string {
	v, err := repo.gitVersionByName("jcarr")
	if err == nil {
		return v
	} else {
		log.Log(GITPBWARN, "gitpb.GitUserVersion() error:", err)
		return ""
	}
}

func (repo *Repo) gitVersionByName(name string) (string, error) {
	name = strings.TrimSpace(name)

	if name == "" {
		// git will return the current tag
		r := repo.RunQuiet([]string{"git", "describe", "--tags", "--always"})
		output := strings.Join(r.Stdout, "\n")
		if r.Error != nil {
			log.Log(GITPBWARN, "gitDescribeByName() output might have worked anyway:", output)
			log.Log(GITPBWARN, "gitDescribeByName() not in a git repo?", r.Error, repo.GoPath)
			return "", r.Error
		}
		return strings.TrimSpace(output), nil
	}
	if ! repo.IsBranch(name) {
		// tag does not exist
		log.Log(GITPBWARN, "LocalTagExists()", name, "did not exist")
		return "", errors.New("gitDescribeByName() git fatal: Not a valid object name: " + name)
	}
	cmd := []string{"git", "describe", "--tags", "--always", name}
	result := repo.RunQuiet(cmd)
	output := strings.Join(result.Stdout, "\n")
	if result.Error != nil {
		log.Log(GITPBWARN, "cmd =", cmd)
		log.Log(GITPBWARN, "err =", result.Error)
		log.Log(GITPBWARN, "output (might have worked with error?) =", output)
		log.Log(GITPBWARN, "not in a git repo or bad tag?", repo.GoPath)
		return "", result.Error
	}

	return strings.TrimSpace(output), nil
}

// find a branch name
// will find "master" or "devel"
// will also find "v0.1.1"
// or will find "patches-from-foo"
// will return *any* match on any git branch because it doesn't
// matter much here yet
// eventually this will be worked out by forge in some future code that hasn't been made yet
func (repo *Repo) IsBranch(findname string) bool {
	loop := repo.Tags.All()
	for loop.Scan() {
		t := loop.Next()
		// log.Info("LocalTagExists() tag:", t.Refname)

		tagname := t.Refname
		if strings.HasPrefix(tagname, "refs/remotes") {
			continue
		}
		path, filename := filepath.Split(tagname)
		log.Log(GITPB, "gitpb.IsBranch() tag:", path, filename, "from", repo.GoPath)
		if filename == findname {
			log.Log(GITPB, "gitpb.IsBranch() found tag:", path, filename, "from", repo.GoPath)
			return true
		}
	}
	log.Log(GITPBWARN, "did not find tag:", findname, "in", repo.GoPath)
	return false
}