aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLars Hjemli2008-09-01 22:40:55 +0200
committerLars Hjemli2008-09-01 22:40:55 +0200
commitd532c4d1612c94347427fa1afda6afb7c34e512a (patch)
tree53f3f86ba8e78051bee96cb65a6219ef43d9adab
parent288d502b3d8e7fa916104b486bbb146521e5c716 (diff)
parent885096c189574b1cf2e0897cc05aadd7b092a677 (diff)
downloadcgit-d532c4d1612c94347427fa1afda6afb7c34e512a.tar.gz
cgit-d532c4d1612c94347427fa1afda6afb7c34e512a.tar.bz2
cgit-d532c4d1612c94347427fa1afda6afb7c34e512a.zip
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
-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
@@ -62,6 +62,7 @@ OBJECTS += ui-commit.o
62OBJECTS += ui-diff.o 62OBJECTS += ui-diff.o
63OBJECTS += ui-log.o 63OBJECTS += ui-log.o
64OBJECTS += ui-patch.o 64OBJECTS += ui-patch.o
65OBJECTS += ui-plain.o
65OBJECTS += ui-refs.o 66OBJECTS += ui-refs.o
66OBJECTS += ui-repolist.o 67OBJECTS += ui-repolist.o
67OBJECTS += ui-shared.o 68OBJECTS += ui-shared.o
diff --git a/cgit.c b/cgit.c
index f49fffa..497337b 100644
--- a/cgit.c
+++ b/cgit.c
@@ -187,6 +187,7 @@ static void prepare_context(struct cgit_context *ctx)
187 ctx->page.mimetype = "text/html"; 187 ctx->page.mimetype = "text/html";
188 ctx->page.charset = PAGE_ENCODING; 188 ctx->page.charset = PAGE_ENCODING;
189 ctx->page.filename = NULL; 189 ctx->page.filename = NULL;
190 ctx->page.size = 0;
190 ctx->page.modified = time(NULL); 191 ctx->page.modified = time(NULL);
191 ctx->page.expires = ctx->page.modified; 192 ctx->page.expires = ctx->page.modified;
192} 193}
diff --git a/cgit.h b/cgit.h
index a1fa841..1615616 100644
--- a/cgit.h
+++ b/cgit.h
@@ -166,6 +166,7 @@ struct cgit_config {
166struct cgit_page { 166struct cgit_page {
167 time_t modified; 167 time_t modified;
168 time_t expires; 168 time_t expires;
169 size_t size;
169 char *mimetype; 170 char *mimetype;
170 char *charset; 171 char *charset;
171 char *filename; 172 char *filename;
diff --git a/cmd.c b/cmd.c
index 40ac53e..a989220 100644
--- a/cmd.c
+++ b/cmd.c
@@ -17,6 +17,7 @@
17#include "ui-diff.h" 17#include "ui-diff.h"
18#include "ui-log.h" 18#include "ui-log.h"
19#include "ui-patch.h" 19#include "ui-patch.h"
20#include "ui-plain.h"
20#include "ui-refs.h" 21#include "ui-refs.h"
21#include "ui-repolist.h" 22#include "ui-repolist.h"
22#include "ui-snapshot.h" 23#include "ui-snapshot.h"
@@ -91,6 +92,11 @@ static void patch_fn(struct cgit_context *ctx)
91 cgit_print_patch(ctx->qry.sha1); 92 cgit_print_patch(ctx->qry.sha1);
92} 93}
93 94
95static void plain_fn(struct cgit_context *ctx)
96{
97 cgit_print_plain(ctx);
98}
99
94static void refs_fn(struct cgit_context *ctx) 100static void refs_fn(struct cgit_context *ctx)
95{ 101{
96 cgit_print_refs(); 102 cgit_print_refs();
@@ -135,6 +141,7 @@ struct cgit_cmd *cgit_get_cmd(struct cgit_context *ctx)
135 def_cmd(ls_cache, 0, 0), 141 def_cmd(ls_cache, 0, 0),
136 def_cmd(objects, 1, 0), 142 def_cmd(objects, 1, 0),
137 def_cmd(patch, 1, 0), 143 def_cmd(patch, 1, 0),
144 def_cmd(plain, 1, 0),
138 def_cmd(refs, 1, 1), 145 def_cmd(refs, 1, 1),
139 def_cmd(repolist, 0, 0), 146 def_cmd(repolist, 0, 0),
140 def_cmd(snapshot, 1, 0), 147 def_cmd(snapshot, 1, 0),
diff --git a/html.c b/html.c
index 1237076..36e9a2f 100644
--- a/html.c
+++ b/html.c
@@ -35,6 +35,11 @@ char *fmt(const char *format, ...)
35 return buf[bufidx]; 35 return buf[bufidx];
36} 36}
37 37
38void html_raw(const char *data, size_t size)
39{
40 write(htmlfd, data, size);
41}
42
38void html(const char *txt) 43void html(const char *txt)
39{ 44{
40 write(htmlfd, txt, strlen(txt)); 45 write(htmlfd, txt, strlen(txt));
@@ -51,9 +56,9 @@ void htmlf(const char *format, ...)
51 html(buf); 56 html(buf);
52} 57}
53 58
54void html_status(int code, int more_headers) 59void html_status(int code, const char *msg, int more_headers)
55{ 60{
56 htmlf("Status: %d\n", code); 61 htmlf("Status: %d %s\n", code, msg);
57 if (!more_headers) 62 if (!more_headers)
58 html("\n"); 63 html("\n");
59} 64}
diff --git a/html.h b/html.h
index 2bde28d..3c32935 100644
--- a/html.h
+++ b/html.h
@@ -3,9 +3,10 @@
3 3
4extern int htmlfd; 4extern int htmlfd;
5 5
6extern void html_raw(const char *txt, size_t size);
6extern void html(const char *txt); 7extern void html(const char *txt);
7extern void htmlf(const char *format,...); 8extern void htmlf(const char *format,...);
8extern void html_status(int code, int more_headers); 9extern void html_status(int code, const char *msg, int more_headers);
9extern void html_txt(char *txt); 10extern void html_txt(char *txt);
10extern void html_ntxt(int len, char *txt); 11extern void html_ntxt(int len, char *txt);
11extern void html_attr(char *txt); 12extern void html_attr(char *txt);
diff --git a/ui-clone.c b/ui-clone.c
index 3a037ad..81e7a4e 100644
--- a/ui-clone.c
+++ b/ui-clone.c
@@ -48,20 +48,18 @@ static void print_pack_info(struct cgit_context *ctx)
48static void send_file(struct cgit_context *ctx, char *path) 48static void send_file(struct cgit_context *ctx, char *path)
49{ 49{
50 struct stat st; 50 struct stat st;
51 int err;
52 51
53 if (stat(path, &st)) { 52 if (stat(path, &st)) {
54 switch (errno) { 53 switch (errno) {
55 case ENOENT: 54 case ENOENT:
56 err = 404; 55 html_status(404, "Not found", 0);
57 break; 56 break;
58 case EACCES: 57 case EACCES:
59 err = 403; 58 html_status(403, "Forbidden", 0);
60 break; 59 break;
61 default: 60 default:
62 err = 400; 61 html_status(400, "Bad request", 0);
63 } 62 }
64 html_status(err, 0);
65 return; 63 return;
66 } 64 }
67 ctx->page.mimetype = "application/octet-stream"; 65 ctx->page.mimetype = "application/octet-stream";
@@ -86,7 +84,7 @@ void cgit_clone_info(struct cgit_context *ctx)
86void cgit_clone_objects(struct cgit_context *ctx) 84void cgit_clone_objects(struct cgit_context *ctx)
87{ 85{
88 if (!ctx->qry.path) { 86 if (!ctx->qry.path) {
89 html_status(400, 0); 87 html_status(400, "Bad request", 0);
90 return; 88 return;
91 } 89 }
92 90
diff --git a/ui-plain.c b/ui-plain.c
new file mode 100644
index 0000000..35888a0
--- /dev/null
+++ b/ui-plain.c
@@ -0,0 +1,82 @@
1/* ui-plain.c: functions for output of plain blobs by path
2 *
3 * Copyright (C) 2008 Lars Hjemli
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9#include "cgit.h"
10#include "html.h"
11#include "ui-shared.h"
12
13char *curr_rev;
14char *match_path;
15int match;
16
17static void print_object(const unsigned char *sha1, const char *path)
18{
19 enum object_type type;
20 char *buf;
21 size_t size;
22
23 type = sha1_object_info(sha1, &size);
24 if (type == OBJ_BAD) {
25 html_status(404, "Not found", 0);
26 return;
27 }
28
29 buf = read_sha1_file(sha1, &type, &size);
30 if (!buf) {
31 html_status(404, "Not found", 0);
32 return;
33 }
34 ctx.page.mimetype = "text/plain";
35 ctx.page.filename = fmt("%s", path);
36 ctx.page.size = size;
37 cgit_print_http_headers(&ctx);
38 html_raw(buf, size);
39 match = 1;
40}
41
42static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
43 const char *pathname, unsigned mode, int stage,
44 void *cbdata)
45{
46 fprintf(stderr, "[cgit] walk_tree.pathname=%s", pathname);
47
48 if (!pathname || strcmp(match_path, pathname))
49 return READ_TREE_RECURSIVE;
50
51 if (S_ISREG(mode))
52 print_object(sha1, pathname);
53
54 return 0;
55}
56
57void cgit_print_plain(struct cgit_context *ctx)
58{
59 const char *rev = ctx->qry.sha1;
60 unsigned char sha1[20];
61 struct commit *commit;
62 const char *paths[] = {ctx->qry.path, NULL};
63
64 if (!rev)
65 rev = ctx->qry.head;
66
67 curr_rev = xstrdup(rev);
68 if (get_sha1(rev, sha1)) {
69 html_status(404, "Not found", 0);
70 return;
71 }
72 commit = lookup_commit_reference(sha1);
73 if (!commit || parse_commit(commit)) {
74 html_status(404, "Not found", 0);
75 return;
76 }
77 match_path = ctx->qry.path;
78 fprintf(stderr, "[cgit] match_path=%s", match_path);
79 read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree, NULL);
80 if (!match)
81 html_status(404, "Not found", 0);
82}
diff --git a/ui-plain.h b/ui-plain.h
new file mode 100644
index 0000000..4373118
--- /dev/null
+++ b/ui-plain.h
@@ -0,0 +1,6 @@
1#ifndef UI_PLAIN_H
2#define UI_PLAIN_H
3
4extern void cgit_print_plain(struct cgit_context *ctx);
5
6#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
@@ -258,6 +258,12 @@ void cgit_tree_link(char *name, char *title, char *class, char *head,
258 reporevlink("tree", name, title, class, head, rev, path); 258 reporevlink("tree", name, title, class, head, rev, path);
259} 259}
260 260
261void cgit_plain_link(char *name, char *title, char *class, char *head,
262 char *rev, char *path)
263{
264 reporevlink("plain", name, title, class, head, rev, path);
265}
266
261void cgit_log_link(char *name, char *title, char *class, char *head, 267void cgit_log_link(char *name, char *title, char *class, char *head,
262 char *rev, char *path, int ofs, char *grep, char *pattern) 268 char *rev, char *path, int ofs, char *grep, char *pattern)
263{ 269{
@@ -433,6 +439,8 @@ void cgit_print_http_headers(struct cgit_context *ctx)
433 ctx->page.charset); 439 ctx->page.charset);
434 else if (ctx->page.mimetype) 440 else if (ctx->page.mimetype)
435 htmlf("Content-Type: %s\n", ctx->page.mimetype); 441 htmlf("Content-Type: %s\n", ctx->page.mimetype);
442 if (ctx->page.size)
443 htmlf("Content-Length: %ld\n", ctx->page.size);
436 if (ctx->page.filename) 444 if (ctx->page.filename)
437 htmlf("Content-Disposition: inline; filename=\"%s\"\n", 445 htmlf("Content-Disposition: inline; filename=\"%s\"\n",
438 ctx->page.filename); 446 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
@@ -12,6 +12,8 @@ extern void cgit_index_link(char *name, char *title, char *class,
12 char *pattern, int ofs); 12 char *pattern, int ofs);
13extern void cgit_tree_link(char *name, char *title, char *class, char *head, 13extern void cgit_tree_link(char *name, char *title, char *class, char *head,
14 char *rev, char *path); 14 char *rev, char *path);
15extern void cgit_plain_link(char *name, char *title, char *class, char *head,
16 char *rev, char *path);
15extern void cgit_log_link(char *name, char *title, char *class, char *head, 17extern void cgit_log_link(char *name, char *title, char *class, char *head,
16 char *rev, char *path, int ofs, char *grep, 18 char *rev, char *path, int ofs, char *grep,
17 char *pattern); 19 char *pattern);
diff --git a/ui-tree.c b/ui-tree.c
index 9a837e2..79332fc 100644
--- a/ui-tree.c
+++ b/ui-tree.c
@@ -35,10 +35,10 @@ static void print_object(const unsigned char *sha1, char *path)
35 return; 35 return;
36 } 36 }
37 37
38 html(" blob: <a href='"); 38 html(" (");
39 html_attr(cgit_pageurl(ctx.qry.repo, "blob", 39 cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
40 fmt("id=%s&path=%s", sha1_to_hex(sha1), path))); 40 curr_rev, path);
41 htmlf("'>%s</a>",sha1_to_hex(sha1)); 41 htmlf(")<br/>blob: %s", sha1_to_hex(sha1));
42 42
43 html("<table summary='blob content' class='blob'>\n"); 43 html("<table summary='blob content' class='blob'>\n");
44 idx = 0; 44 idx = 0;