Skip to content

Instantly share code, notes, and snippets.

@suntong
Forked from goldie-lin/git-bash.awk
Last active July 17, 2019 13:28
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save suntong/9a7afcfd1f18aa5ba49c993036d38f04 to your computer and use it in GitHub Desktop.
git-prompt.sh re-implemented with gawk. >100x faster under git-win (requires git version >= 2.11.0)
#!/usr/bin/env bash
git rev-parse 2>/dev/null && \
git status --porcelain=v2 -b --ignored | gawk '
/^# branch\.head / { head = $0; sub(/# branch\.head /, "", head); next; }
/^# branch\.ab / { match($0, /^# branch\.ab \+([0-9]+) -([0-9]+)$/, ab); ahead = ab[1]; behind = ab[2]; next; }
/^! / { next; } /* ignored */
/^[12] [^.]. / { staged++; next; }
/^[12] \.[MD] / { changed++; next; }
/^\? / { untracked++; next; }
/^[12] U. / { conflicts++; next; }
/^[12] .U / { conflicts++; next; }
/^[12] DD / { conflicts++; next; }
/^[12] AA / { conflicts++; next; }
END {
printf("(");
{
printf("\033[0;32m%s\033[0m", head);
if (untracked + conflicts + changed + staged != 0) {
printf(":");
if (staged ) printf("\033[0;32m+%d\033[0m", staged );
if (changed ) printf("\033[0;31m*%d\033[0m", changed );
if (untracked) printf("\033[0;35m%%%d\033[0m", untracked);
if (conflicts) printf("\033[1;41;30m!%d\033[0m", conflicts);
}
if (behind + ahead != 0) {
printf("\033[1;40;31m↓%d\033[0m", behind );
printf("\033[1;40;36m↑%d\033[0m", ahead );
} else {
printf("\033[0m=");
}
}
printf("\033[0m)");
}
'
# vim: set ft=awk:
#!/usr/bin/env bash
git rev-parse 2>/dev/null && \
git status --porcelain=v2 -b --ignored | gawk '
/^# branch\.head / { head = $0; sub(/# branch\.head /, "", head); next; }
/^# branch\.ab / { match($0, /^# branch\.ab \+([0-9]+) -([0-9]+)$/, ab); ahead = ab[1]; behind = ab[2]; next; }
/* /^! / { next; } ignored */
/^[12] [^.]. / { staged++; next; }
/^[12] \.[MD] / { changed++; next; }
/^\? / { untracked++; next; }
/^[12] U. / { conflicts++; next; }
/^[12] .U / { conflicts++; next; }
/^[12] DD / { conflicts++; next; }
/^[12] AA / { conflicts++; next; }
END {
printf("(");
{
printf(head);
if (untracked + conflicts + changed + staged != 0) {
printf(":");
if (staged ) printf("+%d", staged );
if (changed ) printf("*%d", changed );
if (untracked) printf("%%%d", untracked);
if (conflicts) printf("!%d", conflicts);
}
if (behind + ahead != 0) {
printf("'\''%d", ahead );
printf(",%d", behind );
} else {
printf("=");
}
}
printf(")");
}
'
# vim: set ft=awk:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment