summaryrefslogtreecommitdiffabout
Side-by-side diff
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
@@ -53,24 +53,25 @@ OBJECTS += cgit.o
OBJECTS += cmd.o
OBJECTS += configfile.o
OBJECTS += html.o
OBJECTS += parsing.o
OBJECTS += shared.o
OBJECTS += ui-atom.o
OBJECTS += ui-blob.o
OBJECTS += ui-clone.o
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
OBJECTS += ui-snapshot.o
OBJECTS += ui-summary.o
OBJECTS += ui-tag.o
OBJECTS += ui-tree.o
ifdef NEEDS_LIBICONV
EXTLIBS += -liconv
endif
diff --git a/cgit.c b/cgit.c
index f49fffa..497337b 100644
--- a/cgit.c
+++ b/cgit.c
@@ -178,24 +178,25 @@ static void prepare_context(struct cgit_context *ctx)
ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s";
ctx->cfg.renamelimit = -1;
ctx->cfg.robots = "index, nofollow";
ctx->cfg.root_title = "Git repository browser";
ctx->cfg.root_desc = "a fast webinterface for the git dscm";
ctx->cfg.script_name = CGIT_SCRIPT_NAME;
ctx->cfg.summary_branches = 10;
ctx->cfg.summary_log = 10;
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;
}
struct refmatch {
char *req_ref;
char *first_ref;
int match;
};
int find_current_ref(const char *refname, const unsigned char *sha1,
int flags, void *cb_data)
diff --git a/cgit.h b/cgit.h
index a1fa841..1615616 100644
--- a/cgit.h
+++ b/cgit.h
@@ -157,24 +157,25 @@ struct cgit_config {
int max_repodesc_len;
int nocache;
int renamelimit;
int snapshots;
int summary_branches;
int summary_log;
int summary_tags;
};
struct cgit_page {
time_t modified;
time_t expires;
+ size_t size;
char *mimetype;
char *charset;
char *filename;
char *title;
};
struct cgit_context {
struct cgit_query qry;
struct cgit_config cfg;
struct cgit_repo *repo;
struct cgit_page page;
};
diff --git a/cmd.c b/cmd.c
index 40ac53e..a989220 100644
--- a/cmd.c
+++ b/cmd.c
@@ -8,24 +8,25 @@
#include "cgit.h"
#include "cmd.h"
#include "cache.h"
#include "ui-shared.h"
#include "ui-atom.h"
#include "ui-blob.h"
#include "ui-clone.h"
#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"
#include "ui-summary.h"
#include "ui-tag.h"
#include "ui-tree.h"
static void HEAD_fn(struct cgit_context *ctx)
{
cgit_clone_head(ctx);
}
@@ -82,24 +83,29 @@ static void objects_fn(struct cgit_context *ctx)
}
static void repolist_fn(struct cgit_context *ctx)
{
cgit_print_repolist();
}
static void patch_fn(struct cgit_context *ctx)
{
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();
}
static void snapshot_fn(struct cgit_context *ctx)
{
cgit_print_snapshot(ctx->qry.head, ctx->qry.sha1,
cgit_repobasename(ctx->repo->url), ctx->qry.path,
ctx->repo->snapshots);
}
@@ -126,24 +132,25 @@ struct cgit_cmd *cgit_get_cmd(struct cgit_context *ctx)
static struct cgit_cmd cmds[] = {
def_cmd(HEAD, 1, 0),
def_cmd(atom, 1, 0),
def_cmd(about, 0, 1),
def_cmd(blob, 1, 0),
def_cmd(commit, 1, 1),
def_cmd(diff, 1, 1),
def_cmd(info, 1, 0),
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),
def_cmd(summary, 1, 1),
def_cmd(tag, 1, 1),
def_cmd(tree, 1, 1),
};
int i;
if (ctx->qry.page == NULL) {
if (ctx->repo)
ctx->qry.page = "summary";
diff --git a/html.c b/html.c
index 1237076..36e9a2f 100644
--- a/html.c
+++ b/html.c
@@ -26,43 +26,48 @@ char *fmt(const char *format, ...)
bufidx &= 7;
va_start(args, format);
len = vsnprintf(buf[bufidx], sizeof(buf[bufidx]), format, args);
va_end(args);
if (len>sizeof(buf[bufidx])) {
fprintf(stderr, "[html.c] string truncated: %s\n", format);
exit(1);
}
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));
}
void htmlf(const char *format, ...)
{
static char buf[65536];
va_list args;
va_start(args, format);
vsnprintf(buf, sizeof(buf), format, args);
va_end(args);
html(buf);
}
-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)
html("\n");
}
void html_txt(char *txt)
{
char *t = txt;
while(t && *t){
int c = *t;
if (c=='<' || c=='>' || c=='&') {
write(htmlfd, txt, t - txt);
if (c=='>')
diff --git a/html.h b/html.h
index 2bde28d..3c32935 100644
--- a/html.h
+++ b/html.h
@@ -1,20 +1,21 @@
#ifndef HTML_H
#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);
+extern void html_status(int code, const char *msg, int more_headers);
extern void html_txt(char *txt);
extern void html_ntxt(int len, char *txt);
extern void html_attr(char *txt);
extern void html_hidden(char *name, char *value);
extern void html_option(char *value, char *text, char *selected_value);
extern void html_link_open(char *url, char *title, char *class);
extern void html_link_close(void);
extern void html_fileperm(unsigned short mode);
extern int html_include(const char *filename);
extern int http_parse_querystring(char *txt, void (*fn)(const char *name, const char *value));
diff --git a/ui-clone.c b/ui-clone.c
index 3a037ad..81e7a4e 100644
--- a/ui-clone.c
+++ b/ui-clone.c
@@ -39,63 +39,61 @@ static void print_pack_info(struct cgit_context *ctx)
ctx->page.filename = "objects/info/packs";
cgit_print_http_headers(ctx);
ofs = strlen(ctx->repo->path) + strlen("/objects/pack/");
prepare_packed_git();
for (pack = packed_git; pack; pack = pack->next)
if (pack->pack_local)
htmlf("P %s\n", pack->pack_name + ofs);
}
static void send_file(struct cgit_context *ctx, char *path)
{
struct stat st;
- int err;
if (stat(path, &st)) {
switch (errno) {
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;
}
ctx->page.mimetype = "application/octet-stream";
ctx->page.filename = path;
if (prefixcmp(ctx->repo->path, path))
ctx->page.filename += strlen(ctx->repo->path) + 1;
cgit_print_http_headers(ctx);
html_include(path);
}
void cgit_clone_info(struct cgit_context *ctx)
{
if (!ctx->qry.path || strcmp(ctx->qry.path, "refs"))
return;
ctx->page.mimetype = "text/plain";
ctx->page.filename = "info/refs";
cgit_print_http_headers(ctx);
for_each_ref(print_ref_info, ctx);
}
void cgit_clone_objects(struct cgit_context *ctx)
{
if (!ctx->qry.path) {
- html_status(400, 0);
+ html_status(400, "Bad request", 0);
return;
}
if (!strcmp(ctx->qry.path, "info/packs")) {
print_pack_info(ctx);
return;
}
send_file(ctx, git_path("objects/%s", ctx->qry.path));
}
void cgit_clone_head(struct cgit_context *ctx)
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
@@ -249,24 +249,30 @@ static void reporevlink(char *page, char *name, char *title, char *class,
}
html("'>");
html_txt(name);
html("</a>");
}
void cgit_tree_link(char *name, char *title, char *class, char *head,
char *rev, char *path)
{
reporevlink("tree", name, title, class, head, rev, path);
}
+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,
char *rev, char *path, int ofs, char *grep, char *pattern)
{
char *delim;
delim = repolink(title, class, "log", head, path);
if (rev && strcmp(rev, ctx.qry.head)) {
html(delim);
html("id=");
html_attr(rev);
delim = "&";
}
@@ -424,24 +430,26 @@ void cgit_print_age(time_t t, time_t max_relative, char *format)
}
htmlf("<span class='age-years'>%.0f years</span>",
secs * 1.0 / TM_YEAR);
}
void cgit_print_http_headers(struct cgit_context *ctx)
{
if (ctx->page.mimetype && ctx->page.charset)
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);
htmlf("Last-Modified: %s\n", http_date(ctx->page.modified));
htmlf("Expires: %s\n", http_date(ctx->page.expires));
html("\n");
}
void cgit_print_docstart(struct cgit_context *ctx)
{
char *host = cgit_hosturl();
html(cgit_doctype);
diff --git a/ui-shared.h b/ui-shared.h
index f4123d3..747f092 100644
--- a/ui-shared.h
+++ b/ui-shared.h
@@ -3,24 +3,26 @@
extern char *cgit_hosturl();
extern char *cgit_repourl(const char *reponame);
extern char *cgit_fileurl(const char *reponame, const char *pagename,
const char *filename, const char *query);
extern char *cgit_pageurl(const char *reponame, const char *pagename,
const char *query);
extern void cgit_index_link(char *name, char *title, char *class,
char *pattern, int ofs);
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,
char *rev, char *path, int ofs, char *grep,
char *pattern);
extern void cgit_commit_link(char *name, char *title, char *class, char *head,
char *rev);
extern void cgit_patch_link(char *name, char *title, char *class, char *head,
char *rev);
extern void cgit_refs_link(char *name, char *title, char *class, char *head,
char *rev, char *path);
extern void cgit_snapshot_link(char *name, char *title, char *class,
char *head, char *rev, char *archivename);
extern void cgit_diff_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
@@ -26,28 +26,28 @@ static void print_object(const unsigned char *sha1, char *path)
cgit_print_error(fmt("Bad object name: %s",
sha1_to_hex(sha1)));
return;
}
buf = read_sha1_file(sha1, &type, &size);
if (!buf) {
cgit_print_error(fmt("Error reading object %s",
sha1_to_hex(sha1)));
return;
}
- 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));
html("<table summary='blob content' class='blob'>\n");
idx = 0;
start = 0;
lineno = 0;
while(idx < size) {
if (buf[idx] == '\n') {
buf[idx] = '\0';
htmlf(linefmt, ++lineno);
html_txt(buf + start);
html("</td></tr>\n");
start = idx + 1;