add a "version" command with various outputs, including the git hash and tag

Michael Muré created

Change summary

Makefile                     | 12 +++++-
commands/root.go             |  2 -
commands/version.go          | 71 ++++++++++++++++++++++++++++++++++++++
doc/man/git-bug-version.1    | 41 +++++++++++++++++++++
doc/man/git-bug.1            |  2 
doc/md/git-bug.md            |  1 
doc/md/git-bug_version.md    | 25 +++++++++++++
misc/bash_completion/git-bug | 30 ++++++++++++++++
misc/zsh_completion/git-bug  |  2 
9 files changed, 180 insertions(+), 6 deletions(-)

Detailed changes

Makefile 🔗

@@ -1,13 +1,21 @@
 all: build
 
+GIT_COMMIT:=$(shell git rev-list -1 HEAD)
+GIT_LAST_TAG:=$(shell git describe --abbrev=0 --tags)
+GIT_EXACT_TAG:=$(shell git name-rev --name-only --tags HEAD)
+COMMANDS_PATH:=github.com/MichaelMure/git-bug/commands
+LDFLAGS:=-X ${COMMANDS_PATH}.GitCommit=${GIT_COMMIT} \
+	-X ${COMMANDS_PATH}.GitLastTag=${GIT_LAST_TAG} \
+	-X ${COMMANDS_PATH}.GitExactTag=${GIT_EXACT_TAG}
+
 build:
 	go generate
-	go build .
+	go build -ldflags "$(LDFLAGS)" .
 
 # produce a build debugger friendly
 debug-build:
 	go generate
-	go build -gcflags=all="-N -l" .
+	go build -ldflags "$(LDFLAGS)" -gcflags=all="-N -l" .
 
 install:
 	go generate

commands/root.go 🔗

