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 
     5         kx 
     5         kx static void print_log_start( struct strbuf *sb )
     5         kx {
     5         kx   if( !sb ) return;
     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   strbuf_addf( sb, "              <div class=\"log\">\n\n" );
     5         kx }
     5         kx 
     5         kx static void print_log_stop( struct strbuf *sb )
     5         kx {
     5         kx   if( !sb ) return;
     5         kx   strbuf_addf( sb, "              </div> <!-- End of Log -->\n\n" );
     5         kx }
     5         kx 
     5         kx static void print_direction( struct strbuf *sb, const char *relative_path, int reminder, int limit )
     5         kx {
     5         kx   char *path = NULL;
     5         kx   int   page_size = atoi( ctx.vars.page_size );
     5         kx   int   prev, next;
     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   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       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     else
     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   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       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     else
     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   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   free( path );
     5         kx }
     5         kx 
     5         kx static void print_commit_line( struct strbuf *sb, git_commit *commit, const char *relative_path, const char *revision )
     5         kx {
     5         kx   char *path = NULL;
     5         kx 
     5         kx   if( !sb || !commit ) 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   {
     5         kx     char id[GIT_OID_HEXSZ+1] = { 0 };
     5         kx 
     5         kx     const git_oid       *oid     = git_commit_id( commit );
     5         kx     const char          *message = git_commit_summary( commit );
     5         kx     git_time_t           date    = git_commit_time( commit );
     5         kx     int                  offset  = git_commit_time_offset( commit );
     5         kx     const git_signature *author  = git_commit_author( commit );
     5         kx 
     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     offset = (offset % 60) + ((offset / 60) * 100);
     5         kx 
     5         kx     if( git_oid_fmt( (char *)&id[0], oid ) < 0 || strcmp( (char *)&id[0], revision ) ) return;
     5         kx 
     5         kx 
     5         kx     strbuf_addf( sb, "                <div class=\"row\">\n" );
     5         kx     if( date != -1 )
     5         kx     {
     5         kx       strbuf_addf( sb, "                  <div class=\"col-date\"><div onclick=\"trunc(this)\" class=\"log-date trunc\">" );
     5         kx       cgit_print_age( sb, (time_t)date, offset, 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 *)message );
     5         kx     if( query_string && *query_string )
     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\"><span class=\"rev-short\">%0.8s</span></a></div></div>\n", 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\"><span class=\"rev-short\">%0.8s</span></a></div></div>\n", ctx.repo.name, path, (char *)revision, (char *)revision );
     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->name );
     5         kx     strbuf_addf( sb, "                </div>\n\n" );
     5         kx   }
     5         kx 
     5         kx   free( path );
     5         kx }
     5         kx 
     5         kx 
     5         kx static void cgit_print_log( struct strbuf *sb, const char *relative_path, const char *revision )
     5         kx {
     5         kx   struct cgit_hex_commits *commits = NULL;
     5         kx   size_t i, limit, reminder, page_size = (size_t)atoi( ctx.vars.page_size );
     5         kx 
     5         kx   if( !sb ) return;
     5         kx 
     5         kx   print_log_start( sb );
     5         kx 
     5         kx   cgit_fill_commits_list( &commits, ctx.query.ofs, relative_path, revision );
     5         kx 
     5         kx   limit = min( page_size, commits->len );
     5         kx 
     5         kx   for( i = 0; i < limit; ++i )
     5         kx   {
     5         kx     git_commit *commit = NULL;
     5         kx     commit = lookup_commit_by_hex( commits->hex[i] );
     5         kx 
     5         kx     print_commit_line( sb, commit, relative_path, (const char *)commits->hex[i] );
     5         kx 
     5         kx     git_commit_free( commit );
     5         kx   }
     5         kx 
     5         kx   reminder = commits->len - limit;
     5         kx 
     5         kx   cgit_hex_commits_free( commits );
     5         kx 
     5         kx   print_log_stop( sb );
     5         kx 
     5         kx   if( !(!ctx.query.ofs && !reminder) )
     5         kx     print_direction( sb, relative_path, (int)reminder, (int)limit );
     5         kx }
     5         kx 
     5         kx 
     5         kx void cgit_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=\"cgit-main-content\">\n" );
     5         kx 
     5         kx   if( ctx.repo.name )
     5         kx   {
     5         kx     cgit_print_log( &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   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 }