1 /* ui-snapshot.c: generate snapshot of a commit
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
10 #include "ui-snapshot.h"
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;
19 argv_array_push(&argv, "snapshot");
20 argv_array_push(&argv, format);
22 struct strbuf buf = STRBUF_INIT;
23 strbuf_addstr(&buf, prefix);
24 strbuf_addch(&buf, '/');
25 argv_array_push(&argv, "--prefix");
26 argv_array_push(&argv, buf.buf);
29 argv_array_push(&argv, hex);
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.
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));
40 result = write_archive(argv.argc, nargv, NULL, 1, NULL, 0);
41 argv_array_clear(&argv);
46 static int write_tar_archive(const char *hex, const char *prefix)
48 return write_archive_type("--format=tar", hex, prefix);
51 static int write_zip_archive(const char *hex, const char *prefix)
53 return write_archive_type("--format=zip", hex, prefix);
56 static int write_compressed_tar_archive(const char *hex,
63 f.cmd = filter_argv[0];
66 rv = write_tar_archive(hex, prefix);
67 cgit_close_filter(&f);
71 static int write_tar_gzip_archive(const char *hex, const char *prefix)
73 char *argv[] = { "gzip", "-n", NULL };
74 return write_compressed_tar_archive(hex, prefix, argv);
77 static int write_tar_bzip2_archive(const char *hex, const char *prefix)
79 char *argv[] = { "bzip2", NULL };
80 return write_compressed_tar_archive(hex, prefix, argv);
83 static int write_tar_xz_archive(const char *hex, const char *prefix)
85 char *argv[] = { "xz", NULL };
86 return write_compressed_tar_archive(hex, prefix, argv);
89 const struct cgit_snapshot_format cgit_snapshot_formats[] = {
90 { ".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 },
98 static const struct cgit_snapshot_format *get_format(const char *filename)
100 const struct cgit_snapshot_format *fmt;
103 fl = strlen(filename);
104 for (fmt = cgit_snapshot_formats; fmt->suffix; fmt++) {
105 sl = strlen(fmt->suffix);
108 if (!strcmp(fmt->suffix, filename + fl - sl))
114 static int make_snapshot(const struct cgit_snapshot_format *format,
115 const char *hex, const char *prefix,
116 const char *filename)
118 unsigned char sha1[20];
120 if (get_sha1(hex, sha1)) {
121 cgit_print_error("Bad object id: %s", hex);
124 if (!lookup_commit_reference(sha1)) {
125 cgit_print_error("Not a commit reference: %s", hex);
128 ctx.page.mimetype = xstrdup(format->mimetype);
129 ctx.page.filename = xstrdup(filename);
130 cgit_print_http_headers(&ctx);
131 format->write_func(hex, prefix);
135 /* Try to guess the requested revision from the requested snapshot name.
136 * First the format extension is stripped, e.g. "cgit-0.7.2.tar.gz" become
137 * "cgit-0.7.2". If this is a valid commit object name we've got a winner.
138 * Otherwise, if the snapshot name has a prefix matching the result from
139 * repo_basename(), we strip the basename and any following '-' and '_'
140 * characters ("cgit-0.7.2" -> "0.7.2") and check the resulting name once
141 * more. If this still isn't a valid commit object name, we check if pre-
142 * pending a 'v' or a 'V' to the remaining snapshot name ("0.7.2" ->
143 * "v0.7.2") gives us something valid.
145 static const char *get_ref_from_filename(const char *url, const char *filename,
146 const struct cgit_snapshot_format *format)
148 const char *reponame;
149 unsigned char sha1[20];
150 struct strbuf snapshot = STRBUF_INIT;
153 strbuf_addstr(&snapshot, filename);
154 strbuf_setlen(&snapshot, snapshot.len - strlen(format->suffix));
156 if (get_sha1(snapshot.buf, sha1) == 0)
159 reponame = cgit_repobasename(url);
160 if (prefixcmp(snapshot.buf, reponame) == 0) {
161 const char *new_start = snapshot.buf;
162 new_start += strlen(reponame);
163 while (new_start && (*new_start == '-' || *new_start == '_'))
165 strbuf_splice(&snapshot, 0, new_start - snapshot.buf, "", 0);
168 if (get_sha1(snapshot.buf, sha1) == 0)
171 strbuf_insert(&snapshot, 0, "v", 1);
172 if (get_sha1(snapshot.buf, sha1) == 0)
175 strbuf_splice(&snapshot, 0, 1, "V", 1);
176 if (get_sha1(snapshot.buf, sha1) == 0)
180 strbuf_release(&snapshot);
183 return result ? strbuf_detach(&snapshot, NULL) : NULL;
186 __attribute__((format (printf, 1, 2)))
187 static void show_error(char *fmt, ...)
191 ctx.page.mimetype = "text/html";
192 cgit_print_http_headers(&ctx);
193 cgit_print_docstart(&ctx);
194 cgit_print_pageheader(&ctx);
196 cgit_vprint_error(fmt, ap);
201 void cgit_print_snapshot(const char *head, const char *hex,
202 const char *filename, int snapshots, int dwim)
204 const struct cgit_snapshot_format* f;
208 show_error("No snapshot name specified");
212 f = get_format(filename);
214 show_error("Unsupported snapshot format: %s", filename);
219 hex = get_ref_from_filename(ctx.repo->url, filename, f);
221 html_status(404, "Not found", 0);
224 prefix = xstrdup(filename);
225 prefix[strlen(filename) - strlen(f->suffix)] = '\0';
232 prefix = xstrdup(cgit_repobasename(ctx.repo->url));
234 make_snapshot(f, hex, prefix, filename);