Skip to content

Instantly share code, notes, and snippets.

@nvloff
Created August 15, 2013 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nvloff/6243188 to your computer and use it in GitHub Desktop.
Save nvloff/6243188 to your computer and use it in GitHub Desktop.
Git branch by time - get all branches, group them, sort them by last commit, and check if they're merged in the current branch
#!/bin/bash
HASH="%C(yellow)%h%Creset"
HASH_WHITE="%C(white)%h%Creset"
AUTHOR_RELATIVE_TIME="%Cgreen(%ar)%Creset"
COMMIT_RELATIVE_TIME="%Cgreen(%cr)%Creset"
AUTHOR="%C(bold blue)<%an>%Creset"
REFS="%C(red)%d%Creset"
UNIX_TIME="%ct"
SUBJECT="%C(magenta)%s%Creset"
SUBJECT_WHITE="%C(white)%s%Creset"
FORMAT="$HASH $AUTHOR_RELATIVE_TIME $AUTHOR $REFS $SUBJECT_WHITE"
BRANCH_FORMAT="$UNIX_TIME||$COMMIT_RELATIVE_TIME||$REFS|| ||$HASH_WHITE||# $SUBJECT"
BRANCH_FORMAT_NOT_MERGED="$UNIX_TIME||$COMMIT_RELATIVE_TIME||$REFS||+||$HASH||# $SUBJECT"
simplify_relative_date() {
# Repalce (2 years ago) with (2 years)
sed -Ee 's/(^[^<]*) ago\)/\1)/' |
# Replace (2 years, 5 months) with (2 years)
sed -Ee 's/(^[^<]*), [[:digit:]]+ .*months?\)/\1)/'
}
pager() {
# Page only if we need to
less -FXRS
}
all_refs() {
git show-ref $* |
# don't show tags
grep -v tags |
# get only the commit-id
cut -d ' ' -f 1-1
}
show_refs() {
for ref in $(all_refs $*)
do
if [ "$(git merge-base HEAD $ref)" == $ref ]; then
git log --no-walk --pretty=format:"${BRANCH_FORMAT}\n" $ref
else
git log --no-walk --pretty=format:"${BRANCH_FORMAT_NOT_MERGED}\n" $ref
fi
done
}
git_branch_by_time() {
echo -e "$(show_refs $*)" |
# sort refs by unix timestamp
sort -nr |
# git log --decorate groups all the refs that point to the same thing
uniq |
# simplify the relative date
simplify_relative_date |
# remove the unix timestamp |
cut -d '|' -f2- |
# escape special symbols so that column can work
cat -v - |
# format the output in columns
column -t -s "|" |
# replace the escaped symbols with real ones
sed -Ee $'s/\^\[/\e/g' |
pager
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment