Lars Hjemli | ab2ab95 | 2007-02-08 13:53:13 +0100 | [diff] [blame] | 1 | /* ui-snapshot.c: generate snapshot of a commit |
| 2 | * |
| 3 | * Copyright (C) 2006 Lars Hjemli |
Jason A. Donenfeld | 055e092 | 2012-09-26 02:56:38 +0200 | [diff] [blame] | 4 | * Copyright (C) 2012 Jason A. Donenfeld <Jason@zx2c4.com> |
Lars Hjemli | ab2ab95 | 2007-02-08 13:53:13 +0100 | [diff] [blame] | 5 | * |
| 6 | * Licensed under GNU General Public License v2 |
| 7 | * (see COPYING for full license text) |
| 8 | */ |
| 9 | |
| 10 | #include "cgit.h" |
John Keeping | 8f20879 | 2013-04-06 11:37:59 +0100 | [diff] [blame] | 11 | #include "ui-snapshot.h" |
Lars Hjemli | b1f9b9c | 2008-02-23 22:45:33 +0100 | [diff] [blame] | 12 | #include "html.h" |
Lars Hjemli | a4d1ca1 | 2008-03-24 16:50:57 +0100 | [diff] [blame] | 13 | #include "ui-shared.h" |
Lars Hjemli | ab2ab95 | 2007-02-08 13:53:13 +0100 | [diff] [blame] | 14 | |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 15 | static int write_archive_type(const char *format, const char *hex, const char *prefix) |
| 16 | { |
| 17 | struct argv_array argv = ARGV_ARRAY_INIT; |
Jason A. Donenfeld | 973deda | 2013-03-03 23:41:53 -0500 | [diff] [blame] | 18 | argv_array_push(&argv, "snapshot"); |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 19 | 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 | |
| 28 | static int write_tar_archive(const char *hex, const char *prefix) |
| 29 | { |
| 30 | return write_archive_type("--format=tar", hex, prefix); |
| 31 | } |
| 32 | |
| 33 | static int write_zip_archive(const char *hex, const char *prefix) |
| 34 | { |
| 35 | return write_archive_type("--format=zip", hex, prefix); |
| 36 | } |
| 37 | |
| 38 | static int write_compressed_tar_archive(const char *hex, |
| 39 | const char *prefix, |
| 40 | char *filter_argv[]) |
Michael Krelin | 18a99bd | 2007-07-21 02:05:34 +0200 | [diff] [blame] | 41 | { |
Michael Krelin | 18a99bd | 2007-07-21 02:05:34 +0200 | [diff] [blame] | 42 | int rv; |
Lars Hjemli | 18dfbdc | 2009-07-31 15:52:57 +0200 | [diff] [blame] | 43 | struct cgit_filter f; |
Michael Krelin | 18a99bd | 2007-07-21 02:05:34 +0200 | [diff] [blame] | 44 | |
Jason A. Donenfeld | 055e092 | 2012-09-26 02:56:38 +0200 | [diff] [blame] | 45 | f.cmd = filter_argv[0]; |
| 46 | f.argv = filter_argv; |
Lars Hjemli | 3ec6b30 | 2011-06-06 19:29:58 +0000 | [diff] [blame] | 47 | cgit_open_filter(&f); |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 48 | rv = write_tar_archive(hex, prefix); |
Lars Hjemli | 18dfbdc | 2009-07-31 15:52:57 +0200 | [diff] [blame] | 49 | cgit_close_filter(&f); |
Michael Krelin | 18a99bd | 2007-07-21 02:05:34 +0200 | [diff] [blame] | 50 | return rv; |
| 51 | } |
| 52 | |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 53 | static int write_tar_gzip_archive(const char *hex, const char *prefix) |
Michael Krelin | 4a92cbb | 2007-07-20 20:58:23 +0200 | [diff] [blame] | 54 | { |
Jason A. Donenfeld | 055e092 | 2012-09-26 02:56:38 +0200 | [diff] [blame] | 55 | char *argv[] = { "gzip", "-n", NULL }; |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 56 | return write_compressed_tar_archive(hex, prefix, argv); |
Michael Krelin | 18a99bd | 2007-07-21 02:05:34 +0200 | [diff] [blame] | 57 | } |
Lars Hjemli | 1221adb | 2007-07-23 22:51:45 +0200 | [diff] [blame] | 58 | |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 59 | static int write_tar_bzip2_archive(const char *hex, const char *prefix) |
Michael Krelin | 18a99bd | 2007-07-21 02:05:34 +0200 | [diff] [blame] | 60 | { |
Jason A. Donenfeld | 055e092 | 2012-09-26 02:56:38 +0200 | [diff] [blame] | 61 | char *argv[] = { "bzip2", NULL }; |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 62 | return write_compressed_tar_archive(hex, prefix, argv); |
Michael Krelin | 4a92cbb | 2007-07-20 20:58:23 +0200 | [diff] [blame] | 63 | } |
| 64 | |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 65 | static int write_tar_xz_archive(const char *hex, const char *prefix) |
Andreas Wiese | 0642435 | 2009-12-08 22:18:11 +0100 | [diff] [blame] | 66 | { |
Jason A. Donenfeld | 055e092 | 2012-09-26 02:56:38 +0200 | [diff] [blame] | 67 | char *argv[] = { "xz", NULL }; |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 68 | return write_compressed_tar_archive(hex, prefix, argv); |
Andreas Wiese | 0642435 | 2009-12-08 22:18:11 +0100 | [diff] [blame] | 69 | } |
| 70 | |
Lars Hjemli | f34478c | 2008-03-24 16:00:27 +0100 | [diff] [blame] | 71 | const struct cgit_snapshot_format cgit_snapshot_formats[] = { |
Andreas Wiese | 0642435 | 2009-12-08 22:18:11 +0100 | [diff] [blame] | 72 | { ".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. Donenfeld | bdae1d8 | 2013-03-03 23:21:33 -0500 | [diff] [blame] | 77 | { NULL } |
Michael Krelin | f97c707 | 2007-07-18 14:40:03 +0200 | [diff] [blame] | 78 | }; |
Lars Hjemli | ab2ab95 | 2007-02-08 13:53:13 +0100 | [diff] [blame] | 79 | |
Lars Hjemli | ed7ff09 | 2008-10-11 20:09:42 +0200 | [diff] [blame] | 80 | static 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. Donenfeld | bdae1d8 | 2013-03-03 23:21:33 -0500 | [diff] [blame] | 86 | for (fmt = cgit_snapshot_formats; fmt->suffix; fmt++) { |
Lars Hjemli | ed7ff09 | 2008-10-11 20:09:42 +0200 | [diff] [blame] | 87 | 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 Hjemli | f34478c | 2008-03-24 16:00:27 +0100 | [diff] [blame] | 96 | static int make_snapshot(const struct cgit_snapshot_format *format, |
| 97 | const char *hex, const char *prefix, |
| 98 | const char *filename) |
| 99 | { |
Lars Hjemli | f34478c | 2008-03-24 16:00:27 +0100 | [diff] [blame] | 100 | unsigned char sha1[20]; |
| 101 | |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 102 | if (get_sha1(hex, sha1)) { |
Lars Hjemli | f34478c | 2008-03-24 16:00:27 +0100 | [diff] [blame] | 103 | cgit_print_error(fmt("Bad object id: %s", hex)); |
| 104 | return 1; |
| 105 | } |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 106 | if (!lookup_commit_reference(sha1)) { |
Lars Hjemli | f34478c | 2008-03-24 16:00:27 +0100 | [diff] [blame] | 107 | cgit_print_error(fmt("Not a commit reference: %s", hex)); |
| 108 | return 1; |
| 109 | } |
Lars Hjemli | f34478c | 2008-03-24 16:00:27 +0100 | [diff] [blame] | 110 | ctx.page.mimetype = xstrdup(format->mimetype); |
| 111 | ctx.page.filename = xstrdup(filename); |
| 112 | cgit_print_http_headers(&ctx); |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 113 | format->write_func(hex, prefix); |
Lars Hjemli | f34478c | 2008-03-24 16:00:27 +0100 | [diff] [blame] | 114 | return 0; |
| 115 | } |
Lars Hjemli | 1221adb | 2007-07-23 22:51:45 +0200 | [diff] [blame] | 116 | |
Lars Hjemli | 4b4f8d1 | 2008-12-01 19:13:44 +0100 | [diff] [blame] | 117 | /* 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 Hjemli | ed7ff09 | 2008-10-11 20:09:42 +0200 | [diff] [blame] | 126 | */ |
| 127 | static const char *get_ref_from_filename(const char *url, const char *filename, |
Lars Hjemli | 4b4f8d1 | 2008-12-01 19:13:44 +0100 | [diff] [blame] | 128 | const struct cgit_snapshot_format *format) |
Lars Hjemli | ed7ff09 | 2008-10-11 20:09:42 +0200 | [diff] [blame] | 129 | { |
Lars Hjemli | 4b4f8d1 | 2008-12-01 19:13:44 +0100 | [diff] [blame] | 130 | 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 Hjemli | 4b4f8d1 | 2008-12-01 19:13:44 +0100 | [diff] [blame] | 136 | |
| 137 | if (get_sha1(snapshot, sha1) == 0) |
| 138 | return snapshot; |
| 139 | |
| 140 | reponame = cgit_repobasename(url); |
Lars Hjemli | 4b4f8d1 | 2008-12-01 19:13:44 +0100 | [diff] [blame] | 141 | 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 Hjemli | ed7ff09 | 2008-10-11 20:09:42 +0200 | [diff] [blame] | 155 | } |
| 156 | |
Lukas Fleischer | bafab42 | 2013-03-04 08:52:33 +0100 | [diff] [blame] | 157 | static void show_error(char *msg) |
Lars Hjemli | 6fddad7 | 2009-03-15 08:57:33 +0100 | [diff] [blame] | 158 | { |
| 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 Copa | 314d9ea | 2008-11-29 21:49:07 -0800 | [diff] [blame] | 167 | void cgit_print_snapshot(const char *head, const char *hex, |
Lars Hjemli | ed7ff09 | 2008-10-11 20:09:42 +0200 | [diff] [blame] | 168 | const char *filename, int snapshots, int dwim) |
Lars Hjemli | ab2ab95 | 2007-02-08 13:53:13 +0100 | [diff] [blame] | 169 | { |
Lars Hjemli | f34478c | 2008-03-24 16:00:27 +0100 | [diff] [blame] | 170 | const struct cgit_snapshot_format* f; |
Natanael Copa | 314d9ea | 2008-11-29 21:49:07 -0800 | [diff] [blame] | 171 | char *prefix = NULL; |
Lars Hjemli | eb45342 | 2007-07-23 00:11:15 +0200 | [diff] [blame] | 172 | |
Lars Hjemli | 6fddad7 | 2009-03-15 08:57:33 +0100 | [diff] [blame] | 173 | if (!filename) { |
| 174 | show_error("No snapshot name specified"); |
| 175 | return; |
| 176 | } |
| 177 | |
Lars Hjemli | ed7ff09 | 2008-10-11 20:09:42 +0200 | [diff] [blame] | 178 | f = get_format(filename); |
| 179 | if (!f) { |
Lars Hjemli | 6fddad7 | 2009-03-15 08:57:33 +0100 | [diff] [blame] | 180 | show_error(xstrdup(fmt("Unsupported snapshot format: %s", |
| 181 | filename))); |
Michael Krelin | f97c707 | 2007-07-18 14:40:03 +0200 | [diff] [blame] | 182 | return; |
| 183 | } |
Lars Hjemli | ed7ff09 | 2008-10-11 20:09:42 +0200 | [diff] [blame] | 184 | |
Natanael Copa | 314d9ea | 2008-11-29 21:49:07 -0800 | [diff] [blame] | 185 | if (!hex && dwim) { |
Lars Hjemli | ed7ff09 | 2008-10-11 20:09:42 +0200 | [diff] [blame] | 186 | hex = get_ref_from_filename(ctx.repo->url, filename, f); |
Natanael Copa | c4b45de | 2008-12-02 11:31:34 +0100 | [diff] [blame] | 187 | if (hex == NULL) { |
| 188 | html_status(404, "Not found", 0); |
| 189 | return; |
Natanael Copa | 314d9ea | 2008-11-29 21:49:07 -0800 | [diff] [blame] | 190 | } |
Natanael Copa | c4b45de | 2008-12-02 11:31:34 +0100 | [diff] [blame] | 191 | prefix = xstrdup(filename); |
| 192 | prefix[strlen(filename) - strlen(f->suffix)] = '\0'; |
Natanael Copa | 314d9ea | 2008-11-29 21:49:07 -0800 | [diff] [blame] | 193 | } |
Lars Hjemli | ed7ff09 | 2008-10-11 20:09:42 +0200 | [diff] [blame] | 194 | |
| 195 | if (!hex) |
| 196 | hex = head; |
| 197 | |
Natanael Copa | 314d9ea | 2008-11-29 21:49:07 -0800 | [diff] [blame] | 198 | if (!prefix) |
| 199 | prefix = xstrdup(cgit_repobasename(ctx.repo->url)); |
| 200 | |
Lars Hjemli | ed7ff09 | 2008-10-11 20:09:42 +0200 | [diff] [blame] | 201 | make_snapshot(f, hex, prefix, filename); |
Natanael Copa | 314d9ea | 2008-11-29 21:49:07 -0800 | [diff] [blame] | 202 | free(prefix); |
Michael Krelin | f97c707 | 2007-07-18 14:40:03 +0200 | [diff] [blame] | 203 | } |