Last active
October 17, 2024 14:14
-
-
Save ameiji/1bd466ad479edfe7450219f0e1e5f0f1 to your computer and use it in GitHub Desktop.
git log command wrapper with day by day blocks and colors
This file contains hidden or 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/python3 | |
| """ | |
| Show git log output in a compact way with colors. | |
| MMM. .MMM | |
| MMMMMMMMMMMMMMMMMMM | |
| MMMMMMMMMMMMMMMMMMM ___________________________________ | |
| MMMMMMMMMMMMMMMMMMMMM | | | |
| MMMMMMMMMMMMMMMMMMMMMMM | Emergency plan: git out | | |
| MMMMMMMMMMMMMMMMMMMMMMMM |_ _______________________________| | |
| MMMM::- -:::::::- -::MMMM |/ | |
| MM~:~ ~:::::~ ~:~MM | |
| .. MMMMM::. .:::+:::. .::MMMMM .. | |
| .MM::::: ._. :::::MM. | |
| MMMM;:::::;MMMM | |
| -MM MMMMMMM | |
| ^ M+ MMMMMMMMM | |
| MMMMMMM MM MM MM | |
| MM MM MM MM | |
| MM MM MM MM | |
| .~~MM~MM~MM~MM~~. | |
| ~~~~MM:~MM~~~MM~:MM~~~~ | |
| ~~~~~~==~==~~~==~==~~~~~~ | |
| ~~~~~~==~==~==~==~~~~~~ | |
| :~==~==~==~==~~ | |
| """ | |
| import subprocess | |
| import sys | |
| from os import getenv | |
| from shutil import get_terminal_size | |
| LOG_LIMIT = 28 # default number of log lines to show | |
| REF = "" # file path | |
| def get_git_log(): | |
| cmd = [ | |
| "/usr/bin/git", | |
| "log", | |
| '--pretty="%C(yellow)%h %Cblue%>(12)%ad%x09 %Cgreen%<(10)%aL%Cred%d %Creset%s"', | |
| # "--date=format:%d-%m-%Y %H:%M", | |
| "--date=short", | |
| "--color=always", | |
| "-n", | |
| str(LOG_LIMIT), | |
| ] | |
| if REF: | |
| cmd.append(REF) | |
| try: | |
| output = subprocess.run(cmd, capture_output=True, text=True) | |
| output.check_returncode() | |
| except (FileNotFoundError, subprocess.CalledProcessError, TypeError) as err: | |
| print(err) | |
| if output: | |
| print(output.stderr) | |
| sys.exit(1) | |
| return output.stdout.splitlines() | |
| def print_merged(line): | |
| """Add flag for commit message""" | |
| fields = line.split() | |
| fields[2] = f" {fields[2]} ⇧ " | |
| new_line = " ".join(fields) | |
| print(new_line) | |
| def show_git_log(): | |
| prev = 0 | |
| term_width, _ = map(int, get_terminal_size((60, 10))) | |
| line_width = term_width - 10 | |
| try: | |
| for line in get_git_log(): | |
| line = line[:line_width].lstrip('"') | |
| fields = line.split() | |
| date = fields[2] | |
| header_date = f"---------- {date} " | |
| header = header_date + "-" * (line_width - len(header_date)) | |
| cur = int(date.split("-")[2]) | |
| if prev != 0: | |
| if prev > cur: | |
| print(header) | |
| elif prev == 0: | |
| print(header) | |
| if cur > prev: | |
| print_merged(line) | |
| else: | |
| print(f"{line}") | |
| prev = cur | |
| except IOError: | |
| sys.exit() | |
| except LookupError as err: | |
| log(line) | |
| log(f"{err}") | |
| def log(msg=""): | |
| DEBUG = getenv("DEBUG", None) | |
| if DEBUG: | |
| print(f"DEBUG: {msg}") | |
| def usage(): | |
| print("Git Log Compact") | |
| print(f"Usage: {sys.argv[0]} [max log lines] [ref path]") | |
| sys.exit(1) | |
| if __name__ == "__main__": | |
| try: | |
| if sys.argv[1] == "-h": | |
| usage() | |
| LOG_LIMIT = int(sys.argv[1]) | |
| REF = str(sys.argv[2]) | |
| except LookupError: | |
| pass | |
| except ValueError as err: | |
| REF = sys.argv[1] | |
| show_git_log() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment