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
Index: cgit-cgi.c
===================================================================
--- cgit-cgi.c	(nonexistent)
+++ cgit-cgi.c	(revision 5)
@@ -0,0 +1,315 @@
+
+#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>
+
+#define _GNU_SOURCE
+#include <getopt.h>
+
+#include <git2.h>
+
+#include <nls.h>
+
+#include <defs.h>
+#include <cscm/bcf.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>
+#include <ui-file.h>
+#include <ui-tree.h>
+#include <ui-diff.h>
+#include <ui-blame.h>
+#include <ui-log.h>
+
+
+static void free_resources( void )
+{
+  git_libgit2_shutdown();
+  if( config ) free_config();
+}
+
+int main( int argc, char *argv[] )
+{
+
+  __mctx_init();
+
+  setlocale( LC_ALL, "" );
+#if ENABLE_NLS == 1
+  bindtextdomain( PACKAGE, LOCALEDIR );
+  textdomain( PACKAGE );
+#endif
+
+  git_libgit2_init();
+
+  cgit_prepare_context();
+
+  /************************
+    define error handlers:
+   */
+   system_fatal = fatal_html;
+  wrapper_fatal = fatal_html;
+    dlist_fatal = fatal_html;
+    rlist_fatal = fatal_html;
+     http_fatal = fatal_html;
+     html_fatal = fatal_html;
+     cgit_fatal = fatal_html;
+
+  if( !(config = read_config()) )
+  {
+    fatal_html( "cannot read config file: /dev/shm%s", CGIT_SHM_BCF );
+    free_resources();
+    exit( 1 );
+  }
+
+  cgit_parse_query();
+
+  cgit_prepare_template_variables();
+
+  if( ctx.page.status == 404 )
+  {
+    cgit_print_404_page();
+    cgit_release_template_variables();
+    free_resources();
+    return 0;
+  }
+
+  if( !strcmp( ctx.vars.page_type, ptype_repolist ) )
+  {
+    if( ctx.query.search && *ctx.query.search )
+    {
+      cgit_search_repo( ctx.query.search );
+    }
+    cgit_print_repolist_page();
+  }
+  else if( !strcmp( ctx.vars.page_type, ptype_repo ) )
+  {
+    if( ctx.query.operation && *ctx.query.operation && !strcmp( ctx.query.operation, "diff" ) )
+    {
+      cgit_print_diff_page();
+    }
+    else if( ctx.query.operation && *ctx.query.operation && !strcmp( ctx.query.operation, "log" ) )
+    {
+      cgit_print_log_page();
+    }
+    else if( ctx.repo.relative_info.kind == GIT_OBJECT_BLOB )
+    {
+      if( ctx.query.operation && *ctx.query.operation && !strcmp( ctx.query.operation, "blame" ) )
+      {
+        cgit_print_blame_page();
+      }
+      else
+      {
+        cgit_print_file_page();
+      }
+    }
+    else if( ctx.repo.relative_info.kind == GIT_OBJECT_TREE )
+    {
+      cgit_print_tree_page();
+    }
+    else
+    {
+      cgit_print_repolist_page();
+    }
+  }
+
+#if 0
+  {
+    struct strbuf buf  = STRBUF_INIT;
+
+    strbuf_addf( &buf, "        <div class=\"content segment\">\n" );
+    strbuf_addf( &buf, "          <div class=\"container\">\n" );
+    strbuf_addf( &buf, "            <div class=\"cgit-main-content\">\n" );
+
+    strbuf_addf( &buf, "<pre><code class='language-text'>HTTP_HOST = %s\n", ctx.env.http_host );
+    strbuf_addf( &buf, "HTTPS = %s\n", ctx.env.https );
+    strbuf_addf( &buf, "NO_HTTP = %s\n", ctx.env.no_http );
+    strbuf_addf( &buf, "PATH_INFO = %s\n", ctx.env.path_info );
+    strbuf_addf( &buf, "QUERY_STRING = %s\n", ctx.env.query_string );
+    strbuf_addf( &buf, "REQUEST_URI = %s\n", ctx.env.request_uri );
+    strbuf_addf( &buf, "REQUEST_SCHEME = %s\n", ctx.env.request_scheme );
+    strbuf_addf( &buf, "REQUEST_METHOD = %s\n", ctx.env.request_method );
+    strbuf_addf( &buf, "SCRIPT_NAME = %s\n", ctx.env.script_name );
+    strbuf_addf( &buf, "SERVER_NAME = %s\n", ctx.env.server_name );
+    strbuf_addf( &buf, "SERVER_PORT = %s\n", ctx.env.server_port );
+    strbuf_addf( &buf, "HTTP_COOKIE = %s\n", ctx.env.http_cookie );
+    strbuf_addf( &buf, "CONTENT_LENGTH = %d\n", ctx.env.content_lenght );
+    strbuf_addf( &buf, "HTTP_ROOT = %s\n", ctx.env.http_root );
+
+    strbuf_addf( &buf, "\naddition:\n" );
+    strbuf_addf( &buf, "DOCUMENT_ROOT = %s\n", getenv("DOCUMENT_ROOT") );
+    strbuf_addf( &buf, "DOCUMENT_URI = %s\n", getenv("DOCUMENT_URI") );
+    strbuf_addf( &buf, "PATH_TRANSLATED = %s\n", getenv("PATH_TRANSLATED") );
+    strbuf_addf( &buf, "SERVER_SOFTWARE = %s\n", getenv("SERVER_SOFTWARE") );
+    strbuf_addf( &buf, "SERVER_PROTOCOL = %s\n", getenv("SERVER_PROTOCOL") );
+    strbuf_addf( &buf, "SERVER_ADDR = %s\n", getenv("SERVER_ADDR") );
+    strbuf_addf( &buf, "REMOTE_ADDR = %s\n", getenv("REMOTE_ADDR") );
+    strbuf_addf( &buf, "GATEWAY_INTERFACE = %s\n", getenv("GATEWAY_INTERFACE") );
+    strbuf_addf( &buf, "SERVER_PROTOCOL = %s\n", getenv("SERVER_PROTOCOL") );
+
+    strbuf_addf( &buf, "\nctx:\n" );
+    strbuf_addf( &buf, "ctx.vars.page_size = '%s'\n", ctx.vars.page_size );
+    strbuf_addf( &buf, "ctx.query.ofs = %d;\n", ctx.query.ofs );
+    strbuf_addf( &buf, "ctx.query.rev = '%s';\n", ctx.query.rev );
+    strbuf_addf( &buf, "ctx.query.revision = '%s';\n", ctx.query.revision );
+    strbuf_addf( &buf, "ctx.query.operation = '%s';\n", ctx.query.operation );
+    strbuf_addf( &buf, "ctx.query.search = '%s';\n", ctx.query.search );
+    strbuf_addf( &buf, "\n" );
+    strbuf_addf( &buf, "ctx.repo.name = %s\n", ctx.repo.name );
+    strbuf_addf( &buf, "ctx.repo.git_root = %s\n", ctx.repo.git_root );
+    strbuf_addf( &buf, "ctx.repo.repo_root = %s\n", ctx.repo.repo_root );
+    strbuf_addf( &buf, "ctx.repo.info = {\n" );
+    switch( ctx.repo.info.kind )
+    {
+      case GIT_OBJECT_TREE:
+        strbuf_addf( &buf, "  kind = '%s';\n", "tree" );
+        break;
+      case GIT_OBJECT_BLOB:
+        strbuf_addf( &buf, "  kind = '%s';\n", "blob" );
+        break;
+      default:
+        strbuf_addf( &buf, "  kind = '%s';\n", "unknown" );
+        break;
+    }
+    strbuf_addf( &buf, "  revision = '%s';\n", ctx.repo.info.revision );
+    strbuf_addf( &buf, "  oid = '%s';\n", ctx.repo.info.oid );
+    switch( ctx.repo.info.mode )
+    {
+      case GIT_FILEMODE_TREE:
+        strbuf_addf( &buf, "  mode = '%s';\n", "tree" );
+        break;
+      case GIT_FILEMODE_BLOB:
+        strbuf_addf( &buf, "  mode = '%s';\n", "blob" );
+        break;
+      case GIT_FILEMODE_BLOB_EXECUTABLE:
+        strbuf_addf( &buf, "  mode = '%s';\n", "blob executable" );
+        break;
+      case GIT_FILEMODE_LINK:
+        strbuf_addf( &buf, "  mode = '%s';\n", "link" );
+        break;
+      default:
+        strbuf_addf( &buf, "  mode = '%07o';\n", ctx.repo.info.mode );
+        break;
+    }
+    strbuf_addf( &buf, "  author = '%s';\n", ctx.repo.info.author );
+    strbuf_addf( &buf, "  date = %"PRIdMAX";\n", ctx.repo.info.date );
+    strbuf_addf( &buf, "  offset = %+05d;\n", ctx.repo.info.offset );
+    strbuf_addf( &buf, "  mime = '%s';\n", ctx.repo.info.mime );
+    strbuf_addf( &buf, "  lang = '%s';\n", ctx.repo.info.lang );
+    strbuf_addf( &buf, "}\n" );
+
+    strbuf_addf( &buf, "ctx.repo.relative_path = %s\n", ctx.repo.relative_path );
+    strbuf_addf( &buf, "ctx.repo.relative_info = {\n" );
+    switch( ctx.repo.relative_info.kind )
+    {
+      case GIT_OBJECT_TREE:
+        strbuf_addf( &buf, "  kind = '%s';\n", "tree" );
+        break;
+      case GIT_OBJECT_BLOB:
+        strbuf_addf( &buf, "  kind = '%s';\n", "blob" );
+        break;
+      default:
+        strbuf_addf( &buf, "  kind = '%s';\n", "unknown" );
+        break;
+    }
+    strbuf_addf( &buf, "  revision = '%s';\n", ctx.repo.relative_info.revision );
+    strbuf_addf( &buf, "  oid = '%s';\n", ctx.repo.relative_info.oid );
+    switch( ctx.repo.relative_info.mode )
+    {
+      case GIT_FILEMODE_TREE:
+        strbuf_addf( &buf, "  mode = '%s';\n", "tree" );
+        break;
+      case GIT_FILEMODE_BLOB:
+        strbuf_addf( &buf, "  mode = '%s';\n", "blob" );
+        break;
+      case GIT_FILEMODE_BLOB_EXECUTABLE:
+        strbuf_addf( &buf, "  mode = '%s';\n", "blob executable" );
+        break;
+      case GIT_FILEMODE_LINK:
+        strbuf_addf( &buf, "  mode = '%s';\n", "link" );
+        break;
+      default:
+        strbuf_addf( &buf, "  mode = '%07o';\n", ctx.repo.relative_info.mode );
+        break;
+    }
+    strbuf_addf( &buf, "  author = '%s';\n", ctx.repo.relative_info.author );
+    strbuf_addf( &buf, "  date = %"PRIdMAX";\n", ctx.repo.relative_info.date );
+    strbuf_addf( &buf, "  offset = %+05d;\n", ctx.repo.relative_info.offset );
+    strbuf_addf( &buf, "  mime = '%s';\n", ctx.repo.relative_info.mime );
+    strbuf_addf( &buf, "  lang = '%s';\n", ctx.repo.relative_info.lang );
+    strbuf_addf( &buf, "}\n" );
+
+    strbuf_addf( &buf, "ctx.repo.relative_href = %s\n", ctx.repo.relative_href );
+    strbuf_addf( &buf, "ctx.repo.relative_html = %s\n", ctx.repo.relative_html );
+
+    strbuf_addf( &buf, "ctx.repo.trunk = %s\n", ctx.repo.trunk );
+    strbuf_addf( &buf, "ctx.repo.clone_ro_prefix = %s\n", ctx.repo.clone_ro_prefix );
+    strbuf_addf( &buf, "ctx.repo.clone_prefix = %s\n", ctx.repo.clone_prefix );
+    strbuf_addf( &buf, "ctx.repo.nbranches = %d\n", ctx.repo.nbranches );
+    strbuf_addf( &buf, "ctx.repo.ntags = %d\n", ctx.repo.ntags );
+
+    strbuf_addf( &buf, "ctx.vers.git = %s\n", ctx.vers.git );
+    strbuf_addf( &buf, "ctx.vers.nginx = %s\n", ctx.vers.nginx );
+    strbuf_addf( &buf, "ctx.vers.cgit = %s\n", ctx.vers.cgit );
+
+    strbuf_addf( &buf, "</code></pre>\n" );
+
+    print_config( &buf, config );
+
+    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" );
+
+    ctx.page.size = buf.len;
+
+    strbuf_write( &buf, STDOUT_FILENO );
+    strbuf_release( &buf );
+  }
+
+#endif
+
+  cgit_release_template_variables();
+  free_resources();
+
+  return 0;
+}