From e05f2d3724a3ef74f34ba37d3526f46c2083af8b Mon Sep 17 00:00:00 2001 From: Caio Alonso Date: Sat, 17 Oct 2015 19:48:39 -0300 Subject: [PATCH] adds the option to see the current git commit with `ipfs version --commit` License: MIT Signed-off-by: Caio Alonso --- Makefile | 4 +++- cmd/ipfs/Makefile | 4 +++- core/commands/version.go | 17 +++++++++++++++-- repo/config/version.go | 2 ++ 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 98fac1d09..11a0ff34c 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,8 @@ else go_test=go test endif +commit = `git rev-parse --short HEAD` +ldflags = "-X "github.com/ipfs/go-ipfs/repo/config".CurrentCommit=$(commit)" all: # no-op. try: @@ -24,7 +26,7 @@ install: cd cmd/ipfs && go install build: - cd cmd/ipfs && go build -i + cd cmd/ipfs && go build -i -ldflags=$(ldflags) nofuse: cd cmd/ipfs && go install -tags nofuse diff --git a/cmd/ipfs/Makefile b/cmd/ipfs/Makefile index fccb80330..70275c5df 100644 --- a/cmd/ipfs/Makefile +++ b/cmd/ipfs/Makefile @@ -1,7 +1,9 @@ all: install +commit = `git rev-parse --short HEAD` +ldflags = "-X "github.com/ipfs/go-ipfs/repo/config".CurrentCommit=$(commit)" build: - go build + go build -ldflags=$(ldflags) install: build go install diff --git a/core/commands/version.go b/core/commands/version.go index 59adc0476..2c3bafe07 100644 --- a/core/commands/version.go +++ b/core/commands/version.go @@ -11,6 +11,7 @@ import ( type VersionOutput struct { Version string + Commit string } var VersionCmd = &cmds.Command{ @@ -21,24 +22,36 @@ var VersionCmd = &cmds.Command{ Options: []cmds.Option{ cmds.BoolOption("number", "n", "Only show the version number"), + cmds.BoolOption("commit", "Show the commit hash"), }, Run: func(req cmds.Request, res cmds.Response) { res.SetOutput(&VersionOutput{ Version: config.CurrentVersionNumber, + Commit: config.CurrentCommit, }) }, Marshalers: cmds.MarshalerMap{ cmds.Text: func(res cmds.Response) (io.Reader, error) { v := res.Output().(*VersionOutput) + commit, found, err := res.Request().Option("commit").Bool() + commitTxt := "" + if err != nil { + return nil, err + } + if found && commit { + commitTxt = "-" + v.Commit + } + number, found, err := res.Request().Option("number").Bool() if err != nil { return nil, err } if found && number { - return strings.NewReader(fmt.Sprintln(v.Version)), nil + return strings.NewReader(fmt.Sprintln(v.Version + commitTxt)), nil } - return strings.NewReader(fmt.Sprintf("ipfs version %s\n", v.Version)), nil + + return strings.NewReader(fmt.Sprintf("ipfs version %s%s\n", v.Version, commitTxt)), nil }, }, Type: VersionOutput{}, diff --git a/repo/config/version.go b/repo/config/version.go index 137a5cac2..c53d0e4b7 100644 --- a/repo/config/version.go +++ b/repo/config/version.go @@ -9,6 +9,8 @@ import ( // CurrentVersionNumber is the current application's version literal const CurrentVersionNumber = "0.3.8-dev" +// CurrentCommit is the current git commit, this is set as a ldflag in the Makefile +var CurrentCommit string // Version regulates checking if the most recent version is run type Version struct {