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 <math.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 void cgit_change_location( const char *location )
     5         kx {
     5         kx   if( ctx.env.no_http && !strcmp( ctx.env.no_http, "1" ) )
     5         kx     return;
     5         kx 
     5         kx   htmlf( "Status: %d %s\n", 302, "Found" );
     5         kx   htmlf( "Location: %s\n", location );
     5         kx   html( "\n" );
     5         kx 
     5         kx   exit( 0 );
     5         kx }
     5         kx 
     5         kx void cgit_print_http_headers( void )
     5         kx {
     5         kx   if( ctx.env.no_http && !strcmp( ctx.env.no_http, "1" ) )
     5         kx     return;
     5         kx 
     5         kx   /*****************************************************
     5         kx     If we pass 'Status: 200 OK' header then nginx+uWsgi
     5         kx     will duplicate other headers randomly 
     5         kx    */
     5         kx   if( ctx.page.status != 200 )
     5         kx     htmlf( "Status: %d %s\n", ctx.page.status, ctx.page.status_message );
     5         kx 
     5         kx   if( ctx.page.mimetype && ctx.page.charset )
     5         kx     htmlf( "Content-Type: %s; charset=%s\n", ctx.page.mimetype, ctx.page.charset );
     5         kx   else if( ctx.page.mimetype )
     5         kx     htmlf( "Content-Type: %s\n", ctx.page.mimetype );
     5         kx 
     5         kx   if( ctx.page.size )
     5         kx     htmlf( "Content-Length: %zd\n", ctx.page.size );
     5         kx 
     5         kx   if( !ctx.env.authenticated )
     5         kx     html( "Cache-Control: no-cache, no-store\n" );
     5         kx 
     5         kx   htmlf( "Last-Modified: %s\n", http_date( ctx.page.modified ) );
     5         kx   htmlf( "Expires: %s\n", http_date( ctx.page.expires ) );
     5         kx 
     5         kx   html( "\n" );
     5         kx }
     5         kx 
     5         kx const struct date_mode *cgit_date_mode( enum date_mode_type type )
     5         kx {
     5         kx   static struct date_mode mode;
     5         kx   mode.type = type;
     5         kx   mode.local = 0; /* may be from ctx.cfg.local_time; =0/1 */
     5         kx   return &mode;
     5         kx }
     5         kx /* OR data_mode_from_type():
     5         kx    struct date_mode *mode = DATE_MODE( DATE_ISO8601 );
     5         kx    but in this case mode.local is always zero!
     5         kx  */
     5         kx 
     5         kx static void print_rel_date( struct strbuf *sb, time_t t, int tz,
     5         kx                             const char *class, const char *value )
     5         kx {
     5         kx   if( !sb || !t ) return;
     5         kx 
     5         kx   strbuf_addf( sb, "<span class='%s' title='", class );
     5         kx   show_date( sb, t, tz, cgit_date_mode( DATE_ISO8601 ) );
     5         kx   strbuf_addf( sb, "'>%s</span>", value );
     5         kx }
     5         kx 
     5         kx void cgit_print_age( struct strbuf *sb, time_t t, int tz, time_t max_relative )
     5         kx {
     5         kx   time_t now, secs;
     5         kx   char buf[32] = { 0 };
     5         kx   size_t val;
     5         kx 
     5         kx   if( !sb || !t ) return;
     5         kx 
     5         kx   now = time( NULL );
     5         kx   secs = now - t;
     5         kx   if( secs < 0 )
     5         kx     secs = 0;
     5         kx 
     5         kx   if( secs > max_relative && max_relative >= 0 )
     5         kx   {
     5         kx 
     5         kx     strbuf_addstr( sb, "<span title='" );
     5         kx     show_date( sb, t, tz, cgit_date_mode( DATE_ISO8601 ) );
     5         kx     strbuf_addstr( sb, "'>" );
     5         kx     show_date( sb, t, tz, cgit_date_mode( DATE_SHORT ) );
     5         kx     strbuf_addstr( sb, "</span>" );
     5         kx     return;
     5         kx   }
     5         kx 
     5         kx   if( secs < TM_HOUR * 2 )
     5         kx   {
     5         kx     val = (size_t)round(secs * 1.0 / TM_MIN);
     5         kx     snprintf( (char *)&buf[0], 32, Q_("%"PRIdMAX" minute", "%"PRIdMAX" minutes", val), val );
     5         kx     print_rel_date( sb, t, tz, "age-mins", (const char *)&buf[0] );
     5         kx     return;
     5         kx   }
     5         kx   if( secs < TM_DAY * 2 )
     5         kx   {
     5         kx     val = (size_t)round(secs * 1.0 / TM_HOUR);
     5         kx     snprintf( (char *)&buf[0], 32, Q_("%"PRIdMAX" hour", "%"PRIdMAX" hours", val), val );
     5         kx     print_rel_date( sb, t, tz, "age-hours", (const char *)&buf[0] );
     5         kx     return;
     5         kx   }
     5         kx   if( secs < TM_WEEK * 2 )
     5         kx   {
     5         kx     val = (size_t)round(secs * 1.0 / TM_DAY);
     5         kx     snprintf( (char *)&buf[0], 32, Q_("%"PRIdMAX" day", "%"PRIdMAX" days", val), val );
     5         kx     print_rel_date( sb, t, tz, "age-days", (const char *)&buf[0] );
     5         kx     return;
     5         kx   }
     5         kx   if( secs < TM_MONTH * 2 )
     5         kx   {
     5         kx     val = (size_t)round(secs * 1.0 / TM_WEEK);
     5         kx     snprintf( (char *)&buf[0], 32, Q_("%"PRIdMAX" week", "%"PRIdMAX" weeks", val), val );
     5         kx     print_rel_date( sb, t, tz, "age-weeks", (const char *)&buf[0] );
     5         kx     return;
     5         kx   }
     5         kx   if( secs < TM_YEAR * 2 )
     5         kx   {
     5         kx     val = (size_t)round(secs * 1.0 / TM_MONTH);
     5         kx     snprintf( (char *)&buf[0], 32, Q_("%"PRIdMAX" month", "%"PRIdMAX" months", val), val );
     5         kx     print_rel_date( sb, t, tz, "age-months", (const char *)&buf[0] );
     5         kx     return;
     5         kx   }
     5         kx 
     5         kx   val = (size_t)round(secs * 1.0 / TM_YEAR);
     5         kx   snprintf( (char *)&buf[0], 32, Q_("%"PRIdMAX" year", "%"PRIdMAX" years", val), val );
     5         kx   print_rel_date( sb, t, tz, "age-years", (const char *)&buf[0] );
     5         kx }
     5         kx 
     5         kx void cgit_search_repo( const char *path )
     5         kx {
     5         kx   struct repo *repo = NULL;
     5         kx 
     5         kx   if( !path || !*path ) return;
     5         kx 
     5         kx   repo = lookup_repo( config, path );
     5         kx   if( repo )
     5         kx   {
     5         kx     int position = repo_position( config, repo );
     5         kx     if( position != -1 )
     5         kx     {
     5         kx       char location[1024] = { 0 };
     5         kx 
     5         kx       sprintf( (char *)&location[0], "/?ofs=%d", position );
     5         kx       cgit_change_location( (const char *)&location[0] );
     5         kx     }
     5         kx   }
     5         kx }
     5         kx 
     5         kx void cgit_repo_info( struct cgit_info *info, const char *revision )
     5         kx {
     5         kx   const char *name = NULL, *git_root = NULL, *repo_root = NULL;
     5         kx 
     5         kx   if( !info ) return;
     5         kx 
     5         kx   name      = ctx.repo.name;
     5         kx   git_root  = ctx.repo.git_root;
     5         kx   repo_root = ctx.repo.repo_root;
     5         kx 
     5         kx   if( name && git_root )
     5         kx   {
     5         kx     char 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     sprintf( (char *)&path[0], "%s/", git_root );
     5         kx     if( repo_root && *repo_root )
     5         kx     {
     5         kx       strcat( (char *)&path[0], repo_root );
     5         kx       strcat( (char *)&path[0], "/" );
     5         kx     }
     5         kx     strcat( (char *)&path[0], name );
     5         kx 
     5         kx     if( !is_bare( (char *)&path[0] ) )
     5         kx     {
     5         kx       strcat( (char *)&path[0], "/.git" );
     5         kx     }
     5         kx 
     5         kx     if( revision && *revision )
     5         kx       snprintf( (char *)&cmd[0], 1024, "git --git-dir=%s log --pretty=format:'%%H' -1 %s 2>/dev/null", (char *)&path[0], revision );
     5         kx     else
     5         kx       snprintf( (char *)&cmd[0], 1024, "git --git-dir=%s log --pretty=format:'%%H' -1 refs/heads/%s 2>/dev/null", (char *)&path[0], ctx.repo.trunk );
     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     if( buf.buf[0] )
     5         kx     {
     5         kx       strbuf_trim( &buf );
     5         kx       strncpy( info->revision, buf.buf, GIT_OID_HEXSZ+1 );
     5         kx       fill_commit_info( info, (const char *)&path[0], NULL );
     5         kx     }
     5         kx 
     5         kx     strbuf_release( &buf );
     5         kx 
     5         kx   }
     5         kx   return;
     5         kx }
     5         kx 
     5         kx 
     5         kx static const char *lang_info_by_path( const char *path )
     5         kx {
     5         kx   char *lang = NULL, *name = NULL, *ext = NULL;
     5         kx 
     5         kx   if( !path || !*path ) return (const char *)lang;
     5         kx 
     5         kx   ext = rindex( path, '.' );
     5         kx   if( ext )
     5         kx   {
     5         kx     if( !strncasecmp( ext, ".mk", 3 )       ||
     5         kx         !strncasecmp( ext, ".make", 5 )     ||
     5         kx         !strncasecmp( ext, ".Makefile", 9 )   )
     5         kx     {
     5         kx       lang = (char *)__sbrk( 9 );
     5         kx       sprintf( lang, "Makefile" );
     5         kx     }
     5         kx     else if( !strncasecmp( ext, ".md", 3 ) )
     5         kx     {
     5         kx       lang = (char *)__sbrk( 9 );
     5         kx       sprintf( lang, "Markdown" );
     5         kx     }
     5         kx     else if( !strncasecmp( ext, ".diff", 5 ) ||
     5         kx              !strncasecmp( ext, ".patch", 6 )  )
     5         kx     {
     5         kx       lang = (char *)__sbrk( 9 );
     5         kx       sprintf( lang, "Diff" );
     5         kx     }
     5         kx     else if( !strncasecmp( ext, ".c", 2 ) ||
     5         kx              !strncasecmp( ext, ".h", 2 )   )
     5         kx     {
     5         kx       lang = (char *)__sbrk( 2 );
     5         kx       sprintf( lang, "C" );
     5         kx     }
     5         kx     else if( !strncasecmp( ext, ".cpp", 4 ) ||
     5         kx              !strncasecmp( ext, ".cxx", 4 ) ||
     5         kx              !strncasecmp( ext, ".hpp", 4 )   )
     5         kx     {
     5         kx       lang = (char *)__sbrk( 4 );
     5         kx       sprintf( lang, "C++" );
     5         kx     }
     5         kx     else if( !strncasecmp( ext, ".html", 5 ) )
     5         kx     {
     5         kx       lang = (char *)__sbrk( 5 );
     5         kx       sprintf( lang, "HTML" );
     5         kx     }
     5         kx     else if( !strncasecmp( ext, ".css", 4 ) )
     5         kx     {
     5         kx       lang = (char *)__sbrk( 4 );
     5         kx       sprintf( lang, "CSS" );
     5         kx     }
     5         kx     else if( !strncasecmp( ext, ".js", 3 ) )
     5         kx     {
     5         kx       lang = (char *)__sbrk( 11 );
     5         kx       sprintf( lang, "javascript" );
     5         kx     }
     5         kx     else if( !strncasecmp( ext, ".json", 5 ) )
     5         kx     {
     5         kx       lang = (char *)__sbrk( 5 );
     5         kx       sprintf( lang, "JSON" );
     5         kx     }
     5         kx     else if( !strncasecmp( ext, ".less", 5 ) )
     5         kx     {
     5         kx       lang = (char *)__sbrk( 5 );
     5         kx       sprintf( lang, "LESS" );
     5         kx     }
     5         kx     else if( !strncasecmp( ext, ".scss", 5 ) )
     5         kx     {
     5         kx       lang = (char *)__sbrk( 5 );
     5         kx       sprintf( lang, "SCSS" );
     5         kx     }
     5         kx     else if( !strncasecmp( ext, ".pl", 3 ) ||
     5         kx              !strncasecmp( ext, ".pm", 3 )   )
     5         kx     {
     5         kx       lang = (char *)__sbrk( 5 );
     5         kx       sprintf( lang, "Perl" );
     5         kx     }
     5         kx     else if( !strncasecmp( ext, ".py", 3 ) )
     5         kx     {
     5         kx       lang = (char *)__sbrk( 7 );
     5         kx       sprintf( lang, "Python" );
     5         kx     }
     5         kx     else if( !strncasecmp( ext, ".php", 4 ) )
     5         kx     {
     5         kx       lang = (char *)__sbrk( 4 );
     5         kx       sprintf( lang, "PHP" );
     5         kx     }
     5         kx     else if( !strncasecmp( ext, ".txt", 4 ) )
     5         kx     {
     5         kx       lang = (char *)__sbrk( 10 );
     5         kx       sprintf( lang, "plaintext" );
     5         kx     }
     5         kx     else if( !strncasecmp( ext, ".go", 3 ) )
     5         kx     {
     5         kx       lang = (char *)__sbrk( 3 );
     5         kx       sprintf( lang, "Go" );
     5         kx     }
     5         kx   }
     5         kx 
     5         kx   name = rindex( path, '/' );
     5         kx   if( name )
     5         kx     ++name;
     5         kx   else
     5         kx     name = (char *)path;
     5         kx 
     5         kx   if( name )
     5         kx   {
     5         kx     if( !strncasecmp( name, "Makefile", 8 ) )
     5         kx     {
     5         kx       lang = (char *)__sbrk( 9 );
     5         kx       sprintf( lang, "Makefile" );
     5         kx     }
     5         kx     else if( !strcasecmp( name, "README" ) )
     5         kx     {
     5         kx       lang = (char *)__sbrk( 10 );
     5         kx       sprintf( lang, "Plaintext" );
     5         kx     }
     5         kx     else if( !strcasecmp( name, "LICENSE" ) )
     5         kx     {
     5         kx       lang = (char *)__sbrk( 10 );
     5         kx       sprintf( lang, "Plaintext" );
     5         kx     }
     5         kx   }
     5         kx 
     5         kx   /***************************
     5         kx     all chances are exhausted. probably highlight.js do it correctly...
     5         kx    */
     5         kx   /*
     5         kx   if( !lang )
     5         kx   {
     5         kx     lang = (char *)__sbrk( 10 );
     5         kx     sprintf( lang, "plaintext" );
     5         kx   }
     5         kx   */
     5         kx 
     5         kx   return (const char *)lang;
     5         kx }
     5         kx 
     5         kx static const char *lang_info( const char *mime_type, const char *path )
     5         kx {
     5         kx   char *lang = NULL;
     5         kx 
     5         kx   if( !mime_type ) return (const char *)lang;
     5         kx 
     5         kx   if( !strncmp( mime_type, "text/", 5 ) )
     5         kx   {
     5         kx     if( !strncmp( mime_type, "text/x-c", 8 ) || !strncmp( mime_type, "text/x-csrc", 11 ) )
     5         kx     {
     5         kx       lang = (char *)__sbrk( 2 );
     5         kx       sprintf( lang, "C" );
     5         kx     }
     5         kx     else if( !strncmp( mime_type, "text/x-perl", 11 ) )
     5         kx     {
     5         kx       lang = (char *)__sbrk( 5 );
     5         kx       sprintf( lang, "Perl" );
     5         kx     }
     5         kx     else if( !strncmp( mime_type, "text/x-diff", 11 ) )
     5         kx     {
     5         kx       lang = (char *)__sbrk( 5 );
     5         kx       sprintf( lang, "Diff" );
     5         kx     }
     5         kx     else if( !strncmp( mime_type, "text/x-makefile", 15 ) )
     5         kx     {
     5         kx       lang = (char *)__sbrk( 9 );
     5         kx       sprintf( lang, "Makefile" );
     5         kx     }
     5         kx     else if( !strncmp( mime_type, "text/x-shell", 12 ) )
     5         kx     {
     5         kx       lang = (char *)__sbrk( 5 );
     5         kx       sprintf( lang, "Bash" );
     5         kx     }
     5         kx 
     5         kx     /*
     5         kx       The 'text/plain;' mime type does not mean that the file contains plain text.
     5         kx       But we have to identify the language:
     5         kx      */
     5         kx     if( !lang && path )
     5         kx     {
     5         kx       /* Here we can make lang identification by filename or extension */
     5         kx       lang = (char *)lang_info_by_path( path );
     5         kx     }
     5         kx   }
     5         kx 
     5         kx   return (const char *)lang;
     5         kx }
     5         kx 
     5         kx 
     5         kx void cgit_rpath_info( struct cgit_info *info, const char *relative_path, const char *revision )
     5         kx {
     5         kx   const char *name = NULL, *git_root = NULL, *repo_root = NULL;
     5         kx 
     5         kx   if( !info || !relative_path ) return;
     5         kx 
     5         kx   name      = ctx.repo.name;
     5         kx   git_root  = ctx.repo.git_root;
     5         kx   repo_root = ctx.repo.repo_root;
     5         kx 
     5         kx   if( name && git_root )
     5         kx   {
     5         kx     char ref[PATH_MAX] = { 0 };
     5         kx     char rpath[PATH_MAX] = { 0 };
     5         kx 
     5         kx     char 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     sprintf( (char *)&path[0], "%s/", git_root );
     5         kx     if( repo_root && *repo_root )
     5         kx     {
     5         kx       strcat( (char *)&path[0], repo_root );
     5         kx       strcat( (char *)&path[0], "/" );
     5         kx     }
     5         kx     strcat( (char *)&path[0], name );
     5         kx 
     5         kx     if( !is_bare( (char *)&path[0] ) )
     5         kx     {
     5         kx       strcat( (char *)&path[0], "/.git" );
     5         kx     }
     5         kx 
     5         kx     parse_relative_path( (char *)&ref[0], (char *)&rpath[0], relative_path );
     5         kx 
     5         kx     if( revision && *revision )
     5         kx       snprintf( (char *)&cmd[0], 1024, "git --git-dir=%s log --pretty=format:'%%H' -1 %s -- %s 2>/dev/null", (char *)&path[0], revision, (char *)&rpath[0] );
     5         kx     else
     5         kx       snprintf( (char *)&cmd[0], 1024, "git --git-dir=%s log --pretty=format:'%%H' -1 %s -- %s 2>/dev/null", (char *)&path[0], (char *)&ref[0], (char *)&rpath[0] );
     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     if( buf.buf[0] )
     5         kx     {
     5         kx       strbuf_trim( &buf );
     5         kx       strncpy( info->revision, buf.buf, GIT_OID_HEXSZ+1 );
     5         kx       fill_commit_info( info, (const char *)&path[0], (const char *)&rpath[0] );
     5         kx       if( info->kind == GIT_OBJECT_BLOB )
     5         kx       {
     5         kx         fill_mime_info( info, (const char *)&path[0], (const char *)&rpath[0] );
     5         kx         if( info->mode == GIT_FILEMODE_LINK )
     5         kx         {
     5         kx           int len = 10;
     5         kx           char *lang = (char *)__sbrk( len );
     5         kx           memcpy( (void *)lang, (const void *)"PlainText", len );
     5         kx           info->lang = (const char *)lang;
     5         kx         }
     5         kx         else
     5         kx           info->lang = lang_info( (const char *)(info->mime), (const char *)&rpath[0] );
     5         kx       }
     5         kx     }
     5         kx 
     5         kx     strbuf_release( &buf );
     5         kx 
     5         kx   }
     5         kx   return;
     5         kx }
     5         kx 
     5         kx 
     5         kx void cgit_repo_branches_number( struct cgit_repository *rctx )
     5         kx {
     5         kx   const char *name = NULL, *git_root = NULL, *repo_root = NULL, *trunk = NULL;
     5         kx 
     5         kx   if( !rctx ) return;
     5         kx 
     5         kx   name      = rctx->name;
     5         kx   git_root  = rctx->git_root;
     5         kx   repo_root = rctx->repo_root;
     5         kx   trunk     = rctx->trunk;
     5         kx 
     5         kx   if( name && git_root )
     5         kx   {
     5         kx     char path[PATH_MAX] = { 0 };
     5         kx     sprintf( (char *)&path[0], "%s/", git_root );
     5         kx     if( repo_root && *repo_root )
     5         kx     {
     5         kx       strcat( (char *)&path[0], repo_root );
     5         kx       strcat( (char *)&path[0], "/" );
     5         kx     }
     5         kx     strcat( (char *)&path[0], name );
     5         kx 
     5         kx     if( trunk && *trunk )
     5         kx       rctx->nbranches = (int)branches_number( (const char *)&path[0], trunk );
     5         kx     else
     5         kx       rctx->nbranches = (int)branches_number( (const char *)&path[0], NULL );
     5         kx   }
     5         kx   return;
     5         kx }
     5         kx 
     5         kx void cgit_repo_tags_number( struct cgit_repository *rctx )
     5         kx {
     5         kx   const char *name = NULL, *git_root = NULL, *repo_root = NULL;
     5         kx 
     5         kx   if( !rctx ) return;
     5         kx 
     5         kx   name      = rctx->name;
     5         kx   git_root  = rctx->git_root;
     5         kx   repo_root = rctx->repo_root;
     5         kx 
     5         kx   if( name && git_root )
     5         kx   {
     5         kx     char path[PATH_MAX] = { 0 };
     5         kx     sprintf( (char *)&path[0], "%s/", git_root );
     5         kx     if( repo_root && *repo_root )
     5         kx     {
     5         kx       strcat( (char *)&path[0], repo_root );
     5         kx       strcat( (char *)&path[0], "/" );
     5         kx     }
     5         kx     strcat( (char *)&path[0], name );
     5         kx 
     5         kx     rctx->ntags = (int)tags_number( (const char *)&path[0] );
     5         kx   }
     5         kx   return;
     5         kx }
     5         kx 
     5         kx void cgit_repo_commits_number( struct cgit_repository *rctx )
     5         kx {
     5         kx   const char *name = NULL, *git_root = NULL, *repo_root = NULL;
     5         kx 
     5         kx   if( !rctx ) return;
     5         kx 
     5         kx   name      = rctx->name;
     5         kx   git_root  = rctx->git_root;
     5         kx   repo_root = rctx->repo_root;
     5         kx 
     5         kx   if( name && git_root )
     5         kx   {
     5         kx     char 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     sprintf( (char *)&path[0], "%s/", git_root );
     5         kx     if( repo_root && *repo_root )
     5         kx     {
     5         kx       strcat( (char *)&path[0], repo_root );
     5         kx       strcat( (char *)&path[0], "/" );
     5         kx     }
     5         kx     strcat( (char *)&path[0], name );
     5         kx 
     5         kx     if( !is_bare( (char *)&path[0] ) )
     5         kx     {
     5         kx       strcat( (char *)&path[0], "/.git" );
     5         kx     }
     5         kx 
     5         kx     /* all commits in repo:
     5         kx        snprintf( (char *)&cmd[0], 1024, "git --git-dir=%s rev-list -9999 --all --count 2>/dev/null", (char *)&path[0] );
     5         kx      */
     5         kx     snprintf( (char *)&cmd[0], 1024, "git --git-dir=%s rev-list -9999 HEAD^{commit} --count 2>/dev/null", (char *)&path[0] );
     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     if( buf.buf[0] )
     5         kx     {
     5         kx       strbuf_trim( &buf );
     5         kx       rctx->ncommits = atoi( buf.buf );
     5         kx     }
     5         kx 
     5         kx     strbuf_release( &buf );
     5         kx   }
     5         kx   return;
     5         kx }
     5         kx 
     5         kx void cgit_git_version( struct cgit_versions *vctx )
     5         kx {
     5         kx   char cmd[1024];
     5         kx   struct strbuf buf = STRBUF_INIT;
     5         kx   pid_t p = (pid_t) -1;
     5         kx   int   rc;
     5         kx 
     5         kx   char *version = NULL;
     5         kx   int   len = 0;
     5         kx 
     5         kx   if( !vctx ) return;
     5         kx 
     5         kx   snprintf( (char *)&cmd[0], 1024, "git --version 2>/dev/null | cut -f3 -d' '" );
     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   if( buf.buf[0] )
     5         kx   {
     5         kx     strbuf_trim( &buf );
     5         kx 
     5         kx     len = (int)strlen( buf.buf ) + 1;
     5         kx     version = (char *)__sbrk( len );
     5         kx     memcpy( (void *)version, (const void *)buf.buf, (size_t)len );
     5         kx     vctx->git = (const char *)version;
     5         kx   }
     5         kx 
     5         kx   strbuf_release( &buf );
     5         kx 
     5         kx   return;
     5         kx }
     5         kx 
     5         kx void cgit_nginx_version( struct cgit_versions *vctx )
     5         kx {
     5         kx   char cmd[1024];
     5         kx   struct strbuf buf = STRBUF_INIT;
     5         kx   pid_t p = (pid_t) -1;
     5         kx   int   rc;
     5         kx 
     5         kx   char *version = NULL;
     5         kx   int   len = 0;
     5         kx 
     5         kx   if( !vctx ) return;
     5         kx 
     5         kx   snprintf( (char *)&cmd[0], 1024, "nginx -v 2>&1 | cut -f2 -d'/'" );
     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   if( buf.buf[0] )
     5         kx   {
     5         kx     strbuf_trim( &buf );
     5         kx 
     5         kx     len = (int)strlen( buf.buf ) + 1;
     5         kx     version = (char *)__sbrk( len );
     5         kx     memcpy( (void *)version, (const void *)buf.buf, (size_t)len );
     5         kx     vctx->nginx = (const char *)version;
     5         kx   }
     5         kx 
     5         kx   strbuf_release( &buf );
     5         kx 
     5         kx   return;
     5         kx }
     5         kx 
     5         kx 
     5         kx void cgit_print_404_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   strbuf_addf( &buf, "              <h1>Requested resource not found</h1>\n" );
     5         kx   strbuf_addf( &buf, "              <p class='leading'>Please check the requested URL or try again later.</p>\n" );
     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 }
     5         kx 
     5         kx 
     5         kx void cgit_print_raw_file( struct strbuf *sb, const char *mime )
     5         kx {
     5         kx   struct strbuf buf = STRBUF_INIT;
     5         kx   char *p = (char *)mime;
     5         kx 
     5         kx   char *http_format = "Date: %s\n"
     5         kx                       "Content-Type: %s\n"
     5         kx                       "Content-Length: %ld\n\n";
     5         kx 
     5         kx   if( !sb || !mime ) return;
     5         kx 
     5         kx   while( *p && *p != ';' ) ++p;
     5         kx   if( *p ) *p = '\0';
     5         kx 
     5         kx   strbuf_addf( &buf, http_format, http_date( time(NULL) ), mime, sb->len );
     5         kx   strbuf_addbuf( &buf, (const struct strbuf *)sb );
     5         kx   strbuf_write( &buf, STDOUT_FILENO );
     5         kx   strbuf_release( &buf );
     5         kx 
     5         kx   exit( 0 );
     5         kx }