diff --git a/skilld.c b/skilld.c index 3c1c640..906a63d 100644 --- a/skilld.c +++ b/skilld.c @@ -28,6 +28,8 @@ /* ------------------------------------------------------------------ git */ +static char *skilllib_path(void); + /* Jede Änderung an einer Fähigkeit wird festgeschrieben. Ohne Historie gibt es * keine Antwort auf "wer hat das geändert"; ohne Abgleich überschreibt ein Agent * die Arbeit des anderen. Ist das Verzeichnis kein Repository, bleibt alles wie @@ -40,6 +42,18 @@ static void autocommit(const char *dir, const char *fmt, ...) va_list ap; int rc; + { + char ierr[256]; + char *lib = skilllib_path(); + + /* Konrads Regel: standardmäßig git, wenn git installiert ist. Also beim ersten + * Schreiben ein Repository anlegen, statt zu warten, bis jemand git init tippt. + * Angesprochen wird die BIBLIOTHEK, nicht das Skill-Verzeichnis: sonst entstünde + * ein verschachteltes Repository je Skill, und die Bibliothek bliebe ohne Verlauf. */ + if (skilld_git_init(lib, ierr, sizeof ierr) < 0) + fprintf(stderr, "skilld: no repository in %s: %s\n", lib, ierr); + free(lib); + } if (!skilld_git_present(dir)) return; va_start(ap, fmt); @@ -1043,8 +1057,19 @@ int main(int argc, char **argv) { char *lib = skilllib_path(); - fprintf(stderr, "skilld: library %s (%s)\n", lib, - skilld_git_present(lib) ? "git" : "no git — changes are not versioned"); + /* Ehrlich statt beruhigend: ein Verlauf, der erst beim ersten Schreiben + * entsteht, ist noch kein Verlauf — aber er wird einer. */ + { + const char *state; + + if (skilld_git_present(lib)) + state = "git, every change committed"; + else if (system("git --version >/dev/null 2>&1") == 0) + state = "git is installed, the repository is created on the first change"; + else + state = "git is not installed — changes are not versioned"; + fprintf(stderr, "skilld: library %s (%s)\n", lib, state); + } free(lib); } diff --git a/skilld_git.c b/skilld_git.c index 67e19bb..3c1a672 100644 --- a/skilld_git.c +++ b/skilld_git.c @@ -160,6 +160,65 @@ int skilld_git_commit(const char *dir, const char *path, const char *message, 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; diff --git a/skilld_git.h b/skilld_git.h index 9146a72..daa84bd 100644 --- a/skilld_git.h +++ b/skilld_git.h @@ -9,6 +9,9 @@ /* Ist das Verzeichnis ein Git-Repository? 1 = ja. */ int skilld_git_present(const char *dir); +/* Legt bei Bedarf ein Repository an. 1 = vorhanden, 0 = kein git, -1 = Fehler. */ +int skilld_git_init(const char *dir, char *err, size_t errlen); + /* Kurzer HEAD-Hash oder NULL. */ char *skilld_git_head(const char *dir);