diff --git a/Makefile b/Makefile index fc7eb4b8..a67ccffa 100644 --- a/Makefile +++ b/Makefile @@ -263,7 +263,7 @@ bin/$(TFGEN): provider/*.go provider/go.* .make/upstream # Apply patches to the upstream submodule, if it exists upstream: .make/upstream # Re-run if the upstream commit or the patches change -.make/upstream: $(wildcard patches/*) $(wildcard .git/modules/upstream/HEAD) +.make/upstream: $(wildcard patches/*) $(shell ./upstream.sh file_target) ifneq ("$(wildcard upstream)","") ./upstream.sh init endif diff --git a/upstream.sh b/upstream.sh index ce90039c..736b9854 100755 --- a/upstream.sh +++ b/upstream.sh @@ -23,6 +23,7 @@ COMMANDS check_in Write checkedout commits back to patches, add upstream and patches changes to the git staging area and exit checkout mode. + file_target Print a file path to depend on in make. help Print this help message, plus examples. OPTIONS @@ -334,6 +335,23 @@ re-initializing using updated patches and updated upstream base. EOF } +# file_target prints a file path to depend on in make to trigger an init when required. +# Also updates the file timestamp if the submodule needs updating. +file_target() { + path=.git/modules/upstream/HEAD + # Don't print a file if it doesn't exist - it's probably not initialized yet. + if [[ ! -f "${path}" ]]; then + exit 0 + fi + # If the submodule is changed, touch the file to trigger a re-init. + desired_commit=$(git ls-tree HEAD upstream | cut -d ' ' -f3 | cut -f1 || true) + current_commit=$(cat "${path}") + if [[ "${desired_commit}" != "${current_commit}" ]]; then + touch "${path}" + fi + echo "${path}" +} + if [[ -z ${original_cmd} ]]; then echo "Error: command is required." echo @@ -372,6 +390,9 @@ case ${original_cmd} in check_in|checkin) check_in "$@" ;; + file_target) + file_target "$@" + ;; *) echo "Error: unknown command \"${original_cmd}\"." echo