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 <libxml/parser.h>
     5         kx #include <libxml/tree.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 void xml_csvn_log( struct strbuf *sb, const struct strbuf *buf, const char *relative_path )
     5         kx {
     5         kx   xmlDocPtr doc = NULL;
     5         kx   xmlNode *root = NULL;
     5         kx   char    *path = NULL;
     5         kx 
     5         kx   int count = 0, printed = 0, reminder = 0, page_size = 200;
     5         kx 
     5         kx   if( !sb || !sb->len || !buf || !buf->buf ) return;
     5         kx   if( relative_path && *relative_path )
     5         kx   {
     5         kx     path = (char *)xmalloc( strlen( relative_path ) + 2 );
     5         kx     path[0] = '/';
     5         kx     sprintf( (char *)&path[1], relative_path );
     5         kx   }
     5         kx   else
     5         kx   {
     5         kx     path = (char *)xmalloc( 1 );
     5         kx     path[0] = '\0';
     5         kx   }
     5         kx 
     5         kx   page_size = atoi( ctx.vars.page_size );
     5         kx 
     5         kx   LIBXML_TEST_VERSION
     5         kx 
     5         kx   doc = xmlReadMemory( buf->buf, buf->len, "log.xml", NULL, 0 );
     5         kx   if( !doc )
     5         kx   {
     5         kx     html_fatal( "cannot parse svn log.xml" );
     5         kx     return;
     5         kx   }
     5         kx 
     5         kx   root = xmlDocGetRootElement( doc );
     5         kx   if( !root )
     5         kx   {
     5         kx     free( path );
     5         kx     xmlFreeDoc( doc );
     5         kx     xmlCleanupParser();
     5         kx     return;
     5         kx   }
     5         kx 
     5         kx   if( !strcmp( "log", (char *)root->name ) )
     5         kx   {
     5         kx     xmlNode *node = NULL;
     5         kx 
     5         kx     strbuf_addstr( sb, "\n" );
     5         kx     strbuf_addf( sb, "              <div class=\"repo-log-header\">\n" );
     5         kx     strbuf_addf( sb, "                <div class=\"row\">\n" );
     5         kx     strbuf_addf( sb, "                  <div class=\"col-date\"><div class=\"log-date\">Date</div></div>\n" );
     5         kx     strbuf_addf( sb, "                  <div class=\"col-cmsg\"><div class=\"log-cmsg trunc\">Commit Message</div></div>\n" );
     5         kx     strbuf_addf( sb, "                  <div class=\"col-rev\"><div class=\"log-rev trunc\">Rev</div></div>\n" );
     5         kx     strbuf_addf( sb, "                  <div class=\"col-author\"><div class=\"log-author trunc\">Author</div></div>\n" );
     5         kx     strbuf_addf( sb, "                </div>\n" );
     5         kx     strbuf_addf( sb, "              </div>\n\n" );
     5         kx 
     5         kx     strbuf_addf( sb, "              <div class=\"log\">\n\n" );
     5         kx 
     5         kx     /**************************
     5         kx       Print directories first:
     5         kx      */
     5         kx     for( node = root->children; node; node = node->next )
     5         kx     {
     5         kx       if( node->type == XML_ELEMENT_NODE && !strcmp( "logentry", (char *)node->name ) )
     5         kx       {
     5         kx         xmlNode *param = NULL;
     5         kx         xmlChar *date = NULL, *cmsg = NULL, *revision = NULL, *author = NULL;
     5         kx 
     5         kx         revision = xmlGetProp( node, (const unsigned char *)"revision" );
     5         kx 
     5         kx         for( param = node->children; param; param= param->next )
     5         kx         {
     5         kx           if( param->type == XML_ELEMENT_NODE && !strcmp( "date", (char *)param->name ) )
     5         kx           {
     5         kx             date = xmlNodeGetContent( param );
     5         kx           }
     5         kx           if( param->type == XML_ELEMENT_NODE && !strcmp( "msg", (char *)param->name ) )
     5         kx           {
     5         kx             cmsg = xmlNodeGetContent( param );
     5         kx           }
     5         kx           if( param->type == XML_ELEMENT_NODE && !strcmp( "author", (char *)param->name ) )
     5         kx           {
     5         kx             author = xmlNodeGetContent( param );
     5         kx           }
     5         kx 
     5         kx         } /* End for entry parameters */
     5         kx 
     5         kx         if( date && cmsg && revision && author )
     5         kx         {
     5         kx           struct tm   tm;
     5         kx           time_t      time = -1;
     5         kx           const char *query_string = ctx_remove_query_param( ctx.env.query_string, "rev" );
     5         kx 
     5         kx           query_string = ctx_remove_query_param( query_string, "op" );
     5         kx 
     5         kx           time = parse_date( &tm, (const char *)date );
     5         kx 
     5         kx           ++count;
     5         kx           if( count > ctx.query.ofs && printed < page_size )
     5         kx           {
     5         kx 
     5         kx             strbuf_addf( sb, "                <div class=\"row\">\n" );
     5         kx             if( time != -1 )
     5         kx             {
     5         kx               strbuf_addf( sb, "                  <div class=\"col-date\"><div onclick=\"trunc(this)\" class=\"log-date trunc\">" );
     5         kx               csvn_print_age( sb, time, 0, 0 );
     5         kx               strbuf_addf( sb, "</div></div>\n" );
     5         kx             }
     5         kx             else
     5         kx             {
     5         kx               strbuf_addf( sb, "                  <div class=\"col-date\"><div onclick=\"trunc(this)\" class=\"log-date trunc\">unknown</div></div>\n" );
     5         kx             }
     5         kx             strbuf_addf( sb, "                  <div class=\"col-cmsg\"><div onclick=\"trunc(this)\" class=\"log-cmsg trunc\">%s</div></div>\n", (char *)cmsg );
     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=\"col-rev\"><div onclick=\"trunc(this)\" class=\"log-rev trunc\"><a title=\"Compare with Previous\" href=\"/%s/%s%s/?op=diff&rev=%s&%s\">%s</a></div></div>\n", ctx.repo.repo_root, ctx.repo.name, path, (char *)revision, query_string, (char *)revision );
     5         kx               }
     5         kx               else
     5         kx               {
     5         kx                 strbuf_addf( sb, "                  <div class=\"col-rev\"><div onclick=\"trunc(this)\" class=\"log-rev trunc\"><a title=\"Compare with Previous\" href=\"/%s%s/?op=diff&rev=%s&%s\">%s</a></div></div>\n", ctx.repo.name, path, (char *)revision, query_string, (char *)revision );
     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=\"col-rev\"><div onclick=\"trunc(this)\" class=\"log-rev trunc\"><a title=\"Compare with Previous\" href=\"/%s/%s%s/?op=diff&rev=%s\">%s</a></div></div>\n", ctx.repo.repo_root, path, ctx.repo.name, path, (char *)revision, (char *)revision );
     5         kx               }
     5         kx               else
     5         kx               {
     5         kx                 strbuf_addf( sb, "                  <div class=\"col-rev\"><div onclick=\"trunc(this)\" class=\"log-rev trunc\"><a title=\"Compare with Previous\" href=\"/%s%s/?op=diff&rev=%s\">%s</a></div></div>\n", ctx.repo.name, path, (char *)revision, (char *)revision );
     5         kx               }
     5         kx             }
     5         kx             strbuf_addf( sb, "                  <div class=\"col-author\"><div onclick=\"trunc(this)\" class=\"log-author trunc\">%s</div></div>\n", (char *)author );
     5         kx             strbuf_addf( sb, "                </div>\n\n" );
     5         kx 
     5         kx             ++printed;
     5         kx           }
     5         kx 
     5         kx           xmlFree( date );
     5         kx           xmlFree( cmsg );
     5         kx           xmlFree( revision );
     5         kx           xmlFree( author );
     5         kx         }
     5         kx         else
     5         kx         {
     5         kx           if( date )     xmlFree( date );
     5         kx           if( cmsg )     xmlFree( cmsg );
     5         kx           if( revision ) xmlFree( revision );
     5         kx           if( author )   xmlFree( author );
     5         kx         }
     5         kx       }
     5         kx     }
     5         kx 
     5         kx     strbuf_addf( sb, "              </div> <!-- End of Log -->\n\n" );
     5         kx 
     5         kx     /********************************
     5         kx       Print log direction:
     5         kx      */
     5         kx     reminder = count - ctx.query.ofs - printed;
     5         kx     if( page_size < count )
     5         kx     {
     5         kx       int prev, next;
     5         kx 
     5         kx       strbuf_addf( sb, "              <div class=\"log-direction\">\n" );
     5         kx       strbuf_addf( sb, "                <div class=\"row\">\n" );
     5         kx       strbuf_addf( sb, "                  <div class=\"left col-prev\">\n" );
     5         kx       strbuf_addf( sb, "                    <div class=\"prev-log-direction\">\n" );
     5         kx 
     5         kx       if( ctx.query.ofs )
     5         kx       {
     5         kx         prev = ctx.query.ofs - page_size;
     5         kx         if( prev < 0 ) prev = 0;
     5         kx 
     5         kx         if( ctx.env.query_string && *ctx.env.query_string )
     5         kx         {
     5         kx           if( ctx.repo.repo_root && *ctx.repo.repo_root )
     5         kx           {
     5         kx             strbuf_addf( sb, "                      <a href=\"/%s/%s%s/?ofs=%d&%s\">&#x226a;&nbsp; Prev</a>\n", ctx.repo.repo_root, ctx.repo.name, path, prev, ctx.env.query_string );
     5         kx           }
     5         kx           else
     5         kx           {
     5         kx             strbuf_addf( sb, "                      <a href=\"/%s%s/?ofs=%d&%s\">&#x226a;&nbsp; Prev</a>\n", ctx.repo.name, path, prev, ctx.env.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, "                      <a href=\"/%s/%s%s/?ofs=%d\">&#x226a;&nbsp; Prev</a>\n", ctx.repo.repo_root, ctx.repo.name, path, prev );
     5         kx           }
     5         kx           else
     5         kx           {
     5         kx             strbuf_addf( sb, "                      <a href=\"/%s%s/?ofs=%d\">&#x226a;&nbsp; Prev</a>\n", ctx.repo.name, path, prev );
     5         kx           }
     5         kx         }
     5         kx       }
     5         kx 
     5         kx       strbuf_addf( sb, "                    </div>\n" );
     5         kx       strbuf_addf( sb, "                  </div>\n" );
     5         kx       strbuf_addf( sb, "                  <div class=\"right col-next\">\n" );
     5         kx       strbuf_addf( sb, "                    <div class=\"next-log-direction\">\n" );
     5         kx 
     5         kx       if( reminder )
     5         kx       {
     5         kx         next = ctx.query.ofs + page_size;
     5         kx 
     5         kx         if( ctx.env.query_string && *ctx.env.query_string )
     5         kx         {
     5         kx           if( ctx.repo.repo_root && *ctx.repo.repo_root )
     5         kx           {
     5         kx             strbuf_addf( sb, "                      <a href=\"/%s/%s%s/?ofs=%d&%s\">Next &nbsp;&#x226b;</a>\n", ctx.repo.repo_root, ctx.repo.name, path, next, ctx.env.query_string );
     5         kx           }
     5         kx           else
     5         kx           {
     5         kx             strbuf_addf( sb, "                      <a href=\"/%s%s/?ofs=%d&%s\">Next &nbsp;&#x226b;</a>\n", ctx.repo.name, path, next, ctx.env.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, "                      <a href=\"/%s/%s%s/?ofs=%d\">Next &nbsp;&#x226b;</a>\n", ctx.repo.repo_root, ctx.repo.name, path, next );
     5         kx           }
     5         kx           else
     5         kx           {
     5         kx             strbuf_addf( sb, "                      <a href=\"/%s%s/?ofs=%d\">Next &nbsp;&#x226b;</a>\n", ctx.repo.name, path, next );
     5         kx           }
     5         kx         }
     5         kx       }
     5         kx 
     5         kx       strbuf_addf( sb, "                    </div>\n" );
     5         kx       strbuf_addf( sb, "                  </div>\n" );
     5         kx       strbuf_addf( sb, "                </div>\n" );
     5         kx       strbuf_addf( sb, "              </div>\n" );
     5         kx     }
     5         kx     /*
     5         kx       End of printing log direction.
     5         kx      ********************************/
     5         kx 
     5         kx   }
     5         kx 
     5         kx   free( path );
     5         kx   xmlFreeDoc(doc);
     5         kx   xmlCleanupParser();
     5         kx }
     5         kx 
     5         kx static void csvn_print_log( 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   char *path = NULL;
     5         kx 
     5         kx   if( !sb ) return;
     5         kx 
     5         kx   if( relative_path && *relative_path )
     5         kx   {
     5         kx     path = (char *)xmalloc( strlen( relative_path ) + 2 );
     5         kx     path[0] = '/';
     5         kx     sprintf( (char *)&path[1], relative_path );
     5         kx   }
     5         kx   else
     5         kx   {
     5         kx     path = (char *)xmalloc( 1 );
     5         kx     path[0] = '\0';
     5         kx   }
     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 log --revision %d:0 --xml %s/%s%s 2>/dev/null",
     5         kx                 revision, co_prefix, (char *)&repo_path[0], path );
     5         kx     else
     5         kx       snprintf( (char *)&cmd[0], 1024,
     5         kx                 "svn log --xml %s/%s%s 2>/dev/null",
     5         kx                 co_prefix, (char *)&repo_path[0], 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       free( path );
     5         kx       return;
     5         kx     }
     5         kx 
     5         kx     xml_csvn_log( sb, (const struct strbuf *)&buf, relative_path );
     5         kx 
     5         kx     strbuf_release( &buf );
     5         kx   }
     5         kx 
     5         kx   free( path );
     5         kx   return;
     5         kx }
     5         kx 
     5         kx 
     5         kx void csvn_print_log_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.name )
     5         kx   {
     5         kx     csvn_print_log( &buf, ctx.repo.relative_path, 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   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 }