Skip to content

Instantly share code, notes, and snippets.

@eddiemoya
Created September 26, 2013 19:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eddiemoya/6718997 to your computer and use it in GitHub Desktop.
Save eddiemoya/6718997 to your computer and use it in GitHub Desktop.
Simplifies the workflow of having to ... * Check if a branch can be fast-forward merged * If it is not, checking that branch out * Rebasing that branch into the branch you were originally on. * Ensuring that if you were kicked into a headless state after the rebase, that your actual branch ref points to the new rebased commits. * Checking out th…
#!/bin/bash
function get_merge_revs {
MERGE=$(git rev-parse --abbrev-ref $REV);
MERGE_REV=$(git rev-parse $REV);
SHORT_MERGE_REV=$(echo $MERGE_REV | cut -c 1-7);
}
function get_orig_merge_revs {
ORIG_MERGE=$(git rev-parse --abbrev-ref $REV);
ORIG_MERGE_REV=$(git rev-parse $REV);
ORIG_SHORT_MERGE_REV=$(echo $MERGE_REV | cut -c 1-7);
}
function get_current_revs {
CURRENT=$(git rev-parse --abbrev-ref $REV);
CURRENT_REV=$(git rev-parse $REV);
SHORT_CURRENT_REV=$(echo $CURRENT_REV | cut -c 1-7);
}
function is_ffable {
echo ".....";
git merge-base --is-ancestor $CURRENT_REV $MERGE_REV;
ffable=$?;
if [ $VERBOSE == true ]; then
echo -e "(A): ($CURRENT) \t[$CURRENT_REV]";
echo -e "(B): ($MERGE) \t[$MERGE_REV]\n";
if [ $ffable == 0 ]; then
echo -e "[*] Yes! Revision (B) CAN be Fast-Forward merged into (A)."
else
echo -e "[!] Well, fuck! Revision (B) can NOT be Fast-Forward merged into (A)!"
fi
fi
}
function ferge {
echo "Performing a Fast-Forward merge of $MERGE";
echo ".....";
git merge --ff-only $MERGE_REV;
}
# Performs a reverse rebase where the current revision becomes the base for the revision that is passed.
function mebase {
echo "Checking out $MERGE.....";
git checkout --quiet $MERGE;
TMP_REV=$(git rev-parse HEAD);
# Verify that we have checked out the correct revision
if [ "$MERGE_REV" == "$TMP_REV" ]; then
echo "Performing REBASE of ($MERGE) [$SHORT_MERGE_REV] onto ($CURRENT) [$SHORT_CURRENT_REV].....";
echo ".....";
git rebase -i $CURRENT_REV;
#Reset the merge vars
REV="HEAD";
#update_merge_branch;
get_merge_revs;
get_orig_merge_revs;
echo "Checkout out your original branch: ($CURRENT) [$SHORT_CURRENT_REV].....";
git checkout --quiet $CURRENT;
else
echo "Checkout of $MERGE seems to have failed."
exit() { return $1;}
fi
}
function update_merge_branch {
if [ "$MERGE" -ne "$BRANCH" ]; then
git update-ref refs/heads/$BRANCH $REV;
fi
echo "[*] Revision ($MERGE) [$SHORT_MERGE_REV] has been properly rebased onto ($CURRENT) [$SHORT_CURRENT_REV] and updated.";
}
function ask_rebase {
printf "[?] Perform rebase? [Yes/no] : "
read do_rebase;
echo ".....";
}
function ask_ferge {
printf "[?] Perform merge? [Yes/no] : "
read do_ferge;
echo ".....";
}
echo "Current position is [$SHORT_CURRENT_REV] ($CURRENT)"
REV=$1;
get_merge_revs;
REV="HEAD";
get_current_revs;
#Backup var for safe keeping after rebases.
BRANCH=$MERGE;
VERBOSE=true;
is_ffable;
if [ "$ffable" -gt 0 ]; then
ask_rebase;
if [ $do_rebase == "Yes" ]; then
mebase;
echo -e "\nNew revisions (after rebase)"
is_ffable;
else
echo "Nothing left to do...";
exit() { return $1;}
fi
fi
if [ "$ffable" -eq 0 ]; then
ask_ferge;
if [ $do_ferge == "Yes" ]; then
ferge;
else
echo "Nothing left to do...";
exit() { return $1;}
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment