-1

I am trying to create a git alias that would output the commands to build my git aliases

So far I have this

git config --global --get-regexp alias.* | sed 's/\(alias\.[^ ]\+\) \(.*\)/git config --global \1 \2/'

which yields

git config --global alias.lg log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
git config --global alias.pu !git push --set-upstream origin $(git rev-parse --abbrev-ref HEAD)
git config --global alias.puf !git push --force --set-upstream origin $(git rev-parse --abbrev-ref HEAD)
git config --global alias.r !git fp && GIT_SEQUENCE_EDITOR=true git rebase -i --autosquash origin/HEAD
git config --global alias.fr !git fp && git pull --rebase origin HEAD
git config --global alias.fp fetch --prune
git config --global alias.ac !git add -A && git commit
git config --global alias.amend commit --amend -a --reuse-message=HEAD
git config --global alias.remaster rebase -i origin/HEAD
git config --global alias.rh rehead
git config --global alias.rehead !git fetch && git checkout origin/HEAD -b

But I want it so that the text after the config name would be quoted. e.g.

git config --global alias.rehead '!git fetch && git checkout origin/HEAD -b'

I got as far as

git config --global alias.aliases '!'"git config --global --get-regexp alias.* | sed 's/\(alias\.[^ ]\+\) \(.*\)/git config --global \1 '\''\2'\''/'"

but that last one that gets generated does not work correctly I think it's missing a quote somewhere

git config --global alias.aliases '!git config --global --get-regexp alias.* | sed 's/\(alias\.[^ ]\+\) \(.*\)/git config --global \1 '\''\2'\''/''
Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265
  • If you single quoting something within single quotes use this sequence `'\''` this ends the existing single quote, inserts a quoted single quote and opens another single quote. – potong Oct 22 '20 at 14:30
  • Use double quotes as opposed to single quotes around your sed statement and then you should be able to utilise single quotes in your substituted text. – Raman Sailopal Oct 22 '20 at 14:32
  • Voting to close because the answer for single quote is done. Even if it does not address the final part of my use case which is to use the output as part of another git config. – Archimedes Trajano Oct 22 '20 at 15:15

1 Answers1

0

Since you command uses singe quotes, use '\'' around the parameters to wrap them in quotes;

... config --global '\''\1 \2'\''/'
git config --global --get-regexp alias.* | sed 's/\(alias\.[^ ]\+\) \(.*\)/git config --global '\''\1 \2'\''/'

More info about single quotes escape: How to escape single quote in sed?


Small output example:

$ git config --global alias.co checkout      # Create simple alias
$ git config --global --get-regexp alias.* | sed 's/\(alias\.[^ ]\+\) \(.*\)/git config --global '\''\1 \2'\''/'

git config --global 'alias.co checkout'
0stone0
  • 34,288
  • 4
  • 39
  • 64