Skip to content

Instantly share code, notes, and snippets.

@jeremytregunna
Created July 3, 2011 19:47
Show Gist options
  • Select an option

  • Save jeremytregunna/1062543 to your computer and use it in GitHub Desktop.

Select an option

Save jeremytregunna/1062543 to your computer and use it in GitHub Desktop.
#!/bin/sh
. git-simple-common.sh
if [ X"$1" = X"-h" ]; then
echo "Usage: git develop [new_branch] [parent_branch]"
echo " Creates a new branch [new_branch] detached from [parent_branch] or the current"
echo " branch, if [parent_branch] is not specified."
exit 0
fi
NEW_BRANCH_NAME=$1
[ -z $NEW_BRANCH_NAME ] && die "No branch name was provided."
PARENT_BRANCH_NAME=$2
for branch in `branch_names`; do
[ X"$branch" = X"$NEW_BRANCH_NAME" ] && die "Branch name '$NEW_BRANCH_NAME' is already in use. Try another."
done
# Check if we're in a dirty branch, and if so, stash.
stash_it "switching branches"
# Check out the new branch, detached from a parent.
[ -n $PARENT_BRANCH_NAME ] && git checkout $PARENT_BRANCH_NAME
git checkout -b $NEW_BRANCH_NAME
#!/bin/sh
. git-simple-common.sh
if [ X"$1" = X"-h" ]; then
echo "Usage: git graft [branch] [into_branch]"
echo " Merges [branch] into [into_branch] and then deletes [branch]. Only unpublished"
echo " branches may be merged."
exit 0
fi
TEMP_BRANCH_NAME=$1
[ -z $TEMP_BRANCH_NAME ] && die "No branches were supplied."
PERM_BRANCH_NAME=$2
[ -z $PERM_BRANCH_NAME ] && die "Need a branch to merge into."
FOUND_BRANCH=0
FOUND_PERM_BRANCH=0
for branch in `branch_names`; do
[ X"$branch" = X"$TEMP_BRANCH_NAME" ] && FOUND_BRANCH=1
[ X"$branch" = X"$PERM_BRANCH_NAME" ] && FOUND_PERM_BRANCH=1
done
[ ${FOUND_BRANCH} -eq 0 ] && die "No branch named '$TEMP_BRANCH_NAME' exists, please try another."
[ ${FOUND_PERM_BRANCH} -eq 0 ] && die "No branch named '$PERM_BRANCH_NAME' exists, please try another."
FOUND_REMOTE_BRANCH=0
for branch in `remote_branch_names`; do
[ X"$branch" = X"$TEMP_BRANCH_NAME" ] && FOUND_REMOTE_BRANCH=1
done
[ ${FOUND_REMOTE_BRANCH} -eq 1 ] && die "Can only graft unpublished branches. Unpublish '$TEMP_BRANCH_NAME' and try again."
# Switch to the branch
git switch $PERM_BRANCH_NAME
# Graft branches
git merge --no-ff $TEMP_BRANCH_NAME
if [ $? -eq 0 ]; then
git branch -D $TEMP_BRANCH_NAME
else
die "There was a problem merging, so the branch will not be deleted."
fi
#!/bin/sh
. git-simple-common.sh
if [ X"$1" = X"-h" ]; then
echo "Usage: git publish [branch]"
echo " Publishes a branch to the remote. Presently only publishes to origin."
exit 0
fi
BRANCH=$1
[ -z ${BRANCH} ] && die "No branch was specified."
FOUND_REMOTE_BRANCH=0
for br in remote_branch_names; do
branch=`echo $br | sed -e s,.*/,,g`
[ X"$branch" = X"$BRANCH" ] && FOUND_REMOTE_BRANCH=1
done
[ ${FOUND_REMOTE_BRANCH} -eq 1 ] && die "Branch '$BRANCH' is already published."
# TODO: Support multiple remotes
git push origin $BRANCH
# Log some output to stderr, prefix command name
warn() {
echo "`basename $0`: $@" 2>&1
}
# Error with some string
die() {
warn $1
exit
}
# Checks if a branch is dirty.
is_dirty() {
git diff-index --quiet HEAD
}
# Get all the branches in a way we can iterate over
branch_names() {
git branch | sed -e 's/[ \*]*//g'
}
# Get all the remote branch names
remote_branch_names() {
git branch -r | sed -e 's/[ \*]*//g'
}
# Get the current branch name.
current_branch_name() {
git branch | grep "^\*" | sed -e 's/[ \*]*//g'
}
# Checks if our branch is dirty, if so, stashes it
stash_it() {
[ -z "$1" ] && die "stash_it(): Must supply an argument"
VERB=$1
is_dirty
if [ X"$?" = X"1" ]; then
CURRENT_BRANCH_NAME=`current_branch_name`
warn "Stashing branch '${CURRENT_BRANCH_NAME}'"
git stash save "git-simple: stashing before ${VERB}"
fi
}
#!/bin/sh
. git-simple-common.sh
if [ X"$1" = X"-h" ]; then
echo "Usage: git switch [branch]"
echo " Performs an optional stash of current changes before switching to [branch] where it"
echo " checks for any stashes that can be popped, and applies those."
exit 0
fi
# Exit early if we're already in this branch.
CURRENT_BRANCH_NAME=`current_branch_name`
if [ "${CURRENT_BRANCH_NAME}" = "$1" ]; then
die "Already in branch '$1'"
fi
# Check if we're in a dirty branch, and if so, stash.
stash_it "switching branches"
if [[ -n $1 ]]; then
TO_BRANCH=`git branch | sed -e 's/[ \*]*//g' | grep $1`
# Doesn't exist, uh oh.
[ X"$?" = X"1" ] && die "Branch '$1' does not exist. Did you maybe mean: git develop?"
# Switch the branch.
git checkout $1
if [ X"$?" = X"0" ]; then
# Switched the branch, here we need to check for a stash, apply it if there's one before exiting.
STASH_INDEX=`git stash list | grep ": On $1" | awk -F'[{}]' '$1 ~/stash@$/{print $2}'`
CURRENT_BRANCH=`git branch | grep "^\*" | sed -e 's/[ \*]*//g'`
if [[ -n ${STASH_INDEX} ]]; then
warn "Popping stash for branch ${current_branch_name}"
git stash pop "stash@{${STASH_INDEX}}"
fi
else
die "Unsure what happened. Aborting."
fi
fi
#!/bin/sh
. git-simple-common.sh
if [ X"$1" = X"-h" ]; then
echo "Usage: git sync [branch]"
echo " Synchronizes [branch] with its published counterpart, or if [branch] is not"
echo " supplied, assumes your current branch."
exit 0
fi
BRANCH=$1
[ -z ${BRANCH} ] && BRANCH=`current_branch_name`
FOUND_REMOTE_BRANCH=0
for br in `remote_branch_names`; do
branch=`echo $br | sed -e s,.*/,,g`
[ X"$branch" = X"$BRANCH" ] && FOUND_REMOTE_BRANCH=1
done
[ ${FOUND_REMOTE_BRANCH} -eq 0 ] && die "Could not find a published branch named '${BRANCH}'. Maybe publish first?"
stash_it "synching branches"
git fetch origin
REMOTE_NAME=`git remote`
MERGES=`git log --merges ${REMOTE_NAME}/${BRANCH}..${BRANCH} | grep commit`
if [[ -n ${MERGES} ]]; then
git merge ${REMOTE_NAME}/${BRANCH}
else
git rebase ${REMOTE_NAME}/${BRANCH}
fi
git push ${REMOTE_NAME} ${BRANCH}
#!/bin/sh
. git-simple-common.sh
if [ X"$1" = X"-h" ]; then
echo "Usage: git unpublish [branch]"
echo " Removes a branch from the remote. Presently only talks to the origin remote."
exit 0
fi
BRANCH=$1
[ -z ${BRANCH} ] && die "No branch was specified."
FOUND_REMOTE_BRANCH=0
for br in remote_branch_names; do
branch=`echo $br | sed -e s,.*/,,g`
[ X"$branch" = X"$BRANCH" ] && FOUND_REMOTE_BRANCH=1
done
[ ${FOUND_REMOTE_BRANCH} -eq 0 ] && die "Branch '$BRANCH' is not published."
# TODO: Support multiple remotes
git push origin :$BRANCH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment