blob: 54e659c859200ff8d3d70374a11a293859e81745 [file] [log] [blame]
Lars Hjemliab2ab952007-02-08 13:53:13 +01001/* ui-snapshot.c: generate snapshot of a commit
2 *
3 * Copyright (C) 2006 Lars Hjemli
Jason A. Donenfeld055e0922012-09-26 02:56:38 +02004 * Copyright (C) 2012 Jason A. Donenfeld <Jason@zx2c4.com>
Lars Hjemliab2ab952007-02-08 13:53:13 +01005 *
6 * Licensed under GNU General Public License v2
7 * (see COPYING for full license text)
8 */
9
10#include "cgit.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;
Jason A. Donenfeld973deda2013-03-03 23:41:53 -050017 argv_array_push(&argv, "snapshot");
John Keeping2ab1cd92013-03-02 12:32:12 +000018 argv_array_push(&argv, format);
19 if (prefix) {
20 argv_array_push(&argv, "--prefix");
21 argv_array_push(&argv, fmt("%s/", prefix));
22 }
23 argv_array_push(&argv, hex);
24 return write_archive(argv.argc, argv.argv, NULL, 1, NULL, 0);
25}
26
27static int write_tar_archive(const char *hex, const char *prefix)
28{
29 return write_archive_type("--format=tar", hex, prefix);
30}
31
32static int write_zip_archive(const char *hex, const char *prefix)
33{
34 return write_archive_type("--format=zip", hex, prefix);
35}
36
37static int write_compressed_tar_archive(const char *hex,
38 const char *prefix,
39 char *filter_argv[])
Michael Krelin18a99bd2007-07-21 02:05:34 +020040{
Michael Krelin18a99bd2007-07-21 02:05:34 +020041 int rv;
Lars Hjemli18dfbdc2009-07-31 15:52:57 +020042 struct cgit_filter f;
Michael Krelin18a99bd2007-07-21 02:05:34 +020043
Jason A. Donenfeld055e0922012-09-26 02:56:38 +020044 f.cmd = filter_argv[0];
45 f.argv = filter_argv;
Lars Hjemli3ec6b302011-06-06 19:29:58 +000046 cgit_open_filter(&f);
John Keeping2ab1cd92013-03-02 12:32:12 +000047 rv = write_tar_archive(hex, prefix);
Lars Hjemli18dfbdc2009-07-31 15:52:57 +020048 cgit_close_filter(&f);
Michael Krelin18a99bd2007-07-21 02:05:34 +020049 return rv;
50}
51
John Keeping2ab1cd92013-03-02 12:32:12 +000052static int write_tar_gzip_archive(const char *hex, const char *prefix)
Michael Krelin4a92cbb2007-07-20 20:58:23 +020053{
Jason A. Donenfeld055e0922012-09-26 02:56:38 +020054 char *argv[] = { "gzip", "-n", NULL };
John Keeping2ab1cd92013-03-02 12:32:12 +000055 return write_compressed_tar_archive(hex, prefix, argv);
Michael Krelin18a99bd2007-07-21 02:05:34 +020056}
Lars Hjemli1221adb2007-07-23 22:51:45 +020057
John Keeping2ab1cd92013-03-02 12:32:12 +000058static int write_tar_bzip2_archive(const char *hex, const char *prefix)
Michael Krelin18a99bd2007-07-21 02:05:34 +020059{
Jason A. Donenfeld055e0922012-09-26 02:56:38 +020060 char *argv[] = { "bzip2", NULL };
John Keeping2ab1cd92013-03-02 12:32:12 +000061 return write_compressed_tar_archive(hex, prefix, argv);
Michael Krelin4a92cbb2007-07-20 20:58:23 +020062}
63
John Keeping2ab1cd92013-03-02 12:32:12 +000064static int write_tar_xz_archive(const char *hex, const char *prefix)
Andreas Wiese06424352009-12-08 22:18:11 +010065{
Jason A. Donenfeld055e0922012-09-26 02:56:38 +020066 char *argv[] = { "xz", NULL };
John Keeping2ab1cd92013-03-02 12:32:12 +000067 return write_compressed_tar_archive(hex, prefix, argv);
Andreas Wiese06424352009-12-08 22:18:11 +010068}
69
Lars Hjemlif34478c2008-03-24 16:00:27 +010070const struct cgit_snapshot_format cgit_snapshot_formats[] = {
Andreas Wiese06424352009-12-08 22:18:11 +010071 { ".zip", "application/x-zip", write_zip_archive, 0x01 },
72 { ".tar.gz", "application/x-gzip", write_tar_gzip_archive, 0x02 },
73 { ".tar.bz2", "application/x-bzip2", write_tar_bzip2_archive, 0x04 },
74 { ".tar", "application/x-tar", write_tar_archive, 0x08 },
75 { ".tar.xz", "application/x-xz", write_tar_xz_archive, 0x10 },
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -050076 { NULL }
Michael Krelinf97c7072007-07-18 14:40:03 +020077};
Lars Hjemliab2ab952007-02-08 13:53:13 +010078
Lars Hjemlied7ff092008-10-11 20:09:42 +020079static const struct cgit_snapshot_format *get_format(const char *filename)
80{
81 const struct cgit_snapshot_format *fmt;
82 int fl, sl;
83
84 fl = strlen(filename);
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -050085 for (fmt = cgit_snapshot_formats; fmt->suffix; fmt++) {
Lars Hjemlied7ff092008-10-11 20:09:42 +020086 sl = strlen(fmt->suffix);
87 if (sl >= fl)
88 continue;
89 if (!strcmp(fmt->suffix, filename + fl - sl))
90 return fmt;
91 }
92 return NULL;
93}
94
Lars Hjemlif34478c2008-03-24 16:00:27 +010095static int make_snapshot(const struct cgit_snapshot_format *format,
96 const char *hex, const char *prefix,
97 const char *filename)
98{
Lars Hjemlif34478c2008-03-24 16:00:27 +010099 unsigned char sha1[20];
100
John Keeping2ab1cd92013-03-02 12:32:12 +0000101 if (get_sha1(hex, sha1)) {
Lars Hjemlif34478c2008-03-24 16:00:27 +0100102 cgit_print_error(fmt("Bad object id: %s", hex));
103 return 1;
104 }
John Keeping2ab1cd92013-03-02 12:32:12 +0000105 if (!lookup_commit_reference(sha1)) {
Lars Hjemlif34478c2008-03-24 16:00:27 +0100106 cgit_print_error(fmt("Not a commit reference: %s", hex));
107 return 1;
108 }
Lars Hjemlif34478c2008-03-24 16:00:27 +0100109 ctx.page.mimetype = xstrdup(format->mimetype);
110 ctx.page.filename = xstrdup(filename);
111 cgit_print_http_headers(&ctx);
John Keeping2ab1cd92013-03-02 12:32:12 +0000112 format->write_func(hex, prefix);
Lars Hjemlif34478c2008-03-24 16:00:27 +0100113 return 0;
114}
Lars Hjemli1221adb2007-07-23 22:51:45 +0200115
Lars Hjemli4b4f8d12008-12-01 19:13:44 +0100116/* Try to guess the requested revision from the requested snapshot name.
117 * First the format extension is stripped, e.g. "cgit-0.7.2.tar.gz" become
118 * "cgit-0.7.2". If this is a valid commit object name we've got a winner.
119 * Otherwise, if the snapshot name has a prefix matching the result from
120 * repo_basename(), we strip the basename and any following '-' and '_'
121 * characters ("cgit-0.7.2" -> "0.7.2") and check the resulting name once
122 * more. If this still isn't a valid commit object name, we check if pre-
123 * pending a 'v' to the remaining snapshot name ("0.7.2" -> "v0.7.2") gives
124 * us something valid.
Lars Hjemlied7ff092008-10-11 20:09:42 +0200125 */
126static const char *get_ref_from_filename(const char *url, const char *filename,
Lars Hjemli4b4f8d12008-12-01 19:13:44 +0100127 const struct cgit_snapshot_format *format)
Lars Hjemlied7ff092008-10-11 20:09:42 +0200128{
Lars Hjemli4b4f8d12008-12-01 19:13:44 +0100129 const char *reponame;
130 unsigned char sha1[20];
131 char *snapshot;
132
133 snapshot = xstrdup(filename);
134 snapshot[strlen(snapshot) - strlen(format->suffix)] = '\0';
Lars Hjemli4b4f8d12008-12-01 19:13:44 +0100135
136 if (get_sha1(snapshot, sha1) == 0)
137 return snapshot;
138
139 reponame = cgit_repobasename(url);
Lars Hjemli4b4f8d12008-12-01 19:13:44 +0100140 if (prefixcmp(snapshot, reponame) == 0) {
141 snapshot += strlen(reponame);
142 while (snapshot && (*snapshot == '-' || *snapshot == '_'))
143 snapshot++;
144 }
145
146 if (get_sha1(snapshot, sha1) == 0)
147 return snapshot;
148
149 snapshot = fmt("v%s", snapshot);
150 if (get_sha1(snapshot, sha1) == 0)
151 return snapshot;
152
153 return NULL;
Lars Hjemlied7ff092008-10-11 20:09:42 +0200154}
155
Lars Hjemli6fddad72009-03-15 08:57:33 +0100156void show_error(char *msg)
157{
158 ctx.page.mimetype = "text/html";
159 cgit_print_http_headers(&ctx);
160 cgit_print_docstart(&ctx);
161 cgit_print_pageheader(&ctx);
162 cgit_print_error(msg);
163 cgit_print_docend();
164}
165
Natanael Copa314d9ea2008-11-29 21:49:07 -0800166void cgit_print_snapshot(const char *head, const char *hex,
Lars Hjemlied7ff092008-10-11 20:09:42 +0200167 const char *filename, int snapshots, int dwim)
Lars Hjemliab2ab952007-02-08 13:53:13 +0100168{
Lars Hjemlif34478c2008-03-24 16:00:27 +0100169 const struct cgit_snapshot_format* f;
Natanael Copa314d9ea2008-11-29 21:49:07 -0800170 char *prefix = NULL;
Lars Hjemlieb453422007-07-23 00:11:15 +0200171
Lars Hjemli6fddad72009-03-15 08:57:33 +0100172 if (!filename) {
173 show_error("No snapshot name specified");
174 return;
175 }
176
Lars Hjemlied7ff092008-10-11 20:09:42 +0200177 f = get_format(filename);
178 if (!f) {
Lars Hjemli6fddad72009-03-15 08:57:33 +0100179 show_error(xstrdup(fmt("Unsupported snapshot format: %s",
180 filename)));
Michael Krelinf97c7072007-07-18 14:40:03 +0200181 return;
182 }
Lars Hjemlied7ff092008-10-11 20:09:42 +0200183
Natanael Copa314d9ea2008-11-29 21:49:07 -0800184 if (!hex && dwim) {
Lars Hjemlied7ff092008-10-11 20:09:42 +0200185 hex = get_ref_from_filename(ctx.repo->url, filename, f);
Natanael Copac4b45de2008-12-02 11:31:34 +0100186 if (hex == NULL) {
187 html_status(404, "Not found", 0);
188 return;
Natanael Copa314d9ea2008-11-29 21:49:07 -0800189 }
Natanael Copac4b45de2008-12-02 11:31:34 +0100190 prefix = xstrdup(filename);
191 prefix[strlen(filename) - strlen(f->suffix)] = '\0';
Natanael Copa314d9ea2008-11-29 21:49:07 -0800192 }
Lars Hjemlied7ff092008-10-11 20:09:42 +0200193
194 if (!hex)
195 hex = head;
196
Natanael Copa314d9ea2008-11-29 21:49:07 -0800197 if (!prefix)
198 prefix = xstrdup(cgit_repobasename(ctx.repo->url));
199
Lars Hjemlied7ff092008-10-11 20:09:42 +0200200 make_snapshot(f, hex, prefix, filename);
Natanael Copa314d9ea2008-11-29 21:49:07 -0800201 free(prefix);
Michael Krelinf97c7072007-07-18 14:40:03 +0200202}