From 0ca5825e790fd9ab034dd8ab7511732e1905000c Mon Sep 17 00:00:00 2001
From: Adam Spragg <adam@spra.gg>
Date: Mon, 23 May 2022 16:23:50 +0100
Subject: Abort commits during conflicts if differences exist

If the user is running `git commit` to resolve a conflict, it's
dangerous to just save the metadata from the filesystem. It's possible
that they just fixed a conflict in the metadata file, but didn't update
the on-disk metadata to match.

(I know it's possible, because I did this!)

Our normal mode of operation, of just saving the metadata during a
commit to match whatever's on-disk, would wipe out their careful
conflict resolution! (On the other hand, just overwriting the on-disk
metadata might not be the best idea either.)

But the point of a conflict is that the changes between two different
branches *can't* be resolved automatically, and requires manual
intervention. Given that situation, asking them to resolve the
difference manually seems to be the most obvious option.
---
 examples/hooks/pre-commit | 12 ++++++++++++
 1 file changed, 12 insertions(+)

(limited to 'examples/hooks')

diff --git a/examples/hooks/pre-commit b/examples/hooks/pre-commit
index dc79432..e4cc8ea 100644
--- a/examples/hooks/pre-commit
+++ b/examples/hooks/pre-commit
@@ -24,6 +24,18 @@ exit_on_fail() {
 	fi
 }
 
+if git rev-parse MERGE_HEAD >/dev/null 2>&1 \
+		&& [ -e "$MSFILE" ] \
+		&& ! metastore -c -q -f "$MSFILE"; then
+	echo "In git Conflict resolution, and saved metadata does not match filesystem"
+	echo "metadata. Unable to fix automatically. Try:"
+	echo "  metastore -cf \"$MSFILE\"    -- Shows where the differences are"
+	echo "  metastore -df \"$MSFILE\"    -- Shows the stored metadata"
+	echo "  metastore -sf \"$MSFILE\"    -- Save current filesystem metadata as-is"
+	echo "  metastore -af \"$MSFILE\"    -- Apply saved metadata to filesystem"
+	exit 1
+fi
+
 exit_on_fail \
 	metastore -s -f "$MSFILE"
 
-- 
cgit v1.2.1