all repos — cgit @ 8f40be229cf8ac7ecbf5e03e11098bad8e82e7e5

a hyperfast web frontend for git written in c

ui-snapshot.c (view raw)

  1/* ui-snapshot.c: generate snapshot of a commit
  2 *
  3 * Copyright (C) 2006 Lars Hjemli
  4 * Copyright (C) 2012 Jason A. Donenfeld <Jason@zx2c4.com>
  5 *
  6 * Licensed under GNU General Public License v2
  7 *   (see COPYING for full license text)
  8 */
  9
 10#include "cgit.h"
 11#include "html.h"
 12#include "ui-shared.h"
 13
 14static int write_compressed_tar_archive(struct archiver_args *args, char *filter_argv[])
 15{
 16	int rv;
 17	struct cgit_filter f;
 18
 19	f.cmd = filter_argv[0];
 20	f.argv = filter_argv;
 21	cgit_open_filter(&f);
 22	rv = write_tar_archive(args);
 23	cgit_close_filter(&f);
 24	return rv;
 25}
 26
 27static int write_tar_gzip_archive(struct archiver_args *args)
 28{
 29	char *argv[] = { "gzip", "-n", NULL };
 30	return write_compressed_tar_archive(args, argv);
 31}
 32
 33static int write_tar_bzip2_archive(struct archiver_args *args)
 34{
 35	char *argv[] = { "bzip2", NULL };
 36	return write_compressed_tar_archive(args, argv);
 37}
 38
 39static int write_tar_xz_archive(struct archiver_args *args)
 40{
 41	char *argv[] = { "xz", NULL };
 42	return write_compressed_tar_archive(args, argv);
 43}
 44
 45const 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 },
 51	{}
 52};
 53
 54static const struct cgit_snapshot_format *get_format(const char *filename)
 55{
 56	const struct cgit_snapshot_format *fmt;
 57	int fl, sl;
 58
 59	fl = strlen(filename);
 60	for(fmt = cgit_snapshot_formats; fmt->suffix; fmt++) {
 61		sl = strlen(fmt->suffix);
 62		if (sl >= fl)
 63			continue;
 64		if (!strcmp(fmt->suffix, filename + fl - sl))
 65			return fmt;
 66	}
 67	return NULL;
 68}
 69
 70static int make_snapshot(const struct cgit_snapshot_format *format,
 71			 const char *hex, const char *prefix,
 72			 const char *filename)
 73{
 74	struct archiver_args args;
 75	struct commit *commit;
 76	unsigned char sha1[20];
 77
 78	if(get_sha1(hex, sha1)) {
 79		cgit_print_error(fmt("Bad object id: %s", hex));
 80		return 1;
 81	}
 82	commit = lookup_commit_reference(sha1);
 83	if(!commit) {
 84		cgit_print_error(fmt("Not a commit reference: %s", hex));
 85		return 1;
 86	}
 87	memset(&args, 0, sizeof(args));
 88	if (prefix) {
 89		args.base = fmt("%s/", prefix);
 90		args.baselen = strlen(prefix) + 1;
 91	} else {
 92		args.base = "";
 93		args.baselen = 0;
 94	}
 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);
102	return 0;
103}
104
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.
114 */
115static const char *get_ref_from_filename(const char *url, const char *filename,
116					 const struct cgit_snapshot_format *format)
117{
118	const char *reponame;
119	unsigned char sha1[20];
120	char *snapshot;
121
122	snapshot = xstrdup(filename);
123	snapshot[strlen(snapshot) - strlen(format->suffix)] = '\0';
124
125	if (get_sha1(snapshot, sha1) == 0)
126		return snapshot;
127
128	reponame = cgit_repobasename(url);
129	if (prefixcmp(snapshot, reponame) == 0) {
130		snapshot += strlen(reponame);
131		while (snapshot && (*snapshot == '-' || *snapshot == '_'))
132			snapshot++;
133	}
134
135	if (get_sha1(snapshot, sha1) == 0)
136		return snapshot;
137
138	snapshot = fmt("v%s", snapshot);
139	if (get_sha1(snapshot, sha1) == 0)
140		return snapshot;
141
142	return NULL;
143}
144
145void show_error(char *msg)
146{
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);
152	cgit_print_docend();
153}
154
155void cgit_print_snapshot(const char *head, const char *hex,
156			 const char *filename, int snapshots, int dwim)
157{
158	const struct cgit_snapshot_format* f;
159	char *prefix = NULL;
160
161	if (!filename) {
162		show_error("No snapshot name specified");
163		return;
164	}
165
166	f = get_format(filename);
167	if (!f) {
168		show_error(xstrdup(fmt("Unsupported snapshot format: %s",
169				       filename)));
170		return;
171	}
172
173	if (!hex && dwim) {
174		hex = get_ref_from_filename(ctx.repo->url, filename, f);
175		if (hex == NULL) {
176			html_status(404, "Not found", 0);
177			return;
178		}
179		prefix = xstrdup(filename);
180		prefix[strlen(filename) - strlen(f->suffix)] = '\0';
181	}
182
183	if (!hex)
184		hex = head;
185
186	if (!prefix)
187		prefix = xstrdup(cgit_repobasename(ctx.repo->url));
188
189	make_snapshot(f, hex, prefix, filename);
190	free(prefix);
191}