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

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <sys/sysinfo.h>
#include <sys/types.h>
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#else
#include <stdint.h>
#endif
#include <stddef.h>   /* offsetof(3) */
#include <dirent.h>
#include <sys/stat.h> /* chmod(2)    */
#include <sys/file.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <limits.h>
#include <string.h>   /* strdup(3)   */
#include <libgen.h>   /* basename(3) */
#include <ctype.h>    /* tolower(3)  */
#include <errno.h>
#include <time.h>
#include <sys/time.h>
#include <pwd.h>
#include <grp.h>
#include <stdarg.h>
#include <locale.h>
#include <unistd.h>

#include <git2.h>

#include <nls.h>

#include <defs.h>

#include <fatal.h>
#include <http.h>
#include <html.h>

#include <dlist.h>
#include <strbuf.h>
#include <repolist.h>
#include <wrapper.h>
#include <system.h>
#include <date.h>

#include <ctx.h>
#include <git-shared.h>
#include <ui-shared.h>
#include <ui-repolist.h>


void cgit_print_direction( struct strbuf *sb, int prev, int next )
{
  strbuf_addf( sb, "              <div class=\"direction\">\n" );
  strbuf_addf( sb, "                <div class=\"row\">\n" );
  strbuf_addf( sb, "                  <div class=\"left col-prev\">\n" );
  strbuf_addf( sb, "                    <div class=\"prev-direction\">\n" );

  if( prev )
  {
    if( ctx.env.query_string && *ctx.env.query_string )
      strbuf_addf( sb, "                      <a href=\"%s?ofs=%d&%s\">&#x226a;&nbsp; Prev</a>\n", ctx.repo.relative_href, prev, ctx.env.query_string );
    else
      strbuf_addf( sb, "                      <a href=\"%s?ofs=%d\">&#x226a;&nbsp; Prev</a>\n", ctx.repo.relative_href, prev );
  }
  else
  {
    if( ctx.env.query_string && *ctx.env.query_string )
      strbuf_addf( sb, "                      <a href=\"%s?%s\">&#x226a;&nbsp; Prev</a>\n", ctx.repo.relative_href, ctx.env.query_string );
    else
      strbuf_addf( sb, "                      <a href=\"%s\">&#x226a;&nbsp; Prev</a>\n", ctx.repo.relative_href );
  }

  strbuf_addf( sb, "                    </div>\n" );
  strbuf_addf( sb, "                  </div>\n" );
  strbuf_addf( sb, "                  <div class=\"right col-next\">\n" );
  strbuf_addf( sb, "                    <div class=\"next-direction\">\n" );

  if( next )
  {
    if( ctx.env.query_string && *ctx.env.query_string )
      strbuf_addf( sb, "                      <a href=\"%s?ofs=%d&%s\">Next &nbsp;&#x226b;</a>\n", ctx.repo.relative_href, next, ctx.env.query_string );
    else
      strbuf_addf( sb, "                      <a href=\"%s?ofs=%d\">Next &nbsp;&#x226b;</a>\n", ctx.repo.relative_href, next );
  }
  else
  {
    if( ctx.env.query_string && *ctx.env.query_string )
      strbuf_addf( sb, "                      <a href=\"%s?%s\">Next &nbsp;&#x226b;</a>\n", ctx.repo.relative_href, ctx.env.query_string );
    else
      strbuf_addf( sb, "                      <a href=\"%s\">Next &nbsp;&#x226b;</a>\n", ctx.repo.relative_href );
  }

  strbuf_addf( sb, "                    </div>\n" );
  strbuf_addf( sb, "                  </div>\n" );
  strbuf_addf( sb, "                </div>\n" );
  strbuf_addf( sb, "              </div>\n" );
}

