Skip to content

Instantly share code, notes, and snippets.

@DanielG
Last active January 24, 2021 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanielG/d12035ea90eefd87d6ba1aad2ed2477f to your computer and use it in GitHub Desktop.
Save DanielG/d12035ea90eefd87d6ba1aad2ed2477f to your computer and use it in GitHub Desktop.
Example of automatically embedding git commit info into source tarballs
/git.mk export-subst
# SPDX-License-Identifier: MIT OR FSFAP OR CC0-1.0
F:=$$F
dollar := $$
# This stuff gets expanded at git-archive(1) time due to the export-subst
# setting for this file in .gitattributes. See gitattributes(5)
GIT_COMMIT := $Format:%H$
GIT_DATE := $Format:%cD$
GIT_TIMESTAMP := $Format:%ct$
GIT_SUBJ := $Format:%s$
GIT_DIRTY :=
# If the above wasn't expanded get version info straight from git
ifeq ($(GIT_COMMIT),$(dollar)Format:%H$(dollar))
GIT_COMMIT := $(shell git rev-parse HEAD)
GIT_DATE := $(shell git show -s --pretty=format:%cD)
GIT_TIMESTAMP := $(shell git show -s --pretty=format:%ct)
GIT_SUBJ := $(shell git show -s --pretty=format:%s)
GIT_DIRTY := -dirty
endif
.PHONY: FORCE
git.h: FORCE
@printf ' GIT\t$@\n'
@echo '#pragma once' > $@
@echo '#define GIT_COMMIT "commit $(GIT_COMMIT)$(GIT_DIRTY)"' >> $@
@echo '#define GIT_SUBJ "$(GIT_SUBJ)"' >> $@
@echo '#define GIT_DATE "$(GIT_DATE)"' >> $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment