cmd/gitbayd/version.go

b347d6c8c464e3c965455795f4e22e0aaeed0652
gitbay/cmd/gitbayd/version.go history · blame · raw

70 lines · 2265 bytes

 1package main
 2
 3import (
 4	"fmt"
 5	"log/slog"
 6	"strings"
 7
 8	"github.com/spf13/cobra"
 9
10	"gitbay.org/gitbay/internal/buildinfo"
11	"gitbay.org/gitbay/internal/config"
12	"gitbay.org/gitbay/internal/control"
13	"gitbay.org/gitbay/internal/gitutil"
14)
15
16func versionCmd() *cobra.Command {
17	return &cobra.Command{
18		Use:   "version",
19		Short: "print the commit this binary was built from",
20		Args:  cobra.NoArgs,
21		RunE: func(cmd *cobra.Command, args []string) error {
22			fmt.Println(buildinfo.String())
23			return nil
24		},
25	}
26}
27
28// logBuild announces the running build. An unidentified one is a warning
29// rather than a fact: it was built from a tree that was never committed, so
30// the source it came from no longer exists anywhere.
31func logBuild() {
32	if !buildinfo.Identified() {
33		slog.Warn("gitbayd starting from an uncommitted build", "commit", buildinfo.String())
34		return
35	}
36	slog.Info("gitbayd starting", "commit", buildinfo.String())
37}
38
39// warnIfUnmerged says so when the running build is not on the default branch
40// of the repository this instance develops itself in. Such a build serves
41// perfectly well, which is exactly why it can sit unnoticed for days.
42//
43// Silent unless server.source_repo is set, since an instance that does not
44// host its own source has nothing to check against.
45func warnIfUnmerged(cfg config.Config) {
46	repo := cfg.Server.SourceRepo
47	if repo == "" || !buildinfo.Identified() {
48		return
49	}
50	owner, name, ok := strings.Cut(repo, "/")
51	if !ok || owner == "" || name == "" {
52		slog.Warn("server.source_repo is not owner/name; skipping the build check", "source_repo", repo)
53		return
54	}
55	// HEAD in a bare repository is the default branch, so there is no branch
56	// name to resolve or configure.
57	dir := control.RepoDir(cfg.Server.Root, owner, name)
58	onBranch, err := gitutil.IsAncestor(dir, buildinfo.String(), "HEAD")
59	if err != nil {
60		// An unpushed commit lands here too, and is worth the same warning:
61		// it cannot be checked, so it cannot be vouched for.
62		slog.Warn("cannot check this build against the source repository",
63			"commit", buildinfo.String(), "repo", repo, "err", err)
64		return
65	}
66	if !onBranch {
67		slog.Warn("this build is not on the source repository's default branch",
68			"commit", buildinfo.String(), "repo", repo)
69	}
70}