blob: 9be5dbef808526b3b0dd9d518163b206ead37b9f [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"
John Keeping8f208792013-04-06 11:37:59 +010011#include "ui-snapshot.h"
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010012#include "html.h"
Lars Hjemlia4d1ca12008-03-24 16:50:57 +010013#include "ui-shared.h"
Lars Hjemliab2ab952007-02-08 13:53:13 +010014
John Keeping2ab1cd92013-03-02 12:32:12 +000015static int write_archive_type(const char *format, const char *hex, const char *prefix)
16{
17 struct argv_array argv = ARGV_ARRAY_INIT;
Jason A. Donenfeld973deda2013-03-03 23:41:53 -050018 argv_array_push(&argv, "snapshot");
John Keeping2ab1cd92013-03-02 12:32:12 +000019 argv_array_push(&argv, format);
20 if (prefix) {
21 argv_array_push(&argv, "--prefix");
22 argv_array_push(&argv, fmt("%s/", prefix));
23 }
24 argv_array_push(&argv, hex);
25 return write_archive(argv.argc, argv.argv, NULL, 1, NULL, 0);
26}
27
28static int write_tar_archive(const char *hex, const char *prefix)
29{
30 return write_archive_type("--format=tar", hex, prefix);
31}
32
33static int write_zip_archive(const char *hex, const char *prefix)
34{
35 return write_archive_type("--format=zip", hex, prefix);
36}
37
38static int write_compressed_tar_archive(const char *hex,
39 const char *prefix,
40 char *filter_argv[])
Michael Krelin18a99bd2007-07-21 02:05:34 +020041{
Michael Krelin18a99bd2007-07-21 02:05:34 +020042 int rv;
Lars Hjemli18dfbdc2009-07-31 15:52:57 +020043 struct cgit_filter f;
Michael Krelin18a99bd2007-07-21 02:05:34 +020044
Jason A. Donenfeld055e0922012-09-26 02:56:38 +020045 f.cmd = filter_argv[0];
46 f.argv = filter_argv;
Lars Hjemli3ec6b302011-06-06 19:29:58 +000047 cgit_open_filter(&f);
John Keeping2ab1cd92013-03-02 12:32:12 +000048 rv = write_tar_archive(hex, prefix);
Lars Hjemli18dfbdc2009-07-31 15:52:57 +020049 cgit_close_filter(&f);
Michael Krelin18a99bd2007-07-21 02:05:34 +020050 return rv;
51}
52
John Keeping2ab1cd92013-03-02 12:32:12 +000053static int write_tar_gzip_archive(const char *hex, const char *prefix)
Michael Krelin4a92cbb2007-07-20 20:58:23 +020054{
Jason A. Donenfeld055e0922012-09-26 02:56:38 +020055 char *argv[] = { "gzip", "-n", NULL };
John Keeping2ab1cd92013-03-02 12:32:12 +000056 return write_compressed_tar_archive(hex, prefix, argv);
Michael Krelin18a99bd2007-07-21 02:05:34 +020057}
Lars Hjemli1221adb2007-07-23 22:51:45 +020058
John Keeping2ab1cd92013-03-02 12:32:12 +000059static int write_tar_bzip2_archive(const char *hex, const char *prefix)
Michael Krelin18a99bd2007-07-21 02:05:34 +020060{
Jason A. Donenfeld055e0922012-09-26 02:56:38 +020061 char *argv[] = { "bzip2", NULL };
John Keeping2ab1cd92013-03-02 12:32:12 +000062 return write_compressed_tar_archive(hex, prefix, argv);
Michael Krelin4a92cbb2007-07-20 20:58:23 +020063}
64
John Keeping2ab1cd92013-03-02 12:32:12 +000065static int write_tar_xz_archive(const char *hex, const char *prefix)
Andreas Wiese06424352009-12-08 22:18:11 +010066{
Jason A. Donenfeld055e0922012-09-26 02:56:38 +020067 char *argv[] = { "xz", NULL };
John Keeping2ab1cd92013-03-02 12:32:12 +000068 return write_compressed_tar_archive(hex, prefix, argv);
Andreas Wiese06424352009-12-08 22:18:11 +010069}
70
Lars Hjemlif34478c2008-03-24 16:00:27 +010071const struct cgit_snapshot_format cgit_snapshot_formats[] = {
Andreas Wiese06424352009-12-08 22:18:11 +010072 { ".zip", "application/x-zip", write_zip_archive, 0x01 },
73 { ".tar.gz", "application/x-gzip", write_tar_gzip_archive, 0x02 },
74 { ".tar.bz2", "application/x-bzip2", write_tar_bzip2_archive, 0x04 },
75 { ".tar", "application/x-tar", write_tar_archive, 0x08 },
76 { ".tar.xz", "application/x-xz", write_tar_xz_archive, 0x10 },
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -050077 { NULL }
Michael Krelinf97c7072007-07-18 14:40:03 +020078};
Lars Hjemliab2ab952007-02-08 13:53:13 +010079
Lars Hjemlied7ff092008-10-11 20:09:42 +020080static const struct cgit_snapshot_format *get_format(const char *filename)
81{
82 const struct cgit_snapshot_format *fmt;
83 int fl, sl;
84
85 fl = strlen(filename);
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -050086 for (fmt = cgit_snapshot_formats; fmt->suffix; fmt++) {
Lars Hjemlied7ff092008-10-11 20:09:42 +020087 sl = strlen(fmt->suffix);
88 if (sl >= fl)
89 continue;
90 if (!strcmp(fmt->suffix, filename + fl - sl))
91 return fmt;
92 }
93 return NULL;
94}
95
Lars Hjemlif34478c2008-03-24 16:00:27 +010096static int make_snapshot(const struct cgit_snapshot_format *format,
97 const char *hex, const char *prefix,
98 const char *filename)
99{
Lars Hjemlif34478c2008-03-24 16:00:27 +0100100 unsigned char sha1[20];
101
John Keeping2ab1cd92013-03-02 12:32:12 +0000102 if (get_sha1(hex, sha1)) {
Lars Hjemlif34478c2008-03-24 16:00:27 +0100103 cgit_print_error(fmt("Bad object id: %s", hex));
104 return 1;
105 }
John Keeping2ab1cd92013-03-02 12:32:12 +0000106 if (!lookup_commit_reference(sha1)) {
Lars Hjemlif34478c2008-03-24 16:00:27 +0100107 cgit_print_error(fmt("Not a commit reference: %s", hex));
108 return 1;
109 }
Lars Hjemlif34478c2008-03-24 16:00:27 +0100110 ctx.page.mimetype = xstrdup(format->mimetype);
111 ctx.page.filename = xstrdup(filename);
112 cgit_print_http_headers(&ctx);
John Keeping2ab1cd92013-03-02 12:32:12 +0000113 format->write_func(hex, prefix);
Lars Hjemlif34478c2008-03-24 16:00:27 +0100114 return 0;
115}
Lars Hjemli1221adb2007-07-23 22:51:45 +0200116
Lars Hjemli4b4f8d12008-12-01 19:13:44 +0100117/* Try to guess the requested revision from the requested snapshot name.
118 * First the format extension is stripped, e.g. "cgit-0.7.2.tar.gz" become
119 * "cgit-0.7.2". If this is a valid commit object name we've got a winner.
120 * Otherwise, if the snapshot name has a prefix matching the result from
121 * repo_basename(), we strip the basename and any following '-' and '_'
122 * characters ("cgit-0.7.2" -> "0.7.2") and check the resulting name once
123 * more. If this still isn't a valid commit object name, we check if pre-
124 * pending a 'v' to the remaining snapshot name ("0.7.2" -> "v0.7.2") gives
125 * us something valid.
Lars Hjemlied7ff092008-10-11 20:09:42 +0200126 */
127static const char *get_ref_from_filename(const char *url, const char *filename,
Lars Hjemli4b4f8d12008-12-01 19:13:44 +0100128 const struct cgit_snapshot_format *format)
Lars Hjemlied7ff092008-10-11 20:09:42 +0200129{
Lars Hjemli4b4f8d12008-12-01 19:13:44 +0100130 const char *reponame;
131 unsigned char sha1[20];
132 char *snapshot;
133
134 snapshot = xstrdup(filename);
135 snapshot[strlen(snapshot) - strlen(format->suffix)] = '\0';
Lars Hjemli4b4f8d12008-12-01 19:13:44 +0100136
137 if (get_sha1(snapshot, sha1) == 0)
138 return snapshot;
139
140 reponame = cgit_repobasename(url);
Lars Hjemli4b4f8d12008-12-01 19:13:44 +0100141 if (prefixcmp(snapshot, reponame) == 0) {
142 snapshot += strlen(reponame);
143 while (snapshot && (*snapshot == '-' || *snapshot == '_'))
144 snapshot++;
145 }
146
147 if (get_sha1(snapshot, sha1) == 0)
148 return snapshot;
149
150 snapshot = fmt("v%s", snapshot);
151 if (get_sha1(snapshot, sha1) == 0)
152 return snapshot;
153
154 return NULL;
Lars Hjemlied7ff092008-10-11 20:09:42 +0200155}
156
Lukas Fleischerbafab422013-03-04 08:52:33 +0100157static void show_error(char *msg)
Lars Hjemli6fddad72009-03-15 08:57:33 +0100158{
159 ctx.page.mimetype = "text/html";
160 cgit_print_http_headers(&ctx);
161 cgit_print_docstart(&ctx);
162 cgit_print_pageheader(&ctx);
163 cgit_print_error(msg);
164 cgit_print_docend();
165}
166
Natanael Copa314d9ea2008-11-29 21:49:07 -0800167void cgit_print_snapshot(const char *head, const char *hex,
Lars Hjemlied7ff092008-10-11 20:09:42 +0200168 const char *filename, int snapshots, int dwim)
Lars Hjemliab2ab952007-02-08 13:53:13 +0100169{
Lars Hjemlif34478c2008-03-24 16:00:27 +0100170 const struct cgit_snapshot_format* f;
Natanael Copa314d9ea2008-11-29 21:49:07 -0800171 char *prefix = NULL;
Lars Hjemlieb453422007-07-23 00:11:15 +0200172
Lars Hjemli6fddad72009-03-15 08:57:33 +0100173 if (!filename) {
174 show_error("No snapshot name specified");
175 return;
176 }
177
Lars Hjemlied7ff092008-10-11 20:09:42 +0200178 f = get_format(filename);
179 if (!f) {
Lars Hjemli6fddad72009-03-15 08:57:33 +0100180 show_error(xstrdup(fmt("Unsupported snapshot format: %s",
181 filename)));
Michael Krelinf97c7072007-07-18 14:40:03 +0200182 return;
183 }
Lars Hjemlied7ff092008-10-11 20:09:42 +0200184
Natanael Copa314d9ea2008-11-29 21:49:07 -0800185 if (!hex && dwim) {
Lars Hjemlied7ff092008-10-11 20:09:42 +0200186 hex = get_ref_from_filename(ctx.repo->url, filename, f);
Natanael Copac4b45de2008-12-02 11:31:34 +0100187 if (hex == NULL) {
188 html_status(404, "Not found", 0);
189 return;
Natanael Copa314d9ea2008-11-29 21:49:07 -0800190 }
Natanael Copac4b45de2008-12-02 11:31:34 +0100191 prefix = xstrdup(filename);
192 prefix[strlen(filename) - strlen(f->suffix)] = '\0';
Natanael Copa314d9ea2008-11-29 21:49:07 -0800193 }
Lars Hjemlied7ff092008-10-11 20:09:42 +0200194
195 if (!hex)
196 hex = head;
197
Natanael Copa314d9ea2008-11-29 21:49:07 -0800198 if (!prefix)
199 prefix = xstrdup(cgit_repobasename(ctx.repo->url));
200
Lars Hjemlied7ff092008-10-11 20:09:42 +0200201 make_snapshot(f, hex, prefix, filename);
Natanael Copa314d9ea2008-11-29 21:49:07 -0800202 free(prefix);
Michael Krelinf97c7072007-07-18 14:40:03 +0200203}