0

Environment/Background:

zsh (v5.9)
git (v2.48)
git --exec-path results in /usr/lib/git-core
Other StackOverflow posts read:

Problem

I'd like to set up an alias that prints detailed information about the branches in a repo; something that combines git branch -vv --list --all with git for-each-ref --sort=-creatordate refs/heads/ --format='%(creatordate:short) %(refname:short)'. After reading this, I came up with this:

[alias]
  bls = '!f() { git branch -vv --list --all && echo \"\nSorted by creation date:\" && git for-each-ref --sort=-creatordate refs/heads/ --
format=\"%(creatordate:short) %(refname:short)\"; }; f'

That results in expansion of alias 'bls' failed; 'git' is not a git command.

I'm flummoxed because other aliases work, as well as other aliases using the !git <command> && <command> syntax. But not this syntax: '!f() { git && ; }; f'. Other examples of syntax I've tried:

[alias]
  foo = '!f() { git version && echo foo; }; f'   # results in error "fatal: bad alias.foo string: unclosed quote

  bar = bar = "!f() { git version; echo qux; }; f" # works!

  bls = "!f() { git branch -vv --list --all && echo \"\nSorted by creation date:\" && git for-each-ref --sort=\"-creatordate refs/heads/\" --format=\"%(creatordate:short) %(refname:short)\"; }; f" # error: expansion of alias 'bls' failed; 'git' is not a git command

Edit: I've gotten it to work with this answer to a similar question, but I can't help but wonder if it's possible to do this in one line. Here's my current solution:

[alias]
bls = ! "                   \                              
  f () {                  \                                
  git branch -vv --list --all && \                         
  echo \"\nSorted by creation date:\" && \                 
  git for-each-ref --sort=\"-creatordate refs/heads/\" \   
  --format=\"%(creatordate:short) %(refname:short)\"; \    
  }; \                                                     
  f"                                                       

Edit 2: @AlexHowansky was right. Double quoting the full string works. The troubleshooting process was confounded by the presence of both global and locally scoped aliases with the same name. The one line version of the above would be:

bls = ! "f () { git branch -vv --list --all && echo \"\nSorted by creation date:\" && git for-each-ref --sort=-creatordate refs/heads/ --format=\"%(creatordate:short) %(refname:short)\"; }; f"

But a better version would be:

bls = ! "git for-each-ref --sort=-creatordate refs/heads refs/remotes --format='%(creatordate:short)  %(objectname:short)  %(align:40)%(refname:short)%(end)  %(contents:subject)'"                                                                                                               


5
  • 1
    Your example works fine for me if I just change the outer single quotes to double. Commented Jan 15 at 21:41
  • 1
    you can use single quotes if you're adding the alias from the terminal via git config --local alias.foo '!f() { git version && echo foo; }; f' - otherwise replace your single quotes with double quotes Commented Jan 15 at 21:44
  • @AlexHowansky when I change bls = '!f() { git branch -vv --list --all && echo \"\nSorted by creation date:\" && git for-each-ref --sort=-creatordate refs/heads/ -- format=\"%(creatordate:short) %(refname:short)\"; }; f' to bls = "!f() { git branch -vv --list --all && echo \"\nSorted by creation date:\" && git for-each-ref --sort=-creatordate refs/heads/ --format=\"%(creatordate:short) %(refname:short)\"; }; f", I still receive an error: expansion of alias 'bls' failed; 'git' is not a git command Commented Jan 15 at 22:30
  • I can paste this string from your comment directly into my .gitconfig and it works fine in both bash and zsh. <shrug> Perhaps you've got another alias on a previous line with an unclosed quote? Commented Jan 15 at 22:39
  • @AlexHowansky Yes, you're right. It's perfectly valid. I think my troubleshooting was confounded by the presence of both global and locally scoped aliases with the same name. Going to close this question. Thank you for your help. Commented Jan 15 at 22:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.