Search code examples
gitvariablesgit-config

Git: Referencing of variables from .gitconfig in .gitconfig possible?


Is it possible to use a variable from .gitconfig in another definition in .gitconfig?

In bash I can do:

delimiter="^"
log_res="log --graph --pretty=format:'$delimiter%cr$delimiter%cs$delimiter%h'"

what results in

log --graph --pretty=format:'^%cr^%cs^%h'

Is the same possible in .gitconfig to achieve the result alias???

[alias]
      result    = log --graph --pretty=format:'^%cr^%cs^%h'
      delimiter = "^"
      log-res   = ??? log --graph --pretty=format:'DELIMITER%crDELIMITER%csDELIMITER%h' ???

How do I have to write 'log-res' that a change of the delimiter is taken over? Could this be done nicely?

I know I can do something like this what is ugly if you define a lot of log commands:

[alias]
      result    = log --graph --pretty=format:'^%cr^%cs^%h'
      delimiter = "^"    
      log-res   = !bash -c '"git $(echo \"log --decorate=short --graph --pretty=format:'$(git config alias.delimiter)%cr$(git config alias.delimiter)%cs$(git config alias.delimiter)%h'\")"'

where git result is the same like git log-res, but this horrible to read, isn't it?


Solution

  • No, it's not possible to reference other settings in a .gitconfig. In general, this would be tricky, because of the inheritance rules: if the value changed in the repo local config, would an alias in the global config use the version in the global config or the repo local config?

    As you've noticed, you can indeed use sh to do this, but it can be a little simpler:

    log-res = "!f() { d=\"$(git config alias.delimiter)\"; git log --graph --pretty=\"format:$d%cr$d%cs$d%h\" \"$@\"; };f"
    

    You may want to store the value somewhere other than under the alias section, so that your shell completion doesn't try to complete git delimiter, which wouldn't work.