Konrads Regel heißt "standardmäßig git, wenn git installiert ist" — nicht "wenn jemand vorher git init tippt". Bisher schwieg skilld bei einer Bibliothek ohne Repository und änderte Dateien ohne Verlauf; jetzt wird beim ersten Schreiben eines angelegt (mit .gitignore), und create/update/delete werden committet. Angesprochen wird die BIBLIOTHEK, nicht das Skill-Verzeichnis: sonst entstünde ein verschachteltes Repository je Skill, und die Bibliothek bliebe ohne Verlauf. Ein schon vorhandenes Repository (auch ein übergeordnetes mit Remote) bleibt unangetastet — hier wird nur committet, nie gepusht. Der Start meldet jetzt, was gilt: "git, every change committed" oder "git is installed, the repository is created on the first change" oder "git is not installed".
292 lines
7.5 KiB
C
292 lines
7.5 KiB
C
/* git.c — Git als Unterbau für die Skill-Bibliothek.
|
|
*
|
|
* Warum Git und nicht einfach Dateien: eine Fähigkeit wird von mehreren Agenten
|
|
* auf mehreren Rechnern geändert. Ohne Historie gibt es keine Antwort auf "wer hat
|
|
* das geändert und warum", und ohne Abgleich überschreibt der eine die Arbeit des
|
|
* anderen. Also: jede Änderung wird festgeschrieben, die Bibliothek lässt sich
|
|
* ziehen und schieben, und der Verlauf ist abfragbar.
|
|
*
|
|
* Kein libgit2: git wird über popen aufgerufen, wie tgagent curl aufruft. Eine
|
|
* Abhängigkeit weniger, und das Programm, das die Arbeit macht, ist dasselbe, das
|
|
* der Mensch benutzt.
|
|
*/
|
|
/* popen braucht POSIX, bevor irgendein Header kommt. */
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#include "skilld_git.h"
|
|
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
/* Ein Argument für die Shell unschädlich machen. */
|
|
static void sh_quote(mcp_buf *b, const char *s)
|
|
{
|
|
mcp_buf_puts(b, "'");
|
|
for (const char *p = s; *p; p++) {
|
|
if (*p == '\'')
|
|
mcp_buf_puts(b, "'\\''");
|
|
else
|
|
mcp_buf_add(b, p, 1);
|
|
}
|
|
mcp_buf_puts(b, "'");
|
|
}
|
|
|
|
/* Kommando ausführen, Ausgabe einsammeln. NULL, wenn popen scheitert. */
|
|
static char *run_capture(const char *cmd, int *status_out)
|
|
{
|
|
FILE *f = popen(cmd, "r");
|
|
mcp_buf out;
|
|
char tmp[4096];
|
|
size_t n;
|
|
int st = -1;
|
|
|
|
if (status_out)
|
|
*status_out = -1;
|
|
if (!f)
|
|
return NULL;
|
|
mcp_buf_init(&out);
|
|
while ((n = fread(tmp, 1, sizeof tmp, f)) > 0)
|
|
mcp_buf_add(&out, tmp, n);
|
|
st = pclose(f);
|
|
if (status_out)
|
|
*status_out = st;
|
|
if (out.p == NULL)
|
|
return strdup("");
|
|
return out.p;
|
|
}
|
|
|
|
int skilld_git_present(const char *dir)
|
|
{
|
|
mcp_buf cmd;
|
|
char *out;
|
|
int st;
|
|
|
|
mcp_buf_init(&cmd);
|
|
mcp_buf_puts(&cmd, "git -C ");
|
|
sh_quote(&cmd, dir);
|
|
mcp_buf_puts(&cmd, " rev-parse --git-dir 2>/dev/null");
|
|
out = run_capture(cmd.p, &st);
|
|
mcp_buf_free(&cmd);
|
|
int ok = (st == 0 && out && *out != '\0');
|
|
free(out);
|
|
return ok;
|
|
}
|
|
|
|
char *skilld_git_head(const char *dir)
|
|
{
|
|
mcp_buf cmd;
|
|
char *out;
|
|
int st;
|
|
|
|
mcp_buf_init(&cmd);
|
|
mcp_buf_puts(&cmd, "git -C ");
|
|
sh_quote(&cmd, dir);
|
|
mcp_buf_puts(&cmd, " rev-parse --short HEAD 2>/dev/null");
|
|
out = run_capture(cmd.p, &st);
|
|
mcp_buf_free(&cmd);
|
|
if (st != 0 || !out) {
|
|
free(out);
|
|
return NULL;
|
|
}
|
|
char *nl = strchr(out, '\n');
|
|
if (nl)
|
|
*nl = '\0';
|
|
return out;
|
|
}
|
|
|
|
/* Eine Änderung festschreiben. 0 = festgeschrieben, 1 = nichts zu tun,
|
|
* -1 = Fehler (die Meldung steht in err). */
|
|
int skilld_git_commit(const char *dir, const char *path, const char *message,
|
|
char *err, size_t errlen, char **hash_out)
|
|
{
|
|
mcp_buf cmd;
|
|
char *out;
|
|
int st;
|
|
|
|
if (hash_out)
|
|
*hash_out = NULL;
|
|
|
|
/* Erst prüfen, ob es überhaupt etwas zu schreiben gibt: ein leerer Commit
|
|
* ist ein Fehler, und ein Fehler nach jeder Änderung macht die Werkzeuge
|
|
* unbrauchbar. */
|
|
mcp_buf_init(&cmd);
|
|
mcp_buf_puts(&cmd, "git -C ");
|
|
sh_quote(&cmd, dir);
|
|
mcp_buf_puts(&cmd, " status --porcelain -- ");
|
|
sh_quote(&cmd, path);
|
|
out = run_capture(cmd.p, &st);
|
|
mcp_buf_free(&cmd);
|
|
if (out == NULL) {
|
|
snprintf(err, errlen, "git status failed");
|
|
return -1;
|
|
}
|
|
if (*out == '\0') {
|
|
free(out);
|
|
return 1;
|
|
}
|
|
free(out);
|
|
|
|
mcp_buf_init(&cmd);
|
|
mcp_buf_puts(&cmd, "git -C ");
|
|
sh_quote(&cmd, dir);
|
|
mcp_buf_puts(&cmd, " add -A -- ");
|
|
sh_quote(&cmd, path);
|
|
mcp_buf_puts(&cmd, " && git -C ");
|
|
sh_quote(&cmd, dir);
|
|
mcp_buf_puts(&cmd, " -c user.name=");
|
|
sh_quote(&cmd, "skilld");
|
|
mcp_buf_puts(&cmd, " -c user.email=");
|
|
sh_quote(&cmd, "skilld@local");
|
|
mcp_buf_puts(&cmd, " commit -q -m ");
|
|
sh_quote(&cmd, message);
|
|
mcp_buf_puts(&cmd, " 2>&1");
|
|
out = run_capture(cmd.p, &st);
|
|
mcp_buf_free(&cmd);
|
|
|
|
if (st != 0) {
|
|
snprintf(err, errlen, "%s", out ? out : "commit failed");
|
|
char *nl = strchr(err, '\n');
|
|
if (nl)
|
|
*nl = '\0';
|
|
free(out);
|
|
return -1;
|
|
}
|
|
free(out);
|
|
|
|
if (hash_out)
|
|
*hash_out = skilld_git_head(dir);
|
|
return 0;
|
|
}
|
|
|
|
/* Die Bibliothek wird beim ersten Schreiben ein Repository, wenn git installiert ist.
|
|
* Konrads Regel heißt "standardmäßig git, wenn git da ist" — nicht "wenn jemand vorher
|
|
* git init tippt". 1 = es gibt jetzt ein Repository, 0 = git fehlt, -1 = Fehler.
|
|
* Ein bereits vorhandenes Repository (auch ein übergeordnetes mit Remote) bleibt
|
|
* unangetastet: dort wird nur committet, nie gepusht. */
|
|
int skilld_git_init(const char *dir, char *err, size_t errlen)
|
|
{
|
|
mcp_buf cmd;
|
|
char *out;
|
|
int st;
|
|
|
|
if (skilld_git_present(dir))
|
|
return 1;
|
|
|
|
mcp_buf_init(&cmd);
|
|
mcp_buf_puts(&cmd, "git --version >/dev/null 2>&1");
|
|
out = run_capture(cmd.p, &st);
|
|
mcp_buf_free(&cmd);
|
|
free(out);
|
|
if (st != 0) {
|
|
snprintf(err, errlen, "git is not installed");
|
|
return 0;
|
|
}
|
|
|
|
mcp_buf_init(&cmd);
|
|
mcp_buf_puts(&cmd, "git -C ");
|
|
sh_quote(&cmd, dir);
|
|
mcp_buf_puts(&cmd, " init -q 2>&1");
|
|
out = run_capture(cmd.p, &st);
|
|
mcp_buf_free(&cmd);
|
|
if (st != 0) {
|
|
snprintf(err, errlen, "%s", out ? out : "git init failed");
|
|
char *nl = strchr(err, '\n');
|
|
if (nl)
|
|
*nl = '\0';
|
|
free(out);
|
|
return -1;
|
|
}
|
|
free(out);
|
|
|
|
/* .gitignore, damit nichts Halbfertiges im Verlauf landet. */
|
|
{
|
|
FILE *f;
|
|
char *path;
|
|
|
|
path = malloc(strlen(dir) + sizeof "/.gitignore");
|
|
if (path == NULL)
|
|
return 1;
|
|
sprintf(path, "%s/.gitignore", dir);
|
|
f = fopen(path, "w");
|
|
free(path);
|
|
if (f != NULL) {
|
|
fprintf(f, "*.tmp.*\n.tmp/\n*.lock\n__pycache__/\n");
|
|
fclose(f);
|
|
}
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
char *skilld_git_log(const char *dir, const char *path, int limit)
|
|
{
|
|
mcp_buf cmd;
|
|
char *out;
|
|
|
|
mcp_buf_init(&cmd);
|
|
mcp_buf_puts(&cmd, "git -C ");
|
|
sh_quote(&cmd, dir);
|
|
mcp_buf_puts(&cmd, " log -n ");
|
|
mcp_buf_printf(&cmd, "%d", limit <= 0 ? 20 : limit);
|
|
mcp_buf_puts(&cmd, " --format='%h %ad %an %s' --date=short");
|
|
if (path && *path) {
|
|
mcp_buf_puts(&cmd, " -- ");
|
|
sh_quote(&cmd, path);
|
|
}
|
|
mcp_buf_puts(&cmd, " 2>&1");
|
|
out = run_capture(cmd.p, NULL);
|
|
mcp_buf_free(&cmd);
|
|
return out;
|
|
}
|
|
|
|
char *skilld_git_diff(const char *dir, const char *path)
|
|
{
|
|
mcp_buf cmd;
|
|
char *out;
|
|
|
|
mcp_buf_init(&cmd);
|
|
mcp_buf_puts(&cmd, "git -C ");
|
|
sh_quote(&cmd, dir);
|
|
mcp_buf_puts(&cmd, " status --short");
|
|
if (path && *path) {
|
|
mcp_buf_puts(&cmd, " -- ");
|
|
sh_quote(&cmd, path);
|
|
}
|
|
mcp_buf_puts(&cmd, " 2>&1; git -C ");
|
|
sh_quote(&cmd, dir);
|
|
mcp_buf_puts(&cmd, " diff --stat");
|
|
if (path && *path) {
|
|
mcp_buf_puts(&cmd, " -- ");
|
|
sh_quote(&cmd, path);
|
|
}
|
|
mcp_buf_puts(&cmd, " 2>&1");
|
|
out = run_capture(cmd.p, NULL);
|
|
mcp_buf_free(&cmd);
|
|
return out;
|
|
}
|
|
|
|
char *skilld_git_sync(const char *dir, int push, char *err, size_t errlen)
|
|
{
|
|
mcp_buf cmd;
|
|
char *out;
|
|
int st;
|
|
|
|
mcp_buf_init(&cmd);
|
|
mcp_buf_puts(&cmd, "git -C ");
|
|
sh_quote(&cmd, dir);
|
|
if (push)
|
|
mcp_buf_puts(&cmd, " push 2>&1");
|
|
else
|
|
mcp_buf_puts(&cmd, " pull --rebase 2>&1");
|
|
out = run_capture(cmd.p, &st);
|
|
mcp_buf_free(&cmd);
|
|
|
|
if (st != 0) {
|
|
snprintf(err, errlen, "%s", out ? out : "git failed");
|
|
free(out);
|
|
return NULL;
|
|
}
|
|
return out ? out : strdup("");
|
|
}
|