skill_create, skill_update, skill_clone und skill_delete committen ihre Änderung selbst (skilld: create/update/… <name>). Dazu vier Werkzeuge: skill_history, skill_diff, skill_sync (pull --rebase, push:true für die Gegenrichtung). Ist die Bibliothek kein Repository, behauptet das Werkzeug keinen Verlauf, sondern sagt es; beim Start steht im Log, in welchem Modus skilld läuft. Dazu --http PORT über den gemeinsamen Transport aus libmcp. tests/git.py legt ein echtes Repo mit bare-Remote an und prüft fünf Fälle, darunter eine Änderung von Hand, die der nächste Schreibvorgang mitschreibt.
233 lines
5.9 KiB
C
233 lines
5.9 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;
|
|
}
|
|
|
|
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("");
|
|
}
|