aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Lars Hjemli <hjemli@gmail.com>2006-12-09 23:18:17 (JST)
committerGravatar Lars Hjemli <hjemli@gmail.com>2006-12-09 23:18:17 (JST)
commit0d169ada2ba81210ab1191a5f2212662e90db77e (patch)
tree402b54583db269323ebb28e5fbf2075c4c1b3d85
downloadcgit-0d169ada2ba81210ab1191a5f2212662e90db77e.zip
cgit-0d169ada2ba81210ab1191a5f2212662e90db77e.tar.gz
Import cgit prototype from git tree
This enables basic cgit functionality, using libgit.a and xdiff/lib.a from git + a custom "git.h" + openssl for sha1 routines. Signed-off-by: Lars Hjemli <hjemli@gmail.com>
-rw-r--r--Makefile24
-rw-r--r--cgit.c410
-rw-r--r--cgit.css63
-rw-r--r--cgit.h21
-rw-r--r--config.c73
-rw-r--r--git.h399
-rw-r--r--html.c100
7 files changed, 1090 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..1470c0a
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,24 @@
1
2INSTALL_BIN = /var/www/htdocs/cgit.cgi
3INSTALL_CSS = /var/www/htdocs/cgit.css
4
5EXTLIBS = ../git/libgit.a ../git/xdiff/lib.a -lz -lcrypto
6OBJECTS = cgit.o config.o html.o
7
8all: cgit
9
10install: all
11 install cgit $(INSTALL_BIN)
12 install cgit.css $(INSTALL_CSS)
13
14clean:
15 rm -f cgit *.o
16
17cgit: $(OBJECTS)
18 $(CC) -o cgit $(OBJECTS) $(EXTLIBS)
19
20cgit.o: cgit.h git.h config.o html.o
21
22config.o: cgit.h git.h html.c
23
24html.o: cgit.h git.h html.c
diff --git a/cgit.c b/cgit.c
new file mode 100644
index 0000000..4c14f77
--- /dev/null
+++ b/cgit.c
@@ -0,0 +1,410 @@
1#include "cgit.h"
2
3static const char cgit_doctype[] =
4"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
5" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
6
7static const char cgit_error[] =
8"<div class='error'>%s</div>";
9
10static const char cgit_lib_error[] =
11"<div class='error'>%s: %s</div>";
12
13
14char *cgit_root = "/var/git";
15char *cgit_root_title = "Git repository browser";
16char *cgit_css = "/cgit.css";
17char *cgit_logo = "/git-logo.png";
18char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/";
19char *cgit_virtual_root = NULL;
20
21char *cgit_repo_name = NULL;
22char *cgit_repo_desc = NULL;
23char *cgit_repo_owner = NULL;
24
25char *cgit_query_repo = NULL;
26char *cgit_query_page = NULL;
27char *cgit_query_head = NULL;
28
29int cgit_parse_query(char *txt, configfn fn)
30{
31 char *t = txt, *value = NULL, c;
32
33 if (!txt)
34 return 0;
35
36 while((c=*t) != '\0') {
37 if (c=='=') {
38 *t = '\0';
39 value = t+1;
40 } else if (c=='&') {
41 *t = '\0';
42 (*fn)(txt, value);
43 txt = t+1;
44 value = NULL;
45 }
46 t++;
47 }
48 if (t!=txt)
49 (*fn)(txt, value);
50 return 0;
51}
52
53void cgit_global_config_cb(const char *name, const char *value)
54{
55 if (!strcmp(name, "root"))
56 cgit_root = xstrdup(value);
57 else if (!strcmp(name, "root-title"))
58 cgit_root_title = xstrdup(value);
59 else if (!strcmp(name, "css"))
60 cgit_css = xstrdup(value);
61 else if (!strcmp(name, "logo"))
62 cgit_logo = xstrdup(value);
63 else if (!strcmp(name, "logo-link"))
64 cgit_logo_link = xstrdup(value);
65 else if (!strcmp(name, "virtual-root"))
66 cgit_virtual_root = xstrdup(value);
67}
68
69void cgit_repo_config_cb(const char *name, const char *value)
70{
71 if (!strcmp(name, "name"))
72 cgit_repo_name = xstrdup(value);
73 else if (!strcmp(name, "desc"))
74 cgit_repo_desc = xstrdup(value);
75 else if (!strcmp(name, "owner"))
76 cgit_repo_owner = xstrdup(value);
77}
78
79void cgit_querystring_cb(const char *name, const char *value)
80{
81 if (!strcmp(name,"r"))
82 cgit_query_repo = xstrdup(value);
83 else if (!strcmp(name, "p"))
84 cgit_query_page = xstrdup(value);
85 else if (!strcmp(name, "h"))
86 cgit_query_head = xstrdup(value);
87}
88
89char *cgit_repourl(const char *reponame)
90{
91 if (cgit_virtual_root) {
92 return fmt("%s/%s/", cgit_virtual_root, reponame);
93 } else {
94 return fmt("?r=%s", reponame);
95 }
96}
97
98char *cgit_pageurl(const char *reponame, const char *pagename,
99 const char *query)
100{
101 if (cgit_virtual_root) {
102 return fmt("%s/%s/%s/?%s", cgit_virtual_root, reponame,
103 pagename, query);
104 } else {
105 return fmt("?r=%s&p=%s&%s", reponame, pagename, query);
106 }
107}
108
109static int cgit_print_branch_cb(const char *refname, const unsigned char *sha1,
110 int flags, void *cb_data)
111{
112 struct commit *commit;
113 char buf[256], *url;
114
115 commit = lookup_commit(sha1);
116 if (commit && !parse_commit(commit)){
117 html("<tr><td>");
118 url = cgit_pageurl(cgit_query_repo, "log",
119 fmt("h=%s", refname));
120 html_link_open(url, NULL, NULL);
121 strncpy(buf, refname, sizeof(buf));
122 html_txt(buf);
123 html_link_close();
124 html("</td><td>");
125 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0, buf,
126 sizeof(buf), 0, NULL, NULL, 0);
127 html_txt(buf);
128 html("</td></tr>\n");
129 } else {
130 html("<tr><td>");
131 html_txt(buf);
132 html("</td><td>");
133 htmlf("*** bad ref %s", sha1_to_hex(sha1));
134 html("</td></tr>\n");
135 }
136 return 0;
137}
138
139static void cgit_print_docstart(char *title)
140{
141 html("Content-Type: text/html; charset=utf-8\n");
142 html("\n");
143 html(cgit_doctype);