-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | cgit.c | 39 | ||||
-rw-r--r-- | cgit.h | 10 | ||||
-rw-r--r-- | git.h | 27 | ||||
-rw-r--r-- | shared.c | 17 | ||||
-rw-r--r-- | ui-commit.c | 7 | ||||
-rw-r--r-- | ui-shared.c | 11 | ||||
-rw-r--r-- | ui-snapshot.c | 47 |
8 files changed, 153 insertions, 8 deletions
@@ -8,3 +8,4 @@ EXTLIBS = $(gitsrc)/libgit.a $(gitsrc)/xdiff/lib.a -lz -lcrypto | |||
8 | OBJECTS = shared.o cache.o parsing.o html.o ui-shared.o ui-repolist.o \ | 8 | OBJECTS = shared.o cache.o parsing.o html.o ui-shared.o ui-repolist.o \ |
9 | ui-summary.o ui-log.o ui-view.c ui-tree.c ui-commit.c ui-diff.o | 9 | ui-summary.o ui-log.o ui-view.c ui-tree.c ui-commit.c ui-diff.o \ |
10 | ui-snapshot.o | ||
10 | 11 | ||
@@ -80,2 +80,9 @@ static void cgit_print_repo_page(struct cacheitem *item) | |||
80 | setenv("GIT_DIR", cgit_repo->path, 1); | 80 | setenv("GIT_DIR", cgit_repo->path, 1); |
81 | |||
82 | if (cgit_query_page && !strcmp(cgit_query_page, "snapshot")) { | ||
83 | cgit_print_snapshot(item, cgit_query_sha1, "zip", | ||
84 | cgit_repo->url, cgit_query_name); | ||
85 | return; | ||
86 | } | ||
87 | |||
81 | if (cgit_query_page && !strcmp(cgit_query_page, "log")) | 88 | if (cgit_query_page && !strcmp(cgit_query_page, "log")) |
@@ -87,3 +94,4 @@ static void cgit_print_repo_page(struct cacheitem *item) | |||
87 | } else if (!strcmp(cgit_query_page, "log")) { | 94 | } else if (!strcmp(cgit_query_page, "log")) { |
88 | cgit_print_log(cgit_query_head, cgit_query_ofs, 100, cgit_query_search); | 95 | cgit_print_log(cgit_query_head, cgit_query_ofs, 100, |
96 | cgit_query_search); | ||
89 | } else if (!strcmp(cgit_query_page, "tree")) { | 97 | } else if (!strcmp(cgit_query_page, "tree")) { |
@@ -96,2 +104,4 @@ static void cgit_print_repo_page(struct cacheitem *item) | |||
96 | cgit_print_diff(cgit_query_sha1, cgit_query_sha2); | 104 | cgit_print_diff(cgit_query_sha1, cgit_query_sha2); |
105 | } else { | ||
106 | cgit_print_error("Invalid request"); | ||
97 | } | 107 | } |
@@ -100,9 +110,17 @@ static void cgit_print_repo_page(struct cacheitem *item) | |||
100 | 110 | ||
101 | static void cgit_fill_cache(struct cacheitem *item) | 111 | static void cgit_fill_cache(struct cacheitem *item, int use_cache) |
102 | { | 112 | { |
103 | static char buf[PATH_MAX]; | 113 | static char buf[PATH_MAX]; |
114 | int stdout2; | ||
104 | 115 | ||
105 | getcwd(buf, sizeof(buf)); | 116 | getcwd(buf, sizeof(buf)); |
106 | htmlfd = item->fd; | ||
107 | item->st.st_mtime = time(NULL); | 117 | item->st.st_mtime = time(NULL); |
118 | |||
119 | if (use_cache) { | ||
120 | stdout2 = chk_positive(dup(STDOUT_FILENO), | ||
121 | "Preserving STDOUT"); | ||
122 | chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); | ||
123 | chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); | ||
124 | } | ||
125 | |||
108 | if (cgit_query_repo) | 126 | if (cgit_query_repo) |
@@ -111,2 +129,10 @@ static void cgit_fill_cache(struct cacheitem *item) | |||
111 | cgit_print_repolist(item); | 129 | cgit_print_repolist(item); |
130 | |||
131 | if (use_cache) { | ||
132 | chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); | ||
133 | chk_positive(dup2(stdout2, STDOUT_FILENO), | ||
134 | "Restoring original STDOUT"); | ||
135 | chk_zero(close(stdout2), "Closing temporary STDOUT"); | ||
136 | } | ||
137 | |||
112 | chdir(buf); | 138 | chdir(buf); |
@@ -129,3 +155,3 @@ static void cgit_check_cache(struct cacheitem *item) | |||
129 | if (!cache_exist(item)) { | 155 | if (!cache_exist(item)) { |
130 | cgit_fill_cache(item); | 156 | cgit_fill_cache(item, 1); |
131 | cache_unlock(item); | 157 | cache_unlock(item); |
@@ -136,3 +162,3 @@ static void cgit_check_cache(struct cacheitem *item) | |||
136 | if (cache_expired(item)) { | 162 | if (cache_expired(item)) { |
137 | cgit_fill_cache(item); | 163 | cgit_fill_cache(item, 1); |
138 | cache_unlock(item); | 164 | cache_unlock(item); |
@@ -211,4 +237,3 @@ int main(int argc, const char **argv) | |||
211 | if (cgit_nocache) { | 237 | if (cgit_nocache) { |
212 | item.fd = STDOUT_FILENO; | 238 | cgit_fill_cache(&item, 0); |
213 | cgit_fill_cache(&item); | ||
214 | } else { | 239 | } else { |
@@ -87,2 +87,3 @@ extern char *cgit_query_sha2; | |||
87 | extern char *cgit_query_path; | 87 | extern char *cgit_query_path; |
88 | extern char *cgit_query_name; | ||
88 | extern int cgit_query_ofs; | 89 | extern int cgit_query_ofs; |
@@ -95,2 +96,5 @@ extern void cgit_querystring_cb(const char *name, const char *value); | |||
95 | 96 | ||
97 | extern int chk_zero(int result, char *msg); | ||
98 | extern int chk_positive(int result, char *msg); | ||
99 | |||
96 | extern int hextoint(char c); | 100 | extern int hextoint(char c); |
@@ -132,2 +136,5 @@ extern void cgit_print_docend(); | |||
132 | extern void cgit_print_pageheader(char *title, int show_search); | 136 | extern void cgit_print_pageheader(char *title, int show_search); |
137 | extern void cgit_print_snapshot_start(const char *mimetype, | ||
138 | const char *filename, | ||
139 | struct cacheitem *item); | ||
133 | 140 | ||
@@ -140,2 +147,5 @@ extern void cgit_print_commit(const char *hex); | |||
140 | extern void cgit_print_diff(const char *old_hex, const char *new_hex); | 147 | extern void cgit_print_diff(const char *old_hex, const char *new_hex); |
148 | extern void cgit_print_snapshot(struct cacheitem *item, const char *hex, | ||
149 | const char *format, const char *prefix, | ||
150 | const char *filename); | ||
141 | 151 | ||
@@ -671,2 +671,29 @@ int log_tree_commit(struct rev_info *, struct commit *); | |||
671 | 671 | ||
672 | /* from git:archive.h */ | ||
673 | |||
674 | struct archiver_args { | ||
675 | const char *base; | ||
676 | struct tree *tree; | ||
677 | const unsigned char *commit_sha1; | ||
678 | time_t time; | ||
679 | const char **pathspec; | ||
680 | unsigned int verbose : 1; | ||
681 | void *extra; | ||
682 | }; | ||
683 | |||
684 | typedef int (*write_archive_fn_t)(struct archiver_args *); | ||
685 | |||
686 | typedef void *(*parse_extra_args_fn_t)(int argc, const char **argv); | ||
687 | |||
688 | struct archiver { | ||
689 | const char *name; | ||
690 | struct archiver_args args; | ||
691 | write_archive_fn_t write_archive; | ||
692 | parse_extra_args_fn_t parse_extra; | ||
693 | }; | ||
694 | |||
695 | extern int write_tar_archive(struct archiver_args *); | ||
696 | extern int write_zip_archive(struct archiver_args *); | ||
697 | extern void *parse_extra_zip_args(int argc, const char **argv); | ||
698 | |||
672 | #endif /* GIT_H */ | 699 | #endif /* GIT_H */ |
@@ -46,2 +46,3 @@ char *cgit_query_sha2 = NULL; | |||
46 | char *cgit_query_path = NULL; | 46 | char *cgit_query_path = NULL; |
47 | char *cgit_query_name = NULL; | ||
47 | int cgit_query_ofs = 0; | 48 | int cgit_query_ofs = 0; |
@@ -50,2 +51,16 @@ int htmlfd = 0; | |||
50 | 51 | ||
52 | int chk_zero(int result, char *msg) | ||
53 | { | ||
54 | if (result != 0) | ||
55 | die("%s: %s", msg, strerror(errno)); | ||
56 | return result; | ||
57 | } | ||
58 | |||
59 | int chk_positive(int result, char *msg) | ||
60 | { | ||
61 | if (result <= 0) | ||
62 | die("%s: %s", msg, strerror(errno)); | ||
63 | return result; | ||
64 | } | ||
65 | |||
51 | struct repoinfo *add_repo(const char *url) | 66 | struct repoinfo *add_repo(const char *url) |
@@ -142,2 +157,4 @@ void cgit_querystring_cb(const char *name, const char *value) | |||
142 | cgit_query_path = xstrdup(value); | 157 | cgit_query_path = xstrdup(value); |
158 | } else if (!strcmp(name, "name")) { | ||
159 | cgit_query_name = xstrdup(value); | ||
143 | } | 160 | } |
diff --git a/ui-commit.c b/ui-commit.c index 73fa104..de3f2cf 100644 --- a/ui-commit.c +++ b/ui-commit.c | |||
@@ -130,2 +130,3 @@ void cgit_print_commit(const char *hex) | |||
130 | char *query; | 130 | char *query; |
131 | char *filename; | ||
131 | 132 | ||
@@ -170,2 +171,8 @@ void cgit_print_commit(const char *hex) | |||
170 | } | 171 | } |
172 | htmlf("<tr><th>download</th><td colspan='2' class='sha1'><a href='"); | ||
173 | filename = fmt("%s-%s.zip", cgit_query_repo, hex); | ||
174 | html_attr(cgit_pageurl(cgit_query_repo, "snapshot", | ||
175 | fmt("id=%s&name=%s", hex, filename))); | ||
176 | htmlf("'>%s</a></td></tr>", filename); | ||
177 | |||
171 | html("</table>\n"); | 178 | html("</table>\n"); |
diff --git a/ui-shared.c b/ui-shared.c index 3322561..172499c 100644 --- a/ui-shared.c +++ b/ui-shared.c | |||
@@ -146 +146,12 @@ void cgit_print_pageheader(char *title, int show_search) | |||
146 | } | 146 | } |
147 | |||
148 | void cgit_print_snapshot_start(const char *mimetype, const char *filename, | ||
149 | struct cacheitem *item) | ||
150 | { | ||
151 | htmlf("Content-Type: %s\n", mimetype); | ||
152 | htmlf("Content-Disposition: inline; filename=\"%s\"\n", filename); | ||
153 | htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime)); | ||
154 | htmlf("Expires: %s\n", http_date(item->st.st_mtime + | ||
155 | ttl_seconds(item->ttl))); | ||
156 | html("\n"); | ||
157 | } | ||
diff --git a/ui-snapshot.c b/ui-snapshot.c new file mode 100644 index 0000000..2257d6b --- a/dev/null +++ b/ui-snapshot.c | |||
@@ -0,0 +1,47 @@ | |||
1 | /* ui-snapshot.c: generate snapshot of a commit | ||
2 | * | ||
3 | * Copyright (C) 2006 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 | |||
11 | static void cgit_print_zip(struct cacheitem *item, const char *hex, | ||
12 | const char *prefix, const char *filename) | ||
13 | { | ||
14 | struct archiver_args args; | ||
15 | struct commit *commit; | ||
16 | unsigned char sha1[20]; | ||
17 | |||
18 | if (get_sha1(hex, sha1)) { | ||
19 | cgit_print_error(fmt("Bad object id: %s", hex)); | ||
20 | return; | ||
21 | } | ||
22 | commit = lookup_commit_reference(sha1); | ||
23 | |||
24 | if (!commit) { | ||
25 | cgit_print_error(fmt("Not a commit reference: %s", hex)); | ||
26 | return; | ||
27 | } | ||
28 | |||
29 | memset(&args, 0, sizeof(args)); | ||
30 | args.base = fmt("%s/", prefix); | ||
31 | args.tree = commit->tree; | ||
32 | |||
33 | cgit_print_snapshot_start("application/x-zip", filename, item); | ||
34 | write_zip_archive(&args); | ||
35 | } | ||
36 | |||
37 | |||
38 | void cgit_print_snapshot(struct cacheitem *item, const char *hex, | ||
39 | const char *format, const char *prefix, | ||
40 | const char *filename) | ||
41 | { | ||
42 | if (!strcmp(format, "zip")) | ||
43 | cgit_print_zip(item, hex, prefix, filename); | ||
44 | else | ||
45 | cgit_print_error(fmt("Unsupported snapshot format: %s", | ||
46 | format)); | ||
47 | } | ||