Skip to content

Instantly share code, notes, and snippets.

@yuuki
Last active December 11, 2015 00:19
Show Gist options
  • Save yuuki/4515603 to your computer and use it in GitHub Desktop.
Save yuuki/4515603 to your computer and use it in GitHub Desktop.
cmd_push() {
parse_remote_name "$@"
if [ -z "$REMOTE" ]; then
REMOTE="$ORIGIN"
fi
name_or_current
# sanity checks
require_clean_working_tree
require_branch "$BRANCH"
git fetch -q "$REMOTE"
# are we publishing for the first time, or pushing?
if ! git_branch_exists "$REMOTE/$BRANCH" ; then
# create remote branch
git push "$REMOTE" "$BRANCH:refs/heads/$BRANCH"
git fetch -q "$REMOTE"
# configure remote tracking
git config "branch.$BRANCH.remote" "$REMOTE"
git config "branch.$BRANCH.merge" "refs/heads/$BRANCH"
echo
echo "Summary of actions:"
echo "- A new remote branch '$REMOTE/$BRANCH' was created"
echo "- The local branch '$BRANCH' was configured to track the remote branch"
echo
else
git push "$REMOTE" "$BRANCH:refs/heads/$BRANCH" || die "Push failed"
echo
echo "Summary of actions:"
echo "- The remote branch '$REMOTE/$BRANCH' was updated with your changes"
fi
}
#......
parse_remote_name() {
# parse options
FLAGS "$@" || exit $?
eval set -- "${FLAGS_ARGV}"
# read arguments into global variables
REMOTE=$1
NAME=$2
BRANCH=$PREFIX$NAME
}
#......
name_or_current() {
if [ -z "$NAME" ]; then
use_current_feature_branch_name
fi
}
#......
use_current_feature_branch_name() {
local current_branch=$(git_current_branch)
if startswith "$current_branch" "$PREFIX"; then
BRANCH=$current_branch
NAME=${BRANCH#$PREFIX}
else
warn "The current HEAD is no feature branch."
warn "Please specify a <name> argument."
exit 1
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment