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 print_commit( struct strbuf *sb, git_commit *commit, const char *revision, const char *parent, int files, int insertions, int deletions )
05d292b2 (kx 2023-03-24 03:51:10 +0300  57) {
05d292b2 (kx 2023-03-24 03:51:10 +0300  58)   const git_oid       *oid;
05d292b2 (kx 2023-03-24 03:51:10 +0300  59)   const char          *message;
05d292b2 (kx 2023-03-24 03:51:10 +0300  60)   git_time_t           date;
05d292b2 (kx 2023-03-24 03:51:10 +0300  61)   int                  offset;
05d292b2 (kx 2023-03-24 03:51:10 +0300  62)   const git_signature *author;
05d292b2 (kx 2023-03-24 03:51:10 +0300  63)   const git_signature *committer;
05d292b2 (kx 2023-03-24 03:51:10 +0300  64) 
05d292b2 (kx 2023-03-24 03:51:10 +0300  65)   char id[GIT_OID_HEXSZ+1] = { 0 };
05d292b2 (kx 2023-03-24 03:51:10 +0300  66) 
05d292b2 (kx 2023-03-24 03:51:10 +0300  67)   struct strbuf fbuf = STRBUF_INIT;
05d292b2 (kx 2023-03-24 03:51:10 +0300  68)   struct strbuf ibuf = STRBUF_INIT;
05d292b2 (kx 2023-03-24 03:51:10 +0300  69)   struct strbuf dbuf = STRBUF_INIT;
05d292b2 (kx 2023-03-24 03:51:10 +0300  70) 
05d292b2 (kx 2023-03-24 03:51:10 +0300  71)   struct strbuf atime = STRBUF_INIT;
05d292b2 (kx 2023-03-24 03:51:10 +0300  72)   struct strbuf ctime = STRBUF_INIT;
05d292b2 (kx 2023-03-24 03:51:10 +0300  73)   struct strbuf  cmsg = STRBUF_INIT;
05d292b2 (kx 2023-03-24 03:51:10 +0300  74) 
05d292b2 (kx 2023-03-24 03:51:10 +0300  75)   char *raw = NULL;
05d292b2 (kx 2023-03-24 03:51:10 +0300  76) 
05d292b2 (kx 2023-03-24 03:51:10 +0300  77)   if( !sb || !commit || !revision || !parent ) return;
05d292b2 (kx 2023-03-24 03:51:10 +0300  78) 
05d292b2 (kx 2023-03-24 03:51:10 +0300  79)   oid       = git_commit_id( commit );
05d292b2 (kx 2023-03-24 03:51:10 +0300  80)   if( git_oid_fmt( (char *)&id[0], oid ) < 0 || strcmp( (char *)&id[0], revision ) ) return;
05d292b2 (kx 2023-03-24 03:51:10 +0300  81) 
05d292b2 (kx 2023-03-24 03:51:10 +0300  82)   message   = git_commit_summary( commit );
05d292b2 (kx 2023-03-24 03:51:10 +0300  83)   author    = git_commit_author( commit );
05d292b2 (kx 2023-03-24 03:51:10 +0300  84)   committer = git_commit_committer( commit );
05d292b2 (kx 2023-03-24 03:51:10 +0300  85) 
05d292b2 (kx 2023-03-24 03:51:10 +0300  86)   strbuf_addf( &cmsg, "%s", message );
05d292b2 (kx 2023-03-24 03:51:10 +0300  87)   raw = strbuf_detach( &cmsg, NULL );
05d292b2 (kx 2023-03-24 03:51:10 +0300  88)   strbuf_addstr_xml_quoted( &cmsg, (const char *)raw );
05d292b2 (kx 2023-03-24 03:51:10 +0300  89)   free( raw );
05d292b2 (kx 2023-03-24 03:51:10 +0300  90) 
05d292b2 (kx 2023-03-24 03:51:10 +0300  91)   strbuf_addstr( sb, "              <div class=\"diff-cmsg\">\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300  92)   strbuf_addstr( sb, "                <div class=\"cmsg-commit\">\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300  93) 
05d292b2 (kx 2023-03-24 03:51:10 +0300  94)   date   = author->when.time;
05d292b2 (kx 2023-03-24 03:51:10 +0300  95)   offset = author->when.offset;
05d292b2 (kx 2023-03-24 03:51:10 +0300  96)   offset = (offset % 60) + ((offset / 60) * 100);
05d292b2 (kx 2023-03-24 03:51:10 +0300  97)   show_date( &atime, (time_t)date, offset, cgit_date_mode( DATE_ISO8601 ) );
05d292b2 (kx 2023-03-24 03:51:10 +0300  98) 
05d292b2 (kx 2023-03-24 03:51:10 +0300  99)   strbuf_addf( sb, "<span class=\"head\">   author:</span> %s &lt;%s&gt; %s\n", author->name, author->email, (char *)&atime.buf[0] );
05d292b2 (kx 2023-03-24 03:51:10 +0300 100)   strbuf_release( &atime );
05d292b2 (kx 2023-03-24 03:51:10 +0300 101) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 102)   date   = committer->when.time;
05d292b2 (kx 2023-03-24 03:51:10 +0300 103)   offset = committer->when.offset;
05d292b2 (kx 2023-03-24 03:51:10 +0300 104)   offset = (offset % 60) + ((offset / 60) * 100);
05d292b2 (kx 2023-03-24 03:51:10 +0300 105)   show_date( &ctime, (time_t)date, offset, cgit_date_mode( DATE_ISO8601 ) );
05d292b2 (kx 2023-03-24 03:51:10 +0300 106) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 107)   strbuf_addf( sb, "<span class=\"head\">committer:</span> %s &lt;%s&gt; %s\n", committer->name, committer->email, (char *)&ctime.buf[0] );
05d292b2 (kx 2023-03-24 03:51:10 +0300 108)   strbuf_release( &ctime );
05d292b2 (kx 2023-03-24 03:51:10 +0300 109) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 110)     strbuf_addf( sb, "<span class=\"head\">   commit:</span> <span class=\"revision\">%s</span>\n", revision );
05d292b2 (kx 2023-03-24 03:51:10 +0300 111)     strbuf_addf( sb, "<span class=\"head\">   parent:</span> <span class=\"parent\">%s</span>\n", parent );
05d292b2 (kx 2023-03-24 03:51:10 +0300 112)   strbuf_addstr( sb, "                </div>\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 113)   strbuf_addstr( sb, "                <div class=\"cmsg-summary\">\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 114)   strbuf_addstr( sb, "                  <div class=\"head\">\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 115)   strbuf_addstr( sb, "                    Commit Summary:\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 116)   strbuf_addstr( sb, "                  </div>\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 117)     strbuf_addf( sb, "                  <div class=\"message\">%s</div>\n", (char *)&cmsg.buf[0] );
05d292b2 (kx 2023-03-24 03:51:10 +0300 118)   strbuf_release( &cmsg );
05d292b2 (kx 2023-03-24 03:51:10 +0300 119)   strbuf_addstr( sb, "                </div>\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)   strbuf_addf( &fbuf, Q_("%d file changed", "%d files changed", files), files );
05d292b2 (kx 2023-03-24 03:51:10 +0300 123)   strbuf_addf( &ibuf, Q_("%d insertion", "%d insertions", insertions), insertions );
05d292b2 (kx 2023-03-24 03:51:10 +0300 124)   strbuf_addf( &dbuf, Q_("%d deletion", "%d deletions", deletions), deletions );
05d292b2 (kx 2023-03-24 03:51:10 +0300 125) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 126)   strbuf_addstr( sb, "              <div class=\"diff-stat\">\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 127)   strbuf_addstr( sb, "                <div class=\"head\">Diffstat:</div>\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 128)     strbuf_addf( sb, "                <div class=\"line\">%s, %s, %s</div>\n", (char *)&fbuf.buf[0], (char *)&ibuf.buf[0], (char *)&dbuf.buf[0] );
05d292b2 (kx 2023-03-24 03:51:10 +0300 129)   strbuf_addstr( sb, "              </div>\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 130) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 131)   strbuf_release( &fbuf );
05d292b2 (kx 2023-03-24 03:51:10 +0300 132)   strbuf_release( &ibuf );
05d292b2 (kx 2023-03-24 03:51:10 +0300 133)   strbuf_release( &dbuf );
05d292b2 (kx 2023-03-24 03:51:10 +0300 134) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 135) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 136) static void cgit_print_diff( struct strbuf *sb, const char *relative_path, const char *revision )
05d292b2 (kx 2023-03-24 03:51:10 +0300 137) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 138)   struct strbuf buf   = STRBUF_INIT;
05d292b2 (kx 2023-03-24 03:51:10 +0300 139) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 140)   git_commit *commit = NULL;
05d292b2 (kx 2023-03-24 03:51:10 +0300 141)   char parent_hex[GIT_OID_HEXSZ+1] = { 0 };
05d292b2 (kx 2023-03-24 03:51:10 +0300 142)   int files, insertions, deletions;
05d292b2 (kx 2023-03-24 03:51:10 +0300 143)   char *raw = NULL;
05d292b2 (kx 2023-03-24 03:51:10 +0300 144) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 145)   if( !sb ) return;
05d292b2 (kx 2023-03-24 03:51:10 +0300 146) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 147)   commit = lookup_commit_by_hex( revision );
05d292b2 (kx 2023-03-24 03:51:10 +0300 148) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 149)   cgit_diff_with_parent( &buf, (char *)&parent_hex[0], GIT_OID_HEXSZ+1, &files, &insertions, &deletions, revision, ( relative_path && *relative_path ) ? relative_path : "/" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 150) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 151)   print_commit( sb, commit, revision, (const char *)&parent_hex[0], files, insertions, deletions );
05d292b2 (kx 2023-03-24 03:51:10 +0300 152)   git_commit_free( commit );
05d292b2 (kx 2023-03-24 03:51:10 +0300 153) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 154)   strbuf_addf( sb, "<pre><code class='language-Diff'>" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 155) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 156)   raw = strbuf_detach( &buf, NULL );
05d292b2 (kx 2023-03-24 03:51:10 +0300 157)   strbuf_addstr_xml_quoted( &buf, (const char *)raw );
05d292b2 (kx 2023-03-24 03:51:10 +0300 158)   free( raw );
05d292b2 (kx 2023-03-24 03:51:10 +0300 159) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 160)   strbuf_addbuf( sb, (const struct strbuf *)&buf );
05d292b2 (kx 2023-03-24 03:51:10 +0300 161)   strbuf_release( &buf );
05d292b2 (kx 2023-03-24 03:51:10 +0300 162) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 163)   strbuf_addstr( sb, "\n</code></pre>\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 164) }
05d292b2 (kx 2023-03-24 03:51:10 +0300 165) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 166) void cgit_print_diff_page( void )
05d292b2 (kx 2023-03-24 03:51:10 +0300 167) {
05d292b2 (kx 2023-03-24 03:51:10 +0300 168)   FILE  *fp;
05d292b2 (kx 2023-03-24 03:51:10 +0300 169)   struct strbuf buf = STRBUF_INIT;
05d292b2 (kx 2023-03-24 03:51:10 +0300 170) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 171)   fp = xfopen( ctx.page.header, "r" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 172)   (void)strbuf_env_fread( &buf, fp );
05d292b2 (kx 2023-03-24 03:51:10 +0300 173)   fclose( fp );
05d292b2 (kx 2023-03-24 03:51:10 +0300 174) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 175)   strbuf_addf( &buf, "        <div class=\"content segment\">\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 176)   strbuf_addf( &buf, "          <div class=\"container\">\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 177)   strbuf_addf( &buf, "            <div class=\"cgit-main-content\">\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 178) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 179)   if( ctx.repo.name )
05d292b2 (kx 2023-03-24 03:51:10 +0300 180)   {
05d292b2 (kx 2023-03-24 03:51:10 +0300 181)     cgit_print_diff( &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 182)   }
05d292b2 (kx 2023-03-24 03:51:10 +0300 183)   else
05d292b2 (kx 2023-03-24 03:51:10 +0300 184)   {
05d292b2 (kx 2023-03-24 03:51:10 +0300 185)     strbuf_addf( &buf, "              <h1>Requested resource cannot be shown</h1>\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 186)     strbuf_addf( &buf, "              <p class='leading'>Repository '%s' not found.</p>\n", ctx.repo.name );
05d292b2 (kx 2023-03-24 03:51:10 +0300 187)   }
05d292b2 (kx 2023-03-24 03:51:10 +0300 188) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 189)   strbuf_addf( &buf, "            </div> <!-- End of cgit-main-content -->\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 190)   strbuf_addf( &buf, "          </div> <!-- End of container -->\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 191)   strbuf_addf( &buf, "        </div> <!-- End of content segment -->\n" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 192) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 193)   fp = xfopen( ctx.page.footer, "r" );
05d292b2 (kx 2023-03-24 03:51:10 +0300 194)   (void)strbuf_env_fread( &buf, fp );
05d292b2 (kx 2023-03-24 03:51:10 +0300 195)   fclose( fp );
05d292b2 (kx 2023-03-24 03:51:10 +0300 196) 
05d292b2 (kx 2023-03-24 03:51:10 +0300 197)   ctx.page.size = buf.len;
05d292b2 (kx 2023-03-24 03:51:10 +0300 198)   cgit_print_http_headers();
05d292b2 (kx 2023-03-24 03:51:10 +0300 199)   strbuf_write( &buf, STDOUT_FILENO );
05d292b2 (kx 2023-03-24 03:51:10 +0300 200)   strbuf_release( &buf );
05d292b2 (kx 2023-03-24 03:51:10 +0300 201) }