cSvn-UI for SVN Repositories

cGit-UI – is a web interface for Subversion (SVN) Repositories. cSvn CGI script is writen in C and therefore it's fast enough

15 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 <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 <ui-shared.h>
     5         kx 
     5         kx 
     5         kx static int csvn_file_last_changed_revision( const char *relative_path )
     5         kx {
     5         kx   const char *co_prefix = ctx.repo.checkout_ro_prefix;
     5         kx   const char *name      = ctx.repo.name;
     5         kx   const char *repo_root = ctx.repo.repo_root;
     5         kx   int ret = 0;
     5         kx 
     5         kx   if( !relative_path ) return ret;
     5         kx 
     5         kx   if( co_prefix )
     5         kx   {
     5         kx     char repo_path[PATH_MAX] = { 0 };
     5         kx     char cmd[PATH_MAX];
     5         kx     struct strbuf buf = STRBUF_INIT;
     5         kx     pid_t p = (pid_t) -1;
     5         kx     int   rc;
     5         kx 
     5         kx     if( repo_root && *repo_root )
     5         kx     {
     5         kx       strcat( (char *)&repo_path[0], repo_root );
     5         kx       strcat( (char *)&repo_path[0], "/" );
     5         kx     }
     5         kx     strcat( (char *)&repo_path[0], name );
     5         kx 
     5         kx     snprintf( (char *)&cmd[0], 1024,
     5         kx               "svn info --show-item last-changed-revision --no-newline %s/%s/%s 2>/dev/null",
     5         kx               co_prefix, (char *)&repo_path[0], relative_path );
     5         kx     p = sys_exec_command( &buf, cmd );
     5         kx     rc = sys_wait_command( p, NULL );
     5         kx     if( rc != 0 )
     5         kx     {
     5         kx       return ret;
     5         kx     }
     5         kx     strbuf_trim( &buf );
     5         kx     ret = atoi( buf.buf );
     5         kx     strbuf_release( &buf );
     5         kx   }
     5         kx   return ret;
     5         kx }
     5         kx 
     5         kx static void csvn_print_file_links( struct strbuf *sb, const char *relative_path, int revision, int top )
     5         kx {
     5         kx   const char *query_string = NULL;
     5         kx   const char *place = NULL;
     5         kx   char rev[80] = { 0 };
     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( !revision )
     5         kx     revision = csvn_file_last_changed_revision( relative_path );
     5         kx   sprintf( (char *)&rev[0], "%d", revision );
     5         kx 
     5         kx   if( query_string && *query_string )
     5         kx   {
     5         kx     if( ctx.repo.repo_root && *ctx.repo.repo_root )
     5         kx     {
     5         kx       strbuf_addf( sb, "    <div class=\"item\"><span class=\"icon las la-scroll\"></span><a href=\"/%s/%s/%s/?op=log&rev=%s&%s\">log</a></div>\n", ctx.repo.repo_root, ctx.repo.name, relative_path, (char *)&rev[0], query_string );
     5         kx       strbuf_addf( sb, "    <div class=\"item\"><span class=\"icon las la-diff\"></span><a href=\"/%s/%s/%s/?op=diff&rev=%s&%s\">diff</a></div>\n", ctx.repo.repo_root, ctx.repo.name, relative_path, (char *)&rev[0], query_string );
     5         kx       strbuf_addf( sb, "    <div class=\"item\"><span class=\"icon las la-blame\"></span><a href=\"/%s/%s/%s/?op=blame&rev=%s&%s\">blame</a></div>\n", ctx.repo.repo_root, ctx.repo.name, relative_path, (char *)&rev[0], 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&%s\">log</a></div>\n", ctx.repo.name, relative_path, (char *)&rev[0], 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, (char *)&rev[0], 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, (char *)&rev[0], query_string );
     5         kx     }
     5         kx   }
     5         kx   else
     5         kx   {
     5         kx     if( ctx.repo.repo_root && *ctx.repo.repo_root )
     5         kx     {
     5         kx       strbuf_addf( sb, "    <div class=\"item\"><span class=\"icon las la-scroll\"></span><a href=\"/%s/%s/%s/?op=log&rev=%s\">log</a></div>\n", ctx.repo.repo_root, ctx.repo.name, relative_path, (char *)&rev[0] );
     5         kx       strbuf_addf( sb, "    <div class=\"item\"><span class=\"icon las la-diff\"></span><a href=\"/%s/%s/%s/?op=diff&rev=%s\">diff</a></div>\n", ctx.repo.repo_root, ctx.repo.name, relative_path, (char *)&rev[0] );
     5         kx       strbuf_addf( sb, "    <div class=\"item\"><span class=\"icon las la-blame\"></span><a href=\"/%s/%s/%s/?op=blame&rev=%s\">blame</a></div>\n", ctx.repo.repo_root, ctx.repo.name, relative_path, (char *)&rev[0] );
     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, (char *)&rev[0] );
     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, (char *)&rev[0] );
     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, (char *)&rev[0] );
     5         kx     }
     5         kx   }
     5         kx 
     5         kx   strbuf_addf( sb, "  </div>\n" );
     5         kx   strbuf_addf( sb, "</div>\n" );
     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 csvn_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 static void csvn_print_markdown_file( struct strbuf *sb, const char *relative_path, int revision )
     5         kx {
     5         kx   const char *co_prefix = ctx.repo.checkout_ro_prefix;
     5         kx   const char *name      = ctx.repo.name;
     5         kx   const char *repo_root = ctx.repo.repo_root;
     5         kx 
     5         kx   if( !sb || !relative_path ) return;
     5         kx 
     5         kx   csvn_print_file_links( sb, relative_path, revision, 1 );
     5         kx 
     5         kx   strbuf_addstr( sb, "<div class='markdown-content'>" );
     5         kx 
     5         kx   if( co_prefix )
     5         kx   {
     5         kx     char repo_path[PATH_MAX] = { 0 };
     5         kx     char cmd[PATH_MAX];
     5         kx     struct strbuf buf = STRBUF_INIT;
     5         kx     pid_t p = (pid_t) -1;
     5         kx     int   rc;
     5         kx 
     5         kx     if( repo_root && *repo_root )
     5         kx     {
     5         kx       strcat( (char *)&repo_path[0], repo_root );
     5         kx       strcat( (char *)&repo_path[0], "/" );
     5         kx     }
     5         kx     strcat( (char *)&repo_path[0], name );
     5         kx 
     5         kx     if( revision )
     5         kx       snprintf( (char *)&cmd[0], 1024,
     5         kx                 "svn cat --revision %d %s/%s/%s 2>/dev/null",
     5         kx                 revision, co_prefix, (char *)&repo_path[0], relative_path );
     5         kx     else
     5         kx       snprintf( (char *)&cmd[0], 1024,
     5         kx                 "svn cat %s/%s/%s 2>/dev/null",
     5         kx                 co_prefix, (char *)&repo_path[0], relative_path );
     5         kx     p = sys_exec_command( &buf, cmd );
     5         kx     rc = sys_wait_command( p, NULL );
     5         kx     if( rc != 0 )
     5         kx     {
     5         kx       strbuf_release( &buf );
     5         kx       return;
     5         kx     }
     5         kx 
     5         kx     (void)csvn_write_markdown_content( sb, (const struct strbuf *)&buf );
     5         kx 
     5         kx     strbuf_release( &buf );
     5         kx   }
     5         kx 
     5         kx   strbuf_addstr( sb, "\n</div> <!-- End of Markdown Content -->\n" );
     5         kx 
     5         kx   csvn_print_file_links( sb, relative_path, revision, 0 );
     5         kx 
     5         kx   return;
     5         kx }
     5         kx 
     5         kx 
     5         kx static void csvn_print_file( struct strbuf *sb, const char *relative_path, int revision, const char *lang )
     5         kx {
     5         kx   const char *co_prefix = ctx.repo.checkout_ro_prefix;
     5         kx   const char *name      = ctx.repo.name;
     5         kx   const char *repo_root = ctx.repo.repo_root;
     5         kx 
     5         kx   if( !sb || !relative_path ) return;
     5         kx 
     5         kx   csvn_print_file_links( sb, relative_path, revision, 1 );
     5         kx 
     5         kx   if( lang )
     5         kx     strbuf_addf( sb, "<pre><code class='language-%s'>", lang );
     5         kx   else
     5         kx     strbuf_addstr( sb, "<pre><code class='highlight'>" );
     5         kx 
     5         kx   if( co_prefix )
     5         kx   {
     5         kx     char repo_path[PATH_MAX] = { 0 };
     5         kx     char cmd[PATH_MAX];
     5         kx     struct strbuf buf = STRBUF_INIT;
     5         kx     char  *raw = NULL;
     5         kx     pid_t  p = (pid_t) -1;
     5         kx     int    rc;
     5         kx 
     5         kx     if( repo_root && *repo_root )
     5         kx     {
     5         kx       strcat( (char *)&repo_path[0], repo_root );
     5         kx       strcat( (char *)&repo_path[0], "/" );
     5         kx     }
     5         kx     strcat( (char *)&repo_path[0], name );
     5         kx 
     5         kx     if( revision )
     5         kx       snprintf( (char *)&cmd[0], 1024,
     5         kx                 "svn cat --revision %d %s/%s/%s 2>/dev/null",
     5         kx                 revision, co_prefix, (char *)&repo_path[0], relative_path );
     5         kx     else
     5         kx       snprintf( (char *)&cmd[0], 1024,
     5         kx                 "svn cat %s/%s/%s 2>/dev/null",
     5         kx                 co_prefix, (char *)&repo_path[0], relative_path );
     5         kx     p = sys_exec_command( &buf, cmd );
     5         kx     rc = sys_wait_command( p, NULL );
     5         kx     if( rc != 0 )
     5         kx     {
     5         kx       strbuf_release( &buf );
     5         kx       return;
     5         kx     }
     5         kx 
     5         kx     raw = strbuf_detach( &buf, NULL );
     5         kx     strbuf_addstr_xml_quoted( &buf, (const char *)raw );
     5         kx     free( raw );
     5         kx 
     5         kx     strbuf_addbuf( sb, (const struct strbuf *)&buf );
     5         kx     strbuf_release( &buf );
     5         kx   }
     5         kx 
     5         kx   strbuf_addstr( sb, "\n</code></pre>\n" );
     5         kx 
     5         kx   csvn_print_file_links( sb, relative_path, revision, 0 );
     5         kx 
     5         kx   return;
     5         kx }
     5         kx 
     5         kx static void csvn_print_image_file( struct strbuf *sb, const char *relative_path, int revision )
     5         kx {
     5         kx   const char *co_prefix = ctx.repo.checkout_ro_prefix;
     5         kx   const char *name      = ctx.repo.name;
     5         kx   const char *repo_root = ctx.repo.repo_root;
     5         kx 
     5         kx   if( !sb || !relative_path ) return;
     5         kx 
     5         kx   if( co_prefix )
     5         kx   {
     5         kx     char repo_path[PATH_MAX] = { 0 };
     5         kx     char cmd[PATH_MAX];
     5         kx     struct strbuf buf = STRBUF_INIT;
     5         kx     pid_t  p = (pid_t) -1;
     5         kx     int    rc;
     5         kx 
     5         kx     if( repo_root && *repo_root )
     5         kx     {
     5         kx       strcat( (char *)&repo_path[0], repo_root );
     5         kx       strcat( (char *)&repo_path[0], "/" );
     5         kx     }
     5         kx     strcat( (char *)&repo_path[0], name );
     5         kx 
     5         kx     if( revision )
     5         kx       snprintf( (char *)&cmd[0], 1024,
     5         kx                 "svn cat --revision %d %s/%s/%s 2>/dev/null",
     5         kx                 revision, co_prefix, (char *)&repo_path[0], relative_path );
     5         kx     else
     5         kx       snprintf( (char *)&cmd[0], 1024,
     5         kx                 "svn cat %s/%s/%s 2>/dev/null",
     5         kx                 co_prefix, (char *)&repo_path[0], relative_path );
     5         kx     p = sys_exec_command( &buf, cmd );
     5         kx     rc = sys_wait_command( p, NULL );
     5         kx     if( rc != 0 )
     5         kx     {
     5         kx       strbuf_release( &buf );
     5         kx       return;
     5         kx     }
     5         kx 
     5         kx     /****************************************************
     5         kx       This call should terminate the program on success:
     5         kx      */
     5         kx     csvn_print_raw_file( &buf, ctx.repo.relative_info.mime );
     5         kx 
     5         kx     strbuf_release( &buf );
     5         kx   }
     5         kx 
     5         kx   return;
     5         kx }
     5         kx 
     5         kx void csvn_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=\"csvn-main-content\">\n" );
     5         kx 
     5         kx   if( ctx.repo.relative_info.kind == KIND_FILE )
     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         if( ctx.repo.relative_info.lang )
     5         kx         {
     5         kx           if( !strcmp( ctx.repo.relative_info.lang, "Markdown" ) )
     5         kx             csvn_print_markdown_file( &buf, ctx.repo.relative_path, ctx.query.rev );
     5         kx           else
     5         kx             csvn_print_file( &buf, ctx.repo.relative_path, ctx.query.rev, ctx.repo.relative_info.lang );
     5         kx         }
     5         kx         else
     5         kx           csvn_print_file( &buf, ctx.repo.relative_path, ctx.query.rev, NULL );
     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       csvn_print_image_file( &buf, ctx.repo.relative_path, 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 csvn-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   csvn_print_http_headers();
     5         kx   strbuf_write( &buf, STDOUT_FILENO );
     5         kx   strbuf_release( &buf );
     5         kx }