blob: 8f82119234b6bab42db19b6b8409c69504fd97ad [file] [log] [blame]
Lars Hjemliab2ab952007-02-08 13:53:13 +01001/* ui-snapshot.c: generate snapshot of a commit
2 *
Lukas Fleischerf7f26f82014-01-08 15:10:49 +01003 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
Lars Hjemliab2ab952007-02-08 13:53:13 +01004 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9#include "cgit.h"
John Keeping8f208792013-04-06 11:37:59 +010010#include "ui-snapshot.h"
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010011#include "html.h"
Lars Hjemlia4d1ca12008-03-24 16:50:57 +010012#include "ui-shared.h"
Lars Hjemliab2ab952007-02-08 13:53:13 +010013
John Keeping2ab1cd92013-03-02 12:32:12 +000014static int write_archive_type(const char *format, const char *hex, const char *prefix)
15{
16 struct argv_array argv = ARGV_ARRAY_INIT;
John Keepingfb3655d2013-04-06 10:28:57 +010017 const char **nargv;
18 int result;
Jason A. Donenfeld973deda2013-03-03 23:41:53 -050019 argv_array_push(&argv, "snapshot");
John Keeping2ab1cd92013-03-02 12:32:12 +000020 argv_array_push(&argv, format);
21 if (prefix) {
John Keepingfb3655d2013-04-06 10:28:57 +010022 struct strbuf buf = STRBUF_INIT;
23 strbuf_addstr(&buf, prefix);
24 strbuf_addch(&buf, '/');
John Keeping2ab1cd92013-03-02 12:32:12 +000025 argv_array_push(&argv, "--prefix");
John Keepingfb3655d2013-04-06 10:28:57 +010026 argv_array_push(&argv, buf.buf);
27 strbuf_release(&buf);
John Keeping2ab1cd92013-03-02 12:32:12 +000028 }
29 argv_array_push(&argv, hex);
John Keepingfb3655d2013-04-06 10:28:57 +010030 /*
31 * Now we need to copy the pointers to arguments into a new
32 * structure because write_archive will rearrange its arguments
33 * which may result in duplicated/missing entries causing leaks
34 * or double-frees in argv_array_clear.
35 */
36 nargv = xmalloc(sizeof(char *) * (argv.argc + 1));
37 /* argv_array guarantees a trailing NULL entry. */
38 memcpy(nargv, argv.argv, sizeof(char *) * (argv.argc + 1));
39
40 result = write_archive(argv.argc, nargv, NULL, 1, NULL, 0);
41 argv_array_clear(&argv);
42 free(nargv);
43 return result;
John Keeping2ab1cd92013-03-02 12:32:12 +000044}
45
46static int write_tar_archive(const char *hex, const char *prefix)
47{
48 return write_archive_type("--format=tar", hex, prefix);
49}
50
51static int write_zip_archive(const char *hex, const char *prefix)
52{
53 return write_archive_type("--format=zip", hex, prefix);
54}
55
56static int write_compressed_tar_archive(const char *hex,
57 const char *prefix,
58 char *filter_argv[])
Michael Krelin18a99bd2007-07-21 02:05:34 +020059{
Michael Krelin18a99bd2007-07-21 02:05:34 +020060 int rv;
Lars Hjemli18dfbdc2009-07-31 15:52:57 +020061 struct cgit_filter f;
Michael Krelin18a99bd2007-07-21 02:05:34 +020062
Jason A. Donenfeld055e0922012-09-26 02:56:38 +020063 f.cmd = filter_argv[0];
64 f.argv = filter_argv;
Lars Hjemli3ec6b302011-06-06 19:29:58 +000065 cgit_open_filter(&f);
John Keeping2ab1cd92013-03-02 12:32:12 +000066 rv = write_tar_archive(hex, prefix);
Lars Hjemli18dfbdc2009-07-31 15:52:57 +020067 cgit_close_filter(&f);
Michael Krelin18a99bd2007-07-21 02:05:34 +020068 return rv;
69}
70
John Keeping2ab1cd92013-03-02 12:32:12 +000071static int write_tar_gzip_archive(const char *hex, const char *prefix)
Michael Krelin4a92cbb2007-07-20 20:58:23 +020072{
Jason A. Donenfeld055e0922012-09-26 02:56:38 +020073 char *argv[] = { "gzip", "-n", NULL };
John Keeping2ab1cd92013-03-02 12:32:12 +000074 return write_compressed_tar_archive(hex, prefix, argv);
Michael Krelin18a99bd2007-07-21 02:05:34 +020075}
Lars Hjemli1221adb2007-07-23 22:51:45 +020076
John Keeping2ab1cd92013-03-02 12:32:12 +000077static int write_tar_bzip2_archive(const char *hex, const char *prefix)
Michael Krelin18a99bd2007-07-21 02:05:34 +020078{
Jason A. Donenfeld055e0922012-09-26 02:56:38 +020079 char *argv[] = { "bzip2", NULL };
John Keeping2ab1cd92013-03-02 12:32:12 +000080 return write_compressed_tar_archive(hex, prefix, argv);
Michael Krelin4a92cbb2007-07-20 20:58:23 +020081}
82
John Keeping2ab1cd92013-03-02 12:32:12 +000083static int write_tar_xz_archive(const char *hex, const char *prefix)
Andreas Wiese06424352009-12-08 22:18:11 +010084{
Jason A. Donenfeld055e0922012-09-26 02:56:38 +020085 char *argv[] = { "xz", NULL };
John Keeping2ab1cd92013-03-02 12:32:12 +000086 return write_compressed_tar_archive(hex, prefix, argv);
Andreas Wiese06424352009-12-08 22:18:11 +010087}
88
Lars Hjemlif34478c2008-03-24 16:00:27 +010089const struct cgit_snapshot_format cgit_snapshot_formats[] = {
Andreas Wiese06424352009-12-08 22:18:11 +010090 { ".zip", "application/x-zip", write_zip_archive, 0x01 },
91 { ".tar.gz", "application/x-gzip", write_tar_gzip_archive, 0x02 },
92 { ".tar.bz2", "application/x-bzip2", write_tar_bzip2_archive, 0x04 },
93 { ".tar", "application/x-tar", write_tar_archive, 0x08 },
94 { ".tar.xz", "application/x-xz", write_tar_xz_archive, 0x10 },
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -050095 { NULL }
Michael Krelinf97c7072007-07-18 14:40:03 +020096};
Lars Hjemliab2ab952007-02-08 13:53:13 +010097
Lars Hjemlied7ff092008-10-11 20:09:42 +020098static const struct cgit_snapshot_format *get_format(const char *filename)
99{
100 const struct cgit_snapshot_format *fmt;
Lars Hjemlied7ff092008-10-11 20:09:42 +0200101
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500102 for (fmt = cgit_snapshot_formats; fmt->suffix; fmt++) {
Lukas Fleischere7116792014-01-10 12:44:38 +0100103 if (!suffixcmp(filename, fmt->suffix))
Lars Hjemlied7ff092008-10-11 20:09:42 +0200104 return fmt;
105 }
106 return NULL;
107}
108
Lars Hjemlif34478c2008-03-24 16:00:27 +0100109static int make_snapshot(const struct cgit_snapshot_format *format,
110 const char *hex, const char *prefix,
111 const char *filename)
112{
Lars Hjemlif34478c2008-03-24 16:00:27 +0100113 unsigned char sha1[20];
114
John Keeping2ab1cd92013-03-02 12:32:12 +0000115 if (get_sha1(hex, sha1)) {
John Keepinged5bd302013-04-06 11:23:52 +0100116 cgit_print_error("Bad object id: %s", hex);
Lars Hjemlif34478c2008-03-24 16:00:27 +0100117 return 1;
118 }
John Keeping2ab1cd92013-03-02 12:32:12 +0000119 if (!lookup_commit_reference(sha1)) {
John Keepinged5bd302013-04-06 11:23:52 +0100120 cgit_print_error("Not a commit reference: %s", hex);
Lars Hjemlif34478c2008-03-24 16:00:27 +0100121 return 1;
122 }
Lars Hjemlif34478c2008-03-24 16:00:27 +0100123 ctx.page.mimetype = xstrdup(format->mimetype);
124 ctx.page.filename = xstrdup(filename);
125 cgit_print_http_headers(&ctx);
John Keeping2ab1cd92013-03-02 12:32:12 +0000126 format->write_func(hex, prefix);
Lars Hjemlif34478c2008-03-24 16:00:27 +0100127 return 0;
128}
Lars Hjemli1221adb2007-07-23 22:51:45 +0200129
Lars Hjemli4b4f8d12008-12-01 19:13:44 +0100130/* Try to guess the requested revision from the requested snapshot name.
131 * First the format extension is stripped, e.g. "cgit-0.7.2.tar.gz" become
132 * "cgit-0.7.2". If this is a valid commit object name we've got a winner.
133 * Otherwise, if the snapshot name has a prefix matching the result from
134 * repo_basename(), we strip the basename and any following '-' and '_'
135 * characters ("cgit-0.7.2" -> "0.7.2") and check the resulting name once
136 * more. If this still isn't a valid commit object name, we check if pre-
Lukas Fleischer8c4c2c42013-04-10 13:04:03 +0200137 * pending a 'v' or a 'V' to the remaining snapshot name ("0.7.2" ->
138 * "v0.7.2") gives us something valid.
Lars Hjemlied7ff092008-10-11 20:09:42 +0200139 */
140static const char *get_ref_from_filename(const char *url, const char *filename,
Lars Hjemli4b4f8d12008-12-01 19:13:44 +0100141 const struct cgit_snapshot_format *format)
Lars Hjemlied7ff092008-10-11 20:09:42 +0200142{
Lars Hjemli4b4f8d12008-12-01 19:13:44 +0100143 const char *reponame;
144 unsigned char sha1[20];
John Keepingfb3655d2013-04-06 10:28:57 +0100145 struct strbuf snapshot = STRBUF_INIT;
146 int result = 1;
Lars Hjemli4b4f8d12008-12-01 19:13:44 +0100147
John Keepingfb3655d2013-04-06 10:28:57 +0100148 strbuf_addstr(&snapshot, filename);
149 strbuf_setlen(&snapshot, snapshot.len - strlen(format->suffix));
Lars Hjemli4b4f8d12008-12-01 19:13:44 +0100150
John Keepingfb3655d2013-04-06 10:28:57 +0100151 if (get_sha1(snapshot.buf, sha1) == 0)
152 goto out;
Lars Hjemli4b4f8d12008-12-01 19:13:44 +0100153
154 reponame = cgit_repobasename(url);
John Keepingfb3655d2013-04-06 10:28:57 +0100155 if (prefixcmp(snapshot.buf, reponame) == 0) {
156 const char *new_start = snapshot.buf;
157 new_start += strlen(reponame);
158 while (new_start && (*new_start == '-' || *new_start == '_'))
159 new_start++;
160 strbuf_splice(&snapshot, 0, new_start - snapshot.buf, "", 0);
Lars Hjemli4b4f8d12008-12-01 19:13:44 +0100161 }
162
John Keepingfb3655d2013-04-06 10:28:57 +0100163 if (get_sha1(snapshot.buf, sha1) == 0)
164 goto out;
Lars Hjemli4b4f8d12008-12-01 19:13:44 +0100165
John Keepingfb3655d2013-04-06 10:28:57 +0100166 strbuf_insert(&snapshot, 0, "v", 1);
167 if (get_sha1(snapshot.buf, sha1) == 0)
168 goto out;
Lars Hjemli4b4f8d12008-12-01 19:13:44 +0100169
Lukas Fleischer8c4c2c42013-04-10 13:04:03 +0200170 strbuf_splice(&snapshot, 0, 1, "V", 1);
171 if (get_sha1(snapshot.buf, sha1) == 0)
172 goto out;
173
John Keepingfb3655d2013-04-06 10:28:57 +0100174 result = 0;
175 strbuf_release(&snapshot);
176
177out:
178 return result ? strbuf_detach(&snapshot, NULL) : NULL;
Lars Hjemlied7ff092008-10-11 20:09:42 +0200179}
180
John Keepinged5bd302013-04-06 11:23:52 +0100181__attribute__((format (printf, 1, 2)))
182static void show_error(char *fmt, ...)
Lars Hjemli6fddad72009-03-15 08:57:33 +0100183{
John Keepinged5bd302013-04-06 11:23:52 +0100184 va_list ap;
185
Lars Hjemli6fddad72009-03-15 08:57:33 +0100186 ctx.page.mimetype = "text/html";
187 cgit_print_http_headers(&ctx);
188 cgit_print_docstart(&ctx);
189 cgit_print_pageheader(&ctx);
John Keepinged5bd302013-04-06 11:23:52 +0100190 va_start(ap, fmt);
191 cgit_vprint_error(fmt, ap);
192 va_end(ap);
Lars Hjemli6fddad72009-03-15 08:57:33 +0100193 cgit_print_docend();
194}
195
Natanael Copa314d9ea2008-11-29 21:49:07 -0800196void cgit_print_snapshot(const char *head, const char *hex,
Lars Hjemlied7ff092008-10-11 20:09:42 +0200197 const char *filename, int snapshots, int dwim)
Lars Hjemliab2ab952007-02-08 13:53:13 +0100198{
Lars Hjemlif34478c2008-03-24 16:00:27 +0100199 const struct cgit_snapshot_format* f;
Natanael Copa314d9ea2008-11-29 21:49:07 -0800200 char *prefix = NULL;
Lars Hjemlieb453422007-07-23 00:11:15 +0200201
Lars Hjemli6fddad72009-03-15 08:57:33 +0100202 if (!filename) {
203 show_error("No snapshot name specified");
204 return;
205 }
206
Lars Hjemlied7ff092008-10-11 20:09:42 +0200207 f = get_format(filename);
208 if (!f) {
John Keepinged5bd302013-04-06 11:23:52 +0100209 show_error("Unsupported snapshot format: %s", filename);
Michael Krelinf97c7072007-07-18 14:40:03 +0200210 return;
211 }
Lars Hjemlied7ff092008-10-11 20:09:42 +0200212
Natanael Copa314d9ea2008-11-29 21:49:07 -0800213 if (!hex && dwim) {
Lars Hjemlied7ff092008-10-11 20:09:42 +0200214 hex = get_ref_from_filename(ctx.repo->url, filename, f);
Natanael Copac4b45de2008-12-02 11:31:34 +0100215 if (hex == NULL) {
216 html_status(404, "Not found", 0);
217 return;
Natanael Copa314d9ea2008-11-29 21:49:07 -0800218 }
Natanael Copac4b45de2008-12-02 11:31:34 +0100219 prefix = xstrdup(filename);
220 prefix[strlen(filename) - strlen(f->suffix)] = '\0';
Natanael Copa314d9ea2008-11-29 21:49:07 -0800221 }
Lars Hjemlied7ff092008-10-11 20:09:42 +0200222
223 if (!hex)
224 hex = head;
225
Natanael Copa314d9ea2008-11-29 21:49:07 -0800226 if (!prefix)
227 prefix = xstrdup(cgit_repobasename(ctx.repo->url));
228
Lars Hjemlied7ff092008-10-11 20:09:42 +0200229 make_snapshot(f, hex, prefix, filename);
Natanael Copa314d9ea2008-11-29 21:49:07 -0800230 free(prefix);
Michael Krelinf97c7072007-07-18 14:40:03 +0200231}