aboutsummaryrefslogtreecommitdiff
path: root/internal/version.go
blob: 1a757509093f2221192c64772308af62f64478ac (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
package internal

import (
	"fmt"
	"runtime/debug"
	"strings"
)

// the final version string
var version string

// -ldflags "-X github.com/matrix-org/dendrite/internal.branch=master"
var branch string

// -ldflags "-X github.com/matrix-org/dendrite/internal.build=alpha"
var build string

const (
	VersionMajor = 0
	VersionMinor = 13
	VersionPatch = 6
	VersionTag   = "" // example: "rc1"

	gitRevLen = 7 // 7 matches the displayed characters on github.com
)

func VersionString() string {
	return version
}

func init() {
	version = fmt.Sprintf("%d.%d.%d", VersionMajor, VersionMinor, VersionPatch)
	if VersionTag != "" {
		version += "-" + VersionTag
	}
	parts := []string{}
	if build != "" {
		parts = append(parts, build)
	}
	if branch != "" {
		parts = append(parts, branch)
	}

	defer func() {
		if len(parts) > 0 {
			version += "+" + strings.Join(parts, ".")
		}
	}()

	// Try to get the revision Dendrite was build from.
	// If we can't, e.g. Dendrite wasn't built (go run) or no VCS version is present,
	// we just use the provided version above.
	info, ok := debug.ReadBuildInfo()
	if !ok {
		return
	}

	for _, setting := range info.Settings {
		if setting.Key == "vcs.revision" {
			revLen := len(setting.Value)
			if revLen >= gitRevLen {
				parts = append(parts, setting.Value[:gitRevLen])
			} else {
				parts = append(parts, setting.Value[:revLen])
			}
			break
		}
	}
}