#!/usr/bin/awk -f # Adds a "git describe" (nearest tag) output for `git log` (with support for # color escapes and --graph markers). --oneline is not supported. # # Copyright (c) 2013 Peter Wu # Licensed under GPLv3 or any latter version function has_color(str) { if (str ~ /\033/) return 1; return 0; } { print; } { is_color = has_color($0); # strip color escapes for easier text matching gsub(/\033\[[0-9]*m/, ""); text_len = length($0); # remove --graph markers sub(/^[*|][*|_/\\ ]*/, ""); indent_len = length($0) - text_len; } # match only git's "commit" label, not something from the commit message. $0 ~ /^commit/ && $1 == "commit" && $2 ~ /^[0-9a-f]{5,40}$/ { hash = $2; printf("%" indent_len "sDescribe: ", ""); if (is_color) printf("\033[96m"); # light cyan system("git describe --first-parent " hash " | tr -d '\n'"); if (is_color) printf("\033[m"); printf("\n"); }