2

Maybe it might be a strange need, but I believe it would be helpful to have something like this:

if($branch$ === "master) {
   // Special code for the master branch
} else {
   // Something special for tests. Like extended logging
}

In SVN this was no problem and I'm aware that GIT don't have a keyword expansion system, anyway, it's still interesting to find a solution for this.

Joche
  • 346
  • 7
  • 17
  • A file could be present in many branches, each might have a different commit id, because of different changes in the same file in different branches. – brokenfoot Apr 08 '14 at 19:42
  • Your question is somewhat vague, can you please give an example what you want to make different depending on the branch name? – try-catch-finally Dec 22 '14 at 15:55

3 Answers3

3

No, all meta file information is stored under .git in the root directory. You can detect the current branch with the following bash command (code modified, but originally found here):

function parse_git_branch() {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/\1/"
}
SomeKittens
  • 38,868
  • 19
  • 114
  • 143
1

Git has not keyword expansion but Git has filters:

You would set up a smudge filter that gets run every time you checkout the file to your working copy. In this filter, you can check the current branch and do some magic stuff in your sources (original images taken from (outdated) http://git-scm.com/book/ch7-2.html, now http://git-scm.com/book/en/v2/Customizing-Git-Git-Attributes#Keyword-Expansion):

enter image description here

You would also set up a clean filter that gets run every time the file is staged:

enter image description here

Of course, you would set up the filter not for ALL files you're using but only for a single one that gets included by the other files of your project.

This would be the Git way to do what you want.

eckes
  • 64,417
  • 29
  • 168
  • 201
0

If you need that information in hooks, the usual way is to not rely on a porcelain command, but on plumbing ones:

#!/bin/sh
branch=$(git rev-parse --symbolic --abbrev-ref HEAD)
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250