From 66b4fdb902561f7e56b772a47662fe7337a35a2a Mon Sep 17 00:00:00 2001 From: ducoterra Date: Mon, 1 Nov 2021 09:13:15 -0400 Subject: [PATCH 1/8] Ignore subtree pull failures Rather than potentially leaving the developer in a state where `git stash pop` wasn't run, ignore failures in git subtree and let the developer fix issues and re-run the command. This makes `make-update` more idempotent. --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index d431a5e..465cd3a 100644 --- a/Makefile +++ b/Makefile @@ -12,5 +12,5 @@ include .gitlab/make/truenas.makefile .PHONY: make-update make-update: @git stash - @git subtree pull --prefix .gitlab git@gitlab.ducoterra.net:services/common.git main --squash + -git subtree pull --prefix .gitlab git@gitlab.ducoterra.net:services/common.git main --squash @git stash pop From 2f767593ef71ae7332321316aba45fc968d17d85 Mon Sep 17 00:00:00 2001 From: ducoterra Date: Mon, 1 Nov 2021 09:16:02 -0400 Subject: [PATCH 2/8] Add make-push command Add command to update the subtree from a child repo. This pushes to the main branch of the common repo with only the relevant commits. --- Makefile | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Makefile b/Makefile index 465cd3a..cb8e27c 100644 --- a/Makefile +++ b/Makefile @@ -14,3 +14,10 @@ make-update: @git stash -git subtree pull --prefix .gitlab git@gitlab.ducoterra.net:services/common.git main --squash @git stash pop + +.PHONY: make-push +make-push: + @make make-update + @git remote add common git@gitlab.ducoterra.net:services/common.git + @git subtree push --prefix .gitlab common main + @git remote remove common From 6239af7f744aeca23db083fa122ef2679e16420e Mon Sep 17 00:00:00 2001 From: ducoterra Date: Mon, 1 Nov 2021 09:21:43 -0400 Subject: [PATCH 3/8] Ignore git stash pop failures In the event the stash is empty, don't run stash pop. --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index cb8e27c..99b0031 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ include .gitlab/make/truenas.makefile make-update: @git stash -git subtree pull --prefix .gitlab git@gitlab.ducoterra.net:services/common.git main --squash - @git stash pop + @if [-z "$(shell git stash list)"; then git stash pop; fi; .PHONY: make-push make-push: From afed93f58ab5fc8c7cdb649273885aac94c87a02 Mon Sep 17 00:00:00 2001 From: ducoterra Date: Mon, 1 Nov 2021 09:38:00 -0400 Subject: [PATCH 4/8] Add warning to make-update and make-push Add warning before make-update and make-push to alert developers that running the commands might be destructive. Allow cancelling the command up to 3 seconds before continuing. Also explain that merge conflicts warrant a re-run of the command. --- Makefile | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 99b0031..7cbfd02 100644 --- a/Makefile +++ b/Makefile @@ -9,14 +9,20 @@ include .gitlab/make/helm.makefile include .gitlab/make/kaniko.makefile include .gitlab/make/truenas.makefile +.PHONY: warning +warning: + @printf "Running a potentially destructive command. If a conflict occurs, fix the conflict and re-run the command.\n" + @printf "Cancel with ctrl + c within 3 seconds." + @sleep 3 + .PHONY: make-update -make-update: +make-update: warning @git stash -git subtree pull --prefix .gitlab git@gitlab.ducoterra.net:services/common.git main --squash @if [-z "$(shell git stash list)"; then git stash pop; fi; .PHONY: make-push -make-push: +make-push: warning @make make-update @git remote add common git@gitlab.ducoterra.net:services/common.git @git subtree push --prefix .gitlab common main From f81d846f310d3dbad1d8d8cd31b072bad540105c Mon Sep 17 00:00:00 2001 From: ducoterra Date: Mon, 1 Nov 2021 09:56:37 -0400 Subject: [PATCH 5/8] Only pop stash if changes Only pop the stash if the most recent stash commit matches the STASH name. --- Makefile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 7cbfd02..18e611b 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,7 @@ SHELL := /bin/bash IMAGE ?= $(shell cat IMAGE):$(shell cat VERSION) IMAGE_LATEST ?= $(shell cat IMAGE):latest PWD ?= $(shell pwd) +STASH ?= "common-update-stash" include .gitlab/make/docker.makefile include .gitlab/make/helm.makefile @@ -17,9 +18,9 @@ warning: .PHONY: make-update make-update: warning - @git stash + @git stash save $(STASH) -git subtree pull --prefix .gitlab git@gitlab.ducoterra.net:services/common.git main --squash - @if [-z "$(shell git stash list)"; then git stash pop; fi; + @if [ ! -z "$$(git stash list | grep -r 'stash@{0}.*common')" ]; then git stash pop; fi .PHONY: make-push make-push: warning From 0f6eeb776eed393165e496673e645682834a08d8 Mon Sep 17 00:00:00 2001 From: ducoterra Date: Mon, 1 Nov 2021 10:01:12 -0400 Subject: [PATCH 6/8] Add make-stash-drop command Sometimes you can get stuck in a loop where git stash pop preserves the stash commit. Dropping that commit if it's a STASH commit after an update is totally fine. --- Makefile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Makefile b/Makefile index 18e611b..02f380c 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,10 @@ warning: @printf "Cancel with ctrl + c within 3 seconds." @sleep 3 +.PHONY: make-stash-drop +make-stash-drop: + @if [ ! -z "$$(git stash list | grep -r 'stash@{0}.*common')" ]; then git stash drop; fi + .PHONY: make-update make-update: warning @git stash save $(STASH) From 22a952dd53cc97522d5f0d7826c028acf03413a8 Mon Sep 17 00:00:00 2001 From: ducoterra Date: Mon, 1 Nov 2021 10:02:35 -0400 Subject: [PATCH 7/8] Remove warning from make-update make-update isn't as destructive as it once was. It should be safe to run as long as the developer is fixes any weird merge conflicts after stash pop. --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 02f380c..320f9d4 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,7 @@ make-stash-drop: @if [ ! -z "$$(git stash list | grep -r 'stash@{0}.*common')" ]; then git stash drop; fi .PHONY: make-update -make-update: warning +make-update: @git stash save $(STASH) -git subtree pull --prefix .gitlab git@gitlab.ducoterra.net:services/common.git main --squash @if [ ! -z "$$(git stash list | grep -r 'stash@{0}.*common')" ]; then git stash pop; fi From b07a2c095b55d758ed5fa89b53d7c559b3e8fc9c Mon Sep 17 00:00:00 2001 From: ducoterra Date: Mon, 1 Nov 2021 10:05:43 -0400 Subject: [PATCH 8/8] Run update after make-push To close things out we should re-run make-update to ensure changes are pulled back into the repo we updated from. --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index 320f9d4..61a7dbb 100644 --- a/Makefile +++ b/Makefile @@ -32,3 +32,4 @@ make-push: warning @git remote add common git@gitlab.ducoterra.net:services/common.git @git subtree push --prefix .gitlab common main @git remote remove common + @make make-update