2021-02-21 23:40:31 -06:00
|
|
|
#include <string.h>
|
2021-02-21 22:39:58 -06:00
|
|
|
#include <stdio.h>
|
2021-02-21 23:40:31 -06:00
|
|
|
#include <stdlib.h>
|
2021-02-21 22:39:58 -06:00
|
|
|
|
2021-02-21 22:43:22 -06:00
|
|
|
#include "ep.h"
|
2021-02-21 22:39:58 -06:00
|
|
|
|
2021-02-21 23:40:31 -06:00
|
|
|
static char *git_branch_name, *git_status;
|
|
|
|
void print_git(void)
|
|
|
|
{
|
|
|
|
if (git_branch_name) {
|
|
|
|
p(" ");
|
|
|
|
p(git_branch_name);
|
|
|
|
|
|
|
|
if (git_status) {
|
|
|
|
p(" [");
|
|
|
|
p(git_status);
|
|
|
|
p("]");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct rlfc_data {
|
|
|
|
char *line;
|
|
|
|
size_t n;
|
|
|
|
ssize_t l;
|
|
|
|
int status;
|
|
|
|
};
|
2021-02-21 22:39:58 -06:00
|
|
|
|
2021-02-21 23:40:31 -06:00
|
|
|
static void read_line_from_command(const char *c, struct rlfc_data *d)
|
2021-02-21 22:39:58 -06:00
|
|
|
{
|
|
|
|
FILE *f = popen("git rev-parse --abbrev-ref HEAD 2>/dev/null", "re");
|
2021-02-21 23:40:31 -06:00
|
|
|
if (!f)
|
|
|
|
return;
|
2021-02-21 22:39:58 -06:00
|
|
|
|
2021-02-21 23:40:31 -06:00
|
|
|
d->l = getline(&d->line, &d->n, f);
|
|
|
|
d->status = pclose(f);
|
|
|
|
}
|
2021-02-21 22:39:58 -06:00
|
|
|
|
2021-02-21 23:40:31 -06:00
|
|
|
static char *get_git_status(void);
|
|
|
|
|
|
|
|
void *git_thread(void *arg)
|
|
|
|
{
|
|
|
|
struct rlfc_data c = { 0 };
|
|
|
|
read_line_from_command("git rev-parse --abbrev-ref HEAD 2>/dev/null", &c);
|
|
|
|
|
|
|
|
if (!c.status) {
|
2021-02-21 22:39:58 -06:00
|
|
|
/* if git exits with 0, we are in a repo */
|
|
|
|
|
2021-02-21 23:40:31 -06:00
|
|
|
if (c.l > 0) {
|
2021-02-21 22:39:58 -06:00
|
|
|
/* TODO: treat case where it reads HEAD */
|
2021-02-21 23:40:31 -06:00
|
|
|
c.line[c.l-1] = 0;
|
2021-02-21 22:39:58 -06:00
|
|
|
/* line ownserhip goes to outside this function */
|
2021-02-21 23:40:31 -06:00
|
|
|
git_branch_name = c.line;
|
2021-02-21 22:39:58 -06:00
|
|
|
}
|
2021-02-21 23:40:31 -06:00
|
|
|
} else {
|
|
|
|
return NULL;
|
2021-02-21 22:39:58 -06:00
|
|
|
}
|
|
|
|
|
2021-02-21 23:40:31 -06:00
|
|
|
/* since we are in a repo, read git status;
|
|
|
|
* if we add more stuff to do in repos, launch more threads */
|
|
|
|
|
|
|
|
get_git_status();
|
|
|
|
|
2021-02-21 22:39:58 -06:00
|
|
|
return NULL;
|
|
|
|
}
|
2021-02-21 23:40:31 -06:00
|
|
|
|
|
|
|
static char *get_git_status(void)
|
|
|
|
{
|
|
|
|
size_t modified = 0;
|
|
|
|
|
|
|
|
FILE *f = popen("git status --porcelain=v1 -z 2>/dev/null", "re");
|
|
|
|
if (!f)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
char *line = NULL;
|
|
|
|
size_t n = 0;
|
|
|
|
ssize_t l;
|
|
|
|
while ((l = getdelim(&line, &n, 0, f)) != -1) {
|
|
|
|
modified += l>4 && line[1] == 'M';
|
|
|
|
}
|
|
|
|
free(line);
|
|
|
|
|
|
|
|
if (!pclose(f)) {
|
|
|
|
asprintf(&git_status, "~%zu", modified);
|
|
|
|
}
|
|
|
|
|
|
|
|
return git_status;
|
|
|
|
}
|