Doing something before serving a tarball

I've come into the habit of embedding into my projects Git hashes and other information. That information is then provided to the user along with version info when the -v flag is used, for instance. The Git info will get into the compiled program if the program is build from a cloned Git repo or from a tarball that is created by doing make dist.

I just now got a report from someone who downloaded a tarball rather than cloning the repository. Since this function simply takes a copy of the selected branch, the Git info isn't there. Is there some way I can have the server run a script before generating the tarball to put the info where it needs to go?

Here are relevant selections from my Makefile to show what I'm doing:

# If we're working from git, we have access to proper variables. If
# not, make it clear that we're working from a release.
GIT_DIR ?= .git
ifneq ($(and $(wildcard $(GIT_DIR)),$(shell which git)),)
        GIT_BRANCH = $(shell git rev-parse --abbrev-ref HEAD)
        GIT_HASH = $(shell git rev-parse HEAD)
        GIT_HASH_SHORT = $(shell git rev-parse --short HEAD)
        GIT_TAG = $(shell git describe --abbrev=0 --tags)
else
        GIT_BRANCH = none
        GIT_HASH = none
        GIT_HASH_SHORT = none
        GIT_TAG = none
endif
BUILD_DATE_TIME = $(shell date +%Y%m%d.%k%M%S | sed s/\ //g)

Now here's how the hashes get into the source code:

hash: $(HASH)
$(HASH):
        @echo "** Generating $@"
        @echo "#define GIT_BRANCH \"$(GIT_BRANCH)\"" > $@
        @echo "#define GIT_HASH \"$(GIT_HASH)\"" >> $@
        @echo "#define GIT_HASH_SHORT \"$(GIT_HASH_SHORT)\"" >> $@
        @echo "#define GIT_TAG \"$(GIT_TAG)\"" >> $@

And here's how building a tarball puts Git info into the Makefile itself:

dist: foo-$(GIT_TAG).tar
foo-$(GIT_TAG).tar:
        git archive --format=tar --prefix foo-$(GIT_TAG)/ HEAD | tar xf -
        sed s"/GIT_BRANCH = none/GIT_BRANCH = \"$(GIT_BRANCH)\"/" -i foo-$(GIT_TAG)/Makefile
        sed s"/GIT_HASH = none/GIT_HASH = \"$(GIT_HASH)\"/" -i foo-$(GIT_TAG)/Makefile
        sed s"/GIT_HASH_SHORT = none/GIT_HASH_SHORT = \"$(GIT_HASH_SHORT)\"/" -i foo-$(GIT_TAG)/Makefile
        sed s"/GIT_TAG = none/GIT_TAG = \"$(GIT_TAG)\"/" -i foo-$(GIT_TAG)/Makefile
        tar zcf foo-$(GIT_TAG).tar.gz foo-$(GIT_TAG)
        rm -rf foo-$(GIT_TAG)
Edited by 🤖 GitLab Bot 🤖