Handy Makefile Rules and Patterns

3 January 2019

Patterns

Required variable

$(or $(build),$(error Must set build variable))

Optional variable with fallback

$(or $(branch),master)

Git

Rebase on origin’s master

rebase_origin_master:
	git pull --rebase origin master

Create a git commit message template with co-authored-by set

# Requires card=…, name=…, and a contributors.txt file
pair:
	@echo "$(card) \n\nCo-authored-by: $(shell grep -i $(name) contributors.txt)" > .git/.gitmessage
	@git config commit.template .git/.gitmessage
	@cat .git/.gitmessage

Rails

Run the most recently modified spec

recent_spec:
	bundle exec rspec $(or $(shell find ./spec -name "*_spec.rb" -cmin -5 -exec ls -1tr "{}" + | tail -n 1),$(error No *_spec.rb file modified recently))

Run specs with changes not yet committed

uncommitted_specs:
	bundle exec rspec $(shell git status --porcelain | grep -E "^[^D][^D] .+_spec.rb$" | cut -c4-)