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
, "snapshot");
18 argv_array_push(&argv
, format
);
20 argv_array_push(&argv
, "--prefix");
21 argv_array_push(&argv
, fmt("%s/", prefix
));
23 argv_array_push(&argv
, hex
);
24 return write_archive(argv
.argc
, argv
.argv
, NULL
, 1, NULL
, 0);
27 static int write_tar_archive(const char *hex
, const char *prefix
)
29 return write_archive_type("--format=tar", hex
, prefix
);
32 static int write_zip_archive(const char *hex
, const char *prefix
)
34 return write_archive_type("--format=zip", hex
, prefix
);
37 static int write_compressed_tar_archive(const char *hex
,
44 f
.cmd
= filter_argv
[0];
47 rv
= write_tar_archive(hex
, prefix
);
48 cgit_close_filter(&f
);
52 static int write_tar_gzip_archive(const char *hex
, const char *prefix
)
54 char *argv
[] = { "gzip", "-n", NULL
};
55 return write_compressed_tar_archive(hex
, prefix
, argv
);
58 static int write_tar_bzip2_archive(const char *hex
, const char *prefix
)
60 char *argv
[] = { "bzip2", NULL
};
61 return write_compressed_tar_archive(hex
, prefix
, argv
);
64 static int write_tar_xz_archive(const char *hex
, const char *prefix
)
66 char *argv
[] = { "xz", NULL
};
67 return write_compressed_tar_archive(hex
, prefix
, argv
);
70 const struct cgit_snapshot_format cgit_snapshot_formats
[] = {
71 { ".zip", "application/x-zip", write_zip_archive
, 0x01 },
72 { ".tar.gz", "application/x-gzip", write_tar_gzip_archive
, 0x02 },
73 { ".tar.bz2", "application/x-bzip2", write_tar_bzip2_archive
, 0x04 },
74 { ".tar", "application/x-tar", write_tar_archive
, 0x08 },
75 { ".tar.xz", "application/x-xz", write_tar_xz_archive
, 0x10 },
79 static const struct cgit_snapshot_format
*get_format(const char *filename
)
81 const struct cgit_snapshot_format
*fmt
;
84 fl
= strlen(filename
);
85 for (fmt
= cgit_snapshot_formats
; fmt
->suffix
; fmt
++) {
86 sl
= strlen(fmt
->suffix
);
89 if (!strcmp(fmt
->suffix
, filename
+ fl
- sl
))
95 static int make_snapshot(const struct cgit_snapshot_format
*format
,
96 const char *hex
, const char *prefix
,
99 unsigned char sha1
[20];
101 if (get_sha1(hex
, sha1
)) {
102 cgit_print_error(fmt("Bad object id: %s", hex
));
105 if (!lookup_commit_reference(sha1
)) {
106 cgit_print_error(fmt("Not a commit reference: %s", hex
));
109 ctx
.page
.mimetype
= xstrdup(format
->mimetype
);
110 ctx
.page
.filename
= xstrdup(filename
);
111 cgit_print_http_headers(&ctx
);
112 format
->write_func(hex
, prefix
);
116 /* Try to guess the requested revision from the requested snapshot name.
117 * First the format extension is stripped, e.g. "cgit-0.7.2.tar.gz" become
118 * "cgit-0.7.2". If this is a valid commit object name we've got a winner.
119 * Otherwise, if the snapshot name has a prefix matching the result from
120 * repo_basename(), we strip the basename and any following '-' and '_'
121 * characters ("cgit-0.7.2" -> "0.7.2") and check the resulting name once
122 * more. If this still isn't a valid commit object name, we check if pre-
123 * pending a 'v' to the remaining snapshot name ("0.7.2" -> "v0.7.2") gives
124 * us something valid.
126 static const char *get_ref_from_filename(const char *url
, const char *filename
,
127 const struct cgit_snapshot_format
*format
)
129 const char *reponame
;
130 unsigned char sha1
[20];
133 snapshot
= xstrdup(filename
);
134 snapshot
[strlen(snapshot
) - strlen(format
->suffix
)] = '\0';
136 if (get_sha1(snapshot
, sha1
) == 0)
139 reponame
= cgit_repobasename(url
);
140 if (prefixcmp(snapshot
, reponame
) == 0) {
141 snapshot
+= strlen(reponame
);
142 while (snapshot
&& (*snapshot
== '-' || *snapshot
== '_'))
146 if (get_sha1(snapshot
, sha1
) == 0)
149 snapshot
= fmt("v%s", snapshot
);
150 if (get_sha1(snapshot
, sha1
) == 0)
156 static void show_error(char *msg
)
158 ctx
.page
.mimetype
= "text/html";
159 cgit_print_http_headers(&ctx
);
160 cgit_print_docstart(&ctx
);
161 cgit_print_pageheader(&ctx
);
162 cgit_print_error(msg
);
166 void cgit_print_snapshot(const char *head
, const char *hex
,
167 const char *filename
, int snapshots
, int dwim
)
169 const struct cgit_snapshot_format
* f
;
173 show_error("No snapshot name specified");
177 f
= get_format(filename
);
179 show_error(xstrdup(fmt("Unsupported snapshot format: %s",
185 hex
= get_ref_from_filename(ctx
.repo
->url
, filename
, f
);
187 html_status(404, "Not found", 0);
190 prefix
= xstrdup(filename
);
191 prefix
[strlen(filename
) - strlen(f
->suffix
)] = '\0';
198 prefix
= xstrdup(cgit_repobasename(ctx
.repo
->url
));
200 make_snapshot(f
, hex
, prefix
, filename
);