cGit-UI for Git Repositories

cGit-UI – is a web interface for Git Repositories. cGit CGI script is writen in C and therefore it's fast enough

12 Commits   0 Branches   1 Tag
     5         kx 
     5         kx #ifdef HAVE_CONFIG_H
     5         kx #include <config.h>
     5         kx #endif
     5         kx 
     5         kx #include <stdlib.h>
     5         kx #include <stdio.h>
     5         kx #include <sys/sysinfo.h>
     5         kx #include <sys/types.h>
     5         kx #ifdef HAVE_INTTYPES_H
     5         kx #include <inttypes.h>
     5         kx #else
     5         kx #include <stdint.h>
     5         kx #endif
     5         kx #include <stddef.h>   /* offsetof(3) */
     5         kx #include <dirent.h>
     5         kx #include <sys/stat.h> /* chmod(2)    */
     5         kx #include <sys/file.h>
     5         kx #include <sys/mman.h>
     5         kx #include <fcntl.h>
     5         kx #include <limits.h>
     5         kx #include <string.h>   /* strdup(3)   */
     5         kx #include <libgen.h>   /* basename(3) */
     5         kx #include <ctype.h>    /* tolower(3)  */
     5         kx #include <errno.h>
     5         kx #include <time.h>
     5         kx #include <sys/time.h>
     5         kx #include <pwd.h>
     5         kx #include <grp.h>
     5         kx #include <stdarg.h>
     5         kx #include <locale.h>
     5         kx #include <unistd.h>
     5         kx 
     5         kx #include <md4c.h>
     5         kx #include <md4c-html.h>
     5         kx 
     5         kx #include <git2.h>
     5         kx 
     5         kx #include <nls.h>
     5         kx 
     5         kx #include <defs.h>
     5         kx 
     5         kx #include <fatal.h>
     5         kx #include <http.h>
     5         kx #include <html.h>
     5         kx 
     5         kx #include <dlist.h>
     5         kx #include <strbuf.h>
     5         kx #include <repolist.h>
     5         kx #include <wrapper.h>
     5         kx #include <system.h>
     5         kx #include <date.h>
     5         kx 
     5         kx #include <ctx.h>
     5         kx #include <git-shared.h>
     5         kx #include <ui-shared.h>
     5         kx 
     5         kx 
     5         kx static void cgit_print_file_links( struct strbuf *sb, const char *relative_path, const char *revision, int top )
     5         kx {
     5         kx   const char *query_string = NULL;
     5         kx   const char *place = NULL;
     5         kx 
     5         kx   if( !sb || !relative_path ) return;
     5         kx 
     5         kx   if( top )
     5         kx     place = "top";
     5         kx   else
     5         kx     place = "bottom";
     5         kx 
     5         kx   strbuf_addf( sb, "<div class=\"%s file-links-menu\">\n", place );
     5         kx   strbuf_addf( sb, "  <div class=\"right links-menu\">\n" );
     5         kx 
     5         kx   query_string = ctx_remove_query_param( ctx.env.query_string, "rev" );
     5         kx   query_string = ctx_remove_query_param( query_string, "op" );
     5         kx 
     5         kx   if( query_string && *query_string )
     5         kx   {
     5         kx     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 );
     5         kx     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 );
     5         kx     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 );
     5         kx   }
     5         kx   else
     5         kx   {
     5         kx     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 );
     5         kx     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 );
     5         kx     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 );
     5         kx   }
     5         kx 
     5         kx   strbuf_addf( sb, "  </div>\n" );
     5         kx   strbuf_addf( sb, "</div>\n" );
     5         kx }
     5         kx 
     5         kx 
     5         kx static void process_markdown_output( const MD_CHAR *text, MD_SIZE size, void *sb )
     5         kx {
     5         kx   strbuf_add( (struct strbuf *)sb, (const void *)text, (size_t)size );
     5         kx }
     5         kx 
     5         kx static int cgit_write_markdown_content( struct strbuf *sb, const struct strbuf *input )
     5         kx {
     5         kx   unsigned parser_flags   = MD_DIALECT_GITHUB; /* | MD_DIALECT_COMMONMARK */
     5         kx   unsigned renderer_flags = MD_FLAG_WIKILINKS; /* | MD_HTML_FLAG_DEBUG    */
     5         kx 
     5         kx   /*********************************
     5         kx     md_html() returns 0 on success:
     5         kx    */
     5         kx   return md_html( input->buf, input->len, process_markdown_output,
     5         kx                   (void *)sb, parser_flags, renderer_flags );
     5         kx }
     5         kx 
     5         kx 
     5         kx static void cgit_print_file( struct strbuf *sb, const char *relative_path, const char *revision )
     5         kx {
     5         kx   struct cgit_info info;
     5         kx 
     5         kx   if( !sb || !relative_path ) return;
     5         kx 
     5         kx   if( revision && strcmp( revision, (const char *)&ctx.repo.relative_info.revision[0] ) )
     5         kx   {
     5         kx     cgit_rpath_info( &info, relative_path, revision );
     5         kx     if( info.kind != GIT_OBJECT_BLOB )
     5         kx     {
     5         kx       strbuf_addf( sb, "              <h1>Requested resource cannot be shown</h1>\n" );
     5         kx       strbuf_addf( sb, "              <p class='leading'>File with specified revision '%s' cannot be shown.</p>\n", revision );
     5         kx       return;
     5         kx     }
     5         kx   }
     5         kx   else
     5         kx   {
     5         kx     memcpy( (void *)&info, (void *)&ctx.repo.relative_info, sizeof( struct cgit_info ) );
     5         kx   }
     5         kx 
     5         kx   cgit_print_file_links( sb, relative_path, (const char *)&info.revision[0], 1 );
     5         kx 
     5         kx   if( info.lang )
     5         kx   {
     5         kx     if( !strcmp( info.lang, "Markdown" ) )
     5         kx       strbuf_addstr( sb, "<div class='markdown-content'>" );
     5         kx     else
     5         kx       strbuf_addf( sb, "<pre><code class='language-%s'>", info.lang );
     5         kx   }
     5         kx   else
     5         kx     strbuf_addstr( sb, "<pre><code class='highlight'>" );
     5         kx 
     5         kx   if( info.kind == GIT_OBJECT_BLOB )
     5         kx   {
     5         kx     git_repository *repo = NULL;
     5         kx     git_blob       *blob = NULL;
     5         kx     git_oid         oid;
     5         kx     /* git_off_t       rawsize = 0; */
     5         kx     const char     *rawcontent = NULL;
     5         kx 
     5         kx     if( git_oid_fromstr( &oid, (const char *)&info.oid[0] ) < 0 )
     5         kx     {
     5         kx       if( info.lang && !strcmp( info.lang, "Markdown" ) )
     5         kx         strbuf_addstr( sb, "\n</div> <!-- End of Markdown Content -->\n" );
     5         kx       else
     5         kx         strbuf_addstr( sb, "\n</code></pre>\n" );
     5         kx 
     5         kx       cgit_print_file_links( sb, relative_path, (const char *)&info.revision[0], 0 );
     5         kx       return;
     5         kx     }
     5         kx 
     5         kx     if( !(repo = cgit_open_repository()) )
     5         kx     {
     5         kx       if( info.lang && !strcmp( info.lang, "Markdown" ) )
     5         kx         strbuf_addstr( sb, "\n</div> <!-- End of Markdown Content -->\n" );
     5         kx       else
     5         kx         strbuf_addstr( sb, "\n</code></pre>\n" );
     5         kx 
     5         kx       cgit_print_file_links( sb, relative_path, (const char *)&info.revision[0], 0 );
     5         kx       return;
     5         kx     }
     5         kx 
     5         kx     if( git_blob_lookup( &blob, repo, &oid ) < 0 )
     5         kx     {
     5         kx       close_repository( repo );
     5         kx 
     5         kx       if( info.lang && !strcmp( info.lang, "Markdown" ) )
     5         kx         strbuf_addstr( sb, "\n</div> <!-- End of Markdown Content -->\n" );
     5         kx       else
     5         kx         strbuf_addstr( sb, "\n</code></pre>\n" );
     5         kx 
     5         kx       cgit_print_file_links( sb, relative_path, (const char *)&info.revision[0], 0 );
     5         kx       return;
     5         kx     }
     5         kx 
     5         kx     /* rawsize = git_blob_rawsize( blob ); */
     5         kx     rawcontent = (const char *)git_blob_rawcontent( blob );
     5         kx 
     5         kx     if( !git_blob_is_binary( blob ) && rawcontent && *rawcontent )
     5         kx     {
     5         kx       struct strbuf buf = STRBUF_INIT;
     5         kx 
     5         kx       if( info.lang && !strcmp( info.lang, "Markdown" ) )
     5         kx       {
     5         kx         strbuf_addstr( &buf, (const char *)rawcontent );
     5         kx         (void)cgit_write_markdown_content( sb, (const struct strbuf *)&buf );
     5         kx         strbuf_release( &buf );
     5         kx       }
     5         kx       else
     5         kx       {
     5         kx         strbuf_addstr_xml_quoted( &buf, (const char *)rawcontent );
     5         kx         strbuf_addbuf( sb, (const struct strbuf *)&buf );
     5         kx         strbuf_release( &buf );
     5         kx       }
     5         kx     }
     5         kx     else
     5         kx     {
     5         kx       git_blob_free( blob );
     5         kx       close_repository( repo );
     5         kx 
     5         kx       if( info.lang && !strcmp( info.lang, "Markdown" ) )
     5         kx         strbuf_addstr( sb, "\n</div> <!-- End of Markdown Content -->\n" );
     5         kx       else
     5         kx         strbuf_addstr( sb, "\n</code></pre>\n" );
     5         kx 
     5         kx       cgit_print_file_links( sb, relative_path, (const char *)&info.revision[0], 0 );
     5         kx       return;
     5         kx     }
     5         kx 
     5         kx     git_blob_free( blob );
     5         kx     close_repository( repo );
     5         kx   }
     5         kx 
     5         kx   if( info.lang && !strcmp( info.lang, "Markdown" ) )
     5         kx     strbuf_addstr( sb, "\n</div> <!-- End of Markdown Content -->\n" );
     5         kx   else
     5         kx     strbuf_addstr( sb, "\n</code></pre>\n" );
     5         kx 
     5         kx   cgit_print_file_links( sb, relative_path, (const char *)&info.revision[0], 0 );
     5         kx 
     5         kx   return;
     5         kx }
     5         kx 
     5         kx static void cgit_print_image_file( struct strbuf *sb, const char *relative_path, const char *revision )
     5         kx {
     5         kx   struct cgit_info info;
     5         kx 
     5         kx   if( !sb || !relative_path ) return;
     5         kx 
     5         kx   if( revision && strcmp( revision, (const char *)&ctx.repo.relative_info.revision[0] ) )
     5         kx   {
     5         kx     cgit_rpath_info( &info, relative_path, revision );
     5         kx     if( info.kind != GIT_OBJECT_BLOB )
     5         kx     {
     5         kx       strbuf_addf( sb, "              <h1>Invalid Revision</h1>\n" );
     5         kx       strbuf_addf( sb, "              <p class='leading'>File with specified revision '%s' cannot be shown.</p>\n", revision );
     5         kx       return;
     5         kx     }
     5         kx   }
     5         kx   else
     5         kx   {
     5         kx     memcpy( (void *)&info, (void *)&ctx.repo.relative_info, sizeof( struct cgit_info ) );
     5         kx   }
     5         kx 
     5         kx   if( info.kind == GIT_OBJECT_BLOB )
     5         kx   {
     5         kx     git_repository *repo = NULL;
     5         kx     git_blob       *blob = NULL;
     5         kx     git_oid         oid;
     5         kx     git_off_t       rawsize = 0;
     5         kx     const char     *rawcontent = NULL;
     5         kx 
     5         kx     if( git_oid_fromstr( &oid, (const char *)&info.oid[0] ) < 0 ) return;
     5         kx     if( !(repo = cgit_open_repository()) ) return;
     5         kx 
     5         kx     if( git_blob_lookup( &blob, repo, &oid ) < 0 )
     5         kx     {
     5         kx       close_repository( repo );
     5         kx       return;
     5         kx     }
     5         kx 
     5         kx     rawsize = git_blob_rawsize( blob );
     5         kx     rawcontent = (const char *)git_blob_rawcontent( blob );
     5         kx 
     5         kx     if( rawcontent && *rawcontent )
     5         kx     {
     5         kx       struct strbuf buf = STRBUF_INIT;
     5         kx 
     5         kx       strbuf_add( &buf, (const void *)rawcontent, (size_t)rawsize );
     5         kx 
     5         kx       git_blob_free( blob );
     5         kx       close_repository( repo );
     5         kx 
     5         kx       /****************************************************
     5         kx         This call should terminate the program on success:
     5         kx        */
     5         kx       cgit_print_raw_file( &buf, info.mime );
     5         kx 
     5         kx       strbuf_release( &buf );
     5         kx     }
     5         kx     else
     5         kx     {
     5         kx       git_blob_free( blob );
     5         kx       close_repository( repo );
     5         kx       return;
     5         kx     }
     5         kx 
     5         kx     git_blob_free( blob );
     5         kx     close_repository( repo );
     5         kx   }
     5         kx 
     5         kx   return;
     5         kx }
     5         kx 
     5         kx void cgit_print_file_page( void )
     5         kx {
     5         kx   FILE  *fp;
     5         kx   struct strbuf buf = STRBUF_INIT;
     5         kx 
     5         kx   fp = xfopen( ctx.page.header, "r" );
     5         kx   (void)strbuf_env_fread( &buf, fp );
     5         kx   fclose( fp );
     5         kx 
     5         kx   strbuf_addf( &buf, "        <div class=\"content segment\">\n" );
     5         kx   strbuf_addf( &buf, "          <div class=\"container\">\n" );
     5         kx   strbuf_addf( &buf, "            <div class=\"cgit-main-content\">\n" );
     5         kx 
     5         kx   if( ctx.repo.relative_info.kind == GIT_OBJECT_BLOB )
     5         kx   {
     5         kx     if( !strncmp( ctx.repo.relative_info.mime, "text/", 5 ) )
     5         kx     {
     5         kx       if( ctx.repo.name )
     5         kx       {
     5         kx         cgit_print_file( &buf, ctx.repo.relative_path, (!strcmp( ctx.query.rev, "0" )) ? (const char *)&ctx.repo.relative_info.revision[0] : ctx.query.rev );
     5         kx       }
     5         kx       else
     5         kx       {
     5         kx         strbuf_addf( &buf, "              <h1>Requested resource cannot be shown</h1>\n" );
     5         kx         strbuf_addf( &buf, "              <p class='leading'>Repository '%s' not found.</p>\n", ctx.repo.name );
     5         kx       }
     5         kx     }
     5         kx     else if( !strncmp( ctx.repo.relative_info.mime, "image/", 6 ) )
     5         kx     {
     5         kx       /****************************************************
     5         kx         This call should terminate the program on success:
     5         kx        */
     5         kx       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 );
     5         kx 
     5         kx       strbuf_addf( &buf, "              <h1>Requested file cannot be shown</h1>\n" );
     5         kx       strbuf_addf( &buf, "              <p class='leading'>Files with mime type such as '%s' cannot be present.</p>\n", ctx.repo.relative_info.mime );
     5         kx     }
     5         kx     else
     5         kx     {
     5         kx       strbuf_addf( &buf, "              <h1>Requested file cannot be shown</h1>\n" );
     5         kx       strbuf_addf( &buf, "              <p class='leading'>Files with mime type such as '%s' cannot be present.</p>\n", ctx.repo.relative_info.mime );
     5         kx     }
     5         kx   }
     5         kx   else
     5         kx   {
     5         kx     strbuf_addf( &buf, "              <h1>Requested resource cannot be shown</h1>\n" );
     5         kx     strbuf_addf( &buf, "              <p class='leading'>This page assume plain text files to be present.</p>\n" );
     5         kx   }
     5         kx 
     5         kx   strbuf_addf( &buf, "            </div> <!-- End of cgit-main-content -->\n" );
     5         kx   strbuf_addf( &buf, "          </div> <!-- End of container -->\n" );
     5         kx   strbuf_addf( &buf, "        </div> <!-- End of content segment -->\n" );
     5         kx 
     5         kx   fp = xfopen( ctx.page.footer, "r" );
     5         kx   (void)strbuf_env_fread( &buf, fp );
     5         kx   fclose( fp );
     5         kx 
     5         kx   ctx.page.size = buf.len;
     5         kx   cgit_print_http_headers();
     5         kx   strbuf_write( &buf, STDOUT_FILENO );
     5         kx   strbuf_release( &buf );
     5         kx }