Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
202142a8af | |||
6e5d8a1e6a | |||
d1752c3ac7 | |||
e6029a68e3 | |||
4af00678ca | |||
cab498199e | |||
3fb00b615c | |||
2dc0d582e6 | |||
2a6385fa36 |
2
XD.1
2
XD.1
@ -35,7 +35,7 @@ tab(;) allbox;
|
||||
c;l.
|
||||
|;no signal provided
|
||||
);previous signal is 0
|
||||
O;SIGINT sen't (Ctrl-c)
|
||||
O;SIGINT sent (Ctrl-c)
|
||||
P;permission denied
|
||||
/;command not found
|
||||
(;previous signal is failure
|
||||
|
132
XD.c
132
XD.c
@ -6,9 +6,13 @@
|
||||
#endif
|
||||
|
||||
#ifdef GIT
|
||||
#include <sys/stat.h>
|
||||
#include <dirent.h>
|
||||
#include <git2.h>
|
||||
#endif
|
||||
|
||||
#define P(X) fwrite(X, 1, 1, stdout)
|
||||
|
||||
#ifdef ERR
|
||||
void
|
||||
l(const char *fmt, ...)
|
||||
@ -32,6 +36,45 @@ l(const char *fmt, ...)
|
||||
#endif
|
||||
|
||||
#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
|
||||
*
|
||||
@ -40,27 +83,38 @@ l(const char *fmt, ...)
|
||||
git_repository
|
||||
*init_git()
|
||||
{
|
||||
git_buf buf = GIT_BUF_INIT_CONST(NULL, 0);
|
||||
char *buf;
|
||||
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_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) {
|
||||
L("Failed to initalize libgit2, proceeding without git functionality enabled.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (git_repository_discover(&buf, ".", 0, NULL) < 0) {
|
||||
L("Failed to discover git repo: %s", git_error_last()->message);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (git_repository_open(&repo, buf.ptr) < 0) {
|
||||
if (git_repository_open(&repo, buf) < 0) {
|
||||
L("Failed to open git repo: %s", git_error_last()->message);
|
||||
git_buf_dispose(&buf);
|
||||
free(buf);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* get rid of object containing git repo path and return the repo */
|
||||
git_buf_dispose(&buf);
|
||||
free(buf);
|
||||
return repo;
|
||||
}
|
||||
|
||||
@ -132,9 +186,13 @@ has_staged(git_repository *repo)
|
||||
{
|
||||
git_status_entry entry;
|
||||
git_status_list *list = NULL;
|
||||
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
|
||||
int i, c, r = 0;
|
||||
|
||||
if (git_status_list_new(&list, repo, NULL) < 0) {
|
||||
opts.show = GIT_STATUS_SHOW_INDEX_ONLY;
|
||||
opts.flags = GIT_STATUS_INDEX_NEW;
|
||||
|
||||
if (git_status_list_new(&list, repo, &opts) < 0) {
|
||||
L("Error checking for staged changes: %s", git_error_last()->message);
|
||||
return 0;
|
||||
}
|
||||
@ -144,9 +202,9 @@ has_staged(git_repository *repo)
|
||||
for (i = 0; i < c; i++) {
|
||||
entry = *git_status_byindex(list, i);
|
||||
|
||||
if (entry.status & GIT_STATUS_INDEX_NEW
|
||||
|| entry.status & GIT_STATUS_INDEX_DELETED
|
||||
|| entry.status & GIT_STATUS_INDEX_MODIFIED) {
|
||||
if (entry.status & (GIT_STATUS_INDEX_NEW
|
||||
| GIT_STATUS_INDEX_DELETED
|
||||
| GIT_STATUS_INDEX_MODIFIED)) {
|
||||
r = 1;
|
||||
break;
|
||||
}
|
||||
@ -161,46 +219,41 @@ has_staged(git_repository *repo)
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int i, code = -1;
|
||||
int code = -1;
|
||||
|
||||
/* print version information */
|
||||
for (i = 1; i < argc; i++) {
|
||||
if (strcmp(argv[i], "-v") == 0) {
|
||||
printf("XD v%s\n", VERSION);
|
||||
return 0;
|
||||
}
|
||||
if (argc > 1 && strcmp(argv[1], "-v") == 0) {
|
||||
printf("XD [number] v%s\n", VERSION);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef GIT
|
||||
git_repository *repo;
|
||||
int git_ok = 0;
|
||||
|
||||
if ((repo = init_git())) {
|
||||
git_ok = 1;
|
||||
}
|
||||
|
||||
if (git_ok) {
|
||||
/* change the eyes depending on the current git repo's status */
|
||||
if (has_stashes(repo)) {
|
||||
printf("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)) {
|
||||
printf("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 {
|
||||
printf(";"); /* 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 */
|
||||
if (has_staged(repo)) {
|
||||
printf("*"); /* change to broken nose for staged changes */
|
||||
P("*"); /* change to broken nose for staged changes */
|
||||
} else if (has_untracked(repo)) {
|
||||
printf("^"); /* 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)) {
|
||||
printf("-"); /* 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
|
||||
if (1) {
|
||||
printf(":");
|
||||
P(":");
|
||||
}
|
||||
|
||||
/* get exit code from user args */
|
||||
@ -211,18 +264,13 @@ main(int argc, char *argv[])
|
||||
/* change mouth based on exit code */
|
||||
if (code >= 0) {
|
||||
switch (code) {
|
||||
case 0: printf(")"); break; /* all good */
|
||||
case 130: printf("O"); break; /* Ctrl-c pressed (SIGTERM) */
|
||||
case 126: printf("P"); break; /* permission denied */
|
||||
case 127: printf("/"); break; /* command not found */
|
||||
default: printf("("); break; /* all other codes (usually the program saying it has failed) */
|
||||
case 0: P(")"); break; /* all good */
|
||||
case 130: P("O"); break; /* Ctrl-c pressed (SIGTERM) */
|
||||
case 126: P("P"); break; /* permission denied */
|
||||
case 127: P("/"); break; /* command not found */
|
||||
default: P("("); break; /* all other codes (usually the program saying it has failed) */
|
||||
}
|
||||
} else {
|
||||
printf("|"); /* no code info */
|
||||
P("|"); /* no code info */
|
||||
}
|
||||
|
||||
#ifdef GIT
|
||||
git_repository_free(repo);
|
||||
git_libgit2_shutdown();
|
||||
#endif
|
||||
}
|
||||
|
Reference in New Issue
Block a user