@@ -17,8 +17,6 @@ var repo repository.ClockedRepo
 
 // RootCmd represents the base command when called without any subcommands
 var RootCmd = &cobra.Command{
-	Version: "0.4.0",
-
 	Use:   rootCommandName,
 	Short: "A bug tracker embedded in Git",
 	Long: `git-bug is a bug tracker embedded in git.

commands/version.go 🔗

@@ -0,0 +1,71 @@
+package commands
+
+import (
+	"fmt"
+	"runtime"
+
+	"github.com/spf13/cobra"
+)
+
+// These variables are initialized externally during the build. See the Makefile.
+var GitCommit string
+var GitLastTag string
+var GitExactTag string
+
+var (
+	versionNumber bool
+	versionCommit bool
+	versionAll    bool
+)
+
+func runVersionCmd(cmd *cobra.Command, args []string) {
+	if versionAll {
+		fmt.Printf("%s version: %s\n", rootCommandName, RootCmd.Version)
+		fmt.Printf("System version: %s/%s\n", runtime.GOARCH, runtime.GOOS)
+		fmt.Printf("Golang version: %s\n", runtime.Version())
+		return
+	}
+
+	if versionNumber {
+		fmt.Println(RootCmd.Version)
+		return
+	}
+
+	if versionCommit {
+		fmt.Println(GitCommit)
+		return
+	}
+
+	fmt.Printf("%s version: %s\n", rootCommandName, RootCmd.Version)
+}
+
+var versionCmd = &cobra.Command{
+	Use:   "version",
+	Short: "Show git-bug version information",
+	Run:   runVersionCmd,
+}
+
+func init() {
+	if GitExactTag == "undefined" {
+		GitExactTag = ""
+	}
+
+	RootCmd.Version = GitLastTag
+
+	if GitExactTag == "" {
+		RootCmd.Version = fmt.Sprintf("%s-dev-%.10s", RootCmd.Version, GitCommit)
+	}
+
+	RootCmd.AddCommand(versionCmd)
+	versionCmd.Flags().SortFlags = false
+
+	versionCmd.Flags().BoolVarP(&versionNumber, "number", "n", false,
+		"Only show the version number",
+	)
+	versionCmd.Flags().BoolVarP(&versionCommit, "commit", "c", false,
+		"Only show the commit hash",
+	)
+	versionCmd.Flags().BoolVarP(&versionAll, "all", "a", false,
+		"Show all version informations",
+	)
+}

doc/man/git-bug-version.1 🔗

@@ -0,0 +1,41 @@
+.TH "GIT-BUG" "1" "Feb 2019" "Generated from git-bug's source code" "" 
+.nh
+.ad l
+
+
+.SH NAME
+.PP
+git\-bug\-version \- Show git\-bug version information
+
+
+.SH SYNOPSIS
+.PP
+\fBgit\-bug version [flags]\fP
+
+
+.SH DESCRIPTION
+.PP
+Show git\-bug version information
+
+
+.SH OPTIONS
+.PP
+\fB\-n\fP, \fB\-\-number\fP[=false]
+    Only show the version number
+
+.PP
+\fB\-c\fP, \fB\-\-commit\fP[=false]
+    Only show the commit hash
+
+.PP
+\fB\-a\fP, \fB\-\-all\fP[=false]
+    Show all version informations
+
+.PP
+\fB\-h\fP, \fB\-\-help\fP[=false]
+    help for version
+
+
+.SH SEE ALSO
+.PP
+\fBgit\-bug(1)\fP

doc/man/git-bug.1 🔗

@@ -31,4 +31,4 @@ the same git remote your are already using to collaborate with other peoples.
 
 .SH SEE ALSO
 .PP
-\fBgit\-bug\-add(1)\fP, \fBgit\-bug\-bridge(1)\fP, \fBgit\-bug\-commands(1)\fP, \fBgit\-bug\-comment(1)\fP, \fBgit\-bug\-deselect(1)\fP, \fBgit\-bug\-label(1)\fP, \fBgit\-bug\-ls(1)\fP, \fBgit\-bug\-ls\-label(1)\fP, \fBgit\-bug\-pull(1)\fP, \fBgit\-bug\-push(1)\fP, \fBgit\-bug\-select(1)\fP, \fBgit\-bug\-show(1)\fP, \fBgit\-bug\-status(1)\fP, \fBgit\-bug\-termui(1)\fP, \fBgit\-bug\-title(1)\fP, \fBgit\-bug\-webui(1)\fP
+\fBgit\-bug\-add(1)\fP, \fBgit\-bug\-bridge(1)\fP, \fBgit\-bug\-commands(1)\fP, \fBgit\-bug\-comment(1)\fP, \fBgit\-bug\-deselect(1)\fP, \fBgit\-bug\-label(1)\fP, \fBgit\-bug\-ls(1)\fP, \fBgit\-bug\-ls\-label(1)\fP, \fBgit\-bug\-pull(1)\fP, \fBgit\-bug\-push(1)\fP, \fBgit\-bug\-select(1)\fP, \fBgit\-bug\-show(1)\fP, \fBgit\-bug\-status(1)\fP, \fBgit\-bug\-termui(1)\fP, \fBgit\-bug\-title(1)\fP, \fBgit\-bug\-version(1)\fP, \fBgit\-bug\-webui(1)\fP

doc/md/git-bug.md 🔗

@@ -39,5 +39,6 @@ git-bug [flags]
 * [git-bug status](git-bug_status.md)	 - Display or change a bug status
 * [git-bug termui](git-bug_termui.md)	 - Launch the terminal UI
 * [git-bug title](git-bug_title.md)	 - Display or change a title
+* [git-bug version](git-bug_version.md)	 - Show git-bug version information
 * [git-bug webui](git-bug_webui.md)	 - Launch the web UI
 

doc/md/git-bug_version.md 🔗

@@ -0,0 +1,25 @@
+## git-bug version
+
+Show git-bug version information
+
+### Synopsis
+
+Show git-bug version information
+
+```
+git-bug version [flags]
+```
+
+### Options
+
+```
+  -n, --number   Only show the version number
+  -c, --commit   Only show the commit hash
+  -a, --all      Show all version informations
+  -h, --help     help for version
+```
+
+### SEE ALSO
+
+* [git-bug](git-bug.md)	 - A bug tracker embedded in Git
+

misc/bash_completion/git-bug 🔗

@@ -779,6 +779,35 @@ _git-bug_title()
     noun_aliases=()
 }
 
+_git-bug_version()
+{
+    last_command="git-bug_version"
+
+    command_aliases=()
+
+    commands=()
+
+    flags=()
+    two_word_flags=()
+    local_nonpersistent_flags=()
+    flags_with_completion=()
+    flags_completion=()
+
+    flags+=("--number")
+    flags+=("-n")
+    local_nonpersistent_flags+=("--number")
+    flags+=("--commit")
+    flags+=("-c")
+    local_nonpersistent_flags+=("--commit")
+    flags+=("--all")
+    flags+=("-a")
+    local_nonpersistent_flags+=("--all")
+
+    must_have_one_flag=()
+    must_have_one_noun=()
+    noun_aliases=()
+}
+
 _git-bug_webui()
 {
     last_command="git-bug_webui"
@@ -824,6 +853,7 @@ _git-bug_root_command()
     commands+=("status")
     commands+=("termui")
     commands+=("title")
+    commands+=("version")
     commands+=("webui")
 
     flags=()

misc/zsh_completion/git-bug 🔗

@@ -8,7 +8,7 @@ case $state in
   level1)
     case $words[1] in
       git-bug)
-        _arguments '1: :(add bridge commands comment deselect label ls ls-label pull push select show status termui title webui)'
+        _arguments '1: :(add bridge commands comment deselect label ls ls-label pull push select show status termui title version webui)'
       ;;
       *)
         _arguments '*: :_files'