Skip to content

Instantly share code, notes, and snippets.

@avh4
Last active June 5, 2017 17:10
Show Gist options
  • Save avh4/24cb7bd40e89937a8991c0a280cd8ff8 to your computer and use it in GitHub Desktop.
Save avh4/24cb7bd40e89937a8991c0a280cd8ff8 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
git fetch
CURRENT="$(git rev-parse --abbrev-ref HEAD)"
# find where the current branch branched from origin/master
REFERENCE="origin/master"
BRANCH_POINT="$(git merge-base "$REFERENCE" HEAD)"
# from https://stackoverflow.com/a/4991675/308930
BRANCH_POINT="$(diff -u <(git rev-list --first-parent HEAD) <(git rev-list --first-parent "$REFERENCE") | sed -ne 's/^ //p' | head -1)"
function oldestCommit() {
base="$1"
topic="$2"
git cherry "$topic" "$base" | sed -e '/^+ /{s/^+ //;p;};d' | xargs -n1 git log -1 --pretty=tformat:"%at %H" | sort | head -n1 | cut -d' ' -f2
}
function newestCommit() {
revisionRange="$1"
git log --pretty=format:"%at %H" "$revisionRange" | sort -r | head -n1 | cut -d' ' -f2
}
OLDEST="$(oldestCommit "$REFERENCE" HEAD)"
TIME_OF_BRANCH="$(git log -1 --pretty=format:%at "$OLDEST")"
HUMAN_TIME_OF_BRANCH="$(git log -1 --pretty=format:%ai "$OLDEST")"
echo
echo "Current branch $CURRENT:"
echo " Branched from $REFERENCE at $BRANCH_POINT"
echo
echo "The following branches (newer than $HUMAN_TIME_OF_BRANCH) have conflicts with $CURRENT:"
function authors() {
revisionRange="$1"
git log "$revisionRange" --pretty=format:%an | sort | uniq | paste -s -d',' - | sed -e 's/,/, /g'
}
function showConflicts() {
BASE="$1"
A="$2"
B="$3"
if git merge-tree "$(git merge-base "$A" "$B")" "$A" "$B" | grep "^changed in both" > /dev/null; then
echo
echo "$i"
AUTHORS=$(authors "$REFERENCE".."$A")
echo " $AUTHORS"
git merge-tree "$(git merge-base "$A" "$B")" "$A" "$B" | sed -e "/^changed in both/{N;s/.*\\n//;p;};d" | cut -b 58- | cat -n
fi
}
for i in $(git branch -r | grep -v ' -> '); do
# It would be ideal to find the time of the newest commit on the branch
# instead of the time of the commit at the head of the branch,
# but doing that makes the script too slow;
# is there a faster way to implements newestCommit?
# NEWEST="$(newestCommit "$REFERENCE".."$i")"
TIME_OF_I="$(git log -1 --pretty=format:%at "$i")"
if [ "$TIME_OF_I" -ge "$TIME_OF_BRANCH" ]; then
showConflicts "$REFERENCE" "$i" HEAD
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment