10 Commits
v1.1 ... v2.0

Author SHA1 Message Date
e6029a68e3 version bump
and fix typo
2024-12-21 21:43:01 -05:00
4af00678ca optimize...
make the stage check faster by filtering the status list
2024-12-21 21:36:54 -05:00
cab498199e optimizations :)
instead of initializing libgit2 and then looking for a repo, we
do a naive check to see if one exists and then if so initialize
libgit2
2024-12-21 21:35:07 -05:00
3fb00b615c don't recursively check for -v arg 2024-12-21 21:33:12 -05:00
2dc0d582e6 change libgit2 initialization 2024-12-21 21:32:39 -05:00
2a6385fa36 optimizing...
replace printf with fwrite to make it ever so slightly faster
2024-12-19 13:41:57 -06:00
363f15abd3 add some missing stuff 2024-12-19 13:17:06 -06:00
8089a46d50 optimize further...
make sure to not include useless function calls when errors arent
enabled by wrapping all l calls in a macro
2024-12-19 12:30:55 -06:00
18947be24d optimise
instead of putting each part of the face into an array we just
print it out to the console :)
2024-12-19 12:26:31 -06:00
d3f83e3af8 change compilation flags 2024-12-19 12:05:05 -06:00
4 changed files with 124 additions and 68 deletions

View File

@ -2,7 +2,7 @@ include config.mk
# flags and incs # flags and incs
PKGS = $(GITLIB) PKGS = $(GITLIB)
CFLAGS = -DVERSION=\"$(VERSION)\" -Wall -O1 $(GIT) $(ERR) CFLAGS = -DVERSION=\"$(VERSION)\" -Wall -pedantic -O3 $(GIT) $(ERR)
LIBS = `$(PKG_CONFIG) --libs --cflags $(PKGS)` LIBS = `$(PKG_CONFIG) --libs --cflags $(PKGS)`
all: XD all: XD

2
XD.1
View File

@ -35,7 +35,7 @@ tab(;) allbox;
c;l. c;l.
|;no signal provided |;no signal provided
);previous signal is 0 );previous signal is 0
O;SIGINT sen't (Ctrl-c) O;SIGINT sent (Ctrl-c)
P;permission denied P;permission denied
/;command not found /;command not found
(;previous signal is failure (;previous signal is failure

186
XD.c
View File

@ -6,31 +6,75 @@
#endif #endif
#ifdef GIT #ifdef GIT
#include <sys/stat.h>
#include <dirent.h>
#include <git2.h> #include <git2.h>
#endif #endif
enum face { EYES, NOSE, MOUTH }; #define P(X) fwrite(X, 1, 1, stdout)
#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
/**
* @brief search all parent directories for a git repo
*
* @return absolute path to git repo
*/
char
*find_git_repo()
{
char path[PATH_MAX] = ".", *rpath;
struct stat s;
int i, c;
/* find the number of jumps to the root of the fs */
rpath = realpath(path, NULL);
for (i = c = 0; i < strlen(rpath); i++) {
if (rpath[i] == '/') {
c++;
}
}
free(rpath);
/* start searching */
for (i = c; i > 0; i--) {
strcat(path, "/.git");
/* if there seems to be a git directory return the directory it was found in */
if (stat(path, &s) == 0 && S_ISDIR(s.st_mode)) {
return realpath(path, NULL);
}
/* reset contents of gpath, and go up a directory */
memset(&path[strlen(path) - 4], '.', 2);
memset(&path[strlen(path) - 2], 0, 2);
}
return NULL;
}
/** /**
* @brief open git repo if one is available at the current path * @brief open git repo if one is available at the current path
* *
@ -39,27 +83,40 @@ l(const char *fmt, ...)
git_repository git_repository
*init_git() *init_git()
{ {
git_buf buf = GIT_BUF_INIT_CONST(NULL, 0); char *buf;
git_repository *repo; git_repository *repo;
/* check for a repo before loading libgit2 */
if ((buf = find_git_repo()) == NULL) {
return NULL;
}
/* disable a bunch of git options to hopefully speed things up */
git_libgit2_opts(GIT_OPT_ENABLE_CACHING, 0);
git_libgit2_opts(GIT_OPT_SET_MWINDOW_SIZE, 0);
git_libgit2_opts(GIT_OPT_SET_MWINDOW_MAPPED_LIMIT, 0);
git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, "");
git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, "");
git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, "");
git_libgit2_opts(GIT_OPT_SET_TEMPLATE_PATH, "");
git_libgit2_opts(GIT_OPT_DISABLE_PACK_KEEP_FILE_CHECKS, 1);
/* initialize the git library and repository */
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_open(&repo, buf) < 0) {
l("Failed to discover git repo: %s", git_error_last()->message); L("Failed to open git repo: %s", git_error_last()->message);
return NULL; free(buf);
}
if (git_repository_open(&repo, buf.ptr) < 0) {
l("Failed to open git repo: %s", git_error_last()->message);
git_buf_dispose(&buf);
return NULL; return NULL;
} }
/* get rid of object containing git repo path and return the repo */ /* get rid of object containing git repo path and return the repo */
git_buf_dispose(&buf); free(buf);
return repo; return repo;
} }
@ -77,10 +134,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 +164,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,15 +177,25 @@ 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)
{ {
git_status_entry entry; git_status_entry entry;
git_status_list *list = NULL; git_status_list *list = NULL;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
int i, c, r = 0; int i, c, r = 0;
if (git_status_list_new(&list, repo, NULL) < 0) { opts.flags = GIT_STATUS_INDEX_NEW;
l("Error checking for staged changes: %s", git_error_last()->message); opts.show = GIT_STATUS_SHOW_INDEX_ONLY;
if (git_status_list_new(&list, repo, &opts) < 0) {
L("Error checking for staged changes: %s", git_error_last()->message);
return 0; return 0;
} }
@ -138,9 +204,9 @@ has_staged(git_repository *repo)
for (i = 0; i < c; i++) { for (i = 0; i < c; i++) {
entry = *git_status_byindex(list, i); entry = *git_status_byindex(list, i);
if (entry.status & GIT_STATUS_INDEX_NEW if (entry.status & (GIT_STATUS_INDEX_NEW
|| entry.status & GIT_STATUS_INDEX_DELETED | GIT_STATUS_INDEX_DELETED
|| entry.status & GIT_STATUS_INDEX_MODIFIED) { | GIT_STATUS_INDEX_MODIFIED)) {
r = 1; r = 1;
break; break;
} }
@ -155,45 +221,42 @@ has_staged(git_repository *repo)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int i, code = -1; int code = -1;
char face[] = { ':', 0, '|' };
/* print version information */ /* print version information */
for (i = 1; i < argc; i++) { if (strcmp(argv[1], "-v") == 0) {
if (strcmp(argv[i], "-v") == 0) { printf("XD [number] v%s\n", VERSION);
printf("XD v%s\n", VERSION); return 0;
return 0;
}
} }
#ifdef GIT #ifdef GIT
git_repository *repo; git_repository *repo;
int git_ok = 0;
if ((repo = init_git())) { if ((repo = init_git())) {
git_ok = 1;
}
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 */ P("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 */ P("B"); /* sunglasses if we're in a new repo with no HEAD */
} else { } else {
face[EYES] = ';'; /* wink when we're in a git repo */ P(";"); /* 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 */ P("*"); /* 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 */ P("^"); /* 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 */ P("-"); /* add a minus nose when the HEAD is detached */
} }
} git_repository_free(repo);
git_libgit2_shutdown();
} else
#endif #endif
if (1) {
P(":");
}
/* get exit code from user args */ /* get exit code from user args */
if (argv[1]) { if (argv[1]) {
@ -203,20 +266,13 @@ 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: P(")"); break; /* all good */
case 130: face[MOUTH] = 'O'; break; /* Ctrl-c pressed (SIGTERM) */ case 130: P("O"); break; /* Ctrl-c pressed (SIGTERM) */
case 126: face[MOUTH] = 'P'; break; /* permission denied */ case 126: P("P"); break; /* permission denied */
case 127: face[MOUTH] = '/'; break; /* command not found */ case 127: P("/"); break; /* command not found */
default: face[MOUTH] = '('; break; /* all other codes (usually the program saying it has failed) */ default: P("("); break; /* all other codes (usually the program saying it has failed) */
} }
} else { } else {
face[MOUTH] = '|'; /* no code info */ P("|"); /* no code info */
} }
#ifdef GIT
git_repository_free(repo);
git_libgit2_shutdown();
#endif
printf("%c%c%c", face[EYES], face[NOSE], face[MOUTH]);
} }

View File

@ -1,4 +1,4 @@
VERSION = 1.1 VERSION = 2.0
PKG_CONFIG = pkg-config PKG_CONFIG = pkg-config