LincolnP
LincolnP

Reputation: 204

Makefile if remote Git repository exists set variable

Been trying to get this short Makefile to work with no success:

REPO_NAME = myrepo
PROJECT = project

build: check-if-repo-exists

check-if-repo-exists:
    $(eval RESULT := $(shell git ls-remote ssh://[email protected]/$(PROJECT)/$(REPO_NAME).git 2>/dev/null|grep HEAD|cut -f2 -d' ' 2>/dev/null))
    @printf "REPO-->$(RESULT)\n"
ifneq (,$(findstring HEAD,$(RESULT)))
    @printf "git repository - NOT found\n"
else
    @printf "git repository - found \n"
endif

The result is always

git repository - found

Not caring if repository does or not exist. Is there a possible solution? I have also tried a different approach using bash-like if statements like

@if [ "$(REPO)" == "HEAD" ] ; then \
    @printf "git repository - found \n";\   
    @$(eval MY_ERROR := $(shell printf "TRUE"))
fi 

The result is better, but I am still unable to set a variable in order to use it in another rule. Variable MY_ERROR will not be available in a new rule. I understand that in Makefiles rules do not execute sequentially like in Bash, but it does make sense to check if a repository is available before building the application. Any suggestions using make? This is easy to do with Bash, but I loose the advantages of Makefiles and using a combination of both is not the preferred option. Thanks in advance for the help.

Upvotes: 2

Views: 1041

Answers (2)

LincolnP
LincolnP

Reputation: 204

Finally this is the way I solved it:

REPO_NAME=my-repo
PROJECT=my-project

update-my-repo: display-search-my-repo check-if-my-repo-exists display-my- 
repo-exists-result update-my-repo

update-my-repo:
@printf "Updating git repository - $(REPO_NAME) ..."

check-if-my-repo-exists:
    $(eval MY_REPO := $(shell git ls-remote ssh:///[email protected]/$(PROJECT)/$(REPO_NAME).git 2>/dev/null|grep HEAD|cut -f2 -d'    ' 2>/dev/null))

display-search-my-repo:
    @printf "Searching for repository - $(REPO_NAME) ..."

display-my-repo-exists-result:
    @if [ "$(MY_REPO)" = "HEAD" ] ; then \
        printf "$(shell tput cub 3)[FOUND]\n";\
    fi
    @if [ "$(MY_REPO)" = "" ] ; then \
        printf "\n\n git repository - $(REPO_NAME) NOT found [ERROR]\n\n";\
        exit 2; \
    fi

Upvotes: 0

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136286

ifneq is evaluated before the rules are, so you most likely want:

RESULT := $(shell git ls-remote ssh://[email protected]/$(PROJECT)/$(REPO_NAME).git 2>/dev/null|grep HEAD|cut -f2 -d' ' 2>/dev/null)
ifneq (,$(findstring HEAD,$(RESULT)))
$(error "git repository - NOT found\n")
endif

I.e. define a variable (outside any make rule) which value is the result $(shell ...). And issue a make error if its value is unexpected.

Upvotes: 1

Related Questions