Skip to content

Instantly share code, notes, and snippets.

@minrk
Last active December 7, 2023 11:53
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 minrk/2aed18c552f3239175145f4694548402 to your computer and use it in GitHub Desktop.
Save minrk/2aed18c552f3239175145f4694548402 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
r"""
Count frequency of git alias use, including expanded alias in count output
run with `alias | python3 countalias.py`
for https://social.jvns.ca/@b0rk/111535523669398292
Almost enough (doesn't recover original alias) in bash:
gitalias=$(alias | grep "='git" | cut -f 2 -d' ' | cut -d= -f 1 | perl -pe 'chomp if eof' | tr '\n' '\r' | sed -e 's/\r/|/g')
atuin history list --cmd-only | grep -Eo "^(${gitalias})\b" | sort | uniq -c | sort -rn
"""
import sys
from subprocess import run
from collections import Counter
git_aliases = {}
for line in sys.stdin:
line = line.strip()
_, _, alias = line.partition(" ")
name, _, command = alias.partition("=")
if command.lstrip("'\"").startswith("git"):
git_aliases[name] = line
# or bash history, etc.
history_cmd = ["atuin", "history", "list", "--cmd-only"]
history = run(history_cmd, capture_output=True, check=True, text=True).stdout.splitlines()
# count the use of each alias
counts = Counter()
for line in sorted(history):
if not line:
continue
exe, *rest = line.split(None, 2)
# include all git commands together, or just aliases?
# if exe == "git":
# counts.update([" ".join([exe] + rest[:1])])
if exe in git_aliases:
counts.update([exe])
# report counts, including expanded alias
for cmd, n in counts.most_common():
cmd = git_aliases.get(cmd, cmd)
print(f"{n:>4} {cmd}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment