Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
363f15abd3 | |||
8089a46d50 | |||
18947be24d |
82
XD.c
82
XD.c
@ -9,26 +9,27 @@
|
|||||||
#include <git2.h>
|
#include <git2.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
enum face { EYES, NOSE, MOUTH };
|
#ifdef ERR
|
||||||
|
|
||||||
void
|
void
|
||||||
l(const char *fmt, ...)
|
l(const char *fmt, ...)
|
||||||
{
|
{
|
||||||
#ifdef ERR
|
va_list ap;
|
||||||
va_list ap;
|
|
||||||
|
|
||||||
va_start(ap, fmt);
|
va_start(ap, fmt);
|
||||||
vfprintf(stderr, fmt, ap);
|
vfprintf(stderr, fmt, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
|
||||||
if (fmt[0] && fmt[strlen(fmt) - 1] == ':') {
|
if (fmt[0] && fmt[strlen(fmt) - 1] == ':') {
|
||||||
fputc(' ', stderr);
|
fputc(' ', stderr);
|
||||||
perror(NULL);
|
perror(NULL);
|
||||||
} else {
|
} else {
|
||||||
fputc('\n', stderr);
|
fputc('\n', stderr);
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
#define L(...) l(__VA_ARGS__)
|
||||||
|
#else
|
||||||
|
#define L(...)
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef GIT
|
#ifdef GIT
|
||||||
/**
|
/**
|
||||||
@ -43,17 +44,17 @@ git_repository
|
|||||||
git_repository *repo;
|
git_repository *repo;
|
||||||
|
|
||||||
if (git_libgit2_init() < 0) {
|
if (git_libgit2_init() < 0) {
|
||||||
l("Failed to initalize libgit2, proceeding without git functionality enabled.");
|
L("Failed to initalize libgit2, proceeding without git functionality enabled.");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (git_repository_discover(&buf, ".", 0, NULL) < 0) {
|
if (git_repository_discover(&buf, ".", 0, NULL) < 0) {
|
||||||
l("Failed to discover git repo: %s", git_error_last()->message);
|
L("Failed to discover git repo: %s", git_error_last()->message);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (git_repository_open(&repo, buf.ptr) < 0) {
|
if (git_repository_open(&repo, buf.ptr) < 0) {
|
||||||
l("Failed to open git repo: %s", git_error_last()->message);
|
L("Failed to open git repo: %s", git_error_last()->message);
|
||||||
git_buf_dispose(&buf);
|
git_buf_dispose(&buf);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -77,10 +78,9 @@ has_stashes(git_repository *repo)
|
|||||||
|
|
||||||
e = git_reference_lookup(&stash, repo, "refs/stash");
|
e = git_reference_lookup(&stash, repo, "refs/stash");
|
||||||
if (e == GIT_ENOTFOUND) {
|
if (e == GIT_ENOTFOUND) {
|
||||||
git_error_clear();
|
|
||||||
return 0;
|
return 0;
|
||||||
} else if (e < GIT_OK) {
|
} else if (e < 0) {
|
||||||
l("Error looking up stash reference: %s", git_error_last()->message);
|
L("Error looking up stash reference: %s", git_error_last()->message);
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
e = 1;
|
e = 1;
|
||||||
@ -108,7 +108,7 @@ has_untracked(git_repository *repo)
|
|||||||
GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX;
|
GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX;
|
||||||
|
|
||||||
if (git_status_list_new(&list, repo, &opts) < 0) {
|
if (git_status_list_new(&list, repo, &opts) < 0) {
|
||||||
l("Error checking for untracked changes: %s", git_error_last()->message);
|
L("Error checking for untracked changes: %s", git_error_last()->message);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,6 +121,12 @@ has_untracked(git_repository *repo)
|
|||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief check for staged changes
|
||||||
|
*
|
||||||
|
* @param repo git repository object
|
||||||
|
* @return 1 if any staged changes found 0 otherwise
|
||||||
|
*/
|
||||||
int
|
int
|
||||||
has_staged(git_repository *repo)
|
has_staged(git_repository *repo)
|
||||||
{
|
{
|
||||||
@ -129,7 +135,7 @@ has_staged(git_repository *repo)
|
|||||||
int i, c, r = 0;
|
int i, c, r = 0;
|
||||||
|
|
||||||
if (git_status_list_new(&list, repo, NULL) < 0) {
|
if (git_status_list_new(&list, repo, NULL) < 0) {
|
||||||
l("Error checking for staged changes: %s", git_error_last()->message);
|
L("Error checking for staged changes: %s", git_error_last()->message);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -156,7 +162,6 @@ int
|
|||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int i, code = -1;
|
int i, code = -1;
|
||||||
char face[] = { ':', 0, '|' };
|
|
||||||
|
|
||||||
/* print version information */
|
/* print version information */
|
||||||
for (i = 1; i < argc; i++) {
|
for (i = 1; i < argc; i++) {
|
||||||
@ -177,23 +182,26 @@ main(int argc, char *argv[])
|
|||||||
if (git_ok) {
|
if (git_ok) {
|
||||||
/* change the eyes depending on the current git repo's status */
|
/* change the eyes depending on the current git repo's status */
|
||||||
if (has_stashes(repo)) {
|
if (has_stashes(repo)) {
|
||||||
face[EYES] = '8'; /* goggle eyes if we have some stashed changes */
|
printf("8"); /* goggle eyes if we have some stashed changes */
|
||||||
} else if (git_repository_is_empty(repo)) {
|
} else if (git_repository_is_empty(repo)) {
|
||||||
face[EYES] = 'B'; /* sunglasses if we're in a new repo with no HEAD */
|
printf("B"); /* sunglasses if we're in a new repo with no HEAD */
|
||||||
} else {
|
} else {
|
||||||
face[EYES] = ';'; /* wink when we're in a git repo */
|
printf(";"); /* wink when we're in a git repo */
|
||||||
}
|
}
|
||||||
|
|
||||||
/* change the nose depending on the current git repo's status */
|
/* change the nose depending on the current git repo's status */
|
||||||
if (has_staged(repo)) {
|
if (has_staged(repo)) {
|
||||||
face[NOSE] = '*'; /* change to broken nose for staged changes */
|
printf("*"); /* change to broken nose for staged changes */
|
||||||
} else if (has_untracked(repo)) {
|
} else if (has_untracked(repo)) {
|
||||||
face[NOSE] = '^'; /* add a little nose when there are untracked changes in the repo */
|
printf("^"); /* add a little nose when there are untracked changes in the repo */
|
||||||
} else if (git_repository_head_detached(repo)) {
|
} else if (git_repository_head_detached(repo)) {
|
||||||
face[NOSE] = '-'; /* add a minus nose when the HEAD is detached */
|
printf("-"); /* add a minus nose when the HEAD is detached */
|
||||||
}
|
}
|
||||||
}
|
} else
|
||||||
#endif
|
#endif
|
||||||
|
if (1) {
|
||||||
|
printf(":");
|
||||||
|
}
|
||||||
|
|
||||||
/* get exit code from user args */
|
/* get exit code from user args */
|
||||||
if (argv[1]) {
|
if (argv[1]) {
|
||||||
@ -203,20 +211,18 @@ main(int argc, char *argv[])
|
|||||||
/* change mouth based on exit code */
|
/* change mouth based on exit code */
|
||||||
if (code >= 0) {
|
if (code >= 0) {
|
||||||
switch (code) {
|
switch (code) {
|
||||||
case 0: face[MOUTH] = ')'; break; /* all good */
|
case 0: printf(")"); break; /* all good */
|
||||||
case 130: face[MOUTH] = 'O'; break; /* Ctrl-c pressed (SIGTERM) */
|
case 130: printf("O"); break; /* Ctrl-c pressed (SIGTERM) */
|
||||||
case 126: face[MOUTH] = 'P'; break; /* permission denied */
|
case 126: printf("P"); break; /* permission denied */
|
||||||
case 127: face[MOUTH] = '/'; break; /* command not found */
|
case 127: printf("/"); break; /* command not found */
|
||||||
default: face[MOUTH] = '('; break; /* all other codes (usually the program saying it has failed) */
|
default: printf("("); break; /* all other codes (usually the program saying it has failed) */
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
face[MOUTH] = '|'; /* no code info */
|
printf("|"); /* no code info */
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef GIT
|
#ifdef GIT
|
||||||
git_repository_free(repo);
|
git_repository_free(repo);
|
||||||
git_libgit2_shutdown();
|
git_libgit2_shutdown();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
printf("%c%c%c", face[EYES], face[NOSE], face[MOUTH]);
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user