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 <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 #include <ui-repolist.h>
     5         kx 
     5         kx 
     5         kx void cgit_print_direction( struct strbuf *sb, int prev, int next )
     5         kx {
     5         kx   strbuf_addf( sb, "              <div class=\"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-direction\">\n" );
     5         kx 
     5         kx   if( prev )
     5         kx   {
     5         kx     if( ctx.env.query_string && *ctx.env.query_string )
     5         kx       strbuf_addf( sb, "                      <a href=\"%s?ofs=%d&%s\">&#x226a;&nbsp; Prev</a>\n", ctx.repo.relative_href, prev, ctx.env.query_string );
     5         kx     else
     5         kx       strbuf_addf( sb, "                      <a href=\"%s?ofs=%d\">&#x226a;&nbsp; Prev</a>\n", ctx.repo.relative_href, prev );
     5         kx   }
     5         kx   else
     5         kx   {
     5         kx     if( ctx.env.query_string && *ctx.env.query_string )
     5         kx       strbuf_addf( sb, "                      <a href=\"%s?%s\">&#x226a;&nbsp; Prev</a>\n", ctx.repo.relative_href, ctx.env.query_string );
     5         kx     else
     5         kx       strbuf_addf( sb, "                      <a href=\"%s\">&#x226a;&nbsp; Prev</a>\n", ctx.repo.relative_href );
     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-direction\">\n" );
     5         kx 
     5         kx   if( next )
     5         kx   {
     5         kx     if( ctx.env.query_string && *ctx.env.query_string )
     5         kx       strbuf_addf( sb, "                      <a href=\"%s?ofs=%d&%s\">Next &nbsp;&#x226b;</a>\n", ctx.repo.relative_href, next, ctx.env.query_string );
     5         kx     else
     5         kx       strbuf_addf( sb, "                      <a href=\"%s?ofs=%d\">Next &nbsp;&#x226b;</a>\n", ctx.repo.relative_href, next );
     5         kx   }
     5         kx   else
     5         kx   {
     5         kx     if( ctx.env.query_string && *ctx.env.query_string )
     5         kx       strbuf_addf( sb, "                      <a href=\"%s?%s\">Next &nbsp;&#x226b;</a>\n", ctx.repo.relative_href, ctx.env.query_string );
     5         kx     else
     5         kx       strbuf_addf( sb, "                      <a href=\"%s\">Next &nbsp;&#x226b;</a>\n", ctx.repo.relative_href );
     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 void cgit_print_repolist_header( struct strbuf *sb )
     5         kx {
     5         kx   strbuf_addf( sb, "              <div class=\"repo-list-header\">\n" );
     5         kx   strbuf_addf( sb, "                <div class=\"row\">\n" );
     5         kx   strbuf_addf( sb, "                  <div class=\"col-name\">Name</div>\n" );
     5         kx   strbuf_addf( sb, "                  <div class=\"col-desc\">Description</div>\n" );
     5         kx   strbuf_addf( sb, "                  <div class=\"col-owner\"><div class=\"repo-owner trunc\">Owner</div></div>\n" );
     5         kx   strbuf_addf( sb, "                  <div class=\"col-rev\"><div class=\"repo-rev trunc\">Rev</div></div>\n" );
     5         kx   strbuf_addf( sb, "                  <div class=\"col-idle\"><div class=\"repo-idle trunc\">Idle</div></div>\n" );
     5         kx   strbuf_addf( sb, "                </div>\n" );
     5         kx   strbuf_addf( sb, "              </div>\n" );
     5         kx }
     5         kx 
     5         kx void cgit_print_section_start( struct strbuf *sb, const char *name )
     5         kx {
     5         kx   if( !sb ) return;
     5         kx 
     5         kx   strbuf_addf( sb, "              <div class=\"section\">\n" );
     5         kx   if( name && *name )
     5         kx     strbuf_addf( sb, "                <div class=\"section-header\">%s</div>\n\n", name );
     5         kx   else
     5         kx     strbuf_addf( sb, "                <div class=\"section-header\"></div>\n\n" );
     5         kx }
     5         kx 
     5         kx void cgit_print_section_stop( struct strbuf *sb )
     5         kx {
     5         kx   if( !sb ) return;
     5         kx   strbuf_addf( sb, "              </div> <!-- end of section -->\n\n" );
     5         kx }
     5         kx 
     5         kx void cgit_print_repo( struct strbuf *sb, struct repo *repo )
     5         kx {
     5         kx   struct variable git_root    = { (unsigned char *)"git-root",    { 0 }, DT_PATH   },
     5         kx                   repo_root   = { (unsigned char *)"repo-root",   { 0 }, DT_PATH   },
     5         kx                   owner       = { (unsigned char *)"owner",       { 0 }, DT_STRING },
     5         kx                   description = { (unsigned char *)"description", { 0 }, DT_STRING };
     5         kx   struct variable *groot = NULL;
     5         kx   struct variable *rroot = NULL;
     5         kx   struct variable *auth  = NULL;
     5         kx   struct variable *desc  = NULL;
     5         kx 
     5         kx   struct short_commit_info info = { .rev = { 0 }, .date = -1, .offset = 0 };
     5         kx   char path[PATH_MAX]           = { 0 };
     5         kx 
     5         kx   if( !sb || !repo || !repo->path ) return;
     5         kx 
     5         kx   groot = lookup( repo, &git_root );
     5         kx   rroot = lookup( repo, &repo_root );
     5         kx   auth  = lookup( repo, &owner );
     5         kx   desc  = lookup( repo, &description );
     5         kx 
     5         kx   if( rroot )
     5         kx     sprintf( path, "%s/%s/%s", (const char *)groot->_v.vptr, (const char *)rroot->_v.vptr, repo->path );
     5         kx   else
     5         kx     sprintf( path, "%s/%s", (const char *)groot->_v.vptr, repo->path );
     5         kx 
     5         kx   fill_short_commit_info( &info, (const char *)&path[0] );
     5         kx 
     5         kx   strbuf_addf( sb, "                <div class=\"row\">\n" );
     5         kx   if( rroot )
     5         kx     strbuf_addf( sb, "                  <div class=\"col-name\"><a href=\"/%s/%s/\"><div class=\"repo-name\">%s</div></a></div>\n", (const char *)rroot->_v.vptr, repo->path, repo->path );
     5         kx   else
     5         kx     strbuf_addf( sb, "                  <div class=\"col-name\"><a href=\"/%s/\"><div class=\"repo-name\">%s</div></a></div>\n", repo->path, repo->path );
     5         kx 
     5         kx   if( desc )
     5         kx     strbuf_addf( sb, "                  <div class=\"col-desc\"><div onclick=\"trunc(this)\" class=\"repo-desc trunc\">%s</div></div>\n", (const char *)desc->_v.vptr );
     5         kx   else
     5         kx     strbuf_addf( sb, "                  <div class=\"col-desc\"><div onclick=\"trunc(this)\" class=\"repo-desc trunc\">none</div></div>\n" );
     5         kx 
     5         kx   if( auth )
     5         kx     strbuf_addf( sb, "                  <div class=\"col-owner\"><div onclick=\"trunc(this)\" class=\"repo-owner trunc\">%s</div></div>\n", (const char *)auth->_v.vptr );
     5         kx   else
     5         kx     strbuf_addf( sb, "                  <div class=\"col-owner\"><div onclick=\"trunc(this)\" class=\"repo-owner trunc\">none</div></div>\n" );
     5         kx 
     5         kx   if( info.rev[0] )
     5         kx     strbuf_addf( sb, "                  <div class=\"col-rev\"><div onclick=\"trunc(this)\" class=\"repo-rev trunc\"><span class=\"rev-short\" title=\"%s\">%0.8s</span></div></div>\n", (const char *)&info.rev[0], (const char *)&info.rev[0] );
     5         kx   else
     5         kx     strbuf_addf( sb, "                  <div class=\"col-rev\"><div onclick=\"trunc(this)\" class=\"repo-rev trunc\">%s</div></div>\n", "0" );
     5         kx 
     5         kx   if( info.date != -1 )
     5         kx   {
     5         kx     struct strbuf buf = STRBUF_INIT;
     5         kx     cgit_print_age( &buf, (time_t)info.date, info.offset, TM_YEAR );
     5         kx     strbuf_addf( sb, "                  <div class=\"col-idle\"><div onclick=\"trunc(this)\" class=\"repo-idle trunc\">%s</div></div>\n", buf.buf );
     5         kx     strbuf_release( &buf );
     5         kx   }
     5         kx   else
     5         kx     strbuf_addf( sb, "                  <div class=\"col-idle\"><div onclick=\"trunc(this)\" class=\"repo-idle trunc\">%s</div></div>\n", "unknown" );
     5         kx 
     5         kx   strbuf_addf( sb, "                </div>\n\n" );
     5         kx }
     5         kx 
     5         kx 
     5         kx int cgit_print_page_repolist( struct strbuf *sb, struct dlist *config, int start_no, int n )
     5         kx {
     5         kx   struct dlist *list = NULL;
     5         kx   int length = 0;
     5         kx 
     5         kx   if( !config || !sb ) return length;
     5         kx 
     5         kx   list = config;
     5         kx 
     5         kx   if( start_no > 0 )
     5         kx   {
     5         kx     list = parent_section_node_repolist_nth( config, start_no );
     5         kx     {
     5         kx       struct section *section = (struct section *)list->data;
     5         kx       struct dlist *rlist = parent_rlist_node_repolist_nth( config, start_no );
     5         kx 
     5         kx       cgit_print_section_start( sb, (const char *)section->name );
     5         kx 
     5         kx       while( rlist )
     5         kx       {
     5         kx         struct repo *repo = (struct repo *)rlist->data;
     5         kx 
     5         kx         cgit_print_repo( sb, repo );
     5         kx         if( n && ++length == n )
     5         kx         {
     5         kx           cgit_print_section_stop( sb );
     5         kx           return length;
     5         kx         }
     5         kx 
     5         kx         rlist = dlist_next( rlist );
     5         kx       }
     5         kx       cgit_print_section_stop( sb );
     5         kx     }
     5         kx     list = dlist_next( list );
     5         kx   }
     5         kx 
     5         kx   while( list )
     5         kx   {
     5         kx     struct section *section = (struct section *)list->data;
     5         kx 
     5         kx     if( section->type == ST_REPOS )
     5         kx     {
     5         kx       cgit_print_section_start( sb, (const char *)section->name );
     5         kx 
     5         kx       struct dlist *rlist = section->list;
     5         kx       while( rlist )
     5         kx       {
     5         kx         struct repo *repo = (struct repo *)rlist->data;
     5         kx 
     5         kx         cgit_print_repo( sb, repo );
     5         kx         if( n && ++length == n )
     5         kx         {
     5         kx           cgit_print_section_stop( sb );
     5         kx           return length;
     5         kx         }
     5         kx 
     5         kx         rlist = dlist_next( rlist );
     5         kx       }
     5         kx 
     5         kx       cgit_print_section_stop( sb );
     5         kx     }
     5         kx     list = dlist_next( list );
     5         kx   }
     5         kx 
     5         kx   return length;
     5         kx }
     5         kx 
     5         kx 
     5         kx void cgit_print_repolist_page( void )
     5         kx {
     5         kx   FILE  *fp;
     5         kx   struct strbuf buf = STRBUF_INIT;
     5         kx   int psize = 200, rnum = 0, pos = 0, prev, next;
     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.vars.page_size && *ctx.vars.page_size )
     5         kx     sscanf( ctx.vars.page_size, "%d", &psize );
     5         kx 
     5         kx   if( ctx.vars.num_of_repos && *ctx.vars.num_of_repos )
     5         kx     sscanf( ctx.vars.num_of_repos, "%d", &rnum );
     5         kx 
     5         kx   if( ctx.query.ofs > 0)     pos = ctx.query.ofs;
     5         kx   if( ctx.query.ofs > rnum-1 ) pos = rnum - rnum % psize;
     5         kx   if( pos < 0 )              pos = 0;
     5         kx 
     5         kx   prev = pos - psize;
     5         kx   if( prev < 0 ) prev = 0;
     5         kx 
     5         kx   next = pos + psize;
     5         kx   if( next > rnum-1 ) next = rnum - rnum % psize;
     5         kx   if( next < 0 ) next = 0;
     5         kx 
     5         kx   cgit_print_repolist_header( &buf );
     5         kx   (void)cgit_print_page_repolist( &buf, config, pos, psize );
     5         kx   cgit_print_direction( &buf, prev, next );
     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 }