summaryrefslogtreecommitdiffabout
authorLars Hjemli <hjemli@gmail.com>2008-09-01 20:40:55 (UTC)
committer Lars Hjemli <hjemli@gmail.com>2008-09-01 20:40:55 (UTC)
commitd532c4d1612c94347427fa1afda6afb7c34e512a (patch) (side-by-side diff)
tree53f3f86ba8e78051bee96cb65a6219ef43d9adab
parent288d502b3d8e7fa916104b486bbb146521e5c716 (diff)
parent885096c189574b1cf2e0897cc05aadd7b092a677 (diff)
downloadcgit-d532c4d1612c94347427fa1afda6afb7c34e512a.zip
cgit-d532c4d1612c94347427fa1afda6afb7c34e512a.tar.gz
cgit-d532c4d1612c94347427fa1afda6afb7c34e512a.tar.bz2
Merge branch 'lh/plain'
* lh/plain: Supply status description to html_status() ui-tree: link to plain view instead of blob view Implement plain view
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--Makefile1
-rw-r--r--cgit.c1
-rw-r--r--cgit.h1
-rw-r--r--cmd.c7
-rw-r--r--html.c9
-rw-r--r--html.h3
-rw-r--r--ui-clone.c10
-rw-r--r--ui-plain.c82
-rw-r--r--ui-plain.h6
-rw-r--r--ui-shared.c8
-rw-r--r--ui-shared.h2
-rw-r--r--ui-tree.c8
12 files changed, 125 insertions, 13 deletions
diff --git a/Makefile b/Makefile
index b002d44..e4265f7 100644
--- a/Makefile
+++ b/Makefile
@@ -64,2 +64,3 @@ OBJECTS += ui-log.o
OBJECTS += ui-patch.o
+OBJECTS += ui-plain.o
OBJECTS += ui-refs.o
diff --git a/cgit.c b/cgit.c
index f49fffa..497337b 100644
--- a/cgit.c
+++ b/cgit.c
@@ -189,2 +189,3 @@ static void prepare_context(struct cgit_context *ctx)
ctx->page.filename = NULL;
+ ctx->page.size = 0;
ctx->page.modified = time(NULL);
diff --git a/cgit.h b/cgit.h
index a1fa841..1615616 100644
--- a/cgit.h
+++ b/cgit.h
@@ -168,2 +168,3 @@ struct cgit_page {
time_t expires;
+ size_t size;
char *mimetype;
diff --git a/cmd.c b/cmd.c
index 40ac53e..a989220 100644
--- a/cmd.c
+++ b/cmd.c
@@ -19,2 +19,3 @@
#include "ui-patch.h"
+#include "ui-plain.h"
#include "ui-refs.h"
@@ -93,2 +94,7 @@ static void patch_fn(struct cgit_context *ctx)
+static void plain_fn(struct cgit_context *ctx)
+{
+ cgit_print_plain(ctx);
+}
+
static void refs_fn(struct cgit_context *ctx)
@@ -137,2 +143,3 @@ struct cgit_cmd *cgit_get_cmd(struct cgit_context *ctx)
def_cmd(patch, 1, 0),
+ def_cmd(plain, 1, 0),
def_cmd(refs, 1, 1),
diff --git a/html.c b/html.c
index 1237076..36e9a2f 100644
--- a/html.c
+++ b/html.c
@@ -37,2 +37,7 @@ char *fmt(const char *format, ...)
+void html_raw(const char *data, size_t size)
+{
+ write(htmlfd, data, size);
+}
+
void html(const char *txt)
@@ -53,5 +58,5 @@ void htmlf(const char *format, ...)
-void html_status(int code, int more_headers)
+void html_status(int code, const char *msg, int more_headers)
{
- htmlf("Status: %d\n", code);
+ htmlf("Status: %d %s\n", code, msg);
if (!more_headers)
diff --git a/html.h b/html.h
index 2bde28d..3c32935 100644
--- a/html.h
+++ b/html.h
@@ -5,5 +5,6 @@ extern int htmlfd;
+extern void html_raw(const char *txt, size_t size);
extern void html(const char *txt);
extern void htmlf(const char *format,...);
-extern void html_status(int code, int more_headers);
+extern void html_status(int code, const char *msg, int more_headers);
extern void html_txt(char *txt);
diff --git a/ui-clone.c b/ui-clone.c
index 3a037ad..81e7a4e 100644
--- a/ui-clone.c
+++ b/ui-clone.c
@@ -50,3 +50,2 @@ static void send_file(struct cgit_context *ctx, char *path)
struct stat st;
- int err;
@@ -55,11 +54,10 @@ static void send_file(struct cgit_context *ctx, char *path)
case ENOENT:
- err = 404;
+ html_status(404, "Not found", 0);
break;
case EACCES:
- err = 403;
+ html_status(403, "Forbidden", 0);
break;
default:
- err = 400;
+ html_status(400, "Bad request", 0);
}
- html_status(err, 0);
return;
@@ -88,3 +86,3 @@ void cgit_clone_objects(struct cgit_context *ctx)
if (!ctx->qry.path) {
- html_status(400, 0);
+ html_status(400, "Bad request", 0);
return;
diff --git a/ui-plain.c b/ui-plain.c
new file mode 100644
index 0000000..35888a0
--- a/dev/null
+++ b/ui-plain.c
@@ -0,0 +1,82 @@
+/* ui-plain.c: functions for output of plain blobs by path
+ *
+ * Copyright (C) 2008 Lars Hjemli
+ *
+ * Licensed under GNU General Public License v2
+ * (see COPYING for full license text)
+ */
+
+#include "cgit.h"
+#include "html.h"
+#include "ui-shared.h"
+
+char *curr_rev;
+char *match_path;
+int match;
+
+static void print_object(const unsigned char *sha1, const char *path)
+{
+ enum object_type type;
+ char *buf;
+ size_t size;
+
+ type = sha1_object_info(sha1, &size);
+ if (type == OBJ_BAD) {
+ html_status(404, "Not found", 0);
+ return;
+ }
+
+ buf = read_sha1_file(sha1, &type, &size);
+ if (!buf) {
+ html_status(404, "Not found", 0);
+ return;
+ }
+ ctx.page.mimetype = "text/plain";
+ ctx.page.filename = fmt("%s", path);
+ ctx.page.size = size;
+ cgit_print_http_headers(&ctx);
+ html_raw(buf, size);
+ match = 1;
+}
+
+static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
+ const char *pathname, unsigned mode, int stage,
+ void *cbdata)
+{
+ fprintf(stderr, "[cgit] walk_tree.pathname=%s", pathname);
+
+ if (!pathname || strcmp(match_path, pathname))
+ return READ_TREE_RECURSIVE;
+
+ if (S_ISREG(mode))
+ print_object(sha1, pathname);
+
+ return 0;
+}
+
+void cgit_print_plain(struct cgit_context *ctx)
+{
+ const char *rev = ctx->qry.sha1;
+ unsigned char sha1[20];
+ struct commit *commit;
+ const char *paths[] = {ctx->qry.path, NULL};
+
+ if (!rev)
+ rev = ctx->qry.head;
+
+ curr_rev = xstrdup(rev);
+ if (get_sha1(rev, sha1)) {
+ html_status(404, "Not found", 0);
+ return;
+ }
+ commit = lookup_commit_reference(sha1);
+ if (!commit || parse_commit(commit)) {
+ html_status(404, "Not found", 0);
+ return;
+ }
+ match_path = ctx->qry.path;
+ fprintf(stderr, "[cgit] match_path=%s", match_path);
+ read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree, NULL);
+ if (!match)
+ html_status(404, "Not found", 0);
+}
diff --git a/ui-plain.h b/ui-plain.h
new file mode 100644
index 0000000..4373118
--- a/dev/null
+++ b/ui-plain.h
@@ -0,0 +1,6 @@
+#ifndef UI_PLAIN_H
+#define UI_PLAIN_H
+
+extern void cgit_print_plain(struct cgit_context *ctx);
+
+#endif /* UI_PLAIN_H */
diff --git a/ui-shared.c b/ui-shared.c
index 37c60b2..4818e70 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -260,2 +260,8 @@ void cgit_tree_link(char *name, char *title, char *class, char *head,
+void cgit_plain_link(char *name, char *title, char *class, char *head,
+ char *rev, char *path)
+{
+ reporevlink("plain", name, title, class, head, rev, path);
+}
+
void cgit_log_link(char *name, char *title, char *class, char *head,
@@ -435,2 +441,4 @@ void cgit_print_http_headers(struct cgit_context *ctx)
htmlf("Content-Type: %s\n", ctx->page.mimetype);
+ if (ctx->page.size)
+ htmlf("Content-Length: %ld\n", ctx->page.size);
if (ctx->page.filename)
diff --git a/ui-shared.h b/ui-shared.h
index f4123d3..747f092 100644
--- a/ui-shared.h
+++ b/ui-shared.h
@@ -14,2 +14,4 @@ extern void cgit_tree_link(char *name, char *title, char *class, char *head,
char *rev, char *path);
+extern void cgit_plain_link(char *name, char *title, char *class, char *head,
+ char *rev, char *path);
extern void cgit_log_link(char *name, char *title, char *class, char *head,
diff --git a/ui-tree.c b/ui-tree.c
index 9a837e2..79332fc 100644
--- a/ui-tree.c
+++ b/ui-tree.c
@@ -37,6 +37,6 @@ static void print_object(const unsigned char *sha1, char *path)
- html(" blob: <a href='");
- html_attr(cgit_pageurl(ctx.qry.repo, "blob",
- fmt("id=%s&path=%s", sha1_to_hex(sha1), path)));
- htmlf("'>%s</a>",sha1_to_hex(sha1));
+ html(" (");
+ cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
+ curr_rev, path);
+ htmlf(")<br/>blob: %s", sha1_to_hex(sha1));