A correction on My first git rebase --exec

, .

On , I published My first git rebase --exec, which contained two git rebase --exec command with a multi-line script argument. Today I tried to re-run the second one on a different chain of commits, and discovered to my dismay that it doesn’t actually work! I had “cleaned up” the command for the blog post, which included splitting up the long x; y; z && foo command into multiple lines – but it turns out that the script argument cannot span multiple lines. git rebase --exec really does nothing other than inserting exec lines into a git rebase --interactive script: if the argument contains line breaks, they will be interpreted as additional rebase script commands, and Git will complain that it doesn’t know these commands (it’s expecting pick, squash, etc.).

One solution for this is to not split up the script after all:

git rebase --exec 'cd ../.. && php tests/phpunit/phpunit.php extensions/WikibaseQualityConstraints/tests/phpunit/' master
and (brace yourselves)
git rebase --exec 'composer fix; GIT_PAGER=cat git diff; if ! git diff-index --quiet HEAD --; then read -p "ok? " -r ok; if [ "$ok" = ok ]; then; git commit --all --amend --no-edit; fi; fi' master
But while this works in the shell, it’s clearly terrible for a blog post (overfull hbox, badness 10000). Much nicer: write the script to a file and use that for the command.

cat > run-tests <<- 'EOF'
	#!/bin/sh
	cd ../.. &&
	php tests/phpunit/phpunit.php \
	    extensions/WikibaseQualityConstraints/tests/phpunit/
EOF
chmod +x run-tests
git rebase --exec ./run-tests master
cat > fix-commit <<- 'EOF'
	#!/bin/sh
	composer fix
	GIT_PAGER=cat git diff
	if ! git diff-index --quiet HEAD --; then
	    read -p "ok? " -r ok
	    if [ "$ok" = ok ]; then
	        git commit --all --amend --no-edit
	    fi
	fi
EOF
chmod +x fix-commit
git rebase --exec ./fix-commit master

This has the added benefit that you can later just run git rebase --exec ./fix-commit master without having to dig up the whole script from your own blog post 😉