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)
11 #include "ui-snapshot.h"
13 #include "ui-shared.h"
15 static int write_archive_type(const char *format
, const char *hex
, const char *prefix
)
17 struct argv_array argv
= ARGV_ARRAY_INIT
;
19 char *user_home
, *xdg_home
;
21 argv_array_push(&argv
, "snapshot");
22 argv_array_push(&argv
, format
);
24 struct strbuf buf
= STRBUF_INIT
;
25 strbuf_addstr(&buf
, prefix
);
26 strbuf_addch(&buf
, '/');
27 argv_array_push(&argv
, "--prefix");
28 argv_array_push(&argv
, buf
.buf
);
31 argv_array_push(&argv
, hex
);
33 * Now we need to copy the pointers to arguments into a new
34 * structure because write_archive will rearrange its arguments
35 * which may result in duplicated/missing entries causing leaks
36 * or double-frees in argv_array_clear.
38 nargv
= xmalloc(sizeof(char *) * (argv
.argc
+ 1));
39 /* argv_array guarantees a trailing NULL entry. */
40 memcpy(nargv
, argv
.argv
, sizeof(char *) * (argv
.argc
+ 1));
42 user_home
= getenv("HOME");
43 xdg_home
= getenv("XDG_CONFIG_HOME");
45 unsetenv("XDG_CONFIG_HOME");
46 result
= write_archive(argv
.argc
, nargv
, NULL
, 1, NULL
, 0);
48 setenv("HOME", user_home
, 1);
50 setenv("XDG_CONFIG_HOME", xdg_home
, 1);
51 argv_array_clear(&argv
);
56 static int write_tar_archive(const char *hex
, const char *prefix
)
58 return write_archive_type("--format=tar", hex
, prefix
);
61 static int write_zip_archive(const char *hex
, const char *prefix
)
63 return write_archive_type("--format=zip", hex
, prefix
);
66 static int write_compressed_tar_archive(const char *hex
,
73 f
.cmd
= filter_argv
[0];
76 rv
= write_tar_archive(hex
, prefix
);
77 cgit_close_filter(&f
);
81 static int write_tar_gzip_archive(const char *hex
, const char *prefix
)
83 char *argv
[] = { "gzip", "-n", NULL
};
84 return write_compressed_tar_archive(hex
, prefix
, argv
);
87 static int write_tar_bzip2_archive(const char *hex
, const char *prefix
)
89 char *argv
[] = { "bzip2", NULL
};
90 return write_compressed_tar_archive(hex
, prefix
, argv
);
93 static int write_tar_xz_archive(const char *hex
, const char *prefix
)
95 char *argv
[] = { "xz", NULL
};
96 return write_compressed_tar_archive(hex
, prefix
, argv
);
99 const struct cgit_snapshot_format cgit_snapshot_formats
[] = {
100 { ".zip", "application/x-zip", write_zip_archive
, 0x01 },
101 { ".tar.gz", "application/x-gzip", write_tar_gzip_archive
, 0x02 },
102 { ".tar.bz2", "application/x-bzip2", write_tar_bzip2_archive
, 0x04 },
103 { ".tar", "application/x-tar", write_tar_archive
, 0x08 },
104 { ".tar.xz", "application/x-xz", write_tar_xz_archive
, 0x10 },
108 static const struct cgit_snapshot_format
*get_format(const char *filename
)
110 const struct cgit_snapshot_format
*fmt
;
113 fl
= strlen(filename
);
114 for (fmt
= cgit_snapshot_formats
; fmt
->suffix
; fmt
++) {
115 sl
= strlen(fmt
->suffix
);
118 if (!strcmp(fmt
->suffix
, filename
+ fl
- sl
))
124 static int make_snapshot(const struct cgit_snapshot_format
*format
,
125 const char *hex
, const char *prefix
,
126 const char *filename
)
128 unsigned char sha1
[20];
130 if (get_sha1(hex
, sha1
)) {
131 cgit_print_error("Bad object id: %s", hex
);
134 if (!lookup_commit_reference(sha1
)) {
135 cgit_print_error("Not a commit reference: %s", hex
);
138 ctx
.page
.mimetype
= xstrdup(format
->mimetype
);
139 ctx
.page
.filename
= xstrdup(filename
);
140 cgit_print_http_headers(&ctx
);
141 format
->write_func(hex
, prefix
);
145 /* Try to guess the requested revision from the requested snapshot name.
146 * First the format extension is stripped, e.g. "cgit-0.7.2.tar.gz" become
147 * "cgit-0.7.2". If this is a valid commit object name we've got a winner.
148 * Otherwise, if the snapshot name has a prefix matching the result from
149 * repo_basename(), we strip the basename and any following '-' and '_'
150 * characters ("cgit-0.7.2" -> "0.7.2") and check the resulting name once
151 * more. If this still isn't a valid commit object name, we check if pre-
152 * pending a 'v' or a 'V' to the remaining snapshot name ("0.7.2" ->
153 * "v0.7.2") gives us something valid.
155 static const char *get_ref_from_filename(const char *url
, const char *filename
,
156 const struct cgit_snapshot_format
*format
)
158 const char *reponame
;
159 unsigned char sha1
[20];
160 struct strbuf snapshot
= STRBUF_INIT
;
163 strbuf_addstr(&snapshot
, filename
);
164 strbuf_setlen(&snapshot
, snapshot
.len
- strlen(format
->suffix
));
166 if (get_sha1(snapshot
.buf
, sha1
) == 0)
169 reponame
= cgit_repobasename(url
);
170 if (prefixcmp(snapshot
.buf
, reponame
) == 0) {
171 const char *new_start
= snapshot
.buf
;
172 new_start
+= strlen(reponame
);
173 while (new_start
&& (*new_start
== '-' || *new_start
== '_'))
175 strbuf_splice(&snapshot
, 0, new_start
- snapshot
.buf
, "", 0);
178 if (get_sha1(snapshot
.buf
, sha1
) == 0)
181 strbuf_insert(&snapshot
, 0, "v", 1);
182 if (get_sha1(snapshot
.buf
, sha1
) == 0)
185 strbuf_splice(&snapshot
, 0, 1, "V", 1);
186 if (get_sha1(snapshot
.buf
, sha1
) == 0)
190 strbuf_release(&snapshot
);
193 return result
? strbuf_detach(&snapshot
, NULL
) : NULL
;
196 __attribute__((format (printf
, 1, 2)))
197 static void show_error(char *fmt
, ...)
201 ctx
.page
.mimetype
= "text/html";
202 cgit_print_http_headers(&ctx
);
203 cgit_print_docstart(&ctx
);
204 cgit_print_pageheader(&ctx
);
206 cgit_vprint_error(fmt
, ap
);
211 void cgit_print_snapshot(const char *head
, const char *hex
,
212 const char *filename
, int snapshots
, int dwim
)
214 const struct cgit_snapshot_format
* f
;
218 show_error("No snapshot name specified");
222 f
= get_format(filename
);
224 show_error("Unsupported snapshot format: %s", filename
);
229 hex
= get_ref_from_filename(ctx
.repo
->url
, filename
, f
);
231 html_status(404, "Not found", 0);
234 prefix
= xstrdup(filename
);
235 prefix
[strlen(filename
) - strlen(f
->suffix
)] = '\0';
242 prefix
= xstrdup(cgit_repobasename(ctx
.repo
->url
));
244 make_snapshot(f
, hex
, prefix
, filename
);