void cgit_print_repolist_header( struct strbuf *sb )
{
  strbuf_addf( sb, "              <div class=\"repo-list-header\">\n" );
  strbuf_addf( sb, "                <div class=\"row\">\n" );
  strbuf_addf( sb, "                  <div class=\"col-name\">Name</div>\n" );
  strbuf_addf( sb, "                  <div class=\"col-desc\">Description</div>\n" );
  strbuf_addf( sb, "                  <div class=\"col-owner\"><div class=\"repo-owner trunc\">Owner</div></div>\n" );
  strbuf_addf( sb, "                  <div class=\"col-rev\"><div class=\"repo-rev trunc\">Rev</div></div>\n" );
  strbuf_addf( sb, "                  <div class=\"col-idle\"><div class=\"repo-idle trunc\">Idle</div></div>\n" );
  strbuf_addf( sb, "                </div>\n" );
  strbuf_addf( sb, "              </div>\n" );
}

void cgit_print_section_start( struct strbuf *sb, const char *name )
{
  if( !sb ) return;

  strbuf_addf( sb, "              <div class=\"section\">\n" );
  if( name && *name )
    strbuf_addf( sb, "                <div class=\"section-header\">%s</div>\n\n", name );
  else
    strbuf_addf( sb, "                <div class=\"section-header\"></div>\n\n" );
}

void cgit_print_section_stop( struct strbuf *sb )
{
  if( !sb ) return;
  strbuf_addf( sb, "              </div> <!-- end of section -->\n\n" );
}

void cgit_print_repo( struct strbuf *sb, struct repo *repo )
{
  struct variable git_root    = { (unsigned char *)"git-root",    { 0 }, DT_PATH   },
                  repo_root   = { (unsigned char *)"repo-root",   { 0 }, DT_PATH   },
                  owner       = { (unsigned char *)"owner",       { 0 }, DT_STRING },
                  description = { (unsigned char *)"description", { 0 }, DT_STRING };
  struct variable *groot = NULL;
  struct variable *rroot = NULL;
  struct variable *auth  = NULL;
  struct variable *desc  = NULL;

  struct short_commit_info info = { .rev = { 0 }, .date = -1, .offset = 0 };
  char path[PATH_MAX]           = { 0 };

  if( !sb || !repo || !repo->path ) return;

  groot = lookup( repo, &git_root );
  rroot = lookup( repo, &repo_root );
  auth  = lookup( repo, &owner );
  desc  = lookup( repo, &description );

  if( rroot )
    sprintf( path, "%s/%s/%s", (const char *)groot->_v.vptr, (const char *)rroot->_v.vptr, repo->path );
  else
    sprintf( path, "%s/%s", (const char *)groot->_v.vptr, repo->path );

  fill_short_commit_info( &info, (const char *)&path[0] );

  strbuf_addf( sb, "                <div class=\"row\">\n" );
  if( rroot )
    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 );
  else
    strbuf_addf( sb, "                  <div class=\"col-name\"><a href=\"/%s/\"><div class=\"repo-name\">%s</div></a></div>\n", repo->path, repo->path );

  if( desc )
    strbuf_addf( sb, "                  <div class=\"col-desc\"><div onclick=\"trunc(this)\" class=\"repo-desc trunc\">%s</div></div>\n", (const char *)desc->_v.vptr );
  else
    strbuf_addf( sb, "                  <div class=\"col-desc\"><div onclick=\"trunc(this)\" class=\"repo-desc trunc\">none</div></div>\n" );

  if( auth )
    strbuf_addf( sb, "                  <div class=\"col-owner\"><div onclick=\"trunc(this)\" class=\"repo-owner trunc\">%s</div></div>\n", (const char *)auth->_v.vptr );
  else
    strbuf_addf( sb, "                  <div class=\"col-owner\"><div onclick=\"trunc(this)\" class=\"repo-owner trunc\">none</div></div>\n" );

  if( info.rev[0] )
    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] );
  else
    strbuf_addf( sb, "                  <div class=\"col-rev\"><div onclick=\"trunc(this)\" class=\"repo-rev trunc\">%s</div></div>\n", "0" );

  if( info.date != -1 )
  {
    struct strbuf buf = STRBUF_INIT;
    cgit_print_age( &buf, (time_t)info.date, info.offset, TM_YEAR );
    strbuf_addf( sb, "                  <div class=\"col-idle\"><div onclick=\"trunc(this)\" class=\"repo-idle trunc\">%s</div></div>\n", buf.buf );
    strbuf_release( &buf );
  }
  else
    strbuf_addf( sb, "                  <div class=\"col-idle\"><div onclick=\"trunc(this)\" class=\"repo-idle trunc\">%s</div></div>\n", "unknown" );

  strbuf_addf( sb, "                </div>\n\n" );
}


