author | Lars Hjemli <hjemli@gmail.com> | 2008-09-01 20:40:55 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2008-09-01 20:40:55 (UTC) |
commit | d532c4d1612c94347427fa1afda6afb7c34e512a (patch) (unidiff) | |
tree | 53f3f86ba8e78051bee96cb65a6219ef43d9adab | |
parent | 288d502b3d8e7fa916104b486bbb146521e5c716 (diff) | |
parent | 885096c189574b1cf2e0897cc05aadd7b092a677 (diff) | |
download | cgit-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
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | cgit.c | 1 | ||||
-rw-r--r-- | cgit.h | 1 | ||||
-rw-r--r-- | cmd.c | 7 | ||||
-rw-r--r-- | html.c | 9 | ||||
-rw-r--r-- | html.h | 3 | ||||
-rw-r--r-- | ui-clone.c | 10 | ||||
-rw-r--r-- | ui-plain.c | 82 | ||||
-rw-r--r-- | ui-plain.h | 6 | ||||
-rw-r--r-- | ui-shared.c | 8 | ||||
-rw-r--r-- | ui-shared.h | 2 | ||||
-rw-r--r-- | ui-tree.c | 8 |
12 files changed, 125 insertions, 13 deletions
@@ -59,12 +59,13 @@ OBJECTS += ui-atom.o | |||
59 | OBJECTS += ui-blob.o | 59 | OBJECTS += ui-blob.o |
60 | OBJECTS += ui-clone.o | 60 | OBJECTS += ui-clone.o |
61 | OBJECTS += ui-commit.o | 61 | OBJECTS += ui-commit.o |
62 | OBJECTS += ui-diff.o | 62 | OBJECTS += ui-diff.o |
63 | OBJECTS += ui-log.o | 63 | OBJECTS += ui-log.o |
64 | OBJECTS += ui-patch.o | 64 | OBJECTS += ui-patch.o |
65 | OBJECTS += ui-plain.o | ||
65 | OBJECTS += ui-refs.o | 66 | OBJECTS += ui-refs.o |
66 | OBJECTS += ui-repolist.o | 67 | OBJECTS += ui-repolist.o |
67 | OBJECTS += ui-shared.o | 68 | OBJECTS += ui-shared.o |
68 | OBJECTS += ui-snapshot.o | 69 | OBJECTS += ui-snapshot.o |
69 | OBJECTS += ui-summary.o | 70 | OBJECTS += ui-summary.o |
70 | OBJECTS += ui-tag.o | 71 | OBJECTS += ui-tag.o |
@@ -184,12 +184,13 @@ static void prepare_context(struct cgit_context *ctx) | |||
184 | ctx->cfg.summary_branches = 10; | 184 | ctx->cfg.summary_branches = 10; |
185 | ctx->cfg.summary_log = 10; | 185 | ctx->cfg.summary_log = 10; |
186 | ctx->cfg.summary_tags = 10; | 186 | ctx->cfg.summary_tags = 10; |
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 | } |
193 | 194 | ||
194 | struct refmatch { | 195 | struct refmatch { |
195 | char *req_ref; | 196 | char *req_ref; |
@@ -163,12 +163,13 @@ struct cgit_config { | |||
163 | int summary_tags; | 163 | int summary_tags; |
164 | }; | 164 | }; |
165 | 165 | ||
166 | struct cgit_page { | 166 | struct 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; |
172 | char *title; | 173 | char *title; |
173 | }; | 174 | }; |
174 | 175 | ||
@@ -14,12 +14,13 @@ | |||
14 | #include "ui-blob.h" | 14 | #include "ui-blob.h" |
15 | #include "ui-clone.h" | 15 | #include "ui-clone.h" |
16 | #include "ui-commit.h" | 16 | #include "ui-commit.h" |
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" |
23 | #include "ui-summary.h" | 24 | #include "ui-summary.h" |
24 | #include "ui-tag.h" | 25 | #include "ui-tag.h" |
25 | #include "ui-tree.h" | 26 | #include "ui-tree.h" |
@@ -88,12 +89,17 @@ static void repolist_fn(struct cgit_context *ctx) | |||
88 | 89 | ||
89 | static void patch_fn(struct cgit_context *ctx) | 90 | static void patch_fn(struct cgit_context *ctx) |
90 | { | 91 | { |
91 | cgit_print_patch(ctx->qry.sha1); | 92 | cgit_print_patch(ctx->qry.sha1); |
92 | } | 93 | } |
93 | 94 | ||
95 | static void plain_fn(struct cgit_context *ctx) | ||
96 | { | ||
97 | cgit_print_plain(ctx); | ||
98 | } | ||
99 | |||
94 | static void refs_fn(struct cgit_context *ctx) | 100 | static void refs_fn(struct cgit_context *ctx) |
95 | { | 101 | { |
96 | cgit_print_refs(); | 102 | cgit_print_refs(); |
97 | } | 103 | } |
98 | 104 | ||
99 | static void snapshot_fn(struct cgit_context *ctx) | 105 | static void snapshot_fn(struct cgit_context *ctx) |
@@ -132,12 +138,13 @@ struct cgit_cmd *cgit_get_cmd(struct cgit_context *ctx) | |||
132 | def_cmd(diff, 1, 1), | 138 | def_cmd(diff, 1, 1), |
133 | def_cmd(info, 1, 0), | 139 | def_cmd(info, 1, 0), |
134 | def_cmd(log, 1, 1), | 140 | def_cmd(log, 1, 1), |
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), |
141 | def_cmd(summary, 1, 1), | 148 | def_cmd(summary, 1, 1), |
142 | def_cmd(tag, 1, 1), | 149 | def_cmd(tag, 1, 1), |
143 | def_cmd(tree, 1, 1), | 150 | def_cmd(tree, 1, 1), |
@@ -32,12 +32,17 @@ char *fmt(const char *format, ...) | |||
32 | fprintf(stderr, "[html.c] string truncated: %s\n", format); | 32 | fprintf(stderr, "[html.c] string truncated: %s\n", format); |
33 | exit(1); | 33 | exit(1); |
34 | } | 34 | } |
35 | return buf[bufidx]; | 35 | return buf[bufidx]; |
36 | } | 36 | } |
37 | 37 | ||
38 | void html_raw(const char *data, size_t size) | ||
39 | { | ||
40 | write(htmlfd, data, size); | ||
41 | } | ||
42 | |||
38 | void html(const char *txt) | 43 | void html(const char *txt) |
39 | { | 44 | { |
40 | write(htmlfd, txt, strlen(txt)); | 45 | write(htmlfd, txt, strlen(txt)); |
41 | } | 46 | } |
42 | 47 | ||
43 | void htmlf(const char *format, ...) | 48 | void htmlf(const char *format, ...) |
@@ -48,15 +53,15 @@ void htmlf(const char *format, ...) | |||
48 | va_start(args, format); | 53 | va_start(args, format); |
49 | vsnprintf(buf, sizeof(buf), format, args); | 54 | vsnprintf(buf, sizeof(buf), format, args); |
50 | va_end(args); | 55 | va_end(args); |
51 | html(buf); | 56 | html(buf); |
52 | } | 57 | } |
53 | 58 | ||
54 | void html_status(int code, int more_headers) | 59 | void 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 | } |
60 | 65 | ||
61 | void html_txt(char *txt) | 66 | void html_txt(char *txt) |
62 | { | 67 | { |
@@ -1,14 +1,15 @@ | |||
1 | #ifndef HTML_H | 1 | #ifndef HTML_H |
2 | #define HTML_H | 2 | #define HTML_H |
3 | 3 | ||
4 | extern int htmlfd; | 4 | extern int htmlfd; |
5 | 5 | ||
6 | extern void html_raw(const char *txt, size_t size); | ||
6 | extern void html(const char *txt); | 7 | extern void html(const char *txt); |
7 | extern void htmlf(const char *format,...); | 8 | extern void htmlf(const char *format,...); |
8 | extern void html_status(int code, int more_headers); | 9 | extern void html_status(int code, const char *msg, int more_headers); |
9 | extern void html_txt(char *txt); | 10 | extern void html_txt(char *txt); |
10 | extern void html_ntxt(int len, char *txt); | 11 | extern void html_ntxt(int len, char *txt); |
11 | extern void html_attr(char *txt); | 12 | extern void html_attr(char *txt); |
12 | extern void html_hidden(char *name, char *value); | 13 | extern void html_hidden(char *name, char *value); |
13 | extern void html_option(char *value, char *text, char *selected_value); | 14 | extern void html_option(char *value, char *text, char *selected_value); |
14 | extern void html_link_open(char *url, char *title, char *class); | 15 | extern void html_link_open(char *url, char *title, char *class); |
@@ -45,26 +45,24 @@ static void print_pack_info(struct cgit_context *ctx) | |||
45 | htmlf("P %s\n", pack->pack_name + ofs); | 45 | htmlf("P %s\n", pack->pack_name + ofs); |
46 | } | 46 | } |
47 | 47 | ||
48 | static void send_file(struct cgit_context *ctx, char *path) | 48 | static 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"; |
68 | ctx->page.filename = path; | 66 | ctx->page.filename = path; |
69 | if (prefixcmp(ctx->repo->path, path)) | 67 | if (prefixcmp(ctx->repo->path, path)) |
70 | ctx->page.filename += strlen(ctx->repo->path) + 1; | 68 | ctx->page.filename += strlen(ctx->repo->path) + 1; |
@@ -83,13 +81,13 @@ void cgit_clone_info(struct cgit_context *ctx) | |||
83 | for_each_ref(print_ref_info, ctx); | 81 | for_each_ref(print_ref_info, ctx); |
84 | } | 82 | } |
85 | 83 | ||
86 | void cgit_clone_objects(struct cgit_context *ctx) | 84 | void 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 | ||
93 | if (!strcmp(ctx->qry.path, "info/packs")) { | 91 | if (!strcmp(ctx->qry.path, "info/packs")) { |
94 | print_pack_info(ctx); | 92 | print_pack_info(ctx); |
95 | return; | 93 | 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 @@ | |||
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 | |||
13 | char *curr_rev; | ||
14 | char *match_path; | ||
15 | int match; | ||
16 | |||
17 | static 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 | |||
42 | static 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 | |||
57 | void 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 --- a/dev/null +++ b/ui-plain.h | |||
@@ -0,0 +1,6 @@ | |||
1 | #ifndef UI_PLAIN_H | ||
2 | #define UI_PLAIN_H | ||
3 | |||
4 | extern 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 | |||
@@ -255,12 +255,18 @@ static void reporevlink(char *page, char *name, char *title, char *class, | |||
255 | void cgit_tree_link(char *name, char *title, char *class, char *head, | 255 | void cgit_tree_link(char *name, char *title, char *class, char *head, |
256 | char *rev, char *path) | 256 | char *rev, char *path) |
257 | { | 257 | { |
258 | reporevlink("tree", name, title, class, head, rev, path); | 258 | reporevlink("tree", name, title, class, head, rev, path); |
259 | } | 259 | } |
260 | 260 | ||
261 | void 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 | |||
261 | void cgit_log_link(char *name, char *title, char *class, char *head, | 267 | void 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 | { |
264 | char *delim; | 270 | char *delim; |
265 | 271 | ||
266 | delim = repolink(title, class, "log", head, path); | 272 | delim = repolink(title, class, "log", head, path); |
@@ -430,12 +436,14 @@ void cgit_print_http_headers(struct cgit_context *ctx) | |||
430 | { | 436 | { |
431 | if (ctx->page.mimetype && ctx->page.charset) | 437 | if (ctx->page.mimetype && ctx->page.charset) |
432 | htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype, | 438 | htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype, |
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); |
439 | htmlf("Last-Modified: %s\n", http_date(ctx->page.modified)); | 447 | htmlf("Last-Modified: %s\n", http_date(ctx->page.modified)); |
440 | htmlf("Expires: %s\n", http_date(ctx->page.expires)); | 448 | htmlf("Expires: %s\n", http_date(ctx->page.expires)); |
441 | html("\n"); | 449 | html("\n"); |
diff --git a/ui-shared.h b/ui-shared.h index f4123d3..747f092 100644 --- a/ui-shared.h +++ b/ui-shared.h | |||
@@ -9,12 +9,14 @@ extern char *cgit_pageurl(const char *reponame, const char *pagename, | |||
9 | const char *query); | 9 | const char *query); |
10 | 10 | ||
11 | extern void cgit_index_link(char *name, char *title, char *class, | 11 | extern void cgit_index_link(char *name, char *title, char *class, |
12 | char *pattern, int ofs); | 12 | char *pattern, int ofs); |
13 | extern void cgit_tree_link(char *name, char *title, char *class, char *head, | 13 | extern void cgit_tree_link(char *name, char *title, char *class, char *head, |
14 | char *rev, char *path); | 14 | char *rev, char *path); |
15 | extern void cgit_plain_link(char *name, char *title, char *class, char *head, | ||
16 | char *rev, char *path); | ||
15 | extern void cgit_log_link(char *name, char *title, char *class, char *head, | 17 | extern 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); |
18 | extern void cgit_commit_link(char *name, char *title, char *class, char *head, | 20 | extern void cgit_commit_link(char *name, char *title, char *class, char *head, |
19 | char *rev); | 21 | char *rev); |
20 | extern void cgit_patch_link(char *name, char *title, char *class, char *head, | 22 | extern void cgit_patch_link(char *name, char *title, char *class, char *head, |
@@ -32,16 +32,16 @@ static void print_object(const unsigned char *sha1, char *path) | |||
32 | if (!buf) { | 32 | if (!buf) { |
33 | cgit_print_error(fmt("Error reading object %s", | 33 | cgit_print_error(fmt("Error reading object %s", |
34 | sha1_to_hex(sha1))); | 34 | sha1_to_hex(sha1))); |
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; |
45 | start = 0; | 45 | start = 0; |
46 | lineno = 0; | 46 | lineno = 0; |
47 | while(idx < size) { | 47 | while(idx < size) { |