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_compressed_tar_archive(struct archiver_args *args, char *filter_argv[])
19 f.cmd = filter_argv[0];
22 rv = write_tar_archive(args);
23 cgit_close_filter(&f);
27 static int write_tar_gzip_archive(struct archiver_args *args)
29 char *argv[] = { "gzip", "-n", NULL };
30 return write_compressed_tar_archive(args, argv);
33 static int write_tar_bzip2_archive(struct archiver_args *args)
35 char *argv[] = { "bzip2", NULL };
36 return write_compressed_tar_archive(args, argv);
39 static int write_tar_xz_archive(struct archiver_args *args)
41 char *argv[] = { "xz", NULL };
42 return write_compressed_tar_archive(args, argv);
45 const struct cgit_snapshot_format cgit_snapshot_formats[] = {
46 { ".zip", "application/x-zip", write_zip_archive, 0x01 },
47 { ".tar.gz", "application/x-gzip", write_tar_gzip_archive, 0x02 },
48 { ".tar.bz2", "application/x-bzip2", write_tar_bzip2_archive, 0x04 },
49 { ".tar", "application/x-tar", write_tar_archive, 0x08 },
50 { ".tar.xz", "application/x-xz", write_tar_xz_archive, 0x10 },
54 static const struct cgit_snapshot_format *get_format(const char *filename)
56 const struct cgit_snapshot_format *fmt;
59 fl = strlen(filename);
60 for(fmt = cgit_snapshot_formats; fmt->suffix; fmt++) {
61 sl = strlen(fmt->suffix);
64 if (!strcmp(fmt->suffix, filename + fl - sl))
70 static int make_snapshot(const struct cgit_snapshot_format *format,
71 const char *hex, const char *prefix,
74 struct archiver_args args;
75 struct commit *commit;
76 unsigned char sha1[20];
78 if(get_sha1(hex, sha1)) {
79 cgit_print_error(fmt("Bad object id: %s", hex));
82 commit = lookup_commit_reference(sha1);
84 cgit_print_error(fmt("Not a commit reference: %s", hex));
87 memset(&args, 0, sizeof(args));
89 args.base = fmt("%s/", prefix);
90 args.baselen = strlen(prefix) + 1;
95 args.tree = commit->tree;
96 args.time = commit->date;
97 args.compression_level = Z_DEFAULT_COMPRESSION;
98 ctx.page.mimetype = xstrdup(format->mimetype);
99 ctx.page.filename = xstrdup(filename);
100 cgit_print_http_headers(&ctx);
101 format->write_func(&args);
105 /* Try to guess the requested revision from the requested snapshot name.
106 * First the format extension is stripped, e.g. "cgit-0.7.2.tar.gz" become
107 * "cgit-0.7.2". If this is a valid commit object name we've got a winner.
108 * Otherwise, if the snapshot name has a prefix matching the result from
109 * repo_basename(), we strip the basename and any following '-' and '_'
110 * characters ("cgit-0.7.2" -> "0.7.2") and check the resulting name once
111 * more. If this still isn't a valid commit object name, we check if pre-
112 * pending a 'v' to the remaining snapshot name ("0.7.2" -> "v0.7.2") gives
113 * us something valid.
115 static const char *get_ref_from_filename(const char *url, const char *filename,
116 const struct cgit_snapshot_format *format)
118 const char *reponame;
119 unsigned char sha1[20];
122 snapshot = xstrdup(filename);
123 snapshot[strlen(snapshot) - strlen(format->suffix)] = '\0';
125 if (get_sha1(snapshot, sha1) == 0)
128 reponame = cgit_repobasename(url);
129 if (prefixcmp(snapshot, reponame) == 0) {
130 snapshot += strlen(reponame);
131 while (snapshot && (*snapshot == '-' || *snapshot == '_'))
135 if (get_sha1(snapshot, sha1) == 0)
138 snapshot = fmt("v%s", snapshot);
139 if (get_sha1(snapshot, sha1) == 0)
145 void show_error(char *msg)
147 ctx.page.mimetype = "text/html";
148 cgit_print_http_headers(&ctx);
149 cgit_print_docstart(&ctx);
150 cgit_print_pageheader(&ctx);
151 cgit_print_error(msg);
155 void cgit_print_snapshot(const char *head, const char *hex,
156 const char *filename, int snapshots, int dwim)
158 const struct cgit_snapshot_format* f;
162 show_error("No snapshot name specified");
166 f = get_format(filename);
168 show_error(xstrdup(fmt("Unsupported snapshot format: %s",
174 hex = get_ref_from_filename(ctx.repo->url, filename, f);
176 html_status(404, "Not found", 0);
179 prefix = xstrdup(filename);
180 prefix[strlen(filename) - strlen(f->suffix)] = '\0';
187 prefix = xstrdup(cgit_repobasename(ctx.repo->url));
189 make_snapshot(f, hex, prefix, filename);