Created
March 2, 2015 22:56
-
-
Save raidzero/80a0be45715edebb6fd1 to your computer and use it in GitHub Desktop.
Simple script to find occurrences of a git commit in all branches
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/sh | |
if [ -z "$1" ]; then | |
bname=`basename $0` | |
echo "Usage: $bname SHA" | |
exit 1 | |
fi | |
PATCH_ID=`git show -p $1 | git patch-id | awk '{print$1}'` | |
if [ -z "$PATCH_ID" ]; then | |
echo "Unable to generate patch id. Verify SHA" | |
exit 1 | |
fi | |
NUM_COMMITS=`git log --all --oneline | wc -l | sed 's/^ *//'` | |
echo "Searching $NUM_COMMITS commits, please stand by..." | |
FOUND_COMMITS=0 | |
for SHA in `git log --all -p | git patch-id | grep -w ^$PATCH_ID | awk '{print$2}'`; do | |
let FOUND_COMMITS+=1 | |
BRANCHES=`git branch -r --contains $SHA` | |
echo "Found commit: $SHA ->" | |
echo "$BRANCHES" | |
FOUND_BRANCHES="$FOUND_BRANCHES $BRANCHES" | |
done | |
FOUND_BRANCHES=`echo "$FOUND_BRANCHES" | tr ' ' '\n' | sed '/^$/d' | sort -u` | |
NUM_BRANCHES=`echo "$FOUND_BRANCHES" | wc -l | sed 's/^ *//'` | |
BRANCH_LABEL="branches" | |
COMMIT_LABEL="commits" | |
[ "$FOUND_BRANCHES" == "1" ] && BRANCH_LABEL="branch" | |
[ "$FOUND_COMMITS" == "1" ] && COMMIT_LABEL="commit" | |
echo "Found $FOUND_COMMITS $COMMIT_LABEL in $NUM_BRANCHES $BRANCH_LABEL." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment