TL;DR
How do I reference a .gitconfig
config value in another config value?
.gitconfig
[filter "myFilter"]
foo = Hello world
bar = echo $(git config filter.myFilter.foo)
bash
$(git config filter.myFilter.bar)
# should print "Hello world"
My problem
I am trying to write some smudge
/clean
filters (a la Can git automatically switch between spaces and tabs?) so that on checkout, tabsizes are set to 4 spaces, and on commit, they're reverted to what's already in the repo.
The SO answers suggest this:
git config --global filter.tabspace.smudge 'unexpand --tabs=4' git config --global filter.tabspace.clean 'expand --tabs=4'
Or the equivalent .gitconfig
file:
[filter "tabspace"] smudge = 'unexpand --tabs=4' clean = 'expand --tabs=4'
This works fine if you know that all of your remote repos use tabs. I want to generalize it (and in the process, learn more about git config
), so I can set a value, and the values in ~/.gitconfig
will do the rest.
Desired behavior
My ~/.gitconfig
looks like
#...
[filter "tabspace"]
repoTabs = false
repoTabSize = 4
smudge = # TODO
clean = # TODO
#...
When I clone a repo that uses tabs or non-4 spaces, I run
# for repos that use tabs
git config filter.tabspace.repoTabs true
# for repos that use 2 spaces
git config filter.tabspace.repoTabSize 2
and then create a file at .git/info/attributes
that looks like
*.js filter=tabspace
*.jsx filter=tabspace
*.ts filter=tabspace
*.tsx filter=tabspace
*.json filter=tabspace
Partial solution
The scripts I want to use for smudge
and clean
should be relatively straight forward (I've written these each on single lines, in my ~/.gitconfig
file in place of the two # TODO
s):
# smudge
if [ `#filter.tabspace.repoTabs` == true ]; then
expand --tabs=4
else
unexpand --tabs=`#filter.tabspace.repoTabSize` | expand --tabs=4
fi
# clean
if [ `#filter.tabspace.repoTabs` == true ]; then
unexpand --tabs=4
else
unexpand --tabs=4 | expand --tabs=`#filter.tabspace.repoTabSize`
fi
What I can't for the life of me figure out is how to get the values of filter.tabspace.repoTabs
and filter.tabspace.repoTabSize
to work within the script.
My method for testing is setting smudge = #my-script-here
, and then running
$(git config filter.tabspace.smudge)
where #my-script-here
has been things like echo $(git config filter.tabspace.repoTabSize)
with dozens of attempts to enclose various parts of the value with single quotes, double quotes, escaped double-quotes, and backticks instead of $()
. Everything I've tried either evaluates $(git config filter.tabspace.repoTabSize)
literally or fails outright.
I also tried simply using repoTabSize
, hoping it would just insert the scoped variable, but no such luck.
As a sanity check:
git config filter.tabspace.repoTabSize 2
echo $(git config filter.tabspace.repoTabSize)
# prints
2
I also checked that piping a value into an if
statement does what I need it to, assuming that git checkout
and git commit
pipe the files through the values of filter.tabspace.smudge
and filter.tabspace.clean
respectively.
echo -e '\t'foo | if [ true ]; then expand --tabs=4; fi
# prints (with 4 spaces):
foo