|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Pull Makefile, scripts/ and .github/ from the template repo into the |
| 4 | +# current repo, so it can be reviewed with `git diff` and committed by hand. |
| 5 | +# |
| 6 | +# The repo's PROJECT_NAME line in the Makefile is preserved. Files that only |
| 7 | +# exist in this repo (extra scripts, extra workflows) are left alone. |
| 8 | +# |
| 9 | +# Override the source with TEMPLATE_REPO=<url-or-path> (default: github master). |
| 10 | + |
| 11 | +set -e |
| 12 | + |
| 13 | +TEMPLATE_REPO="${TEMPLATE_REPO:-https://github.com/bitplane/example-python-project.git}" |
| 14 | + |
| 15 | +if [ ! -f Makefile ] || [ ! -d .git ]; then |
| 16 | + echo "Run this from the root of a git repo with a Makefile" >&2 |
| 17 | + exit 1 |
| 18 | +fi |
| 19 | + |
| 20 | +if ! git diff --quiet Makefile scripts .github 2>/dev/null; then |
| 21 | + echo "warning: Makefile/scripts/.github have uncommitted changes; the diff will mix them" >&2 |
| 22 | +fi |
| 23 | + |
| 24 | +# Remember this repo's identity |
| 25 | +project_name=$(sed -n 's/^PROJECT_NAME := //p' Makefile) |
| 26 | + |
| 27 | +# Fetch the template |
| 28 | +TMP_DIR=$(mktemp -d) |
| 29 | +cleanup() { |
| 30 | + rm -rf "$TMP_DIR" |
| 31 | +} |
| 32 | +trap cleanup EXIT |
| 33 | + |
| 34 | +echo "Fetching template from $TEMPLATE_REPO..." |
| 35 | +git clone --quiet --depth=1 "$TEMPLATE_REPO" "$TMP_DIR" |
| 36 | + |
| 37 | +# Copy template-owned files over ours, leaving repo-specific extras in place |
| 38 | +cp "$TMP_DIR/Makefile" Makefile |
| 39 | +mkdir -p scripts .github |
| 40 | +cp -r "$TMP_DIR/scripts/." scripts/ |
| 41 | +cp -r "$TMP_DIR/.github/." .github/ |
| 42 | + |
| 43 | +# Restore this repo's identity |
| 44 | +if [ -n "$project_name" ]; then |
| 45 | + sed -i "s/^PROJECT_NAME := .*/PROJECT_NAME := $project_name/" Makefile |
| 46 | +fi |
| 47 | + |
| 48 | +echo "Template applied. Review with: git diff Makefile scripts .github" |
0 commit comments