-rw-r--r-- | cgit.h | 8 | ||||
-rw-r--r-- | shared.c | 27 | ||||
-rw-r--r-- | ui-shared.c | 17 | ||||
-rw-r--r-- | ui-snapshot.c | 98 |
4 files changed, 77 insertions, 73 deletions
@@ -177,10 +177,18 @@ struct cgit_context { | |||
177 | struct cgit_page page; | 177 | struct cgit_page page; |
178 | }; | 178 | }; |
179 | 179 | ||
180 | struct cgit_snapshot_format { | ||
181 | const char *suffix; | ||
182 | const char *mimetype; | ||
183 | write_archive_fn_t write_func; | ||
184 | int bit; | ||
185 | }; | ||
186 | |||
180 | extern const char *cgit_version; | 187 | extern const char *cgit_version; |
181 | 188 | ||
182 | extern struct cgit_repolist cgit_repolist; | 189 | extern struct cgit_repolist cgit_repolist; |
183 | extern struct cgit_context ctx; | 190 | extern struct cgit_context ctx; |
191 | extern const struct cgit_snapshot_format cgit_snapshot_formats[]; | ||
184 | extern int cgit_cmd; | 192 | extern int cgit_cmd; |
185 | 193 | ||
186 | extern void cgit_prepare_context(struct cgit_context *ctx); | 194 | extern void cgit_prepare_context(struct cgit_context *ctx); |
@@ -479,3 +479,30 @@ void cgit_diff_commit(struct commit *commit, filepair_fn fn) | |||
479 | old_sha1 = commit->parents->item->object.sha1; | 479 | old_sha1 = commit->parents->item->object.sha1; |
480 | cgit_diff_tree(old_sha1, commit->object.sha1, fn, NULL); | 480 | cgit_diff_tree(old_sha1, commit->object.sha1, fn, NULL); |
481 | } | 481 | } |
482 | |||
483 | int cgit_parse_snapshots_mask(const char *str) | ||
484 | { | ||
485 | const struct cgit_snapshot_format *f; | ||
486 | static const char *delim = " \t,:/|;"; | ||
487 | int tl, sl, rv = 0; | ||
488 | |||
489 | /* favor legacy setting */ | ||
490 | if(atoi(str)) | ||
491 | return 1; | ||
492 | for(;;) { | ||
493 | str += strspn(str,delim); | ||
494 | tl = strcspn(str,delim); | ||
495 | if (!tl) | ||
496 | break; | ||
497 | for (f = cgit_snapshot_formats; f->suffix; f++) { | ||
498 | sl = strlen(f->suffix); | ||
499 | if((tl == sl && !strncmp(f->suffix, str, tl)) || | ||
500 | (tl == sl-1 && !strncmp(f->suffix+1, str, tl-1))) { | ||
501 | rv |= f->bit; | ||
502 | break; | ||
503 | } | ||
504 | } | ||
505 | str += tl; | ||
506 | } | ||
507 | return rv; | ||
508 | } | ||
diff --git a/ui-shared.c b/ui-shared.c index 2596023..aa65988 100644 --- a/ui-shared.c +++ b/ui-shared.c | |||
@@ -573,4 +573,19 @@ void cgit_print_filemode(unsigned short mode) | |||
573 | html_fileperm(mode); | 573 | html_fileperm(mode); |
574 | } | 574 | } |
575 | 575 | ||
576 | /* vim:set sw=8: */ | 576 | void cgit_print_snapshot_links(const char *repo, const char *head, |
577 | const char *hex, int snapshots) | ||
578 | { | ||
579 | const struct cgit_snapshot_format* f; | ||
580 | char *filename; | ||
581 | |||
582 | for (f = cgit_snapshot_formats; f->suffix; f++) { | ||
583 | if (!(snapshots & f->bit)) | ||
584 | continue; | ||
585 | filename = fmt("%s-%s%s", cgit_repobasename(repo), hex, | ||
586 | f->suffix); | ||
587 | cgit_snapshot_link(filename, NULL, NULL, (char *)head, | ||
588 | (char *)hex, filename); | ||
589 | html("<br/>"); | ||
590 | } | ||
591 | } | ||
diff --git a/ui-snapshot.c b/ui-snapshot.c index c741469..512fcd2 100644 --- a/ui-snapshot.c +++ b/ui-snapshot.c | |||
@@ -55,105 +55,59 @@ static int write_tar_bzip2_archive(struct archiver_args *args) | |||
55 | return write_compressed_tar_archive(args,"bzip2"); | 55 | return write_compressed_tar_archive(args,"bzip2"); |
56 | } | 56 | } |
57 | 57 | ||
58 | static const struct snapshot_archive_t { | 58 | const struct cgit_snapshot_format cgit_snapshot_formats[] = { |
59 | const char *suffix; | ||
60 | const char *mimetype; | ||
61 | write_archive_fn_t write_func; | ||
62 | int bit; | ||
63 | }snapshot_archives[] = { | ||
64 | { ".zip", "application/x-zip", write_zip_archive, 0x1 }, | 59 | { ".zip", "application/x-zip", write_zip_archive, 0x1 }, |
65 | { ".tar.gz", "application/x-tar", write_tar_gzip_archive, 0x2 }, | 60 | { ".tar.gz", "application/x-tar", write_tar_gzip_archive, 0x2 }, |
66 | { ".tar.bz2", "application/x-tar", write_tar_bzip2_archive, 0x4 }, | 61 | { ".tar.bz2", "application/x-tar", write_tar_bzip2_archive, 0x4 }, |
67 | { ".tar", "application/x-tar", write_tar_archive, 0x8 } | 62 | { ".tar", "application/x-tar", write_tar_archive, 0x8 }, |
63 | {} | ||
68 | }; | 64 | }; |
69 | 65 | ||
70 | #define snapshot_archives_len (sizeof(snapshot_archives) / sizeof(*snapshot_archives)) | 66 | static int make_snapshot(const struct cgit_snapshot_format *format, |
71 | 67 | const char *hex, const char *prefix, | |
72 | void cgit_print_snapshot(const char *head, const char *hex, const char *prefix, | 68 | const char *filename) |
73 | const char *filename, int snapshots) | ||
74 | { | 69 | { |
75 | const struct snapshot_archive_t* sat; | ||
76 | struct archiver_args args; | 70 | struct archiver_args args; |
77 | struct commit *commit; | 71 | struct commit *commit; |
78 | unsigned char sha1[20]; | 72 | unsigned char sha1[20]; |
79 | int f, sl, fnl = strlen(filename); | ||
80 | 73 | ||
81 | for(f=0; f<snapshot_archives_len; f++) { | ||
82 | sat = &snapshot_archives[f]; | ||
83 | if(!(snapshots & sat->bit)) | ||
84 | continue; | ||
85 | sl = strlen(sat->suffix); | ||
86 | if(fnl<sl || strcmp(&filename[fnl-sl],sat->suffix)) | ||
87 | continue; | ||
88 | if (!hex) | ||
89 | hex = head; | ||
90 | if(get_sha1(hex, sha1)) { | 74 | if(get_sha1(hex, sha1)) { |
91 | cgit_print_error(fmt("Bad object id: %s", hex)); | 75 | cgit_print_error(fmt("Bad object id: %s", hex)); |
92 | return; | 76 | return 1; |
93 | } | 77 | } |
94 | commit = lookup_commit_reference(sha1); | 78 | commit = lookup_commit_reference(sha1); |
95 | if(!commit) { | 79 | if(!commit) { |
96 | cgit_print_error(fmt("Not a commit reference: %s", hex)); | 80 | cgit_print_error(fmt("Not a commit reference: %s", hex)); |
97 | return;; | 81 | return 1; |
98 | } | 82 | } |
99 | memset(&args,0,sizeof(args)); | 83 | memset(&args,0,sizeof(args)); |
100 | args.base = fmt("%s/", prefix); | 84 | args.base = fmt("%s/", prefix); |
101 | args.tree = commit->tree; | 85 | args.tree = commit->tree; |
102 | args.time = commit->date; | 86 | args.time = commit->date; |
103 | ctx.page.mimetype = xstrdup(sat->mimetype); | 87 | ctx.page.mimetype = xstrdup(format->mimetype); |
104 | ctx.page.filename = xstrdup(filename); | 88 | ctx.page.filename = xstrdup(filename); |
105 | cgit_print_http_headers(&ctx); | 89 | cgit_print_http_headers(&ctx); |
106 | (*sat->write_func)(&args); | 90 | format->write_func(&args); |
107 | return; | 91 | return 0; |
108 | } | ||
109 | cgit_print_error(fmt("Unsupported snapshot format: %s", filename)); | ||
110 | } | 92 | } |
111 | 93 | ||
112 | void cgit_print_snapshot_links(const char *repo, const char *head, | 94 | void cgit_print_snapshot(const char *head, const char *hex, const char *prefix, |
113 | const char *hex, int snapshots) | 95 | const char *filename, int snapshots) |
114 | { | 96 | { |
115 | const struct snapshot_archive_t* sat; | 97 | const struct cgit_snapshot_format* f; |
116 | char *filename; | 98 | int sl, fnl; |
117 | int f; | ||
118 | 99 | ||
119 | for(f=0; f<snapshot_archives_len; f++) { | 100 | fnl = strlen(filename); |
120 | sat = &snapshot_archives[f]; | 101 | if (!hex) |
121 | if(!(snapshots & sat->bit)) | 102 | hex = head; |
103 | for (f = cgit_snapshot_formats; f->suffix; f++) { | ||
104 | if (!(snapshots & f->bit)) | ||
122 | continue; | 105 | continue; |
123 | filename = fmt("%s-%s%s", cgit_repobasename(repo), hex, | 106 | sl = strlen(f->suffix); |
124 | sat->suffix); | 107 | if(fnl < sl || strcmp(&filename[fnl-sl], f->suffix)) |
125 | cgit_snapshot_link(filename, NULL, NULL, (char *)head, | 108 | continue; |
126 | (char *)hex, filename); | 109 | make_snapshot(f, hex, prefix, filename); |
127 | html("<br/>"); | 110 | return; |
128 | } | ||
129 | } | ||
130 | |||
131 | int cgit_parse_snapshots_mask(const char *str) | ||
132 | { | ||
133 | const struct snapshot_archive_t* sat; | ||
134 | static const char *delim = " \t,:/|;"; | ||
135 | int f, tl, sl, rv = 0; | ||
136 | |||
137 | /* favor legacy setting */ | ||
138 | if(atoi(str)) | ||
139 | return 1; | ||
140 | for(;;) { | ||
141 | str += strspn(str,delim); | ||
142 | tl = strcspn(str,delim); | ||
143 | if(!tl) | ||
144 | break; | ||
145 | for(f=0; f<snapshot_archives_len; f++) { | ||
146 | sat = &snapshot_archives[f]; | ||
147 | sl = strlen(sat->suffix); | ||
148 | if((tl == sl && !strncmp(sat->suffix, str, tl)) || | ||
149 | (tl == sl-1 && !strncmp(sat->suffix+1, str, tl-1))) { | ||
150 | rv |= sat->bit; | ||
151 | break; | ||
152 | } | ||
153 | } | ||
154 | str += tl; | ||
155 | } | 111 | } |
156 | return rv; | 112 | cgit_print_error(fmt("Unsupported snapshot format: %s", filename)); |
157 | } | 113 | } |
158 | |||
159 | /* vim:set sw=8: */ | ||