Skip to content

Instantly share code, notes, and snippets.

@sinelaw
Last active June 2, 2016 13:13
Show Gist options
  • Save sinelaw/d16342b96b007ba63cb66bda3e2617b1 to your computer and use it in GitHub Desktop.
Save sinelaw/d16342b96b007ba63cb66bda3e2617b1 to your computer and use it in GitHub Desktop.
Given branch/ref (e.g. my_branch) and target (e.g. origin/master), lists commits in branch that have no "equivalent" ones in target (by author, date and message)
#!/bin/bash
set -eu
# Similar to git branch --merged but looks at commit author, date and
# subject, instead of hash.
#
# Given branch/ref (e.g. my_branch) and target (e.g. origin/master),
# lists commits in branch that have no "equivalent" ones in target (by
# author, date and message).
# Example usage:
#
# git unmerged-commits origin/master my_branch -- my_path
#
# You can use it to (very slowly) list all branches that are merged
# (even after being rebased, so that commit hashes don't matter):
#
# git branch | grep -vF '*' | xargs -n1 -iBRANCH bash -c 'git unmerged-commits origin/master BRANCH >/dev/null || echo BRANCH'
#
TARGET="$1" # e.g. origin/master
shift
BRANCH="$1" # e.g. my_branch
shift
EXTRA_ARGS_TO_GIT_LOG="$@"
MERGE_BASE=$(git merge-base $TARGET $BRANCH)
list_commits() {
HEAD="$1"
shift
EXTRA_ARGS="$@"
git log $MERGE_BASE..$HEAD --format="%an %ai %s" $EXTRA_ARGS
}
TARGET_COMMITS_FILE="$(mktemp)"
cleanup() {
test -f "$TARGET_COMMITS_FILE" && rm "$TARGET_COMMITS_FILE"
}
trap "cleanup" EXIT
list_commits $TARGET $EXTRA_ARGS_TO_GIT_LOG > $TARGET_COMMITS_FILE
list_commits $BRANCH $EXTRA_ARGS_TO_GIT_LOG | grep -vxFf $TARGET_COMMITS_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment