#!/usr/bin/env bash
# Exercise 15.5: a merge conflict made on purpose, and resolved.
set -e
export GIT_AUTHOR_NAME="Sara Khan" GIT_AUTHOR_EMAIL="sara@example.com"
export GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME" GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL"
export GIT_CONFIG_GLOBAL=/dev/null  # ignore your own Git settings while the script runs
run() { local s="\$"; for a in "$@"; do if [[ $a == *" "* ]]; then s+=" \"$a\""; else s+=" $a"; fi; done; echo "$s"; "$@"; }
cd "$(mktemp -d)"
git init -q -b main
printf '<h1>Sara Khan</h1>\n<p>Web developer</p>\n' > index.html
git add . && git commit -q -m "Add profile page"

echo "# The same line, changed differently on two branches:"
run git switch -q -c tagline
printf '<h1>Sara Khan</h1>\n<p>Frontend developer from Alexandria</p>\n' > index.html
run git commit -q -am "Tagline: frontend"
run git switch -q main
printf '<h1>Sara Khan</h1>\n<p>Junior web developer</p>\n' > index.html
run git commit -q -am "Tagline: junior"

run git merge tagline || true
run cat index.html

echo "# Resolve: keep the right text and delete the three marker lines (<<<<<<<, =======, >>>>>>>)."
printf '<h1>Sara Khan</h1>\n<p>Junior frontend developer from Alexandria</p>\n' > index.html
run git add index.html
run git commit -q -m "Merge tagline: keep both ideas"
run git log --oneline --graph
run cat index.html
