cgit

commit e5da4bca54574522b28f88cab0dc8ebad9e35a73

Author: Lars Hjemli <hjemli@gmail.com>

Implement plain view

This implements a way to access plain blobs by path (similar to the
tree view) instead of by sha1.

Signed-off-by: Lars Hjemli <hjemli@gmail.com>

 Makefile | 1 
 cgit.c | 1 
 cgit.h | 1 
 cmd.c | 7 ++++
 html.c | 5 +++
 html.h | 1 
 ui-plain.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 ui-plain.h | 6 ++++
 ui-shared.c | 2 +


diff --git a/Makefile b/Makefile
index 78aad100e74af1d035445ad7ecb2e77a20894d33..a3058943d9ffe759c68c3816c1d5fc046f9b076d 100644
--- a/Makefile
+++ b/Makefile
@@ -61,6 +61,7 @@ OBJECTS += ui-commit.o
 OBJECTS += ui-diff.o
 OBJECTS += ui-log.o
 OBJECTS += ui-patch.o
+OBJECTS += ui-plain.o
 OBJECTS += ui-refs.o
 OBJECTS += ui-repolist.o
 OBJECTS += ui-shared.o




diff --git a/cgit.c b/cgit.c
index f49fffadf6191f904b07a3267597a0846f952433..497337b8e203188e54827bd4909e8cb8ff4d953a 100644
--- a/cgit.c
+++ b/cgit.c
@@ -187,6 +187,7 @@ 	ctx->cfg.summary_tags = 10;
 	ctx->page.mimetype = "text/html";
 	ctx->page.charset = PAGE_ENCODING;
 	ctx->page.filename = NULL;
+	ctx->page.size = 0;
 	ctx->page.modified = time(NULL);
 	ctx->page.expires = ctx->page.modified;
 }




diff --git a/cgit.h b/cgit.h
index b01fa314fbfc850108bfe012e0e6ad0cfa821466..e2af0c2e285f7e669fde2e7cd004775ad1c15f90 100644
--- a/cgit.h
+++ b/cgit.h
@@ -165,6 +165,7 @@
 struct cgit_page {
 	time_t modified;
 	time_t expires;
+	size_t size;
 	char *mimetype;
 	char *charset;
 	char *filename;




diff --git a/cmd.c b/cmd.c
index 03e165c169c7c4c6bc99350552b66ca7e795c0e6..2b3418998087d30c5fb3335dc08f8cdef22a12ff 100644
--- a/cmd.c
+++ b/cmd.c
@@ -16,6 +16,7 @@ #include "ui-commit.h"
 #include "ui-diff.h"
 #include "ui-log.h"
 #include "ui-patch.h"
+#include "ui-plain.h"
 #include "ui-refs.h"
 #include "ui-repolist.h"
 #include "ui-snapshot.h"
@@ -85,6 +86,11 @@ {
 	cgit_print_patch(ctx->qry.sha1);
 }
 
+static void plain_fn(struct cgit_context *ctx)
+{
+	cgit_print_plain(ctx);
+}
+
 static void refs_fn(struct cgit_context *ctx)
 {
 	cgit_print_refs();
@@ -128,6 +134,7 @@ 		def_cmd(log, 1, 1),
 		def_cmd(ls_cache, 0, 0),
 		def_cmd(objects, 1, 0),
 		def_cmd(patch, 1, 0),
+		def_cmd(plain, 1, 0),
 		def_cmd(refs, 1, 1),
 		def_cmd(repolist, 0, 0),
 		def_cmd(snapshot, 1, 0),




diff --git a/html.c b/html.c
index 1237076b32702fb8bee99672746c31e82752bd69..83fc7a9b7646a2dc96f87f277460c8c0857249ec 100644
--- a/html.c
+++ b/html.c
@@ -35,6 +35,11 @@ 	}
 	return buf[bufidx];
 }
 
+void html_raw(const char *data, size_t size)
+{
+	write(htmlfd, data, size);
+}
+
 void html(const char *txt)
 {
 	write(htmlfd, txt, strlen(txt));




diff --git a/html.h b/html.h
index 2bde28dc921fa164f0093a7c9f2b947f6851998b..49462a20ff6d96ec44947fdd14f100e82145fa3c 100644
--- a/html.h
+++ b/html.h
@@ -3,6 +3,7 @@ #define HTML_H
 
 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);




diff --git a/ui-plain.c b/ui-plain.c
new file mode 100644
index 0000000000000000000000000000000000000000..28deae58d748fec3546df50b3d0da90d0348e71e
--- /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, 0);
+		return;
+	}
+
+	buf = read_sha1_file(sha1, &type, &size);
+	if (!buf) {
+		html_status(404, 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, 0);
+		return;
+	}
+	commit = lookup_commit_reference(sha1);
+	if (!commit || parse_commit(commit)) {
+		html_status(404, 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, 0);
+}




diff --git a/ui-plain.h b/ui-plain.h
new file mode 100644
index 0000000000000000000000000000000000000000..4373118cb3feb2a37b163770dc71d0594fcb9e58
--- /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 197ee37409b7497d27c1ea766ebd6d4405d843bc..4408969c4a3a244b98d2d6678fcdff2c93a03a04 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -418,6 +418,8 @@ 		htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype,
 		      ctx->page.charset);
 	else if (ctx->page.mimetype)
 		htmlf("Content-Type: %s\n", ctx->page.mimetype);
+	if (ctx->page.size)
+		htmlf("Content-Length: %ld\n", ctx->page.size);
 	if (ctx->page.filename)
 		htmlf("Content-Disposition: inline; filename=\"%s\"\n",
 		      ctx->page.filename);