1 /* ui-snapshot.c: generate snapshot of a commit
3 * Copyright (C) 2006 Lars Hjemli
4 * Copyright (C) 2012 Jason A. Donenfeld <Jason@zx2c4.com>
6 * Licensed under GNU General Public License v2
7 * (see COPYING for full license text)
12 #include "ui-shared.h"
14 static int write_archive_type(const char *format, const char *hex, const char *prefix)
16 struct argv_array argv = ARGV_ARRAY_INIT;
17 argv_array_push(&argv, format);
19 argv_array_push(&argv, "--prefix");
20 argv_array_push(&argv, fmt("%s/", prefix));
22 argv_array_push(&argv, hex);
23 return write_archive(argv.argc, argv.argv, NULL, 1, NULL, 0);
26 static int write_tar_archive(const char *hex, const char *prefix)
28 return write_archive_type("--format=tar", hex, prefix);
31 static int write_zip_archive(const char *hex, const char *prefix)
33 return write_archive_type("--format=zip", hex, prefix);
36 static int write_compressed_tar_archive(const char *hex,
43 f.cmd = filter_argv[0];
46 rv = write_tar_archive(hex, prefix);
47 cgit_close_filter(&f);
51 static int write_tar_gzip_archive(const char *hex, const char *prefix)
53 char *argv[] = { "gzip", "-n", NULL };
54 return write_compressed_tar_archive(hex, prefix, argv);
57 static int write_tar_bzip2_archive(const char *hex, const char *prefix)
59 char *argv[] = { "bzip2", NULL };
60 return write_compressed_tar_archive(hex, prefix, argv);
63 static int write_tar_xz_archive(const char *hex, const char *prefix)
65 char *argv[] = { "xz", NULL };
66 return write_compressed_tar_archive(hex, prefix, argv);
69 const struct cgit_snapshot_format cgit_snapshot_formats[] = {
70 { ".zip", "application/x-zip", write_zip_archive, 0x01 },
71 { ".tar.gz", "application/x-gzip", write_tar_gzip_archive, 0x02 },
72 { ".tar.bz2", "application/x-bzip2", write_tar_bzip2_archive, 0x04 },
73 { ".tar", "application/x-tar", write_tar_archive, 0x08 },
74 { ".tar.xz", "application/x-xz", write_tar_xz_archive, 0x10 },
78 static const struct cgit_snapshot_format *get_format(const char *filename)
80 const struct cgit_snapshot_format *fmt;
83 fl = strlen(filename);
84 for (fmt = cgit_snapshot_formats; fmt->suffix; fmt++) {
85 sl = strlen(fmt->suffix);
88 if (!strcmp(fmt->suffix, filename + fl - sl))
94 static int make_snapshot(const struct cgit_snapshot_format *format,
95 const char *hex, const char *prefix,
98 unsigned char sha1[20];
100 if (get_sha1(hex, sha1)) {
101 cgit_print_error(fmt("Bad object id: %s", hex));
104 if (!lookup_commit_reference(sha1)) {
105 cgit_print_error(fmt("Not a commit reference: %s", hex));
108 ctx.page.mimetype = xstrdup(format->mimetype);
109 ctx.page.filename = xstrdup(filename);
110 cgit_print_http_headers(&ctx);
111 format->write_func(hex, prefix);
115 /* Try to guess the requested revision from the requested snapshot name.
116 * First the format extension is stripped, e.g. "cgit-0.7.2.tar.gz" become
117 * "cgit-0.7.2". If this is a valid commit object name we've got a winner.
118 * Otherwise, if the snapshot name has a prefix matching the result from
119 * repo_basename(), we strip the basename and any following '-' and '_'
120 * characters ("cgit-0.7.2" -> "0.7.2") and check the resulting name once
121 * more. If this still isn't a valid commit object name, we check if pre-
122 * pending a 'v' to the remaining snapshot name ("0.7.2" -> "v0.7.2") gives
123 * us something valid.
125 static const char *get_ref_from_filename(const char *url, const char *filename,
126 const struct cgit_snapshot_format *format)
128 const char *reponame;
129 unsigned char sha1[20];
132 snapshot = xstrdup(filename);
133 snapshot[strlen(snapshot) - strlen(format->suffix)] = '\0';
135 if (get_sha1(snapshot, sha1) == 0)
138 reponame = cgit_repobasename(url);
139 if (prefixcmp(snapshot, reponame) == 0) {
140 snapshot += strlen(reponame);
141 while (snapshot && (*snapshot == '-' || *snapshot == '_'))
145 if (get_sha1(snapshot, sha1) == 0)
148 snapshot = fmt("v%s", snapshot);
149 if (get_sha1(snapshot, sha1) == 0)
155 void show_error(char *msg)
157 ctx.page.mimetype = "text/html";
158 cgit_print_http_headers(&ctx);
159 cgit_print_docstart(&ctx);
160 cgit_print_pageheader(&ctx);
161 cgit_print_error(msg);
165 void cgit_print_snapshot(const char *head, const char *hex,
166 const char *filename, int snapshots, int dwim)
168 const struct cgit_snapshot_format* f;
172 show_error("No snapshot name specified");
176 f = get_format(filename);
178 show_error(xstrdup(fmt("Unsupported snapshot format: %s",
184 hex = get_ref_from_filename(ctx.repo->url, filename, f);
186 html_status(404, "Not found", 0);
189 prefix = xstrdup(filename);
190 prefix[strlen(filename) - strlen(f->suffix)] = '\0';
197 prefix = xstrdup(cgit_repobasename(ctx.repo->url));
199 make_snapshot(f, hex, prefix, filename);