]> gitweb.ps.run Git - ps-cgit/blob - ui-snapshot.c
add support for snapshot tarballs
[ps-cgit] / ui-snapshot.c
1 /* ui-snapshot.c: generate snapshot of a commit
2  *
3  * Copyright (C) 2006 Lars Hjemli
4  *
5  * Licensed under GNU General Public License v2
6  *   (see COPYING for full license text)
7  */
8
9 #include "cgit.h"
10
11 static const struct snapshot_archive_t {
12         const char *suffix;
13         const char *mimetype;
14         write_archive_fn_t write_func;
15 }       snapshot_archives[] = {
16         { ".zip", "application/x-zip", write_zip_archive },
17         { ".tar.gz", "application/x-gzip", write_tar_archive }
18 };
19
20 void cgit_print_snapshot(struct cacheitem *item, const char *hex, 
21                          const char *prefix, const char *filename)
22 {
23         int fnl = strlen(filename);
24         int f;
25         for(f=0;f<(sizeof(snapshot_archives)/sizeof(*snapshot_archives));++f) {
26                 const struct snapshot_archive_t* sat = &snapshot_archives[f];
27                 int sl = strlen(sat->suffix);
28                 if(fnl<sl || strcmp(&filename[fnl-sl],sat->suffix))
29                         continue;
30
31                 struct archiver_args args;
32                 struct commit *commit;
33                 unsigned char sha1[20];
34
35                 if(get_sha1(hex, sha1)) {
36                         cgit_print_error(fmt("Bad object id: %s", hex));
37                         return;
38                 }
39                 commit = lookup_commit_reference(sha1);
40
41                 if(!commit) {
42                         cgit_print_error(fmt("Not a commit reference: %s", hex));
43                         return;;
44                 }
45
46                 memset(&args,0,sizeof(args));
47                 args.base = fmt("%s/", prefix);
48                 args.tree = commit->tree;
49
50                 cgit_print_snapshot_start(sat->mimetype, filename, item);
51                 (*sat->write_func)(&args);
52                 return;
53         }
54         cgit_print_error(fmt("Unsupported snapshot format: %s", filename));
55 }
56
57 void cgit_print_snapshot_links(const char *repo,const char *hex)
58 {
59         char *filename;
60         int f;
61         for(f=0;f<(sizeof(snapshot_archives)/sizeof(*snapshot_archives));++f) {
62                 const struct snapshot_archive_t* sat = &snapshot_archives[f];
63                 filename = fmt("%s-%s%s",repo,hex,sat->suffix);
64                 htmlf("<a href='%s'>%s</a><br/>",
65                         cgit_pageurl(repo,"snapshot",
66                             fmt("id=%s&amp;name=%s",hex,filename)), filename);
67         }
68 }