简体   繁体   中英

In a Makefile, how can I fetch and assign a git commit hash to a variable?

A make all clones the git repo. I want to know what the commit hash is and assign the git commit hash to a variable which can be used later in the Makefile

eg

all: download
      echo '$(GIT_COMMIT)'

download:
      cd buildarea && git clone git@github.com:proj/project.git
      $(eval GIT_COMMIT = $(shell cd buildarea/project && git log -l --pretty=format:"%H"))
      echo '$(GIT_COMMIT)'

How about use shell function and 2 targets for it?

all: download getver

getver: VER=$(shell cd buildarea/project && git log -1 --pretty=format:"%H")
getver:
        @echo GIT_COMMIT=$(VER)

download:
        mkdir -p buildarea && cd buildarea && git@github.com:proj/project.git

I'm not sure exactly why, but it appears that after the git clone $(shell) is unable to cd into the directory. What you can do is simply perform the whole act in a single $(shell) call.

all: download
      echo '$(GIT_COMMIT)'

download:
      $(eval GIT_COMMIT = $(shell git clone git@github.com:proj/project.git buildarea/project && cd buildarea/project && git rev-parse HEAD))
      echo '$(GIT_COMMIT)'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM