git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Two potential bugs in aliases that expand to shell commands

From
HHan <laughinghan@gmail.com>
Date
Apr 4, 2013, 02:05 UTC
Message-ID
<CACE=nd1dwhCyrKdzFq7O4UgD1TH8-c5R+4L2rH0NGW-jWqLr_g@mail.gmail.com>
These might just be documentation bugs.

Aliases that expand to shell commands are only documented fairly superficially, but `git help config` does say: "If the alias expansion is prefixed with an exclamation point, it will be treated as a shell command."

It also says, nearer the beginning: "String values may be entirely or partially enclosed in double quotes. You need to enclose variable values in double quotes if you want to preserve leading or trailing whitespace, or if the variable value contains comment characters (i.e. it contains # or ;)."

There appears to be another case string values need to be enclosed in quotes, which is a shell command where you want to preserve quote characters (not leading or trailing); a minimal example is

  shortcut = !cd "" && pwd
will act like you ran, in bash,
$ cd  && pwd
and print your homedir path, whereas
  shortcut = !"cd \"\" && pwd"
will act like you ran, in bash,
$ cd "" && pwd

and stay in the top-level directory of the repo (which is where aliases that expand to shell commands are run from).

This is problematic precisely because aliases that expand to shell commands are run from the top-level directory of the repo: if you had an alias that worked great at the top-level directory, like

  shortcut = !do_something

but it was doing the wrong thing in subdirectories, my first instinct, at least, upon reading "Note that shell commands will be executed from the top-level directory of a repository, which may not necessarily be the current directory. GIT_PREFIX is set as returned by running git rev-parse --show-prefix from the original current directory.", is to do

  shortcut = !cd "$GIT_PREFIX" && do_something

which will now do the right thing in subdirectories, but at the top-level directory of the repo, $GIT_PREFIX is undefined/the empty string, and it cds to your homedir.

The other bug I'm much more confused by. If you have an alias like
  shortcut = !"echo -n lol; echo wut"
it will, in fact, print

-n lol wut

which is, uh, not what bash prints. Is git special-casing echo?

(I discovered these while adding something semi-jokingly to my .gitconfig: https://github.com/laughinghan/dotfiles/commit/34f5528825b287ff40acfe57808b32931a87261c )

Han
Next: Jeff King
Message 1 of 2 in “Two potential bugs in aliases that expand to shell commands”
  1. HanApr 4, 2013
  2. Jeff KingApr 4, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.