Skip to content

Instantly share code, notes, and snippets.

@jaysephjw
Created December 2, 2020 21:29
Show Gist options
  • Save jaysephjw/516e43a5ab6c2549e907415d1d6c3898 to your computer and use it in GitHub Desktop.
Save jaysephjw/516e43a5ab6c2549e907415d1d6c3898 to your computer and use it in GitHub Desktop.
(WIP) - Find all submodules that ref a commit not on origin/master
git config --file .gitmodules --get-regexp path | awk '{ print $2 }' | xargs -I{} bash -c '[[ $(git -C {} --no-pager branch -r --contains HEAD) != *origin/master* ]] || echo "{} NOT OK"'
@jaysephjw
Copy link
Author

rough idea for using as a pre-commit hook:

#!/bin/sh

remote="$1"
url="$2"

while read local_ref local_sha remote_ref remote_sha
do
	# Check no submodules are dirty (how about no git diff at all)
	if [ -n "${git diff --machine}" ]
	then
		echo >&2 "Git status is dirty; not pushing"
		exit 1
	fi

	# Check each submodule's commit is on master in the remote. 
	badbranches=git config --file .gitmodules --get-regexp path | awk '{ print $2 }' | xargs -I{} bash -c '[[ $(git -C {} --no-pager branch -r --contains HEAD) != *origin* ]] && echo "{} NOT OK"'
	if [ -n "$badbranches" ]
	then
		echo >&2 "Found submodule references that are not pushed to remote; not pushing"
		echo >&2 $badbranches
		exit 1
	fi
done

exit 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment