aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorthe little girl <yui@blekksprut.net>2015-02-28 20:39:23 +0100
committerthe little girl <yui@blekksprut.net>2015-02-28 20:39:23 +0100
commite6444fb2c122ae84dbdcaddd7e4d453be427de49 (patch)
tree126e9cdc9b48c508e711f83c1504e6031fb7bf90
parentd0800c0c9f76d6432fbbf4a766d7e87413d2cc13 (diff)
downloadukulele-git.tar.xz
denne branchen kommer til å bli til en helt egen ting...en.....gitargit
-rw-r--r--Makefile2
-rw-r--r--ukulele.c271
2 files changed, 100 insertions, 173 deletions
diff --git a/Makefile b/Makefile
index 24073b2..d4744db 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
all: ukulele.cgi
ukulele.cgi: config.h ukulele.c
- ${CC} -std=c99 -pedantic -Os -o $@ ukulele.c -lmarxup -lgit2
+ ${CC} -std=c99 -Wall -pedantic -Os -o $@ ukulele.c -lmarxup -lgit2
config.h:
cp config.def.h $@
diff --git a/ukulele.c b/ukulele.c
index 8738041..acb18ca 100644
--- a/ukulele.c
+++ b/ukulele.c
@@ -20,60 +20,68 @@ static const char *badchars = "\\?%:|\"<> ";
int problem(int, char*);
-int commit(char *path, char *msg) {
+int store(char *raw, int len) {
+ char *id = basename(strdup(raw));
+ char page[len];
+ char *out = &page[0];
+
+ int pos = 0;
+ unsigned int decoded;
+ char buffer[3] = { 0 };
+ for(int i = 0; i < len; i++) {
+ buffer[pos] = getchar();
+ if(buffer[pos] == '+') buffer[pos] = ' ';
+ if(buffer[pos] == '&') { buffer[pos] = '\0'; break; }
+ if(pos == 2) {
+ if(buffer[0] == '%' && isxdigit(buffer[1]) && isxdigit(buffer[2])) {
+ sscanf(buffer, "%%%2x", &decoded);
+ *out++ = decoded;
+ memset(buffer, 0, 3);
+ pos = 0;
+ } else {
+ *out++ = buffer[0];
+ memmove(buffer, &buffer[1], 2);
+ buffer[2] = 0;
+ }
+ }
+ else pos++;
+ }
+ *out++ = buffer[0]; *out++ = buffer[1]; *out++ = buffer[2]; *out++ = '\0';
+
git_repository *repo = 0;
+
+ git_oid blob_id, commit_id, tree_id;
+
git_signature *sig = 0;
- git_index *index = 0;
git_tree *tree = 0;
- git_oid tree_id, id;
- git_oid t;
- git_commit *p;
+ git_commit *commit;
- if(git_repository_open(&repo, "."))
- return problem(500, "Can't open git repo");
+ git_treebuilder *builder = NULL;
+ if(git_repository_open(&repo, "pages"))
+ return problem(500, "Can't open git repo");
if(git_signature_now(&sig, "anonymous", ""))
return problem(500, "Can't set up signature");
+ if(git_blob_create_frombuffer(&blob_id, repo, page, strlen(page)))
+ return problem(500, "Uhhhh");
+ if(git_reference_name_to_id(&commit_id, repo, "HEAD"))
+ return problem(500, "No HEAD");
+ if(git_commit_lookup(&commit, repo, &commit_id))
+ return problem(500, "Whoa whoa whoa");
+ if(git_commit_tree(&tree, commit))
+ return problem(500, "Finner ikke commit tree");
- if(git_repository_index(&index, repo))
- return problem(500, "Index trouble");
-
- int i;
- if((i = git_index_add_bypath(index, path)))
- return problem(500, get_current_dir_name()); //path);
-
- git_index_write_tree(&tree_id, index);
- git_index_write(index);
+ git_treebuilder_create(&builder, tree);
+ if(git_treebuilder_insert(NULL, builder, id, &blob_id, GIT_FILEMODE_BLOB))
+ return problem(500, "Yo");
+ if(git_treebuilder_write(&tree_id, repo, builder))
+ return problem(500, "Ffff d");
if(git_tree_lookup(&tree, repo, &tree_id))
- return problem(500, "Uhm");
-
- if(git_reference_name_to_id(&t, repo, "HEAD")) {
- git_commit_create_v(&id, repo, "HEAD", sig, sig, "UTF-8", msg, tree, 0, 0);
- return 0;
- }
-
- if(git_commit_lookup(&p, repo, &t))
- return problem(500, "Whoa whoa whoa");
-
- git_commit_create_v(&id, repo, "HEAD", sig, sig, "UTF-8", msg, tree, 1, p);
+ return problem(500, "Ffff");
- return 0;
-}
+ git_commit_create_v(&commit_id, repo, "HEAD", sig, sig, "UTF-8", "yo", tree, 1, commit);
-int writable(char *path) {
- struct stat s;
- if(!stat(path, &s)) {
- if(S_ISREG(s.st_mode))
- return !eaccess(path, W_OK);
- else if(S_ISDIR(s.st_mode))
- return 0;
- }
- char *dir = dirname(strdup(path));
- if(!stat(dir, &s)) {
- if(S_ISDIR(s.st_mode))
- return !eaccess(dir, W_OK);
- }
return 0;
}
@@ -87,7 +95,7 @@ void head(char *page, char *link, char *what) {
if(link && what && !strcmp(what, "rediger"))
printf("<a data-text='%s' href='%s'>%s</a>", what, link, page);
else if(link && what)
- printf("<a data-text='%s' href='%s/%s'>%s</a>", what, base, link, page);
+ printf("<a data-text='%s' href='%s/%s'>%s</a>", what, script, link, page);
else
printf("%s", page);
printf("</h1>\n");
@@ -159,65 +167,6 @@ int unauthorized(int status) {
return 1;
}
-int edit(char *id) {
- char path[strlen(id) + 5];
- snprintf(path, strlen(id) + 5, "%s.txt", id);
- if(!writable(path)) return unauthorized(403);
- FILE *fp = fopen(path, "r");
- fp ? head(id, path, "vis") : head(id, NULL, NULL);
- char buffer[sysconf(_SC_PAGESIZE)];
- printf("<form method='post'>\n");
- printf("<p class='edit'>\n");
- if(orz)
- printf("<a id='orz' href='#'>@picto</a><br>\n");
- printf("<textarea name=t rows=24 cols=72>");
- if(fp)
- while(fgets(buffer, sizeof(buffer), fp))
- printf("%s", buffer);
- printf("</textarea>\n");
- printf("<p><input type=submit value=Update>");
- printf("</form>");
- foot(1);
- if(fp) fclose(fp);
- return 0;
-}
-
-// v fæl
-int store(char *raw, int len) {
- char *dir = dirname(strdup(raw));
- char *id = basename(strdup(raw));
- if(chdir(dir))
- return problem(404, "Not found");
- char path[strlen(id) + 5];
- snprintf(path, strlen(id) + 5, "%s.txt", id);
- FILE *fp = fopen(path, "w");
- if(!fp) return redirect("fff", NULL);
- int pos = 0;
- unsigned int decoded;
- char buffer[3] = { 0 };
- for(int i = 0; i < len; i++) {
- buffer[pos] = getchar();
- if(buffer[pos] == '+') buffer[pos] = ' ';
- if(buffer[pos] == '&') { buffer[pos] = '\0'; break; }
- if(pos == 2) {
- if(buffer[0] == '%' && isxdigit(buffer[1]) && isxdigit(buffer[2])) {
- sscanf(buffer, "%%%2x", &decoded);
- fprintf(fp, "%c", decoded);
- memset(buffer, 0, 3);
- pos = 0;
- } else {
- fprintf(fp, "%c", buffer[0]);
- memmove(buffer, &buffer[1], 2);
- buffer[2] = 0;
- }
- }
- else pos++;
- }
- fprintf(fp, "%.3s", buffer);
- fputc('\0', fp);
- fclose(fp);
- return commit(path, "test");
-}
int post(char *path) {
char *header;
@@ -232,52 +181,6 @@ int post(char *path) {
return redirect(path, NULL);
}
-// gjør denne penere!
-int list(char *raw, int dir, int inc) {
- int len = strlen(raw);
- char *pattern = malloc(len + 3);
- if(len > 0 && raw[len - 1] == '/')
- raw[len - 1] = '\0';
- if(dir)
- snprintf(pattern, len + 3, "%s/*", raw);
- else
- snprintf(pattern, len + 1, "%s", raw);
- glob_t res;
- if(glob(pattern, GLOB_MARK, NULL, &res)) {
- if(!inc) {
- head("ingen treff", NULL, NULL);
- foot(0);
- }
- return 0;
- }
- if(!inc)
- head("treff", NULL, NULL);
- char *path;
- printf("<ul class='glob'>");
- for(int i = 0; i < res.gl_pathc; i++) {
- path = res.gl_pathv[i];
- if(*path == '.' && *path + 1 == '/') path += 2;
- printf("<li><a href='%s/%s'>%s</a>", script, path, path);
- }
- printf("</ul>");
- if(!inc)
- foot(0);
- free(pattern);
- return 0;
-}
-
-int text(char *path, char *type) {
- FILE *fp = fopen(path, "r");
- if(fp) {
- char buffer[sysconf(_SC_PAGESIZE)];
- printf("Content-Type: %s; charset=utf-8\n\n", type);
- while(fgets(buffer, sizeof(buffer), fp))
- printf("%s", buffer);
- }
- fclose(fp);
- return 0;
-}
-
void include(FILE *out, char *path) {
struct stat s;
int plain = 0;
@@ -288,7 +191,6 @@ void include(FILE *out, char *path) {
if(*path == '/') return;
stat(path, &s);
if(S_ISDIR(s.st_mode)) {
- list(path, 1, 1);
return;
}
FILE *fp = fopen(path, "r");
@@ -318,21 +220,53 @@ void magic(FILE *out, char *line) {
}
}
-int view(char *id) {
- struct stat s;
- char path[strlen(id) + 5];
- snprintf(path, strlen(id) + 5, "%s.txt", id);
+int view(char *id, int edit) {
+ git_repository *repo = 0;
+ git_oid commit_id;
+ git_commit *commit;
+ git_tree *tree;
+ git_tree_entry *page;
- stat(path, &s);
- if(S_ISDIR(s.st_mode)) {
- if(dirlist)
- return list(path, 1, 0);
- return redirect(home, path);
+ if(git_repository_open(&repo, "pages"))
+ return problem(500, "Can't open git repo");
+ if(git_reference_name_to_id(&commit_id, repo, "HEAD"))
+ return problem(500, "No HEAD");
+ if(git_commit_lookup(&commit, repo, &commit_id))
+ return problem(500, "Whoa whoa whoa");
+ if(git_commit_tree(&tree, commit))
+ return problem(500, "Finner ikke commit tree");
+
+ void *text;
+ if(git_tree_entry_bypath(&page, tree, id)) {
+ if(!edit)
+ redirect("?e", 0);
+ text = (void *) "\0";
+ } else {
+ const git_oid *blob_id = git_tree_entry_id(page);
+ git_blob *blob;
+ if(git_blob_lookup(&blob, repo, blob_id))
+ return problem(500, "Finner ikke blob");
+ text = (void *) git_blob_rawcontent(blob);
}
- FILE *fp = fopen(path, "r");
- if(!fp) return redirect("?e", NULL);
- writable(path) ? head(id, "?e", "rediger") : head(id, NULL, NULL);
- marxup(fp, stdout, magic);
+
+ if(edit) {
+ head(id, id, "vis");
+ printf("<form method='post'>\n");
+ printf("<p class='edit'>\n");
+ if(orz)
+ printf("<a id='orz' href='#'>@picto</a><br>\n");
+ printf("<textarea name=t rows=24 cols=72>");
+ printf("%s", (char *) text);
+ printf("</textarea>\n");
+ printf("<p><input type=submit value=Update>");
+ printf("</form>");
+ foot(1);
+ return 0;
+ }
+
+ head(id, "?e", "rediger");
+ if(strlen(text) > 0)
+ marxup(fmemopen(text, strlen(text), "r"), stdout, magic);
foot(1);
return 0;
}
@@ -355,24 +289,17 @@ int main(int argc, char **argv) {
if(!legit(page++)) return redirect(home, NULL);
setenv("MARXUP_HEADER", "2", 1);
- setenv("MARXUP_PREFIX", base, 1);
+ setenv("MARXUP_PREFIX", script, 1);
setenv("MARXUP_WIKI", "1", 1);
- if(chdir(pages))
- return problem(503, "Service unavailable");
-
if(!strncmp(verb, "POST", 4)) return post(page);
- if(strchr(page, '*'))
- return list(page, 0, 0);
while(qstr && *qstr) {
switch(*qstr) {
- case 'e': return edit(page);
- case 't': return text(page, "text/plain");
- case 'c': return text(page, "text/css");
+ case 'e': return view(page, 1);
}
qstr++;
}
- return view(page);
+ return view(page, 0);
}