diff --git a/Dockerfile b/Dockerfile index b641c4b463d..87bd52c965c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -32,6 +32,8 @@ ENV GOPATH /go ENV PATH /go/bin:$PATH ENV SRC_PATH /go/src/github.com/ipfs/go-ipfs +ARG IPFS_VERSION + # Get the go-ipfs sourcecode COPY . $SRC_PATH @@ -53,10 +55,11 @@ RUN apk add --update musl go=$GO_VERSION git bash wget ca-certificates \ # This saves us quite a bit of image size. && ref="$(cat .git/HEAD | cut -d' ' -f2)" \ && commit="$(cat .git/$ref | head -c 7)" \ - && echo "ldflags=-X github.com/ipfs/go-ipfs/repo/config.CurrentCommit=$commit" \ - # Build and install IPFS and entrypoint script + && ldflags="-X github.com/ipfs/go-ipfs/repo/config.CurrentCommit=$commit \ + -X github.com/ipfs/go-ipfs/repo/config.CurrentVersionNumber=$IPFS_VERSION" \ + && echo "ldflags: $ldflags" \ && cd $SRC_PATH/cmd/ipfs \ - && go build -ldflags "-X github.com/ipfs/go-ipfs/repo/config.CurrentCommit=$commit" \ + && go build -ldflags "$ldflags" \ && cp ipfs /usr/local/bin/ipfs \ && cp $SRC_PATH/bin/container_daemon /usr/local/bin/start_ipfs \ && chmod 755 /usr/local/bin/start_ipfs \ diff --git a/Makefile b/Makefile index 8a3bddb0173..7020a37c699 100644 --- a/Makefile +++ b/Makefile @@ -5,8 +5,11 @@ else go_test=go test endif -COMMIT := $(shell git rev-parse --short HEAD) -ldflags = "-X "github.com/ipfs/go-ipfs/repo/config".CurrentCommit=$(COMMIT)" +VERSION := $(shell bin/genversion --version) +COMMIT := $(shell bin/genversion --commit) +ldflags = "-X "github.com/ipfs/go-ipfs/repo/config".CurrentVersionNumber=$(VERSION) \ +-X "github.com/ipfs/go-ipfs/repo/config".CurrentCommit=$(COMMIT)" + MAKEFLAGS += --no-print-directory @@ -59,8 +62,11 @@ clean: uninstall: cd cmd/ipfs && go clean -i -ldflags=$(ldflags) +docker: + docker build --build-arg IPFS_VERSION=$(VERSION) . + PHONY += all help godep toolkit_upgrade gx_upgrade gxgo_upgrade gx_check -PHONY += go_check deps vendor install build nofuse clean uninstall +PHONY += go_check deps vendor install build nofuse clean uninstall docker ############################################################## # tests targets diff --git a/bin/genversion b/bin/genversion new file mode 100755 index 00000000000..030aa6d9847 --- /dev/null +++ b/bin/genversion @@ -0,0 +1,78 @@ +#!/bin/bash + +# +# Calls git describe and extracts SemVer-compatible version and meta info +# Assumes git tags are properly formatted: vX.Y.Z or vX.Y.Z-some-tag +# +# Examples: +# +# $ genversion --version +# 0.4.0-rc2 +# +# or: +# +# $ genversion --commit +# c148.91c6f0f.dirty +# +# (148 commits ahead of the version 0.4.0-rc2, dirty workdir with HEAD at 91c6f0f) + + +PRINT_VERSION=0 +PRINT_COMMIT=0 + +test $# -ge 1 || PRINT_VERSION=1 + +while test $# -gt 0; do + case "$1" in + "--version") + PRINT_VERSION=1 + ;; + "--commit") + PRINT_COMMIT=1 + ;; + esac + shift +done + + +string_join (){ + local IFS="$1" + shift + echo "$*" +} + +if [[ "$PRINT_VERSION" == "1" ]] || [[ "$PRINT_COMMIT" == "1" ]]; then + DESCRIBE=( $(git describe --always --tags --match 'v*' --dirty --long | tr '-' '\n') ) + + FIELDS=${#DESCRIBE[@]} + if [[ "${DESCRIBE[$FIELDS-1]}" == "dirty" ]]; then + DIRTY=1 + FIELDS=$FIELDS-1 + fi + COMMIT_HASH=${DESCRIBE[$FIELDS-1]} + COMMIT_NO=${DESCRIBE[$FIELDS-2]} + VERSION=$(string_join - ${DESCRIBE[@]:0:$FIELDS-2}) + VERSION=${VERSION:1} + HASH=${DESCRIBE[$FIELDS-1]} + COMMIT=$(printf "c%s.%s" ${DESCRIBE[$FIELDS-2]} ${HASH:1}) + + if [[ "$DIRTY" == "1" ]]; then + COMMIT="$COMMIT.dirty" + fi + +fi + +if [[ "$PRINT_VERSION" == "1" ]]; then + printf $VERSION +fi + +if [[ "$PRINT_VERSION" == "1" ]] && [[ "$PRINT_COMMIT" == "1" ]]; then + printf "+" +fi + +if [[ "$PRINT_COMMIT" == "1" ]]; then + printf $COMMIT +fi + +echo + diff --git a/core/commands/version.go b/core/commands/version.go index c8d3212e2e6..9338ddf2770 100644 --- a/core/commands/version.go +++ b/core/commands/version.go @@ -53,7 +53,7 @@ var VersionCmd = &cmds.Command{ return nil, err } if found && commit { - commitTxt = "-" + v.Commit + commitTxt = "+" + v.Commit } number, found, err := res.Request().Option("number").Bool() diff --git a/repo/config/version.go b/repo/config/version.go index 3acab699bfb..c0e515c7add 100644 --- a/repo/config/version.go +++ b/repo/config/version.go @@ -7,13 +7,15 @@ import ( "time" ) +// CurrentVersionNumber and CurrentCommit are normally set via ldflags -X + // CurrentCommit is the current git commit, this is set as a ldflag in the Makefile var CurrentCommit string // CurrentVersionNumber is the current application's version literal -const CurrentVersionNumber = "0.4.0-dev" +var CurrentVersionNumber = "0.0.0-dev" -const ApiVersion = "/go-ipfs/" + CurrentVersionNumber + "/" +var ApiVersion = "/go-ipfs/" + CurrentVersionNumber + "/" // Version regulates checking if the most recent version is run type Version struct { diff --git a/test/Dockerfile b/test/Dockerfile index 61a4a2c1f90..3ce2971ea50 100644 --- a/test/Dockerfile +++ b/test/Dockerfile @@ -42,12 +42,16 @@ RUN apk add --update musl go=$GO_VERSION git bash wget ca-certificates \ COPY . $SRC_PATH +ARG IPFS_VERSION + RUN cd $SRC_PATH \ && ref="$(cat .git/HEAD | cut -d' ' -f2)" \ && commit="$(cat .git/$ref | head -c 7)" \ - && echo "ldflags=-X github.com/ipfs/go-ipfs/repo/config.CurrentCommit=$commit" \ + && ldflags="-X github.com/ipfs/go-ipfs/repo/config.CurrentCommit=$commit \ + -X github.com/ipfs/go-ipfs/repo/config.CurrentVersionNumber=$IPFS_VERSION" \ + && echo "ldflags: $ldflags" \ && cd $SRC_PATH/cmd/ipfs \ - && go build -ldflags "-X github.com/ipfs/go-ipfs/repo/config.CurrentCommit=$commit" \ + && go build -ldflags "$ldflags" \ && cp ipfs /usr/local/bin/ipfs \ && cp $SRC_PATH/bin/container_daemon /usr/local/bin/start_ipfs \ && chmod 755 /usr/local/bin/start_ipfs \ diff --git a/test/ipfs-test-lib.sh b/test/ipfs-test-lib.sh index 94ed5a77ff0..504381e986b 100644 --- a/test/ipfs-test-lib.sh +++ b/test/ipfs-test-lib.sh @@ -48,7 +48,17 @@ shellquote() { # This takes a Dockerfile, and a build context directory docker_build() { - docker build --rm -f "$1" "$2" + ipfs_version=$($2/bin/genversion --version) + + # Workaround for docker < 1.9 + if test "$TRAVIS" = true + then + cat "$1" | sed "s/^ARG IPFS_VERSION.*/ENV IPFS_VERSION=$ipfs_version/" > "$1.travis" + docker build --rm -f "$1.travis" "$2" + rm "$1.travis" + else + docker build --rm --build-arg IPFS_VERSION=$ipfs_version -f "$1" "$2" + fi } # This takes an image as argument and writes a docker ID on stdout