How to create macro with git branch name?

Third time I try to figure out how can I use scripts in Projucer. And with now success. Please help me.

Now I want to have any string variable or macro that keep my git current branch.
I try to do that for 2 hours and no success.

My last try was to use that line:
GIT_NAME=echo $git branch --show-current

Inside the box “Extra Preprocessor Definitions” in Projucer.

But I get error.

For any help great thanks in advance,
Best Regards

I also tried that:
${GIT_NAME =echo $git branch --show-current}

Inside box “Extra Compiler Flags”. But it also doesn’t work.

I don’t think there’s a way of doing this directly from the Projucer.

The simplest way is probably to use a pre-build script that generates a .cpp file containing a constant string (const char* currentGitBranchName = ...) representing the branch name, and to declare extern const char* currentGitBranchName; in one of your headers.

To avoid unnecessary rebuilds/links, your pre-build script might check the current contents of the file and avoid modifying it if the git branch hasn’t changed.

1 Like

Hello reuk,
great thanks for your reply.

Yes I’ve searched whole world internet :slight_smile:
And I found many advices exactly as yours, to create with script the file containing string with branch name.

But the problem is I can’t find exactly what command should I use and exactly where in Projucer should I paste it? I can’t even find any tutorial how to write such scripts. So I don’t even know what syntax should be used.

I tired to use everything was shown here:

I pasted code to Projucer. But after all I always have errors. I’ve already spent about 4 hours to solve that, but still with no success.

I’ve also was tying solutions showed here:

But also with no success.

Probably I use wrong syntax and in wrong place of Projucer.

Are you building on multiple platforms? If so, there may not be a single answer. On Windows, you’d probably need a Batch script to do it, and you might use sh or bash on macOS/Linux. Alternatively, you could ensure that your build machine has Python installed, and then use that.

This sort of thing is one reason that developers use tools like CMake. CMake is a scripting language as well as a build system, so it can be used to implement these sorts of custom behaviours.

In one project, I used the following setup to add a git hash to my builds (branch name could be achieved similarly):

#include <git_hash.hpp>

char const* git::hash = "@GIT_HASH@";
char const* git::version = "@PROJECT_VERSION@";


#pragma once

namespace git {
extern char const *hash;
extern char const *version;
} // namespace git


add_library(git_version_info STATIC)


  "${GIT_EXECUTABLE}" describe --match=NeVeRmAtCh --always --abbrev=40 --dirty

    "${CMAKE_CURRENT_BINARY_DIR}/git_hash.cpp" @ONLY)

target_sources(git_version_info PRIVATE

target_include_directories(git_version_info PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")

target_compile_features(git_version_info PUBLIC cxx_std_17)

set_target_properties(git_version_info PROPERTIES

This creates a static library that just defines git and project info strings. This library can be linked into targets that require this information.

Hello reuk,
great great great thanks for your detailed help.

Unfortunately it doesn’t help me. At the moment I have only VS available to test. Later I will try on my Mac in my home.

By the way it still looks very complicated for me.

While I just need to call in command line:
git branch --show-current

And then the result I need to assign to some macro like #define CURRENT_BRANCH or anything like that. So do I really need so much line of code to do do that?

Best Regards

I have an idea for a very naive approach. You can generate a header with a following content

echo "#define CURRENT_BRANCH \"$(git branch --show-current)\"" > branch.h

and include it

#include "branch.h"

Szemek, great thanks I will test it for a moment.

I’ve already found my own script, but due to fact I don’t know the syntax, my script is much longer but probably do the same as your one line. My looks like that:

git branch --show-current >> temp_file.h
set /p branchname=<temp_file.h
set branchmacro=#define BRANCH_NAME %branchname%
echo #pragma once >> git_data.h
echo %branchmacro% >> git_data.h
del temp_file.h

But the problem is I still have no idea how to use it in Projucer, to force Projucer to launch those lines while opening project in VS from Projucer?

Szemek, unfortunately your line of code doesn’t work. It produces branch.h file, but inside of that file I get line:
"#define CURRENT_BRANCH \"$(git branch --show-current)\""

So there is not my current branch name, there is only git command printed.
I tried to remove quotation marks, but it desn’t help.

But actually it doesn’t matter while I have my own lines which work fine. But I have no idea how to use that from Projucer.

And the second question is, why do you say it’s very naive approach? What’s wrong with that?

OK finally I’ve done that.
I created myScript.bat file.

And inside that file I added those lines:

git branch --show-current >> temp_file.h
set /p branchname=<temp_file.h
set branchmacro=#define BRANCH_NAME %branchname%
echo #pragma once >> git_data.h
echo %branchmacro% >> git_data.h
del temp_file.h

And then in Projucer project settings in field “Post-Export Shell Command (Windows)” I added line:

And finally it works as I expect. But I spent whole day to do such small thing. And still somebody say it’s naive approach. So I am really sad, but still happy I found the solution :slight_smile:

I tested my one-liner in zsh shell on macOS, but command prompt or PowerShell on Windows have different interpolations - that’s why my one-liner didn’t work for you.

Answering your second question - nothing is wrong with naive approach. If it works for you, then it works for you. I’m glad that you found the solution :slight_smile:

Well, I wrote something different :wink: I was describing only my idea, not yours.

Hey great thanks for your help.

But it looks like I have another problem. Now when I have the current branch name I don’t know how to compare strings in macro defining.

I know I can use strcmp but actually it gives me result during runtime. But I want to know it in compile time.

I tried that:
#if strcmp(BRANCH_NAME, “master”) == 0

But of course it doesn’t work.

OK, I’ve just realised I can compare strings directly inside bat file with script.