2

I'm trying to run this couple of lines to get the git commit in a makefile, outside of any recipe

GIT_VERSION := $(shell cd "C:\Users\made\up\filepath" && git describe --always)
$(info GIT_VERSION is '$(GIT_VERSION)')

But it always prints GIT_VERSION is ''

However, if I run

PWD := $(shell cd "C:\Users\made\up\filepath" && pwd)
$(info PWD is $(PWD))

it prints out PWD is /cygdrive/c/Users/made/up/filepath as I might expect.

This led me to think there was something wrong with the shell the makefile was using. SHELL := $(shell echo $$SHELL) gives /bin/bash, which I note is different from the /usr/bin/bash that opening a git-bash shell gives me, although I'm not sure how to interpret this, being on Windows. Any clue how I can start getting some output from the git call?


update: still struggling with this. Have tried the -C git option as reccommended in comments, wrapping in eval, still getting nothing in GIT_VERSION

ACarter
  • 5,688
  • 9
  • 39
  • 56
  • 2
    As an aside, assigning to `PWD` is dangerous, because `PWD` is reset every time you use `cd`. – Benjamin W. Nov 17 '22 at 20:29
  • 1
    Does it help if instead of actually changing directory, you just use `git`'s `-C` option to tell it where to look? `GIT_VERSION := $(shell git -C "C:\Users\made\up\filepath" describe --always)`. Aside from minimizing use of `cd` being a good practice, that has the advantage that it will induce an error message instead of failing silently if the target directory does not exist or is inaccessible. – John Bollinger Nov 17 '22 at 22:48
  • @JohnBollinger no change :( in fact, even if I do put in a made up filepath, I still get nothing out. It's almost like `git` always fails silently in this shell – ACarter Nov 18 '22 at 17:36
  • 1
    As a diagnostic tool, try using `set +x` inside your shell command, to make it print the commands it runs to stderr (or other chosen bash file descriptor, if using bash, where `-x` is fancy and configurable). Meanwhile, note that it's a good idea to use *forward* slashes so that you don't have to quote some of your backslashes. – torek Nov 22 '22 at 08:53
  • Are you sure you're cd'ing in to a functioning repo? One way to get the behavior you're describing is to redirect stderr and run `git describe` in a directory that's not in any repository Git can recognize. – jthill Mar 17 '23 at 18:24
  • @jthill yeah, the `$(shell cd "C:\Users\made\up\filepath" && pwd)` test confirms the directory is right – ACarter Mar 19 '23 at 21:24
  • FWIW: on linux, with some specific shell errors, the output gets sent to stderr instead of stdout : I wanted to test `VAR:=$(shell echo foo && false); $(info got: $(VAR))` (which outputs `got: foo`, even though the script exits with non zero code), but due to a phat phingers condition ;) I first typed `VAR:=$(shell echo foo && fale); $(info got: $(VAR))`, which printed *on stderr*: `/bin/sh: 1: fale: not found \n foo` and VAR was empty. – LeGEC Mar 21 '23 at 04:54
  • tested one more thing: the above seems to be triggered by `exit 127` (not by any other exit code). tested on Ubuntu 22.10, with GNU Make 4.3 built for x86_64-pc-linux-gnu – LeGEC Mar 21 '23 at 05:02

0 Answers0