add performance logging

This commit is contained in:
Squibid 2025-11-14 10:54:10 -05:00
parent 73c2acd37e
commit 1f96830f10
Signed by: squibid
GPG key ID: BECE5684D3C4005D
5 changed files with 71 additions and 1 deletions

32
XD.c
View file

@ -3,6 +3,7 @@
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#ifdef ERR
#include <errno.h>
@ -31,6 +32,7 @@
char
*find_git_repo(void)
{
PS();
char path[PATH_MAX] = ".", fstr[PATH_MAX], *rpath, *res;
struct stat s;
FILE *f;
@ -40,6 +42,7 @@ char
rpath = realpath(path, NULL);
if (!rpath) {
L("realpath: %s", strerror(errno));
PE();
return NULL;
}
for (i = c = 0; i < strlen(rpath); i++) {
@ -56,22 +59,26 @@ char
/* if there seems to be a git directory return the directory it was found in */
if (stat(path, &s) == 0) {
if (S_ISDIR(s.st_mode)) {
PE();
return realpath(path, NULL);
} else if (S_ISREG(s.st_mode)) {
/* we do some special magic here to check if we're in a submodule */
f = fopen(path, "r");
if (!f) {
L("fopen: %s", strerror(errno));
PE();
return NULL;
}
res = fgets(fstr, PATH_MAX, f);
fclose(f);
if (!res) {
L("fgets: %s", strerror(errno));
PE();
return NULL;
}
if (strncmp(fstr, "gitdir: ", strlen("gitdir: ")) == 0) {
fstr[strlen(fstr) - 1] = '\0';
PE();
return realpath(fstr + strlen("gitdir: "), NULL);
}
}
@ -82,6 +89,7 @@ char
memset(&path[strlen(path) - 2], 0, 2);
}
PE();
return NULL;
}
@ -93,11 +101,13 @@ char
git_repository
*init_git(void)
{
PS();
char *buf;
git_repository *repo;
/* check for a repo before loading libgit2 */
if ((buf = find_git_repo()) == NULL) {
PE();
return NULL;
}
@ -113,17 +123,20 @@ git_repository
/* initialize the git library and repository */
if (git_libgit2_init() < 0) {
L("Failed to initalize libgit2, proceeding without git functionality enabled.");
PE();
return NULL;
}
if (git_repository_open(&repo, buf) < 0) {
L("Failed to open git repo: %s", git_error_last()->message);
free(buf);
PE();
return NULL;
}
/* get rid of object containing git repo path and return the repo */
free(buf);
PE();
return repo;
}
@ -136,20 +149,24 @@ git_repository
int
has_stashes(git_repository *repo)
{
PS();
git_reference *stash = NULL;
int e;
e = git_reference_lookup(&stash, repo, "refs/stash");
if (e == GIT_ENOTFOUND) {
PE();
return 0;
} else if (e < 0) {
L("Error looking up stash reference: %s", git_error_last()->message);
PE();
return 0;
} else {
e = 1;
}
git_reference_free(stash);
PE();
return e;
}
@ -162,6 +179,7 @@ has_stashes(git_repository *repo)
int
has_untracked(git_repository *repo)
{
PS();
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
git_status_list *list = NULL;
repohash *storedhash;
@ -173,6 +191,7 @@ has_untracked(git_repository *repo)
&& storedhash->hash == newhash) {
r = storedhash->changes;
free(storedhash);
PE();
return r;
}
#endif
@ -187,6 +206,7 @@ has_untracked(git_repository *repo)
if (git_status_list_new(&list, repo, &opts) < 0) {
L("Error checking for untracked changes: %s", git_error_last()->message);
PE();
return 0;
}
@ -197,6 +217,7 @@ has_untracked(git_repository *repo)
#endif
git_status_list_free(list);
PE();
return r;
}
@ -209,6 +230,7 @@ has_untracked(git_repository *repo)
int
has_staged(git_repository *repo)
{
PS();
git_status_entry entry;
git_status_list *list = NULL;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
@ -219,6 +241,7 @@ has_staged(git_repository *repo)
if (git_status_list_new(&list, repo, &opts) < 0) {
L("Error checking for staged changes: %s", git_error_last()->message);
PE();
return 0;
}
@ -237,6 +260,7 @@ has_staged(git_repository *repo)
}
git_status_list_free(list);
PE();
return r;
}
#endif
@ -244,17 +268,20 @@ has_staged(git_repository *repo)
inline unsigned
numcat(unsigned x, unsigned y)
{
PS();
unsigned pow = 10;
while(y >= pow) {
pow *= 10;
}
PE();
return (x * pow) + y;
}
int
str_to_int(char *str)
{
PS();
int res = -1;
char *c;
@ -266,17 +293,20 @@ str_to_int(char *str)
}
}
PE();
return res;
}
int
main(int argc, char *argv[])
{
PS();
int code = -1;
/* print version information */
if (argc > 1 && strcmp(argv[1], "-v") == 0) {
printf("XD [number] %s\n", VERSION);
PE();
return 0;
#ifdef EXPLAIN
} else if (argc > 1 && strcmp(argv[1], "-e") == 0) {
@ -328,6 +358,7 @@ main(int argc, char *argv[])
code = str_to_int(argv[argc - 1]);
if (code < 0) {
L("Return code, %d, not valid", code);
PE();
exit(1);
}
}
@ -361,5 +392,6 @@ main(int argc, char *argv[])
P("|"); /* no code info */
}
PE();
return code;
}