Skip to content

Break script & return exit code on failure #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions git-subsplit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
# git-subsplit.sh: Automate and simplify the process of managing one-way
# read-only subtree splits.
#
# exit code:
# 1 git add-remote/pull/fetch operation failed
# 2 failed updating repo
# 3 git push operation failed
# 4 failed on git subtree command
#
# Copyright (C) 2012 Dragonfly Development Inc.
#
if [ $# -eq 0 ]; then
Expand Down Expand Up @@ -106,7 +112,14 @@ say()
echo "$@" >&2
fi
}

fatal()
{
RC=${1:-1}
shift
say "${@:-## Error occurs}"
popd >/dev/null
exit $RC
}
subsplit_require_work_dir()
{
if [ ! -e "$WORK_DIR" ]
Expand Down Expand Up @@ -185,7 +198,7 @@ subsplit_publish()

if ! git remote | grep "^${REMOTE_NAME}$" >/dev/null
then
git remote add "$REMOTE_NAME" "$REMOTE_URL"
git remote add "$REMOTE_NAME" "$REMOTE_URL" || fatal 1 "## Failed adding remote $REMOTE_NAME $REMOTE_URL"

if [ -n "$VERBOSE" ];
then
Expand Down Expand Up @@ -217,11 +230,11 @@ subsplit_publish()

say " - syncing branch '${HEAD}'"

git checkout master >/dev/null 2>&1
git checkout master >/dev/null 2>&1 || fatal 1 "## Failed while git checkout master"
git branch -D "$LOCAL_BRANCH" >/dev/null 2>&1
git branch -D "${LOCAL_BRANCH}-checkout" >/dev/null 2>&1
git checkout -b "${LOCAL_BRANCH}-checkout" "origin/${HEAD}" >/dev/null 2>&1
git subtree split -q --prefix="$SUBPATH" --branch="$LOCAL_BRANCH" "origin/${HEAD}" >/dev/null
git checkout -b "${LOCAL_BRANCH}-checkout" "origin/${HEAD}" >/dev/null 2>&1 || fatal 1 "## Failed while git checkout"
git subtree split -q --prefix="$SUBPATH" --branch="$LOCAL_BRANCH" "origin/${HEAD}" >/dev/null || fatal 4 "## Failed while git subtree split for HEADS"
RETURNCODE=$?

if [ -n "$VERBOSE" ];
Expand All @@ -247,7 +260,7 @@ subsplit_publish()
echo \# $PUSH_CMD
$PUSH_CMD
else
$PUSH_CMD
$PUSH_CMD || fatal 3 "## Failed pushing branchs to remote repo"
fi
fi
done
Expand Down Expand Up @@ -292,7 +305,7 @@ subsplit_publish()
fi

say " - subtree split for '${TAG}'"
git subtree split -q --annotate="${ANNOTATE}" --prefix="$SUBPATH" --branch="$LOCAL_TAG" "$TAG" >/dev/null
git subtree split -q --annotate="${ANNOTATE}" --prefix="$SUBPATH" --branch="$LOCAL_TAG" "$TAG" >/dev/null || fatal 4 "## Failed while git subtree split for TAGS"
RETURNCODE=$?

if [ -n "$VERBOSE" ];
Expand All @@ -315,7 +328,7 @@ subsplit_publish()
echo \# $PUSH_CMD
$PUSH_CMD
else
$PUSH_CMD
$PUSH_CMD || fatal 3 "## Failed pushing tags to remote repo"
fi
fi
done
Expand All @@ -330,9 +343,9 @@ subsplit_update()

say "Updating subsplit from origin"

git fetch -q -t origin
git checkout master
git reset --hard origin/master
git fetch -q -t origin || fatal 2 "## Failed updating repo"
git checkout master || fatal 2 "## Failed updating repo"
git reset --hard origin/master || fatal 2 "## Failed updating repo"

if [ -n "$VERBOSE" ];
then
Expand Down