.PHONY: image tag show-tag
default: push

ORG?=linuxkit
HASH?=$(shell git ls-tree --full-tree HEAD -- $(CURDIR) | awk '{print $$3}')
BASE_DEPS=Dockerfile Makefile

DIRTY=$(shell git diff-index --quiet HEAD -- $(CURDIR) || echo "-dirty")
TAG=$(ORG)/$(IMAGE):$(HASH)$(DIRTY)

# Get a release tag, if present
RELEASE=$(shell git tag -l --points-at HEAD)

ifdef NETWORK
NET_OPT=
else
NET_OPT=--network=none
endif

show-tag:
	@echo $(TAG)

tag: $(BASE_DEPS) $(DEPS)
	DOCKER_CONTENT_TRUST=1 docker pull $(TAG) || \
	docker build $(NET_OPT) -t $(TAG) .

push: tag
ifneq ($(DIRTY),)
	$(error Your repository is not clean. Will not push package image.)
endif
	DOCKER_CONTENT_TRUST=1 docker pull $(TAG) || \
	DOCKER_CONTENT_TRUST=1 docker push $(TAG)
ifneq ($(RELEASE),)
	docker tag $(TAG) $(ORG)/$(IMAGE):$(RELEASE)
	DOCKER_CONTENT_TRUST=1 docker push $(ORG)/$(IMAGE):$(RELEASE)
endif

.PHONY: image tag show-tag
default: push

ORG?=linuxkit
HASH_COMMIT?=HEAD # Setting this is only really useful with the show-tag target
HASH?=$(shell git ls-tree --full-tree $(HASH_COMMIT) -- $(CURDIR) | awk '{print $$3}')
BASE_DEPS=Dockerfile Makefile

ifneq ($(HASH_COMMIT),HEAD) # Others can't be dirty by definition
DIRTY=$(shell git diff-index --quiet HEAD -- $(CURDIR) || echo "-dirty")
endif
TAG=$(ORG)/$(IMAGE):$(HASH)$(DIRTY)

# Get a release tag, if present
RELEASE=$(shell git tag -l --points-at HEAD)

ifdef NETWORK
NET_OPT=
else
NET_OPT=--network=none
endif

show-tag:
	@echo $(TAG)

tag: $(BASE_DEPS) $(DEPS)
	DOCKER_CONTENT_TRUST=1 docker pull $(TAG) || \
	docker build $(NET_OPT) -t $(TAG) .

push: tag
ifneq ($(DIRTY),)
	$(error Your repository is not clean. Will not push package image.)
endif
	DOCKER_CONTENT_TRUST=1 docker pull $(TAG) || \
	DOCKER_CONTENT_TRUST=1 docker push $(TAG)
ifneq ($(RELEASE),)
	docker tag $(TAG) $(ORG)/$(IMAGE):$(RELEASE)
	DOCKER_CONTENT_TRUST=1 docker push $(ORG)/$(IMAGE):$(RELEASE)
endif

Allow override of commit to calculate hash for show-tag
pkg: Allow override of commit to calculate hash for show-tag

Useful to answer the question of what the hash was at the point of some older
commit.

$ make --no-print-directory -C pkg/init/ show-tag
linuxkit/init:36c56f0664d49c5a6adc1120d1bf5ba6ac30b389
$ make --no-print-directory -C pkg/init/ show-tag HASH_COMMIT=4699f80ef73141ee87a77d03f93065e541fab382
linuxkit/init:17693d233dd009b2a3a8d23673cb85969e1dce80

Signed-off-by: Ian Campbell <9be92b3dbbd6611e3e4c9209fb3d04b4919e7cca@docker.com>
