I'm trying to make a quick bash function that returns a --oneline log of every commit since the current branch was branched off of its parent, and including the line for that previous commit. Here is what I have so far.
# stolen from another Stack Overflow question
__git__get_parent_branch() {
echo $(git show-branch 2>/dev/null |
sed "s/].*//" |
grep "\*" |
grep -v "$(git rev-parse --abbrev-ref HEAD)" |
head -n1 |
sed "s/^.*\[//" |
sed "s/[\^\~].*$//")
}
lp() {
if [[ $(git rev-parse --abbrev-ref HEAD) = master ]]; then
echo Already on master branch.
return 1
fi
local commits="$(git log --oneline $(git merge-base $(__git__get_parent_branch) HEAD)..HEAD)"
[[ -z $commits ]] && 1>&2 echo No commits since $(__git__get_parent_branch). || echo -e "$commits"
}
This correctly gets every commit after the parent's commit. I had thought I could just append ^ or ~1 to the git merge-base result:
local commits="$(git log --oneline $(git merge-base $(__git__get_parent_branch) HEAD)^..HEAD)"
# or
local commits="$(git log --oneline $(git merge-base $(__git__get_parent_branch) HEAD)~1..HEAD)"
However, this doesn't seem to work if the branched SHA was of a merge. Since most of my branches are off of master, this will always be the case. Instead of showing the merge SHA, it shows every entry since the first commit of that merge. If a merge contained two commits, I see the lines I want to see, plus two additional ones I don't want.
I believe the crux of this problem is that ^ and ~1 don't do quite what I want.
(I also see that my [[ -z $commits ]] won't work if I achieve this, but that's something I can solve later.)
Edit: Perhaps the simplest solution is just to retrieve and append the line directly.
local parent="$(__git__get_parent_branch)"
local commits="$(git log --oneline $(git merge-base $parent HEAD)..HEAD)
$(git log --oneline -1 $parent)"