Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

makefile

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

e.g.

all: download
      echo '$(GIT_COMMIT)'

download:
      cd buildarea && git clone [email protected]:proj/project.git
      $(eval GIT_COMMIT = $(shell cd buildarea/project && git log -l --pretty=format:"%H"))
      echo '$(GIT_COMMIT)'
like image 781
user2569618 Avatar asked Jan 11 '16 01:01

user2569618


People also ask

How do you find the hash of a commit?

The long string following the word commit is called the commit hash.

How does git determine commit hash?

The commit hash by hashing the data you see with cat-file . This includes the tree object hash and commit information like author, time, commit message, and the parent commit hash if it's not the first commit.


2 Answers

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 && [email protected]:proj/project.git
like image 183
Eric Avatar answered Oct 10 '22 18:10

Eric


Because of the way that make evaluates the target the git clone executes after $(shell), so cd tries to move to a directory that doesn't exist yet. 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 [email protected]:proj/project.git buildarea/project && cd buildarea/project && git rev-parse HEAD))
      echo '$(GIT_COMMIT)'
like image 42
Guildencrantz Avatar answered Oct 10 '22 16:10

Guildencrantz