summaryrefslogtreecommitdiff
path: root/gitTags.query.go
blob: faea8e268fbb0bb7a07a93970c0cc9f77816a0a0 (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
package gitpb

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

import (
	"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.Info("git LastTag() error:", result.Stdout)
		return "error"
	}

	hash := result.Stdout[0]

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

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

	return result.Stdout[0]
}

/*
func (repo *Repo) gitDescribeByName(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.Warn("gitDescribeByName() not in a git repo?", r.Error, repo.GoPath)
		}
		return strings.TrimSpace(output), r.Error
	}
	if !repo.LocalTagExists(name) {
		// tag does not exist
		return "", errors.New("gitDescribeByName() git fatal: Not a valid object name")
	}
	cmd := []string{"git", "describe", "--tags", "--always", name}
	r := repo.RunQuiet(cmd)
	output := strings.Join(r.Stdout, "\n")
	if r.Error != nil {
		log.Warn("cmd =", cmd)
		log.Warn("err =", r.Error)
		log.Warn("not in a git repo or bad tag?", rs.Path())
	}

	return strings.TrimSpace(output), r.Error
}

func (repo *Repo) LocalTagExists(findname string) bool {
	allTags := repo.Tags.ListAll()
	for _, t := range allTags {
		tagname := t.TagString()
		if strings.HasPrefix(tagname, "refs/remotes") {
			continue
		}
		path, filename := filepath.Split(tagname)
		log.Log(INFO, "tag:", path, filename, "from", rs.Path())
		if filename == findname {
			log.Log(INFO, "found tag:", path, filename, "from", rs.Path())
			return true
		}
	}
	return false
}
*/