Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westonruter/8c7ef1b2a7b22d0bc9ad to your computer and use it in GitHub Desktop.
Save westonruter/8c7ef1b2a7b22d0bc9ad to your computer and use it in GitHub Desktop.
#!/bin/bash
# git-internal-submodule-init.sh
# Author: Weston Ruter (@westonruter), X-Team WP
show_usage()
{
echo "
Usage: $0 path/to/submodule [initial_commit]
Create a submodule pointing to a branch (prefixed by 'internal-submodules/') in
the same repo. The branch's head will be pointing to the initial commit of the repo,
unless you supply an initial_commit argument (which can be a hash or a branch, etc).
You must run this from the root of the repo that you want to add the submodule to.
"
}
while getopts "h" OPTION
do
case $OPTION in
h)
show_usage
exit 1
;;
?)
show_usage
exit 1
;;
esac
done
if [ ! -e .git ]; then
echo "You must run in the root of the repo (or submodule)"
exit 1
fi
submodule_path="$1"
if [ -z "$submodule_path" ]; then
echo "You must supply the path to the plugin as the first argument (e.g. plugins/example)."
exit 1
fi
if [ -e "$submodule_path" ]; then
echo "Plugin seems to already exist at $submodule_path. Aborting."
exit 1
fi
if [ -n "$2" ]; then
initial_commit="$2"
else
initial_commit=$( git rev-list --max-parents=0 HEAD | head -n 1 )
fi
# Check if submodule branch exists
if ! git show-ref --verify --quiet "refs/heads/internal-submodules/$submodule_path"; then
echo "Create branch for submodule"
git branch "internal-submodules/$submodule_path" "$initial_commit"
fi
# Checkout submodule
git submodule add -b "internal-submodules/$submodule_path" ./ "$submodule_path"
echo "Your internal submodule is at $submodule_path."
echo "The initialized submodule has been staged."
echo "You can commit now, or add files (and commits) to the submodule."
echo "Commit updates to the plugin submodule when they are ready to be shared."
echo "You can use git-submodule-commit.sh to automate the drafting of a helpful shortlog commit message."
#!/bin/bash
# Commit changes to a single submodule supplied
# Author: Weston Ruter (@westonruter), X-Team WP
set -e
submodule_path="$1"
show_usage()
{
echo "
Usage: $0 path/to/submodule
When submodule repos have new commits in them, commit the updated reference with a
a helpful commit message in the super repo, including the commit range that the
submodule repo was updated and a oneline git log. Make sure that the submodule's
commits are pushed up to the repo as well. This does not push the commits for the
parent repo.
"
}
while getopts "h" OPTION
do
case $OPTION in
h)
show_usage
exit 1
;;
?)
show_usage
exit 1
;;
esac
done
submodule_path=$(sed 's:/$::' <<< "$submodule_path")
if [ -z "$submodule_path" ]; then
show_usage
exit 1
fi
if [ ! -d "$submodule_path" ]; then
echo "Non-existent directory: $submodule_path"
fi
if [ ! -f "$submodule_path/.git" ]; then
echo "Not a Git repo at $submodule_path"
fi
cd "$submodule_path"
if grep -s github <<< "$(git config remote.origin.url)"; then
org_repo=$(git config remote.origin.url | sed 's=.*github\.com[/:]==' | sed 's:\.git$::')
org=$(sed 's:/.*::' <<< "$org_repo")
name=$(sed 's:.*/::' <<< "$org_repo")
sedarg='s:Merge pull request \(#[0-9][0-9]*\) from '$org'/\([a-zA-Z0-9_-][a-zA-Z0-9_-]*\):Merge pull request '$org/$name'\1 from branch \2:g'
else
name=$(basename "$submodule_path")
sedarg='s:THISISAHACKANDSHOULDBEELIMINATED::'
fi
cd - > /dev/null
if ! git diff --staged --quiet -- "$submodule_path" ; then
old_sha1=$(git diff --staged -- "$submodule_path" | grep "Subproject commit" | cut -c20-26 | head -n 1)
new_sha1=$(git diff --staged -- "$submodule_path" | grep "Subproject commit" | cut -c20-26 | tail -n 1)
cd "$submodule_path"
git show -s --format="Update $name $old_sha1...$new_sha1: %s" "$new_sha1" | sed "$sedarg" > /tmp/SUBMODULE_COMMIT_MSG
if grep -s github <<< "$(git config remote.origin.url)"; then
echo "https://github.com/$org/$name/compare/$old_sha1...$new_sha1" >> /tmp/SUBMODULE_COMMIT_MSG
fi
echo >> /tmp/SUBMODULE_COMMIT_MSG
git --no-pager log --reverse --oneline $old_sha1...$new_sha1 | sed "$sedarg" >> /tmp/SUBMODULE_COMMIT_MSG
cd - > /dev/null
git commit --edit -F /tmp/SUBMODULE_COMMIT_MSG
else
echo "Nothing to do. Submodule is clean or not staged: $submodule_path"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment