diff --git a/.gitignore b/.gitignore index 20fe18d..64c4322 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /tmp +tmp Gemfile.lock diff --git a/bin/git-branch-override b/bin/git-branch-override new file mode 100755 index 0000000..ed4bf7b --- /dev/null +++ b/bin/git-branch-override @@ -0,0 +1,16 @@ +#!/bin/bash +# Removes a branch specified as temp of another +if [ "$1" == "" ]; then + echo -e "ERROR: Must pass branch name." 1>&2 + exit 1 +fi + +if [ "$2" == "" ]; then + echo -e "ERROR: Must pass the new branch name." 1>&2 + exit 1 +fi + +git checkout "$1" +git checkout -b "$2" +git branch -D "$1" +git logra diff --git a/bin/git-parent-branch b/bin/git-parent-branch new file mode 100755 index 0000000..7a8cd88 --- /dev/null +++ b/bin/git-parent-branch @@ -0,0 +1,3 @@ +#!/bin/bash +# Detect the base or parent remote branch (best guess) +git log --pretty=format:'%d' --abbrev-commit --decorate-refs-exclude=refs/tags | grep origin | head -2 | tail -1 | sed '1s/tag:[^,]*, //g' | sed -nr '1s/.*\(([^,]+),?.*/\1/p' | cut -d ')' -f 1 | cut -d '(' -f 1 diff --git a/bin/git-remove-temp-branch b/bin/git-remove-temp-branch new file mode 100755 index 0000000..0de1c60 --- /dev/null +++ b/bin/git-remove-temp-branch @@ -0,0 +1,17 @@ +#!/bin/bash +# Removes a branch specified as temp of another +if [ "$1" == "" ]; then + echo -e "ERROR: Must pass branch name." 1>&2 + exit 1 +fi + +if [ "$2" == "" ]; then + echo -e "ERROR: Must specify a temp branch name." 1>&2 + exit 1 +fi + +git checkout "$2" +git branch -D "$1" +git checkout -b "$1" +git branch -D "$2" +git logra diff --git a/bin/git-update-parent-branch b/bin/git-update-parent-branch new file mode 100755 index 0000000..3daad7e --- /dev/null +++ b/bin/git-update-parent-branch @@ -0,0 +1,89 @@ +#!/bin/bash +# Updates the base or parent branch +CURRENT_BRANCH="$(git current-branch)" +PARENT_BRANCH="" +COMMIT_MODE="multi-commit" +UNTIL_COMMIT="" +COMMITS="" + +while getopts ":-:" opt; do + case ${opt} in + -) + case ${OPTARG} in + "single-commit"*) COMMIT_MODE="single-commit";; + esac + esac +done + +if [ "$COMMIT_MODE" == "single-commit" ]; then + if [ "$2" == "" ]; then + PARENT_BRANCH="$(git parent-branch)" + else + PARENT_BRANCH="$2" + fi + COMMITS="$(git rev-parse "$CURRENT_BRANCH")" +else + if [ "$1" == "" ]; then + PARENT_BRANCH="$(git parent-branch)" + COMMITS="$(git rev-list "$CURRENT_BRANCH"..."$PARENT_BRANCH")" + else + PARENT_BRANCH="$1" + COMMON_ANCESTOR="$(git merge-base "$CURRENT_BRANCH" "$PARENT_BRANCH")" + COMMITS="$(git rev-list "$CURRENT_BRANCH"..."$COMMON_ANCESTOR")" + fi + + if [ "$2" != "" ]; then + UNTIL_COMMIT="$2" + fi +fi +# TODO - What if we don't have commits? + +echo "CURRENT BRANCH -> $CURRENT_BRANCH" +echo "PARENT BRANCH -> $PARENT_BRANCH" +echo "COMMIT LIST -> $COMMITS" +echo "----" + +# TODO - What if a checkout fails? +git checkout -b "$CURRENT_BRANCH-temp" +git fetch + +git checkout "$PARENT_BRANCH" > /dev/null 2>&1 +git branch -D "$CURRENT_BRANCH" > /dev/null 2>&1 +git checkout -b "$CURRENT_BRANCH" > /dev/null 2>&1 + +# TODO - What if a cherry-pick fails? +if [ "$COMMIT_MODE" == "single-commit" ]; then + echo "----" + echo "Cherry Picking -> $COMMITS" + git cherry-pick "$COMMITS" +else + echo 'Multi Commit Mode Enabled!' + COMMIT_ARRAY=($COMMITS) + COMMITS_TO_CHERRY=() + + if [ "$UNTIL_COMMIT" != "" ]; then + for commit_id in "${COMMIT_ARRAY[@]}" + do + if [[ "$commit_id" =~ "$UNTIL_COMMIT" ]]; then + break + fi + COMMITS_TO_CHERRY+=("$commit_id") + done + fi + + size=${#COMMITS_TO_CHERRY[@]} + for (( i=size-1; i>=0; i-- )); do + echo "----" + if [[ "${COMMITS_TO_CHERRY[i]}" =~ "$UNTIL_COMMIT" ]]; then + echo "NOT Cherry Picking! -> ${COMMITS_TO_CHERRY[i]}" + else + echo "Cherry Picking -> ${COMMITS_TO_CHERRY[i]}" + git cherry-pick "${COMMITS_TO_CHERRY[i]}" + fi + done +fi + +echo "----" +git push --set-upstream origin -f "$CURRENT_BRANCH" + +git log --oneline --color --decorate --graph --all