05d292b2 (kx 2023-03-24 03:51:10 +0300 1)
05d292b2 (kx 2023-03-24 03:51:10 +0300 2) #ifdef HAVE_CONFIG_H
05d292b2 (kx 2023-03-24 03:51:10 +0300 3) #include <config.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 4) #endif
05d292b2 (kx 2023-03-24 03:51:10 +0300 5)
05d292b2 (kx 2023-03-24 03:51:10 +0300 6) #include <stdlib.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 7) #include <stdio.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 8) #include <sys/sysinfo.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 9) #include <sys/types.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 10) #ifdef HAVE_INTTYPES_H
05d292b2 (kx 2023-03-24 03:51:10 +0300 11) #include <inttypes.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 12) #else
05d292b2 (kx 2023-03-24 03:51:10 +0300 13) #include <stdint.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 14) #endif
05d292b2 (kx 2023-03-24 03:51:10 +0300 15) #include <stddef.h> /* offsetof(3) */
05d292b2 (kx 2023-03-24 03:51:10 +0300 16) #include <dirent.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 17) #include <sys/stat.h> /* chmod(2) */
05d292b2 (kx 2023-03-24 03:51:10 +0300 18) #include <sys/file.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 19) #include <sys/mman.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 20) #include <fcntl.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 21) #include <limits.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 22) #include <string.h> /* strdup(3) */
05d292b2 (kx 2023-03-24 03:51:10 +0300 23) #include <libgen.h> /* basename(3) */
05d292b2 (kx 2023-03-24 03:51:10 +0300 24) #include <ctype.h> /* tolower(3) */
05d292b2 (kx 2023-03-24 03:51:10 +0300 25) #include <errno.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 26) #include <time.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 27) #include <sys/time.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 28) #include <pwd.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 29) #include <grp.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 30) #include <stdarg.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 31) #include <locale.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 32) #include <unistd.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 33)
05d292b2 (kx 2023-03-24 03:51:10 +0300 34) #include <md4c.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 35) #include <md4c-html.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 36)
05d292b2 (kx 2023-03-24 03:51:10 +0300 37) #include <git2.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 38)
05d292b2 (kx 2023-03-24 03:51:10 +0300 39) #include <nls.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 40)
05d292b2 (kx 2023-03-24 03:51:10 +0300 41) #include <defs.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 42)
05d292b2 (kx 2023-03-24 03:51:10 +0300 43) #include <fatal.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 44) #include <http.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 45) #include <html.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 46)
05d292b2 (kx 2023-03-24 03:51:10 +0300 47) #include <dlist.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 48) #include <strbuf.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 49) #include <repolist.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 50) #include <wrapper.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 51) #include <system.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 52) #include <date.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 53)
05d292b2 (kx 2023-03-24 03:51:10 +0300 54) #include <ctx.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 55) #include <git-shared.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 56) #include <ui-shared.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300 57)
05d292b2 (kx 2023-03-24 03:51:10 +0300 58)
05d292b2 (kx 2023-03-24 03:51:10 +0300 59) static void cgit_print_file_links( struct strbuf *sb, const char *relative_path, const char *revision, int top )
05d292b2 (kx 2023-03-24 03:51:10 +0300 60) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 61) const char *query_string = NULL;
05d292b2 (kx 2023-03-24 03:51:10 +0300 62) const char *place = NULL;
05d292b2 (kx 2023-03-24 03:51:10 +0300 63)
05d292b2 (kx 2023-03-24 03:51:10 +0300 64) if( !sb || !relative_path ) return;
05d292b2 (kx 2023-03-24 03:51:10 +0300 65)
05d292b2 (kx 2023-03-24 03:51:10 +0300 66) if( top )
05d292b2 (kx 2023-03-24 03:51:10 +0300 67) place = "top";
05d292b2 (kx 2023-03-24 03:51:10 +0300 68) else
05d292b2 (kx 2023-03-24 03:51:10 +0300 69) place = "bottom";
05d292b2 (kx 2023-03-24 03:51:10 +0300 70)
05d292b2 (kx 2023-03-24 03:51:10 +0300 71) strbuf_addf( sb, "<div class=\"%s file-links-menu\">\n", place );
05d292b2 (kx 2023-03-24 03:51:10 +0300 72) strbuf_addf( sb, " <div class=\"right links-menu\">\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 73)
05d292b2 (kx 2023-03-24 03:51:10 +0300 74) query_string = ctx_remove_query_param( ctx.env.query_string, "rev" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 75) query_string = ctx_remove_query_param( query_string, "op" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 76)
05d292b2 (kx 2023-03-24 03:51:10 +0300 77) if( query_string && *query_string )
05d292b2 (kx 2023-03-24 03:51:10 +0300 78) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 79) strbuf_addf( sb, " <div class=\"item\"><span class=\"icon las la-scroll\"></span><a href=\"/%s/%s/?op=log&rev=%s&%s\">log</a></div>\n", ctx.repo.name, relative_path, revision, query_string );
05d292b2 (kx 2023-03-24 03:51:10 +0300 80) strbuf_addf( sb, " <div class=\"item\"><span class=\"icon las la-diff\"></span><a href=\"/%s/%s/?op=diff&rev=%s&%s\">diff</a></div>\n", ctx.repo.name, relative_path, revision, query_string );
05d292b2 (kx 2023-03-24 03:51:10 +0300 81) strbuf_addf( sb, " <div class=\"item\"><span class=\"icon las la-blame\"></span><a href=\"/%s/%s/?op=blame&rev=%s&%s\">blame</a></div>\n", ctx.repo.name, relative_path, revision, query_string );
05d292b2 (kx 2023-03-24 03:51:10 +0300 82) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 83) else
05d292b2 (kx 2023-03-24 03:51:10 +0300 84) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 85) strbuf_addf( sb, " <div class=\"item\"><span class=\"icon las la-scroll\"></span><a href=\"/%s/%s/?op=log&rev=%s\">log</a></div>\n", ctx.repo.name, relative_path, revision );
05d292b2 (kx 2023-03-24 03:51:10 +0300 86) strbuf_addf( sb, " <div class=\"item\"><span class=\"icon las la-diff\"></span><a href=\"/%s/%s/?op=diff&rev=%s\">diff</a></div>\n", ctx.repo.name, relative_path, revision );
05d292b2 (kx 2023-03-24 03:51:10 +0300 87) strbuf_addf( sb, " <div class=\"item\"><span class=\"icon las la-blame\"></span><a href=\"/%s/%s/?op=blame&rev=%s\">blame</a></div>\n", ctx.repo.name, relative_path, revision );
05d292b2 (kx 2023-03-24 03:51:10 +0300 88) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 89)
05d292b2 (kx 2023-03-24 03:51:10 +0300 90) strbuf_addf( sb, " </div>\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 91) strbuf_addf( sb, "</div>\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 92) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 93)
05d292b2 (kx 2023-03-24 03:51:10 +0300 94)
05d292b2 (kx 2023-03-24 03:51:10 +0300 95) static void process_markdown_output( const MD_CHAR *text, MD_SIZE size, void *sb )
05d292b2 (kx 2023-03-24 03:51:10 +0300 96) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 97) strbuf_add( (struct strbuf *)sb, (const void *)text, (size_t)size );
05d292b2 (kx 2023-03-24 03:51:10 +0300 98) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 99)
05d292b2 (kx 2023-03-24 03:51:10 +0300 100) static int cgit_write_markdown_content( struct strbuf *sb, const struct strbuf *input )
05d292b2 (kx 2023-03-24 03:51:10 +0300 101) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 102) unsigned parser_flags = MD_DIALECT_GITHUB; /* | MD_DIALECT_COMMONMARK */
05d292b2 (kx 2023-03-24 03:51:10 +0300 103) unsigned renderer_flags = MD_FLAG_WIKILINKS; /* | MD_HTML_FLAG_DEBUG */
05d292b2 (kx 2023-03-24 03:51:10 +0300 104)
05d292b2 (kx 2023-03-24 03:51:10 +0300 105) /*********************************
05d292b2 (kx 2023-03-24 03:51:10 +0300 106) md_html() returns 0 on success:
05d292b2 (kx 2023-03-24 03:51:10 +0300 107) */
05d292b2 (kx 2023-03-24 03:51:10 +0300 108) return md_html( input->buf, input->len, process_markdown_output,
05d292b2 (kx 2023-03-24 03:51:10 +0300 109) (void *)sb, parser_flags, renderer_flags );
05d292b2 (kx 2023-03-24 03:51:10 +0300 110) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 111)
05d292b2 (kx 2023-03-24 03:51:10 +0300 112)
05d292b2 (kx 2023-03-24 03:51:10 +0300 113) static void cgit_print_file( struct strbuf *sb, const char *relative_path, const char *revision )
05d292b2 (kx 2023-03-24 03:51:10 +0300 114) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 115) struct cgit_info info;
05d292b2 (kx 2023-03-24 03:51:10 +0300 116)
05d292b2 (kx 2023-03-24 03:51:10 +0300 117) if( !sb || !relative_path ) return;
05d292b2 (kx 2023-03-24 03:51:10 +0300 118)
05d292b2 (kx 2023-03-24 03:51:10 +0300 119) if( revision && strcmp( revision, (const char *)&ctx.repo.relative_info.revision[0] ) )
05d292b2 (kx 2023-03-24 03:51:10 +0300 120) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 121) cgit_rpath_info( &info, relative_path, revision );
05d292b2 (kx 2023-03-24 03:51:10 +0300 122) if( info.kind != GIT_OBJECT_BLOB )
05d292b2 (kx 2023-03-24 03:51:10 +0300 123) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 124) strbuf_addf( sb, " <h1>Requested resource cannot be shown</h1>\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 125) strbuf_addf( sb, " <p class='leading'>File with specified revision '%s' cannot be shown.</p>\n", revision );
05d292b2 (kx 2023-03-24 03:51:10 +0300 126) return;
05d292b2 (kx 2023-03-24 03:51:10 +0300 127) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 128) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 129) else
05d292b2 (kx 2023-03-24 03:51:10 +0300 130) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 131) memcpy( (void *)&info, (void *)&ctx.repo.relative_info, sizeof( struct cgit_info ) );
05d292b2 (kx 2023-03-24 03:51:10 +0300 132) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 133)
05d292b2 (kx 2023-03-24 03:51:10 +0300 134) cgit_print_file_links( sb, relative_path, (const char *)&info.revision[0], 1 );
05d292b2 (kx 2023-03-24 03:51:10 +0300 135)
05d292b2 (kx 2023-03-24 03:51:10 +0300 136) if( info.lang )
05d292b2 (kx 2023-03-24 03:51:10 +0300 137) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 138) if( !strcmp( info.lang, "Markdown" ) )
05d292b2 (kx 2023-03-24 03:51:10 +0300 139) strbuf_addstr( sb, "<div class='markdown-content'>" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 140) else
05d292b2 (kx 2023-03-24 03:51:10 +0300 141) strbuf_addf( sb, "<pre><code class='language-%s'>", info.lang );
05d292b2 (kx 2023-03-24 03:51:10 +0300 142) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 143) else
05d292b2 (kx 2023-03-24 03:51:10 +0300 144) strbuf_addstr( sb, "<pre><code class='highlight'>" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 145)
05d292b2 (kx 2023-03-24 03:51:10 +0300 146) if( info.kind == GIT_OBJECT_BLOB )
05d292b2 (kx 2023-03-24 03:51:10 +0300 147) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 148) git_repository *repo = NULL;
05d292b2 (kx 2023-03-24 03:51:10 +0300 149) git_blob *blob = NULL;
05d292b2 (kx 2023-03-24 03:51:10 +0300 150) git_oid oid;
05d292b2 (kx 2023-03-24 03:51:10 +0300 151) /* git_off_t rawsize = 0; */
05d292b2 (kx 2023-03-24 03:51:10 +0300 152) const char *rawcontent = NULL;
05d292b2 (kx 2023-03-24 03:51:10 +0300 153)
05d292b2 (kx 2023-03-24 03:51:10 +0300 154) if( git_oid_fromstr( &oid, (const char *)&info.oid[0] ) < 0 )
05d292b2 (kx 2023-03-24 03:51:10 +0300 155) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 156) if( info.lang && !strcmp( info.lang, "Markdown" ) )
05d292b2 (kx 2023-03-24 03:51:10 +0300 157) strbuf_addstr( sb, "\n</div> <!-- End of Markdown Content -->\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 158) else
05d292b2 (kx 2023-03-24 03:51:10 +0300 159) strbuf_addstr( sb, "\n</code></pre>\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 160)
05d292b2 (kx 2023-03-24 03:51:10 +0300 161) cgit_print_file_links( sb, relative_path, (const char *)&info.revision[0], 0 );
05d292b2 (kx 2023-03-24 03:51:10 +0300 162) return;
05d292b2 (kx 2023-03-24 03:51:10 +0300 163) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 164)
05d292b2 (kx 2023-03-24 03:51:10 +0300 165) if( !(repo = cgit_open_repository()) )
05d292b2 (kx 2023-03-24 03:51:10 +0300 166) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 167) if( info.lang && !strcmp( info.lang, "Markdown" ) )
05d292b2 (kx 2023-03-24 03:51:10 +0300 168) strbuf_addstr( sb, "\n</div> <!-- End of Markdown Content -->\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 169) else
05d292b2 (kx 2023-03-24 03:51:10 +0300 170) strbuf_addstr( sb, "\n</code></pre>\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 171)
05d292b2 (kx 2023-03-24 03:51:10 +0300 172) cgit_print_file_links( sb, relative_path, (const char *)&info.revision[0], 0 );
05d292b2 (kx 2023-03-24 03:51:10 +0300 173) return;
05d292b2 (kx 2023-03-24 03:51:10 +0300 174) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 175)
05d292b2 (kx 2023-03-24 03:51:10 +0300 176) if( git_blob_lookup( &blob, repo, &oid ) < 0 )
05d292b2 (kx 2023-03-24 03:51:10 +0300 177) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 178) close_repository( repo );
05d292b2 (kx 2023-03-24 03:51:10 +0300 179)
05d292b2 (kx 2023-03-24 03:51:10 +0300 180) if( info.lang && !strcmp( info.lang, "Markdown" ) )
05d292b2 (kx 2023-03-24 03:51:10 +0300 181) strbuf_addstr( sb, "\n</div> <!-- End of Markdown Content -->\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 182) else
05d292b2 (kx 2023-03-24 03:51:10 +0300 183) strbuf_addstr( sb, "\n</code></pre>\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 184)
05d292b2 (kx 2023-03-24 03:51:10 +0300 185) cgit_print_file_links( sb, relative_path, (const char *)&info.revision[0], 0 );
05d292b2 (kx 2023-03-24 03:51:10 +0300 186) return;
05d292b2 (kx 2023-03-24 03:51:10 +0300 187) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 188)
05d292b2 (kx 2023-03-24 03:51:10 +0300 189) /* rawsize = git_blob_rawsize( blob ); */
05d292b2 (kx 2023-03-24 03:51:10 +0300 190) rawcontent = (const char *)git_blob_rawcontent( blob );
05d292b2 (kx 2023-03-24 03:51:10 +0300 191)
05d292b2 (kx 2023-03-24 03:51:10 +0300 192) if( !git_blob_is_binary( blob ) && rawcontent && *rawcontent )
05d292b2 (kx 2023-03-24 03:51:10 +0300 193) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 194) struct strbuf buf = STRBUF_INIT;
05d292b2 (kx 2023-03-24 03:51:10 +0300 195)
05d292b2 (kx 2023-03-24 03:51:10 +0300 196) if( info.lang && !strcmp( info.lang, "Markdown" ) )
05d292b2 (kx 2023-03-24 03:51:10 +0300 197) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 198) strbuf_addstr( &buf, (const char *)rawcontent );
05d292b2 (kx 2023-03-24 03:51:10 +0300 199) (void)cgit_write_markdown_content( sb, (const struct strbuf *)&buf );
05d292b2 (kx 2023-03-24 03:51:10 +0300 200) strbuf_release( &buf );
05d292b2 (kx 2023-03-24 03:51:10 +0300 201) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 202) else
05d292b2 (kx 2023-03-24 03:51:10 +0300 203) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 204) strbuf_addstr_xml_quoted( &buf, (const char *)rawcontent );
05d292b2 (kx 2023-03-24 03:51:10 +0300 205) strbuf_addbuf( sb, (const struct strbuf *)&buf );
05d292b2 (kx 2023-03-24 03:51:10 +0300 206) strbuf_release( &buf );
05d292b2 (kx 2023-03-24 03:51:10 +0300 207) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 208) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 209) else
05d292b2 (kx 2023-03-24 03:51:10 +0300 210) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 211) git_blob_free( blob );
05d292b2 (kx 2023-03-24 03:51:10 +0300 212) close_repository( repo );
05d292b2 (kx 2023-03-24 03:51:10 +0300 213)
05d292b2 (kx 2023-03-24 03:51:10 +0300 214) if( info.lang && !strcmp( info.lang, "Markdown" ) )
05d292b2 (kx 2023-03-24 03:51:10 +0300 215) strbuf_addstr( sb, "\n</div> <!-- End of Markdown Content -->\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 216) else
05d292b2 (kx 2023-03-24 03:51:10 +0300 217) strbuf_addstr( sb, "\n</code></pre>\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 218)
05d292b2 (kx 2023-03-24 03:51:10 +0300 219) cgit_print_file_links( sb, relative_path, (const char *)&info.revision[0], 0 );
05d292b2 (kx 2023-03-24 03:51:10 +0300 220) return;
05d292b2 (kx 2023-03-24 03:51:10 +0300 221) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 222)
05d292b2 (kx 2023-03-24 03:51:10 +0300 223) git_blob_free( blob );
05d292b2 (kx 2023-03-24 03:51:10 +0300 224) close_repository( repo );
05d292b2 (kx 2023-03-24 03:51:10 +0300 225) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 226)
05d292b2 (kx 2023-03-24 03:51:10 +0300 227) if( info.lang && !strcmp( info.lang, "Markdown" ) )
05d292b2 (kx 2023-03-24 03:51:10 +0300 228) strbuf_addstr( sb, "\n</div> <!-- End of Markdown Content -->\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 229) else
05d292b2 (kx 2023-03-24 03:51:10 +0300 230) strbuf_addstr( sb, "\n</code></pre>\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 231)
05d292b2 (kx 2023-03-24 03:51:10 +0300 232) cgit_print_file_links( sb, relative_path, (const char *)&info.revision[0], 0 );
05d292b2 (kx 2023-03-24 03:51:10 +0300 233)
05d292b2 (kx 2023-03-24 03:51:10 +0300 234) return;
05d292b2 (kx 2023-03-24 03:51:10 +0300 235) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 236)
05d292b2 (kx 2023-03-24 03:51:10 +0300 237) static void cgit_print_image_file( struct strbuf *sb, const char *relative_path, const char *revision )
05d292b2 (kx 2023-03-24 03:51:10 +0300 238) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 239) struct cgit_info info;
05d292b2 (kx 2023-03-24 03:51:10 +0300 240)
05d292b2 (kx 2023-03-24 03:51:10 +0300 241) if( !sb || !relative_path ) return;
05d292b2 (kx 2023-03-24 03:51:10 +0300 242)
05d292b2 (kx 2023-03-24 03:51:10 +0300 243) if( revision && strcmp( revision, (const char *)&ctx.repo.relative_info.revision[0] ) )
05d292b2 (kx 2023-03-24 03:51:10 +0300 244) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 245) cgit_rpath_info( &info, relative_path, revision );
05d292b2 (kx 2023-03-24 03:51:10 +0300 246) if( info.kind != GIT_OBJECT_BLOB )
05d292b2 (kx 2023-03-24 03:51:10 +0300 247) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 248) strbuf_addf( sb, " <h1>Invalid Revision</h1>\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 249) strbuf_addf( sb, " <p class='leading'>File with specified revision '%s' cannot be shown.</p>\n", revision );
05d292b2 (kx 2023-03-24 03:51:10 +0300 250) return;
05d292b2 (kx 2023-03-24 03:51:10 +0300 251) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 252) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 253) else
05d292b2 (kx 2023-03-24 03:51:10 +0300 254) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 255) memcpy( (void *)&info, (void *)&ctx.repo.relative_info, sizeof( struct cgit_info ) );
05d292b2 (kx 2023-03-24 03:51:10 +0300 256) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 257)
05d292b2 (kx 2023-03-24 03:51:10 +0300 258) if( info.kind == GIT_OBJECT_BLOB )
05d292b2 (kx 2023-03-24 03:51:10 +0300 259) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 260) git_repository *repo = NULL;
05d292b2 (kx 2023-03-24 03:51:10 +0300 261) git_blob *blob = NULL;
05d292b2 (kx 2023-03-24 03:51:10 +0300 262) git_oid oid;
05d292b2 (kx 2023-03-24 03:51:10 +0300 263) git_off_t rawsize = 0;
05d292b2 (kx 2023-03-24 03:51:10 +0300 264) const char *rawcontent = NULL;
05d292b2 (kx 2023-03-24 03:51:10 +0300 265)
05d292b2 (kx 2023-03-24 03:51:10 +0300 266) if( git_oid_fromstr( &oid, (const char *)&info.oid[0] ) < 0 ) return;
05d292b2 (kx 2023-03-24 03:51:10 +0300 267) if( !(repo = cgit_open_repository()) ) return;
05d292b2 (kx 2023-03-24 03:51:10 +0300 268)
05d292b2 (kx 2023-03-24 03:51:10 +0300 269) if( git_blob_lookup( &blob, repo, &oid ) < 0 )
05d292b2 (kx 2023-03-24 03:51:10 +0300 270) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 271) close_repository( repo );
05d292b2 (kx 2023-03-24 03:51:10 +0300 272) return;
05d292b2 (kx 2023-03-24 03:51:10 +0300 273) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 274)
05d292b2 (kx 2023-03-24 03:51:10 +0300 275) rawsize = git_blob_rawsize( blob );
05d292b2 (kx 2023-03-24 03:51:10 +0300 276) rawcontent = (const char *)git_blob_rawcontent( blob );
05d292b2 (kx 2023-03-24 03:51:10 +0300 277)
05d292b2 (kx 2023-03-24 03:51:10 +0300 278) if( rawcontent && *rawcontent )
05d292b2 (kx 2023-03-24 03:51:10 +0300 279) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 280) struct strbuf buf = STRBUF_INIT;
05d292b2 (kx 2023-03-24 03:51:10 +0300 281)
05d292b2 (kx 2023-03-24 03:51:10 +0300 282) strbuf_add( &buf, (const void *)rawcontent, (size_t)rawsize );
05d292b2 (kx 2023-03-24 03:51:10 +0300 283)
05d292b2 (kx 2023-03-24 03:51:10 +0300 284) git_blob_free( blob );
05d292b2 (kx 2023-03-24 03:51:10 +0300 285) close_repository( repo );
05d292b2 (kx 2023-03-24 03:51:10 +0300 286)
05d292b2 (kx 2023-03-24 03:51:10 +0300 287) /****************************************************
05d292b2 (kx 2023-03-24 03:51:10 +0300 288) This call should terminate the program on success:
05d292b2 (kx 2023-03-24 03:51:10 +0300 289) */
05d292b2 (kx 2023-03-24 03:51:10 +0300 290) cgit_print_raw_file( &buf, info.mime );
05d292b2 (kx 2023-03-24 03:51:10 +0300 291)
05d292b2 (kx 2023-03-24 03:51:10 +0300 292) strbuf_release( &buf );
05d292b2 (kx 2023-03-24 03:51:10 +0300 293) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 294) else
05d292b2 (kx 2023-03-24 03:51:10 +0300 295) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 296) git_blob_free( blob );
05d292b2 (kx 2023-03-24 03:51:10 +0300 297) close_repository( repo );
05d292b2 (kx 2023-03-24 03:51:10 +0300 298) return;
05d292b2 (kx 2023-03-24 03:51:10 +0300 299) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 300)
05d292b2 (kx 2023-03-24 03:51:10 +0300 301) git_blob_free( blob );
05d292b2 (kx 2023-03-24 03:51:10 +0300 302) close_repository( repo );
05d292b2 (kx 2023-03-24 03:51:10 +0300 303) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 304)
05d292b2 (kx 2023-03-24 03:51:10 +0300 305) return;
05d292b2 (kx 2023-03-24 03:51:10 +0300 306) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 307)
05d292b2 (kx 2023-03-24 03:51:10 +0300 308) void cgit_print_file_page( void )
05d292b2 (kx 2023-03-24 03:51:10 +0300 309) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 310) FILE *fp;
05d292b2 (kx 2023-03-24 03:51:10 +0300 311) struct strbuf buf = STRBUF_INIT;
05d292b2 (kx 2023-03-24 03:51:10 +0300 312)
05d292b2 (kx 2023-03-24 03:51:10 +0300 313) fp = xfopen( ctx.page.header, "r" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 314) (void)strbuf_env_fread( &buf, fp );
05d292b2 (kx 2023-03-24 03:51:10 +0300 315) fclose( fp );
05d292b2 (kx 2023-03-24 03:51:10 +0300 316)
05d292b2 (kx 2023-03-24 03:51:10 +0300 317) strbuf_addf( &buf, " <div class=\"content segment\">\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 318) strbuf_addf( &buf, " <div class=\"container\">\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 319) strbuf_addf( &buf, " <div class=\"cgit-main-content\">\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 320)
05d292b2 (kx 2023-03-24 03:51:10 +0300 321) if( ctx.repo.relative_info.kind == GIT_OBJECT_BLOB )
05d292b2 (kx 2023-03-24 03:51:10 +0300 322) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 323) if( !strncmp( ctx.repo.relative_info.mime, "text/", 5 ) )
05d292b2 (kx 2023-03-24 03:51:10 +0300 324) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 325) if( ctx.repo.name )
05d292b2 (kx 2023-03-24 03:51:10 +0300 326) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 327) cgit_print_file( &buf, ctx.repo.relative_path, (!strcmp( ctx.query.rev, "0" )) ? (const char *)&ctx.repo.relative_info.revision[0] : ctx.query.rev );
05d292b2 (kx 2023-03-24 03:51:10 +0300 328) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 329) else
05d292b2 (kx 2023-03-24 03:51:10 +0300 330) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 331) strbuf_addf( &buf, " <h1>Requested resource cannot be shown</h1>\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 332) strbuf_addf( &buf, " <p class='leading'>Repository '%s' not found.</p>\n", ctx.repo.name );
05d292b2 (kx 2023-03-24 03:51:10 +0300 333) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 334) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 335) else if( !strncmp( ctx.repo.relative_info.mime, "image/", 6 ) )
05d292b2 (kx 2023-03-24 03:51:10 +0300 336) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 337) /****************************************************
05d292b2 (kx 2023-03-24 03:51:10 +0300 338) This call should terminate the program on success:
05d292b2 (kx 2023-03-24 03:51:10 +0300 339) */
05d292b2 (kx 2023-03-24 03:51:10 +0300 340) cgit_print_image_file( &buf, ctx.repo.relative_path, (!strcmp( ctx.query.rev, "0" )) ? (const char *)&ctx.repo.relative_info.revision[0] : ctx.query.rev );
05d292b2 (kx 2023-03-24 03:51:10 +0300 341)
05d292b2 (kx 2023-03-24 03:51:10 +0300 342) strbuf_addf( &buf, " <h1>Requested file cannot be shown</h1>\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 343) strbuf_addf( &buf, " <p class='leading'>Files with mime type such as '%s' cannot be present.</p>\n", ctx.repo.relative_info.mime );
05d292b2 (kx 2023-03-24 03:51:10 +0300 344) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 345) else
05d292b2 (kx 2023-03-24 03:51:10 +0300 346) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 347) strbuf_addf( &buf, " <h1>Requested file cannot be shown</h1>\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 348) strbuf_addf( &buf, " <p class='leading'>Files with mime type such as '%s' cannot be present.</p>\n", ctx.repo.relative_info.mime );
05d292b2 (kx 2023-03-24 03:51:10 +0300 349) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 350) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 351) else
05d292b2 (kx 2023-03-24 03:51:10 +0300 352) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 353) strbuf_addf( &buf, " <h1>Requested resource cannot be shown</h1>\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 354) strbuf_addf( &buf, " <p class='leading'>This page assume plain text files to be present.</p>\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 355) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 356)
05d292b2 (kx 2023-03-24 03:51:10 +0300 357) strbuf_addf( &buf, " </div> <!-- End of cgit-main-content -->\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 358) strbuf_addf( &buf, " </div> <!-- End of container -->\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 359) strbuf_addf( &buf, " </div> <!-- End of content segment -->\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 360)
05d292b2 (kx 2023-03-24 03:51:10 +0300 361) fp = xfopen( ctx.page.footer, "r" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 362) (void)strbuf_env_fread( &buf, fp );
05d292b2 (kx 2023-03-24 03:51:10 +0300 363) fclose( fp );
05d292b2 (kx 2023-03-24 03:51:10 +0300 364)
05d292b2 (kx 2023-03-24 03:51:10 +0300 365) ctx.page.size = buf.len;
05d292b2 (kx 2023-03-24 03:51:10 +0300 366) cgit_print_http_headers();
05d292b2 (kx 2023-03-24 03:51:10 +0300 367) strbuf_write( &buf, STDOUT_FILENO );
05d292b2 (kx 2023-03-24 03:51:10 +0300 368) strbuf_release( &buf );
05d292b2 (kx 2023-03-24 03:51:10 +0300 369) }