Skip to content

Instantly share code, notes, and snippets.

@elfsternberg
Created December 13, 2018 16:39
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 elfsternberg/01175deccf9eecd5d6ae7cd92c8295c9 to your computer and use it in GitHub Desktop.
Save elfsternberg/01175deccf9eecd5d6ae7cd92c8295c9 to your computer and use it in GitHub Desktop.
Git extension to list the most recent branches, in most-recent-commit order, with a human-friendly interval display.
#!/usr/bin/env python3
# GIT-RBAGO
#
# For any given git repository, this script will print out the
# (default) ten _most recently worked on_ branches, along with a
# fairly human-readable version of how long ago that was.
#
# Example:
#
# $ git rbago
# develop 19 hours ago
# master 21 hours ago
#
# Requirements:
# Python 3.6+
# timeago (https://github.com/hustcc/timeago)
import re
import sys
import timeago
import datetime
import subprocess
from dateutil.parser import parse
count = 10
if len(sys.argv) > 1:
try:
if sys.argv[1] == "-c":
count = int(sys.argv[2], 10)
else:
raise
except:
print("Usage: git rb [-c count]")
sys.exit(1)
GITCMD = ["git", "for-each-ref", "--sort=-committerdate", "--count={}".format(count),
'--format=%(refname:short)\t%(committerdate:iso)',
"refs/heads/"]
try:
references = [i.decode("utf-8").strip().split("\t")
for i in subprocess.check_output(GITCMD).splitlines()]
except subprocess.CalledProcessError as e:
sys.exit(e.returncode)
maxname = max([len(i[0]) for i in references])
output = "{:" + str(maxname + 7) + "} {}"
now = datetime.datetime.now()
m_re = re.compile(r' -\d+\s*$')
def extract_date(i):
return datetime.datetime.strptime(m_re.sub('', i), '%Y-%m-%d %H:%M:%S')
print("\n".join([
output.format(i[0], timeago.format(extract_date(i[1]), now))
for i in references]))
# COPYRIGHT:
# This program is copyrighted (c) 2018
# Elf M. Sternberg (elf.sternberg@gmail.com)
#
# LICENSE:
# https://creativecommons.org/licenses/by/4.0/
#
# This is free software released under the CC-BY license. Users of
# this software enjoy the following rights and responsibilities:
#
# Share — You may copy and redistribute the material in any medium or format
#
# Adapt — You may remix, transform, and build upon the material for
# any purpose, even commercially.
#
# Attribution — You must give appropriate credit, provide a link to
# the license, and indicate if changes were made. You may do so in any
# reasonable manner, but not in any way that suggests the licensor
# endorses you or your use.
#
# You may not employ technological measures or legal terms that
# legally prevent others from doing anything the license permits. THE
# SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment