diff --git a/git-subsplit.sh b/git-subsplit.sh index 5c84c3b..e90c901 100755 --- a/git-subsplit.sh +++ b/git-subsplit.sh @@ -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 @@ -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" ] @@ -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 @@ -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" ]; @@ -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 @@ -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" ]; @@ -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 @@ -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