Last active
November 26, 2015 08:46
-
-
Save themouette/46d36157a6f7e5458c11 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
BRANCH="$1" | |
ORIGINAL_BRANCH="develop" | |
if [[ -z "${BRANCH}" ]]; then | |
echo "You must provide a branch as first argument" | |
echo "" | |
echo " $0 features/develop/new-sidebars" | |
exit 1; | |
fi | |
EXISTING_BRANCH=$(git branch -a |grep $BRANCH |wc -l) | |
if [[ $EXISTING_BRANCH == 0 ]] ; then | |
echo "Unknown branch $BRANCH" | |
exit 1 | |
fi | |
FORMAT="--pretty=format:%h%x09%an%x09%s" | |
TMP_BRANCH="${BRANCH}-tmp" | |
FINAL_COMMIT=$(git log $FORMAT $BRANCH |head -n1|cut -f1) | |
find_ancestor_commit() { | |
OLDEST_ANCESTOR=$(git merge-base $BRANCH $ORIGINAL_BRANCH) | |
ORIGIN_COMMIT="" | |
while read line ; do | |
if [[ -n "${ORIGIN_COMMIT}" ]]; then | |
continue; | |
fi | |
HASH=$(echo "$line" | cut -f1) | |
SUBJECT=$(echo "$line"|cut -f3) | |
# check if subject exists in $ORIGINAL_BRANCH | |
echo $line | |
SUBJECT_EXISTS=$(git log $FORMAT $ORIGINAL_BRANCH|head -n50|grep "$SUBJECT"|wc -l) | |
if [[ $SUBJECT_EXISTS -gt 0 ]]; then | |
ORIGIN_COMMIT="$HASH" | |
fi | |
done <<< "$(git log $FORMAT "${OLDEST_ANCESTOR}".."${BRANCH}")" | |
if [[ -z "$ORIGIN_COMMIT" ]]; then | |
ORIGIN_COMMIT=$OLDEST_ANCESTOR | |
echo "Fallback to merge point" | |
fi | |
echo "Common ancestor: $ORIGIN_COMMIT" #$(git show --oneline $ORIGIN_COMMIT)" | |
} | |
init_cherry_pick() { | |
EXISTING_TMP_BRANCH=$(git branch |grep $TMP_BRANCH |wc -l) | |
if [[ $EXISTING_TMP_BRANCH == 0 ]] ; then | |
echo "create tmp branch ${TMP_BRANCH}" | |
git checkout develop | |
git checkout -b $TMP_BRANCH | |
git cherry-pick ${ORIGIN_COMMIT}..${FINAL_COMMIT} | |
fi | |
} | |
exit_if_conflict() { | |
CONFLICT_STATE=$(git ls-files -u |wc -l) | |
if [[ $CONFLICT_STATE -gt 0 ]] ; then | |
echo "###########################################" | |
echo "" | |
echo " You must resolve conflicts first" | |
echo "" | |
echo "###########################################" | |
git status | |
exit 1; | |
fi | |
} | |
continue_if_clean_state() { | |
UNMERGED_STATE=$(git status --porcelain | grep -v '^??' |wc -l) | |
if [[ $UNMERGED_STATE -gt 0 ]] ; then | |
git cherry-pick --continue | |
fi | |
UNMERGED_STATE=$(git status --porcelain | grep -v '^??' |wc -l) | |
if [[ $UNMERGED_STATE -gt 0 ]] ; then | |
# cherry-pick finished in a non clean state | |
echo "###########################################" | |
echo "" | |
echo " Something went wrong with commit." | |
echo " Fix it then relanch script." | |
echo "" | |
echo "###########################################" | |
exit 1; | |
fi | |
} | |
delete_tmp_branch() { | |
EXISTING_TMP_BRANCH=$(git branch |grep $TMP_BRANCH |wc -l) | |
if [[ $EXISTING_TMP_BRANCH -gt 0 ]] ; then | |
echo "replace branch" | |
git branch -D $BRANCH | |
git checkout -b $BRANCH | |
git branch -D $TMP_BRANCH | |
echo "###########################################" | |
echo "" | |
echo " Rebase is done." | |
echo " Launch your stack to check it went well" | |
echo "" | |
echo "###########################################" | |
fi | |
} | |
find_ancestor_commit | |
init_cherry_pick | |
exit_if_conflict | |
continue_if_clean_state | |
exit_if_conflict | |
delete_tmp_branch |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment