Skip to content

Instantly share code, notes, and snippets.

@pvdb
Last active July 19, 2025 07:38
Show Gist options
  • Save pvdb/04fd1bdbe9936f2de9651a8c814d5e1a to your computer and use it in GitHub Desktop.
Save pvdb/04fd1bdbe9936f2de9651a8c814d5e1a to your computer and use it in GitHub Desktop.

Revisions

  1. pvdb created this gist Apr 18, 2023.
    66 changes: 66 additions & 0 deletions git-branch--list--recent.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    #!/usr/bin/env ruby
    # frozen_string_literal: true

    #
    # INSTALLATION
    #
    # ln -s ${PWD}/git-branch--list--recent.rb $(brew --prefix)/bin/git-branch--list--recent
    # sudo ln -s ${PWD}/git-branch--list--recent.rb /usr/local/bin/git-branch--list--recent
    #
    # CONFIGURATION
    #
    # git config --global alias.blr branch--list--recent
    #
    # INSPIRATION
    #
    # https://github.com/paulirish/git-recent
    #

    nl = '%0a' # ASCII NEWLINE
    delim = '%07' # ASCII BELL

    head = '%(if)%(HEAD)%(then)%(end)' # emoji prefix for current branch
    empty = '%(color:black) %(color:reset)' # to make `column` work correctly

    pattern, deref = if ARGV.delete('--tags')
    ['refs/tags', '*']
    elsif ARGV.delete('--all')
    'refs/heads refs/remotes'
    elsif ARGV.delete('--remotes')
    'refs/remotes'
    else
    'refs/heads'
    end

    branch_name = '%(refname:short)'
    branch_name = "%(color:yellow)#{branch_name}%(color:reset)"

    object_name = '%(objectname:short)'
    object_name = "%(color:red)#{object_name}%(color:reset)"

    commit_date = "%(#{deref}committerdate:relative)"
    commit_date = "%(color:bold green)(#{commit_date})%(color:reset)"

    author_name = "%(#{deref}authorname)"
    author_name = "%(color:bold blue)#{author_name}%(color:reset)"

    commit_subject = '%(contents:subject)'

    format = "#{head} #{branch_name}#{delim}"
    format = "#{format}#{object_name} #{commit_date} #{author_name}"
    format = "#{format}#{nl}#{empty}#{delim}#{commit_subject}"
    format = "#{format}#{nl}#{empty}#{delim}" # empty separator line

    require 'shellwords'

    cmd = <<-"EOCMD"
    git for-each-ref \
    --color=always \
    --sort=-committerdate \
    --format="#{format}" \
    #{pattern} #{ARGV.shelljoin} | column -t -s $'\a' | less --RAW-CONTROL-CHARS
    EOCMD

    Kernel.exec(cmd)

    # That's all Folks!
    64 changes: 64 additions & 0 deletions git-branch--list--recent.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    #!/usr/bin/env bash

    #
    # INSTALLATION
    #
    # ln -s ${PWD}/git-branch--list--recent.sh $(brew --prefix)/bin/git-branch--list--recent
    # sudo ln -s ${PWD}/git-branch--list--recent.sh /usr/local/bin/git-branch--list--recent
    #
    # CONFIGURATION
    #
    # git config --global alias.blr branch--list--recent
    #
    # INSPIRATION
    #
    # https://github.com/paulirish/git-recent
    #

    NL='%0a' ; # ASCII NEWLINE
    DELIM='%07' ; # ASCII BELL

    HEAD="%(if)%(HEAD)%(then)%(end)" ; # emoji prefix for current branch
    EMPTY='%(color:black) %(color:reset)' ; # to make `column` work correctly

    case $1 in
    --all)
    PATTERN='refs/heads refs/remotes' ;
    DEREF='' ;
    shift ;
    ;;
    --remotes)
    PATTERN='refs/remotes' ;
    DEREF='' ;
    shift ;
    ;;
    --tags)
    PATTERN='refs/tags' ;
    DEREF='*' ;
    shift ;
    ;;
    *)
    PATTERN='refs/heads' ;
    DEREF='' ;
    ;;
    esac

    BRANCH_NAME="%(color:yellow)%(refname:short)%(color:reset)" ;
    OBJECT_NAME="%(color:red)%(objectname:short)%(color:reset)" ;
    COMMIT_DATE="%(color:bold green)(%(${DEREF}committerdate:relative))%(color:reset)" ;
    AUTHOR_NAME="%(color:bold blue)%(${DEREF}authorname)%(color:reset)" ;

    COMMIT_SUBJECT='%(contents:subject)' ;

    FORMAT="${HEAD} ${BRANCH_NAME}${DELIM}" ;
    FORMAT="${FORMAT}${OBJECT_NAME} ${COMMIT_DATE} ${AUTHOR_NAME}" ;
    FORMAT="${FORMAT}${NL}${EMPTY}${DELIM}${COMMIT_SUBJECT}" ;
    FORMAT="${FORMAT}${NL}${EMPTY}${DELIM}" ; # empty separator line

    git for-each-ref \
    --color=always \
    --sort=-committerdate \
    --format="${FORMAT}" \
    "${PATTERN}" "$@" | column -t -s $'\a' | less --RAW-CONTROL-CHARS ;

    # That's all Folks!