Last active
April 18, 2023 08:47
-
-
Save pvdb/04fd1bdbe9936f2de9651a8c814d5e1a to your computer and use it in GitHub Desktop.
list branches (refs, really) ordered by "recency"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# frozen_string_literal: true | |
# | |
# INSTALLATION | |
# | |
# ln -s ${PWD}/git-branch--list--recent $(brew --prefix)/bin/ | |
# sudo ln -s ${PWD}/git-branch--list--recent /usr/local/bin/ | |
# | |
# 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! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment