I'm trying to make config file branch dependant : I have three branches on my repo (master, stable and testing). In that repo there is pom.xml file that have to depend on the branch (see that as a config file). If I use only a pom.xml file and that I merge stable on master I lost the config on master.
I already tried the solution here : Best practice - Git + Build automation - Keeping configs separate but without success.
I have untracked the pom.xml file and I added three file : pom.xml.master, pom.xml.stable and pom.xml.testing. The goal is to make a smudge filter that is able to detect the current branch and copy/rename the file : If I'm on the stable branch then it copied the pom.xml.stable into pom.xml.
I have the impression that the smudge filter only runs on the files that we define in the .gitattribute file. That is, the scope of the filter is the content of the file and it loops over all files without any context (path, filename, etc.). If we do indentation tasks or something else it works since we care about the content. But in our case this is not what we want.
So we can't make a script with a more "global" scope that will look for the right pom.xml according to its name (if we are on the testing branch then we take pom.testing) and generate the file properly.
Update : It was a problem with the encoding of the file. The .gitattributes wasn't taken into account so the smudge script was never triggered. Here is my script to modify pom.xml depending on the branch:
#!/bin/sh
set -e
branch=$(git rev-parse --symbolic --abbrev-ref HEAD)
# we have to read the content of the file otherwise, the file become empty
while IFS= read -r line
do
echo "$line"
done
#take the pom depending on the branch
if [ -e "pom.xml.$branch" ]
then
cp "pom.xml.$branch" "pom.xml"
else
# sometimes, the file isn't download yet, so we have to check the existence
if [ -e "pom.xml.master" ]
then
#by default : take master
cp "pom.xml.master" "pom.xml"
fi
fi
I had to do a "hack" because the content of the file was "passed" in parameter in the script (like I explained before). So to make it working I had to do a loop to handle that text otherwise the file became empty (because we do "nothing" with the input).
But now I face to another problem. I thought that the smudge filter was triggered at each "git checkout" command (including branch changing) but it's not the case. I checked the .gitattributes documentation on Git and it seems to be triggered only the first time we checkout the project. When I checkout to another branch nothing happens... That's not the behavior I want. I saw that if we deleted the index file in the .git folder it forces the smudge to run again but I'm not able to make it working properly. Is there a property or something to make it working? Thank a lot for your help !