Skip to content

Instantly share code, notes, and snippets.

@lionello
Last active August 29, 2015 13:56
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 lionello/9166529 to your computer and use it in GitHub Desktop.
Save lionello/9166529 to your computer and use it in GitHub Desktop.
__git_ps1 for 4NT/TCC, shows current GIT branch in prompt
/*
@cl "/Tp%~f0" /nologo /GS- /link /SUBSYSTEM:console /nodefaultlib /entry:_main kernel32.lib
@goto :EOF
__git_ps1 for 4NT/TCC, by Lionello Lunesu, placed in the Public Domain
Usage: prompt %%@exec[@gb.exe]$e[1m$P$e[0m$_$+$g
*/
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
const WCHAR root[] = L"..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\.git\\HEAD";
int __stdcall _main() {
int offset = sizeof(root)/2 - 10;
while (offset >= 0) {
HANDLE h = CreateFileW(root + offset, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (h != INVALID_HANDLE_VALUE) {
char buf[64];
DWORD read = 0;
if (ReadFile(h, buf, sizeof(buf), &read, NULL) && read > 16) {
DWORD off = 0;
DWORD len = 7; // show 7 hex digits
if ((int&)buf[0] == ':fer') {
off = 16; // skip ref: refs/heads/
len = read - off ; // keep LF
}
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buf + off, len, NULL, NULL);
}
return 13 - offset / 3;
}
offset -= 3;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment