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

3 Commits   0 Branches   1 Tag
05d292b2 (kx 2023-03-24 03:51:10 +0300   1) 
05d292b2 (kx 2023-03-24 03:51:10 +0300   2) #ifdef HAVE_CONFIG_H
05d292b2 (kx 2023-03-24 03:51:10 +0300   3) #include <config.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300   4) #endif
05d292b2 (kx 2023-03-24 03:51:10 +0300   5) 
05d292b2 (kx 2023-03-24 03:51:10 +0300   6) #include <stdlib.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300   7) #include <stdio.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300   8) #include <sys/sysinfo.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300   9) #include <sys/types.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300  10) #ifdef HAVE_INTTYPES_H
05d292b2 (kx 2023-03-24 03:51:10 +0300  11) #include <inttypes.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300  12) #else
05d292b2 (kx 2023-03-24 03:51:10 +0300  13) #include <stdint.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300  14) #endif
05d292b2 (kx 2023-03-24 03:51:10 +0300  15) #include <stddef.h>   /* offsetof(3) */
05d292b2 (kx 2023-03-24 03:51:10 +0300  16) #include <dirent.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300  17) #include <sys/stat.h> /* chmod(2)    */
05d292b2 (kx 2023-03-24 03:51:10 +0300  18) #include <sys/file.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300  19) #include <sys/mman.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300  20) #include <fcntl.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300  21) #include <limits.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300  22) #include <string.h>   /* strdup(3)   */
05d292b2 (kx 2023-03-24 03:51:10 +0300  23) #include <libgen.h>   /* basename(3) */
05d292b2 (kx 2023-03-24 03:51:10 +0300  24) #include <ctype.h>    /* tolower(3)  */
05d292b2 (kx 2023-03-24 03:51:10 +0300  25) #include <errno.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300  26) #include <time.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300  27) #include <sys/time.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300  28) #include <pwd.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300  29) #include <grp.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300  30) #include <stdarg.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300  31) #include <locale.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300  32) #include <unistd.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300  33) 
05d292b2 (kx 2023-03-24 03:51:10 +0300  34) #include <git2.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300  35) 
05d292b2 (kx 2023-03-24 03:51:10 +0300  36) #include <nls.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300  37) 
05d292b2 (kx 2023-03-24 03:51:10 +0300  38) #include <defs.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300  39) 
05d292b2 (kx 2023-03-24 03:51:10 +0300  40) #include <fatal.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300  41) #include <http.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300  42) #include <html.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300  43) 
05d292b2 (kx 2023-03-24 03:51:10 +0300  44) #include <dlist.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300  45) #include <strbuf.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300  46) #include <repolist.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300  47) #include <wrapper.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300  48) #include <system.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300  49) #include <date.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300  50) 
05d292b2 (kx 2023-03-24 03:51:10 +0300  51) #include <ctx.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300  52) #include <git-shared.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300  53) #include <ui-shared.h>
05d292b2 (kx 2023-03-24 03:51:10 +0300  54) 
05d292b2 (kx 2023-03-24 03:51:10 +0300  55) 
05d292b2 (kx 2023-03-24 03:51:10 +0300  56) static void cgit_print_blame( struct strbuf *sb, const char *relative_path, const char *revision )
05d292b2 (kx 2023-03-24 03:51:10 +0300  57) {
05d292b2 (kx 2023-03-24 03:51:10 +0300  58)   const char *name = NULL, *git_root = NULL, *repo_root = NULL;
05d292b2 (kx 2023-03-24 03:51:10 +0300  59) 
05d292b2 (kx 2023-03-24 03:51:10 +0300  60)   if( !sb || !relative_path ) return;
05d292b2 (kx 2023-03-24 03:51:10 +0300  61) 
05d292b2 (kx 2023-03-24 03:51:10 +0300  62)   name      = ctx.repo.name;
05d292b2 (kx 2023-03-24 03:51:10 +0300  63)   git_root  = ctx.repo.git_root;
05d292b2 (kx 2023-03-24 03:51:10 +0300  64)   repo_root = ctx.repo.repo_root;
05d292b2 (kx 2023-03-24 03:51:10 +0300  65) 
05d292b2 (kx 2023-03-24 03:51:10 +0300  66)   strbuf_addf( sb, "<div class=\"blame\">" );
05d292b2 (kx 2023-03-24 03:51:10 +0300  67)   strbuf_addf( sb, "  <pre><code class='language-PlainText'>" );
05d292b2 (kx 2023-03-24 03:51:10 +0300  68) 
05d292b2 (kx 2023-03-24 03:51:10 +0300  69)   if( name && git_root )
05d292b2 (kx 2023-03-24 03:51:10 +0300  70)   {
05d292b2 (kx 2023-03-24 03:51:10 +0300  71)     char ref[PATH_MAX] = { 0 };
05d292b2 (kx 2023-03-24 03:51:10 +0300  72)     char rpath[PATH_MAX] = { 0 };
05d292b2 (kx 2023-03-24 03:51:10 +0300  73) 
05d292b2 (kx 2023-03-24 03:51:10 +0300  74)     char path[PATH_MAX] = { 0 };
05d292b2 (kx 2023-03-24 03:51:10 +0300  75)     char cmd[PATH_MAX];
05d292b2 (kx 2023-03-24 03:51:10 +0300  76)     struct strbuf buf = STRBUF_INIT;
05d292b2 (kx 2023-03-24 03:51:10 +0300  77)     char *raw = NULL;
05d292b2 (kx 2023-03-24 03:51:10 +0300  78)     pid_t p = (pid_t) -1;
05d292b2 (kx 2023-03-24 03:51:10 +0300  79)     int   rc;
05d292b2 (kx 2023-03-24 03:51:10 +0300  80) 
05d292b2 (kx 2023-03-24 03:51:10 +0300  81)     sprintf( (char *)&path[0], "%s/", git_root );
05d292b2 (kx 2023-03-24 03:51:10 +0300  82)     if( repo_root && *repo_root )
05d292b2 (kx 2023-03-24 03:51:10 +0300  83)     {
05d292b2 (kx 2023-03-24 03:51:10 +0300  84)       strcat( (char *)&path[0], repo_root );
05d292b2 (kx 2023-03-24 03:51:10 +0300  85)       strcat( (char *)&path[0], "/" );
05d292b2 (kx 2023-03-24 03:51:10 +0300  86)     }
05d292b2 (kx 2023-03-24 03:51:10 +0300  87)     strcat( (char *)&path[0], name );
05d292b2 (kx 2023-03-24 03:51:10 +0300  88) 
05d292b2 (kx 2023-03-24 03:51:10 +0300  89)     if( !is_bare( (char *)&path[0] ) )
05d292b2 (kx 2023-03-24 03:51:10 +0300  90)     {
05d292b2 (kx 2023-03-24 03:51:10 +0300  91)       strcat( (char *)&path[0], "/.git" );
05d292b2 (kx 2023-03-24 03:51:10 +0300  92)     }
05d292b2 (kx 2023-03-24 03:51:10 +0300  93) 
05d292b2 (kx 2023-03-24 03:51:10 +0300  94)     parse_relative_path( (char *)&ref[0], (char *)&rpath[0], relative_path );
05d292b2 (kx 2023-03-24 03:51:10 +0300  95) 
05d292b2 (kx 2023-03-24 03:51:10 +0300  96)     if( revision && *revision )
05d292b2 (kx 2023-03-24 03:51:10 +0300  97)       snprintf( (char *)&cmd[0], 1024, "git --git-dir=%s blame %s -- %s 2>/dev/null", (char *)&path[0], revision, (char *)&rpath[0] );
05d292b2 (kx 2023-03-24 03:51:10 +0300  98)     else
05d292b2 (kx 2023-03-24 03:51:10 +0300  99)       snprintf( (char *)&cmd[0], 1024, "git --git-dir=%s blame -- %s 2>/dev/null", (char *)&path[0], (char *)&rpath[0] );
05d292b2 (kx 2023-03-24 03:51:10 +0300 100)     p = sys_exec_command( &buf, cmd );
05d292b2 (kx 2023-03-24 03:51:10 +0300 101)     rc = sys_wait_command( p, NULL );
05d292b2 (kx 2023-03-24 03:51:10 +0300 102)     if( rc != 0 )
05d292b2 (kx 2023-03-24 03:51:10 +0300 103)     {
05d292b2 (kx 2023-03-24 03:51:10 +0300 104)       strbuf_release( &buf );
05d292b2 (kx 2023-03-24 03:51:10 +0300 105)       return;
05d292b2 (kx 2023-03-24 03:51:10 +0300 106)     }
05d292b2 (kx 2023-03-24 03:51:10 +0300 107) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 108)     if( buf.buf[0] )
05d292b2 (kx 2023-03-24 03:51:10 +0300 109)     {
05d292b2 (kx 2023-03-24 03:51:10 +0300 110)       raw = strbuf_detach( &buf, NULL );
05d292b2 (kx 2023-03-24 03:51:10 +0300 111)       strbuf_addstr_xml_quoted( &buf, (const char *)raw );
05d292b2 (kx 2023-03-24 03:51:10 +0300 112)       free( raw );
05d292b2 (kx 2023-03-24 03:51:10 +0300 113) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 114)       strbuf_addbuf( sb, (const struct strbuf *)&buf );
05d292b2 (kx 2023-03-24 03:51:10 +0300 115)     }
05d292b2 (kx 2023-03-24 03:51:10 +0300 116)     strbuf_release( &buf );
05d292b2 (kx 2023-03-24 03:51:10 +0300 117)   }
05d292b2 (kx 2023-03-24 03:51:10 +0300 118) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 119)   strbuf_addstr( sb, "\n  </code></pre>\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 120)   strbuf_addstr( sb, "</div>\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 121) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 122)   return;
05d292b2 (kx 2023-03-24 03:51:10 +0300 123) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 124) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 125) void cgit_print_blame_page( void )
05d292b2 (kx 2023-03-24 03:51:10 +0300 126) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 127)   FILE  *fp;
05d292b2 (kx 2023-03-24 03:51:10 +0300 128)   struct strbuf buf = STRBUF_INIT;
05d292b2 (kx 2023-03-24 03:51:10 +0300 129) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 130)   fp = xfopen( ctx.page.header, "r" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 131)   (void)strbuf_env_fread( &buf, fp );
05d292b2 (kx 2023-03-24 03:51:10 +0300 132)   fclose( fp );
05d292b2 (kx 2023-03-24 03:51:10 +0300 133) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 134)   strbuf_addf( &buf, "        <div class=\"content segment\">\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 135)   strbuf_addf( &buf, "          <div class=\"container\">\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 136)   strbuf_addf( &buf, "            <div class=\"cgit-main-content\">\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 137) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 138)   if( ctx.repo.name )
05d292b2 (kx 2023-03-24 03:51:10 +0300 139)   {
05d292b2 (kx 2023-03-24 03:51:10 +0300 140)     cgit_print_blame( &buf, ctx.repo.relative_path, (!strcmp( ctx.query.rev, "0" )) ? (const char *)&ctx.repo.relative_info.revision[0] : ctx.query.rev );
05d292b2 (kx 2023-03-24 03:51:10 +0300 141)   }
05d292b2 (kx 2023-03-24 03:51:10 +0300 142)   else
05d292b2 (kx 2023-03-24 03:51:10 +0300 143)   {
05d292b2 (kx 2023-03-24 03:51:10 +0300 144)     strbuf_addf( &buf, "              <h1>Requested resource cannot be shown</h1>\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 145)     strbuf_addf( &buf, "              <p class='leading'>Repository '%s' not found.</p>\n", ctx.repo.name );
05d292b2 (kx 2023-03-24 03:51:10 +0300 146)   }
05d292b2 (kx 2023-03-24 03:51:10 +0300 147) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 148)   strbuf_addf( &buf, "            </div> <!-- End of cgit-main-content -->\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 149)   strbuf_addf( &buf, "          </div> <!-- End of container -->\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 150)   strbuf_addf( &buf, "        </div> <!-- End of content segment -->\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 151) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 152)   fp = xfopen( ctx.page.footer, "r" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 153)   (void)strbuf_env_fread( &buf, fp );
05d292b2 (kx 2023-03-24 03:51:10 +0300 154)   fclose( fp );
05d292b2 (kx 2023-03-24 03:51:10 +0300 155) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 156)   ctx.page.size = buf.len;
05d292b2 (kx 2023-03-24 03:51:10 +0300 157)   cgit_print_http_headers();
05d292b2 (kx 2023-03-24 03:51:10 +0300 158)   strbuf_write( &buf, STDOUT_FILENO );
05d292b2 (kx 2023-03-24 03:51:10 +0300 159)   strbuf_release( &buf );
05d292b2 (kx 2023-03-24 03:51:10 +0300 160) }