tasksd: jede Zustandsänderung wird festgeschrieben, wenn git da ist
Ist git installiert, wird $TASKDIR beim ersten Schreiben ein Repository und jedes task_save committet (Meldung: <zustand> <kennung>, z. B. "done t0001"). Eine Aufgabe, deren Verlauf niemand mehr nachsehen kann, ist nur ein Zettel — mit git ist "wann wurde das erledigt und was stand vorher da" eine Frage an das Repository. Die Meldung besteht nur aus eigenen Kennungen und Zuständen, kein Modell- oder Menschentext. Ohne git läuft alles weiter. Nebenbei behoben: der Include zeigte auf "../libmcp/mcp.h" und traf nur, solange zufällig ein libmcp-Verzeichnis neben dem Quellbaum lag — aus einem frischen Clone fand er nichts. Jetzt "mcp.h" über -Ivendor/libmcp, wie der Makefile es meint.
This commit is contained in:
1 parent
2a54a86049
commit
ddb7355868
1 file changed
+79
-1
@@ -26,7 +26,7 @@
|
||||
* paused dropped
|
||||
*/
|
||||
#define MCP_IMPLEMENTATION
|
||||
#include "../libmcp/mcp.h"
|
||||
#include "mcp.h" /* -Ivendor/libmcp; "../libmcp" zeigte aus einem Clone ins Leere */
|
||||
|
||||
#include <ctype.h>
|
||||
#include <dirent.h>
|
||||
@@ -286,6 +286,8 @@ static void put_s(mcp_buf *b, const char *key, const char *val)
|
||||
mcp_buf_puts(b, "\"");
|
||||
}
|
||||
|
||||
static void git_commit(const char *verb, const char *what);
|
||||
|
||||
static int task_save(const struct task *t)
|
||||
{
|
||||
char *p = task_path(t->id);
|
||||
@@ -312,6 +314,8 @@ static int task_save(const struct task *t)
|
||||
rc = write_file_atomic(p, b.p);
|
||||
mcp_buf_free(&b);
|
||||
free(p);
|
||||
if (rc == 0)
|
||||
git_commit(t->state != NULL ? t->state : "task", t->id);
|
||||
return rc;
|
||||
}
|
||||
|
||||
@@ -407,8 +411,82 @@ static void free_ids(char **ids, size_t n)
|
||||
free(ids);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ git */
|
||||
/*
|
||||
* Ist git installiert, wird die Ablage beim ersten Schreiben ein Repository und jede
|
||||
* Änderung festgeschrieben — eine Aufgabe, deren Verlauf niemand mehr nachsehen kann,
|
||||
* ist nur ein Zettel. Ist git nicht da, läuft alles weiter; der Dienst sagt es beim
|
||||
* Start, damit niemand Verlauf annimmt, den es nicht gibt.
|
||||
*
|
||||
* Die Meldung besteht nur aus eigenen Kennungen (t0001) und Zuständen — kein Modell- oder
|
||||
* Menschentext, also nichts zu quoten und nichts zu injizieren.
|
||||
*/
|
||||
static int g_git = -1; /* -1 ungeprüft, 0 kein git, 1 git vorhanden */
|
||||
|
||||
static int git_available(void)
|
||||
{
|
||||
if (g_git >= 0)
|
||||
return g_git;
|
||||
g_git = (system("git --version >/dev/null 2>&1") == 0) ? 1 : 0;
|
||||
return g_git;
|
||||
}
|
||||
|
||||
static void git_ensure_repo(void)
|
||||
{
|
||||
static int done;
|
||||
|
||||
if (done || !git_available())
|
||||
return;
|
||||
done = 1;
|
||||
{
|
||||
char *root = tasks_root();
|
||||
mcp_buf cmd;
|
||||
|
||||
mcp_buf_init(&cmd);
|
||||
mcp_buf_printf(&cmd, "git -C '%s' rev-parse --git-dir >/dev/null 2>&1 || "
|
||||
"git -C '%s' init -q", root, root);
|
||||
if (system(cmd.p) != 0) {
|
||||
fprintf(stderr, "tasksd: git init in %s failed\n", root);
|
||||
} else {
|
||||
FILE *ign;
|
||||
mcp_buf pb;
|
||||
|
||||
mcp_buf_init(&pb);
|
||||
mcp_buf_printf(&pb, "%s/.gitignore", root);
|
||||
ign = fopen(pb.p != NULL ? pb.p : "/dev/null", "w");
|
||||
if (ign != NULL) {
|
||||
fprintf(ign, "*.tmp.*\n.tmp/\n*.lock\n");
|
||||
fclose(ign);
|
||||
}
|
||||
mcp_buf_free(&pb);
|
||||
}
|
||||
mcp_buf_free(&cmd);
|
||||
free(root);
|
||||
}
|
||||
}
|
||||
|
||||
static void git_commit(const char *verb, const char *what)
|
||||
{
|
||||
char *root;
|
||||
mcp_buf cmd;
|
||||
|
||||
if (!git_available())
|
||||
return;
|
||||
git_ensure_repo();
|
||||
root = tasks_root();
|
||||
mcp_buf_init(&cmd);
|
||||
mcp_buf_printf(&cmd, "git -C '%s' add -A >/dev/null 2>&1 && "
|
||||
"git -C '%s' -c user.name=tasksd -c user.email=tasksd@localhost "
|
||||
"commit -q -m \"%s %s\" >/dev/null 2>&1", root, root, verb, what);
|
||||
if (system(cmd.p) != 0)
|
||||
fprintf(stderr, "tasksd: git commit failed (%s %s)\n", verb, what);
|
||||
mcp_buf_free(&cmd);
|
||||
free(root);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ replies */
|
||||
|
||||
|
||||
static void say(mcp_conn *c, const char *fmt, ...)
|
||||
{
|
||||
char tmp[512];
|
||||
|
||||
Reference in new issue
Block a user