Makefile: make actually work

This commit is contained in:
Ryan Heywood 2024-01-18 18:47:09 -05:00
parent 8e24555d09
commit ab53b3e495
Signed by: ryan
GPG Key ID: 8E401478A3FBEF72
1 changed files with 25 additions and 12 deletions

View File

@ -1,24 +1,37 @@
BASE_REF ?= main BASE_REF ?= main
HEAD_REF ?= $(shell git rev-parse HEAD) HEAD_REF ?= HEAD
define clone-repo
mkdir -p $(1)
git -C $(1) init
git -C $(1) remote add origin $(2) || true
git -C $(1) fetch origin $(3)
git -C $(1) -c advice.detachedHead=false checkout $(3)
test `git -C $(1) rev-parse HEAD` = $(3)
endef
.PHONY: review .PHONY: review
review: review:
@echo "Ensuring Cargo.lock is in clean state, not modified" $(eval BASE_REF_PARSED := $(shell git rev-parse $(BASE_REF)))
test -z "$(shell git status --porcelain | grep -E 'M +Cargo.lock')" $(eval HEAD_REF_PARSED := $(shell git rev-parse $(HEAD_REF)))
@echo "Ensuring current HEAD_REF is not BASE_REF" @echo "Ensuring current HEAD_REF is not BASE_REF"
test "$(shell git rev-parse $(BASE_REF))" != "$(HEAD_REF)" test "$(BASE_REF_PARSED)" != "$(HEAD_REF_PARSED)"
@echo "Verifying if any changes happened in Cargo.lock that require review" @echo "Verifying if any changes happened in Cargo.lock that require review; otherwise, use `git difftool` directly"
test "$(shell git show $(BASE_REF):Cargo.lock | sha256sum)" != "$(shell git show $(HEAD_REF):Cargo.lock | sha256sum)" test "$(shell git show $(BASE_REF_PARSED):Cargo.lock | sha256sum)" != "$(shell git show $(HEAD_REF_PARSED):Cargo.lock | sha256sum)"
$(eval TEMP_REPO := $(shell mktemp -d)) $(eval TEMP_REPO := $(shell mktemp -d))
git -C $(TEMP_REPO) init $(call clone-repo,$(TEMP_REPO),$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST)))),$(BASE_REF_PARSED))
# Unstage current commit before refreeze
git -C $(TEMP_REPO) reset --hard 'HEAD^1'
# Fetch files for BASE_REF # Fetch files for BASE_REF
git show $(BASE_REF):Cargo.lock > $(TEMP_REPO)/Cargo.lock cd $(TEMP_REPO) && CARGO_HOME=$(TEMP_REPO)/cargo-home cargo fetch
CARGO_HOME=$(TEMP_REPO)/cargo-home cargo fetch
git -C $(TEMP_REPO) add . git -C $(TEMP_REPO) add .
git -C $(TEMP_REPO) commit --no-gpg-sign -m "base ref: $(BASE_REF)" git -C $(TEMP_REPO) commit --no-gpg-sign -m "base ref: $(BASE_REF)"
# git -C $(TEMP_REPO) ls-files -z | (cd $(TEMP_REPO); xargs -0 -n1 rm)
git -C $(TEMP_REPO) fetch origin $(HEAD_REF_PARSED)
git -C $(TEMP_REPO) merge --no-commit $(HEAD_REF_PARSED)
rm -rf $(TEMP_REPO)/cargo-home
# Fetch files for HEAD_REF # Fetch files for HEAD_REF
git show $(HEAD_REF):Cargo.lock > $(TEMP_REPO)/Cargo.lock cd $(TEMP_REPO) && CARGO_HOME=$(TEMP_REPO)/cargo-home cargo fetch
CARGO_HOME=$(TEMP_REPO)/cargo-home cargo fetch
git -C $(TEMP_REPO) add -A . git -C $(TEMP_REPO) add -A .
git -C $(TEMP_REPO) difftool HEAD || true git -C $(TEMP_REPO) difftool HEAD || true
rm -rf $(TEMP_REPO) # rm -rf $(TEMP_REPO)