Home > OS >  git config alias adding slashes
git config alias adding slashes

Time:09-14

I'm creating an install script for a load aliases and git config is not playing nice with them

Here is the command in the shell script

git config --global alias.sync-fork '"!f() { oldhash="$(git rev-parse -q --verify refs/stash)"; (git fetch --all && git stash --include-untracked) && ( (git checkout "$(git default-branch)" && git pull && git merge upstream/"$(git default-branch)" && git push && git checkout -); [ "$(git rev-parse -q --verify refs/stash)" != "$oldhash" ] || git stash pop) }; f"'

When I run it and look in the ~/.gitconfig file, I see "\" everywhere and it doesn't work. Using echo, I can see the string I want. This is driving me crazy.

CodePudding user response:

The most immediate problem is that you have too many quotes; '"..."' should just be '...' without the "s inside. However, I can't guarantee that you won't have more issues after you fix that one.

Instead of trying to escape your shell function by hand, have the shell itself do it for you using declare -f to serialize your function.

f() {
  oldhash="$(git rev-parse -q --verify refs/stash)"
  (git fetch --all &&
   git stash --include-untracked
  ) && (
   (git checkout "$(git default-branch)" &&
    git pull &&
    git merge upstream/"$(git default-branch)" &&
    git push &&
    git checkout -)
   [ "$(git rev-parse -q --verify refs/stash)" != "$oldhash" ] || git stash pop)
}
git config --global alias.sync-fork '!'"$(declare -f f); f"

This does mean git will add some extra escapes to fit the function into proper config-file form, but when you use declare -f the output is guaranteed to be well-formed (at least if your /bin/sh is provided by bash).

  • Related