int cgit_print_page_repolist( struct strbuf *sb, struct dlist *config, int start_no, int n )
{
  struct dlist *list = NULL;
  int length = 0;

  if( !config || !sb ) return length;

  list = config;

  if( start_no > 0 )
  {
    list = parent_section_node_repolist_nth( config, start_no );
    {
      struct section *section = (struct section *)list->data;
      struct dlist *rlist = parent_rlist_node_repolist_nth( config, start_no );

      cgit_print_section_start( sb, (const char *)section->name );

      while( rlist )
      {
        struct repo *repo = (struct repo *)rlist->data;

        cgit_print_repo( sb, repo );
        if( n && ++length == n )
        {
          cgit_print_section_stop( sb );
          return length;
        }

        rlist = dlist_next( rlist );
      }
      cgit_print_section_stop( sb );
    }
    list = dlist_next( list );
  }

  while( list )
  {
    struct section *section = (struct section *)list->data;

    if( section->type == ST_REPOS )
    {
      cgit_print_section_start( sb, (const char *)section->name );

      struct dlist *rlist = section->list;
      while( rlist )
      {
        struct repo *repo = (struct repo *)rlist->data;

        cgit_print_repo( sb, repo );
        if( n && ++length == n )
        {
          cgit_print_section_stop( sb );
          return length;
        }

        rlist = dlist_next( rlist );
      }

      cgit_print_section_stop( sb );
    }
    list = dlist_next( list );
  }

  return length;
}


void cgit_print_repolist_page( void )
{
  FILE  *fp;
  struct strbuf buf = STRBUF_INIT;
  int psize = 200, rnum = 0, pos = 0, prev, next;

  fp = xfopen( ctx.page.header, "r" );
  (void)strbuf_env_fread( &buf, fp );
  fclose( fp );

  strbuf_addf( &buf, "        <div class=\"content segment\">\n" );
  strbuf_addf( &buf, "          <div class=\"container\">\n" );
  strbuf_addf( &buf, "            <div class=\"cgit-main-content\">\n" );

  if( ctx.vars.page_size && *ctx.vars.page_size )
    sscanf( ctx.vars.page_size, "%d", &psize );

  if( ctx.vars.num_of_repos && *ctx.vars.num_of_repos )
    sscanf( ctx.vars.num_of_repos, "%d", &rnum );

  if( ctx.query.ofs > 0)     pos = ctx.query.ofs;
  if( ctx.query.ofs > rnum-1 ) pos = rnum - rnum % psize;
  if( pos < 0 )              pos = 0;

  prev = pos - psize;
  if( prev < 0 ) prev = 0;

  next = pos + psize;
  if( next > rnum-1 ) next = rnum - rnum % psize;
  if( next < 0 ) next = 0;

  cgit_print_repolist_header( &buf );
  (void)cgit_print_page_repolist( &buf, config, pos, psize );
  cgit_print_direction( &buf, prev, next );

  strbuf_addf( &buf, "            </div> <!-- End of cgit-main-content -->\n" );
  strbuf_addf( &buf, "          </div> <!-- End of container -->\n" );
  strbuf_addf( &buf, "        </div> <!-- End of content segment -->\n" );

  fp = xfopen( ctx.page.footer, "r" );
  (void)strbuf_env_fread( &buf, fp );
  fclose( fp );

  ctx.page.size = buf.len;
  cgit_print_http_headers();
  strbuf_write( &buf, STDOUT_FILENO );
  strbuf_release( &buf );
}