1#!/usr/bin/env bash 2 3set -euo pipefail 4 5# https://stackoverflow.com/a/246128/6605742 6SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) 7 8# Allows using a local directory for temporary files, 9# which can then be inspected after the run 10if (( $# > 0 )); then 11 tmp=$(realpath "$1/tmp") 12 if [[ -e "$tmp" ]]; then 13 rm -rf "$tmp" 14 fi 15 mkdir -p "$tmp" 16else 17 tmp=$(mktemp -d) 18 trap 'rm -rf "$tmp"' exit 19fi 20 21# Tests a scenario where two poorly formatted files were modified on both the 22# main branch and the feature branch, while the main branch also did a treewide 23# format. 24 25git init "$tmp/repo" 26cd "$tmp/repo" || exit 27git branch -m main 28 29# Some initial poorly-formatted files 30cat > a.nix <<EOF 31{ x 32, y 33 34, z 35}: 36null 37EOF 38 39cat > b.nix <<EOF 40{ 41 this = "is"; 42 43 44 some="set" ; 45 } 46EOF 47 48git add -A 49git commit -m "init" 50 51git switch -c feature 52 53# Some changes 54sed 's/set/value/' -i b.nix 55git commit -a -m "change b" 56sed '/, y/d' -i a.nix 57git commit -a -m "change a" 58 59git switch main 60 61# A change to cause a merge conflict 62sed 's/y/why/' -i a.nix 63git commit -a -m "change a" 64 65cat > treefmt.toml <<EOF 66[formatter.nix] 67command = "nixfmt" 68includes = [ "*.nix" ] 69EOF 70git add -A 71git commit -a -m "introduce treefmt" 72 73# Treewide reformat 74treefmt 75git commit -a -m "format" 76 77echo "$(git rev-parse HEAD) # !autorebase treefmt" > .git-blame-ignore-revs 78git add -A 79git commit -a -m "update ignored revs" 80 81git switch feature 82 83# Setup complete 84 85git log --graph --oneline feature main 86 87# This expectedly fails with a merge conflict that has to be manually resolved 88"$SCRIPT_DIR"/../run.sh main && exit 1 89sed '/<<</,/>>>/d' -i a.nix 90git add a.nix 91GIT_EDITOR=true git rebase --continue 92 93"$SCRIPT_DIR"/../run.sh main 94 95git log --graph --oneline feature main 96 97checkDiff() { 98 local ref=$1 99 local file=$2 100 expectedDiff=$(cat "$file") 101 actualDiff=$(git diff "$ref"~ "$ref") 102 if [[ "$expectedDiff" != "$actualDiff" ]]; then 103 echo -e "Expected this diff:\n$expectedDiff" 104 echo -e "But got this diff:\n$actualDiff" 105 exit 1 106 fi 107} 108 109checkDiff HEAD~ "$SCRIPT_DIR"/first.diff 110checkDiff HEAD "$SCRIPT_DIR"/second.diff 111 112echo "Success!"