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 <cmpvers.h>
     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 /********************************
     5         kx   Sorted dlist functions:
     5         kx  */
     5         kx struct tree_line {
     5         kx   char           *name;
     5         kx   char           *revision;
     5         kx   git_filemode_t  mode;
     5         kx   git_time_t      date;
     5         kx   int             offset;
     5         kx   git_off_t       size;
     5         kx };
     5         kx 
     5         kx static struct dlist *directories = NULL;
     5         kx static struct dlist *files       = NULL;
     5         kx 
     5         kx static struct tree_line *tree_line_alloc( const char *name, const char *revision, git_filemode_t mode, git_time_t date, int offset, git_off_t size  )
     5         kx {
     5         kx   struct tree_line *line = NULL;
     5         kx   line = (struct tree_line *)xmalloc( sizeof(struct tree_line) );
     5         kx 
     5         kx   if( name )     { line->name     = xstrdup( name );     }
     5         kx   if( revision ) { line->revision = xstrdup( revision ); }
     5         kx 
     5         kx   if( mode ) line->mode = mode;
     5         kx   else       line->mode = GIT_FILEMODE_TREE;
     5         kx 
     5         kx   if( date ) line->date = date;
     5         kx   else       line->date = -1;
     5         kx 
     5         kx   if( size ) line->size = size;
     5         kx   else       line->size = 4096;
     5         kx 
     5         kx   return line;
     5         kx }
     5         kx 
     5         kx static void __line_free( void *data, void *user_data )
     5         kx {
     5         kx   struct tree_line *line = (struct tree_line *)data;
     5         kx   if( line )
     5         kx   {
     5         kx     if( line->name )     { free( line->name );     line->name     = NULL; }
     5         kx     if( line->revision ) { free( line->revision ); line->revision = NULL; }
     5         kx 
     5         kx     free( line );
     5         kx   }
     5         kx }
     5         kx 
     5         kx static struct dlist *tree_lines_free( struct dlist *lines )
     5         kx {
     5         kx   if( lines )
     5         kx   {
     5         kx     dlist_free( lines, __line_free );
     5         kx     lines = NULL;
     5         kx   }
     5         kx   return lines;
     5         kx }
     5         kx 
     5         kx static int __cmp_tree_lines_byname( const void *a, const void *b )
     5         kx {
     5         kx   int ret = -1;
     5         kx 
     5         kx   struct tree_line *line1 = (struct tree_line *)a;
     5         kx   struct tree_line *line2 = (struct tree_line *)b;
     5         kx 
     5         kx   if( line1->name && line2->name )
     5         kx   {
     5         kx     ret = strcmp( line1->name, line2->name );
     5         kx   }
     5         kx   else
     5         kx     return ret;
     5         kx 
     5         kx   return ret;
     5         kx }
     5         kx 
     5         kx static int __cmp_tree_lines_bytag( const void *a, const void *b )
     5         kx {
     5         kx   char *v1 = NULL, *v2 = NULL;
     5         kx   int   ret = -1;
     5         kx 
     5         kx   struct tree_line *line1 = (struct tree_line *)a;
     5         kx   struct tree_line *line2 = (struct tree_line *)b;
     5         kx 
     5         kx   if( line1->name && line2->name )
     5         kx   {
     5         kx     v1 = line1->name;
     5         kx     v2 = line2->name;
     5         kx     while( *v1 && !isdigit( *v1 ) ) ++v1;
     5         kx     while( *v2 && !isdigit( *v2 ) ) ++v2;
     5         kx     ret = strcmp( line1->name, line2->name );
     5         kx   }
     5         kx   else
     5         kx     return ret;
     5         kx 
     5         kx   if( !*v1 || !*v2 )
     5         kx   {
     5         kx     return ret;
     5         kx   }
     5         kx 
     5         kx   /******************************************
     5         kx     sort reversive to show newest tag first:
     5         kx    */
     5         kx   return -cmp_version( (const char *)v1, (const char *)v2 );
     5         kx }
     5         kx 
     5         kx static struct dlist *tree_lines_sort_byname( struct dlist *lines )
     5         kx {
     5         kx   if( lines ) { lines = dlist_sort( lines, __cmp_tree_lines_byname ); }
     5         kx   return lines;
     5         kx }
     5         kx 
     5         kx static struct dlist *tree_lines_sort_bytag( struct dlist *lines )
     5         kx {
     5         kx   if( lines ) { lines = dlist_sort( lines, __cmp_tree_lines_bytag ); }
     5         kx   return lines;
     5         kx }
     5         kx /*
     5         kx   End of sorted dlist functions.
     5         kx  ********************************/
     5         kx 
     5         kx static void cgit_print_branches( struct strbuf *sb, const char *relative_path )
     5         kx {
     5         kx   git_repository *repo = NULL;
     5         kx   git_reference_iterator *iter = NULL;
     5         kx   const char *name = NULL;
     5         kx   const char *refs = "refs/heads/*";
     5         kx 
     5         kx   const char *query_string = NULL;
     5         kx 
     5         kx   if( !sb || !relative_path || !*relative_path ) return;
     5         kx 
     5         kx   query_string = ctx_remove_query_param( ctx.env.query_string, "rev" );
     5         kx   query_string = ctx_remove_query_param( query_string, "op" );
     5         kx 
     5         kx   if( !(repo = cgit_open_repository()) ) return;
     5         kx 
     5         kx   if( git_reference_iterator_glob_new( &iter, repo, refs ) < 0 )
     5         kx   {
     5         kx     close_repository( repo );
     5         kx     return;
     5         kx   }
     5         kx 
     5         kx   strbuf_addstr( sb, "\n" );
     5         kx   strbuf_addf( sb, "              <div class=\"repo-tree-header\">\n" );
     5         kx   strbuf_addf( sb, "                <div class=\"row\">\n" );
     5         kx   strbuf_addf( sb, "                  <div class=\"col-path\">Path</div>\n" );
     5         kx   strbuf_addf( sb, "                  <div class=\"col-size\"><div class=\"tree-size trunc\">Size</div></div>\n" );
     5         kx   strbuf_addf( sb, "                  <div class=\"col-rev\"><div class=\"tree-rev trunc\">Rev</div></div>\n" );
     5         kx   strbuf_addf( sb, "                  <div class=\"col-date\"><div class=\"tree-date trunc\">Date</div></div>\n" );
     5         kx   strbuf_addf( sb, "                  <div class=\"col-links\"><div class=\"tree-links trunc\">Links</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=\"tree\">\n\n" );
     5         kx 
     5         kx   while( !git_reference_next_name( &name, iter ) )
     5         kx   {
     5         kx     struct tree_line *line = NULL;
     5         kx     git_commit       *commit = NULL;
     5         kx     const git_oid    *oid;
     5         kx     git_time_t        date;
     5         kx     int               offset;
     5         kx     char id[GIT_OID_HEXSZ+1] = { 0 };
     5         kx 
     5         kx     if( !(commit = get_commit_by_ref( repo, name )) ) continue;
     5         kx 
     5         kx     oid     = git_commit_id( commit );
     5         kx     date    = git_commit_time( commit );
     5         kx     offset  = git_commit_time_offset( commit );
     5         kx 
     5         kx     offset = (offset % 60) + ((offset / 60) * 100);
     5         kx 
     5         kx     if( git_oid_fmt( (char *)&id[0], oid ) < 0 )
     5         kx     {
     5         kx       git_commit_free( commit );
     5         kx       continue;
     5         kx     }
     5         kx 
     5         kx     line = tree_line_alloc( (const char *)&name[11], (const char *)&id[0], 0, date, offset, 0 );
     5         kx     directories = dlist_append( directories, (void *)line );
     5         kx 
     5         kx     git_commit_free( commit );
     5         kx   }
     5         kx 
     5         kx   git_reference_iterator_free( iter );
     5         kx   close_repository( repo );
     5         kx 
     5         kx   /*************************
     5         kx     Print directories list:
     5         kx    */
     5         kx   if( directories )
     5         kx   {
     5         kx     struct dlist *list = NULL;
     5         kx 
     5         kx     directories = tree_lines_sort_byname( directories );
     5         kx 
     5         kx     list = directories;
     5         kx 
     5         kx     while( list )
     5         kx     {
     5         kx       struct tree_line *line = (struct tree_line *)list->data;
     5         kx 
     5         kx       if( strcmp( line->name, ctx.repo.trunk ) ) /* Do not print Trunk. */
     5         kx       {
     5         kx         /*************************
     5         kx           Print branch Reference:
     5         kx          */
     5         kx         strbuf_addf( sb, "                <div class=\"row\">\n" );
     5         kx         if( ctx.env.query_string && *ctx.env.query_string )
     5         kx           strbuf_addf( sb, "                  <div class=\"col-path\"><a href=\"/%s/branches/%s/?%s\"><div class=\"tree-path dir\">%s/</div></a></div>\n", ctx.repo.name, line->name, ctx.env.query_string, line->name );
     5         kx         else
     5         kx           strbuf_addf( sb, "                  <div class=\"col-path\"><a href=\"/%s/branches/%s/\"><div class=\"tree-path dir\">%s/</div></a></div>\n", ctx.repo.name, line->name, line->name );
     5         kx         strbuf_addf( sb, "                  <div class=\"col-size\"><div onclick=\"trunc(this)\" class=\"tree-size trunc\">%ld</div></div>\n", line->size );
     5         kx         strbuf_addf( sb, "                  <div class=\"col-rev\"><div onclick=\"trunc(this)\" class=\"tree-rev trunc\"><span class=\"rev-short\" title=\"%s\">%0.8s</span></div></div>\n", line->revision, line->revision );
     5         kx         if( line->date != -1 )
     5         kx         {
     5         kx           strbuf_addf( sb, "                  <div class=\"col-date\"><div onclick=\"trunc(this)\" class=\"tree-date trunc\">" );
     5         kx           cgit_print_age( sb, (time_t)line->date, line->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=\"tree-date trunc\">unknown</div></div>\n" );
     5         kx         }
     5         kx         strbuf_addf( sb, "                  <div class=\"col-links\">\n" );
     5         kx         strbuf_addf( sb, "                    <div onclick=\"trunc(this)\" class=\"tree-links trunc\">\n" );
     5         kx         if( query_string && *query_string )
     5         kx         {
     5         kx           strbuf_addf( sb, "                      <a title=\"Log from: %s\" href=\"/%s/branches/%s/?op=log&rev=%s&%s\">log</a> &nbsp;\n", line->revision, ctx.repo.name, line->name, line->revision, query_string );
     5         kx           strbuf_addf( sb, "                      <a title=\"Compare with Previous\" href=\"/%s/branches/%s/?op=diff&rev=%s&%s\">diff</a>\n", ctx.repo.name, line->name, line->revision, query_string );
     5         kx         }
     5         kx         else
     5         kx         {
     5         kx           strbuf_addf( sb, "                      <a title=\"Log from: %s\" href=\"/%s/branches/%s/?op=log&rev=%s\">log</a> &nbsp;\n", line->revision, ctx.repo.name, line->name, line->revision );
     5         kx           strbuf_addf( sb, "                      <a title=\"Compare with Previous\" href=\"/%s/branches/%s/?op=diff&rev=%s\">diff</a>\n", ctx.repo.name, line->name, line->revision );
     5         kx         }
     5         kx         strbuf_addf( sb, "                    </div>\n" );
     5         kx         strbuf_addf( sb, "                  </div>\n" );
     5         kx         strbuf_addf( sb, "                </div>\n\n" );
     5         kx         /*
     5         kx           End of printing.
     5         kx          *************************/
     5         kx       }
     5         kx 
     5         kx       list = dlist_next( list );
     5         kx     }
     5         kx     directories = tree_lines_free( directories );
     5         kx   }
     5         kx 
     5         kx   strbuf_addf( sb, "              </div> <!-- End of Tree -->\n\n" );
     5         kx }
     5         kx 
     5         kx 
     5         kx static void cgit_print_tags( struct strbuf *sb, const char *relative_path )
     5         kx {
     5         kx   git_repository *repo = NULL;
     5         kx   git_reference_iterator *iter = NULL;
     5         kx   const char *name = NULL;
     5         kx   const char *refs = "refs/tags/*";
     5         kx 
     5         kx   const char *query_string = NULL;
     5         kx 
     5         kx   if( !sb || !relative_path || !*relative_path ) return;
     5         kx 
     5         kx   query_string = ctx_remove_query_param( ctx.env.query_string, "rev" );
     5         kx   query_string = ctx_remove_query_param( query_string, "op" );
     5         kx 
     5         kx   if( !(repo = cgit_open_repository()) ) return;
     5         kx 
     5         kx   if( git_reference_iterator_glob_new( &iter, repo, refs ) < 0 )
     5         kx   {
     5         kx     close_repository( repo );
     5         kx     return;
     5         kx   }
     5         kx 
     5         kx   strbuf_addstr( sb, "\n" );
     5         kx   strbuf_addf( sb, "              <div class=\"repo-tree-header\">\n" );
     5         kx   strbuf_addf( sb, "                <div class=\"row\">\n" );
     5         kx   strbuf_addf( sb, "                  <div class=\"col-path\">Path</div>\n" );
     5         kx   strbuf_addf( sb, "                  <div class=\"col-size\"><div class=\"tree-size trunc\">Size</div></div>\n" );
     5         kx   strbuf_addf( sb, "                  <div class=\"col-rev\"><div class=\"tree-rev trunc\">Rev</div></div>\n" );
     5         kx   strbuf_addf( sb, "                  <div class=\"col-date\"><div class=\"tree-date trunc\">Date</div></div>\n" );
     5         kx   strbuf_addf( sb, "                  <div class=\"col-links\"><div class=\"tree-links trunc\">Links</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=\"tree\">\n\n" );
     5         kx 
     5         kx   while( !git_reference_next_name( &name, iter ) )
     5         kx   {
     5         kx     struct tree_line *line = NULL;
     5         kx     git_commit       *commit = NULL;
     5         kx     const git_oid    *oid;
     5         kx     git_time_t        date;
     5         kx     int               offset;
     5         kx     char id[GIT_OID_HEXSZ+1] = { 0 };
     5         kx 
     5         kx     if( !(commit = get_commit_by_ref( repo, name )) ) continue;
     5         kx 
     5         kx     oid     = git_commit_id( commit );
     5         kx     date    = git_commit_time( commit );
     5         kx     offset  = git_commit_time_offset( commit );
     5         kx 
     5         kx     offset = (offset % 60) + ((offset / 60) * 100);
     5         kx 
     5         kx     if( git_oid_fmt( (char *)&id[0], oid ) < 0 )
     5         kx     {
     5         kx       git_commit_free( commit );
     5         kx       continue;
     5         kx     }
     5         kx 
     5         kx     line = tree_line_alloc( (const char *)&name[10], (const char *)&id[0], 0, date, offset, 0 );
     5         kx     directories = dlist_append( directories, (void *)line );
     5         kx 
     5         kx     git_commit_free( commit );
     5         kx   }
     5         kx 
     5         kx   git_reference_iterator_free( iter );
     5         kx   close_repository( repo );
     5         kx 
     5         kx   /*************************
     5         kx     Print directories list:
     5         kx    */
     5         kx   if( directories )
     5         kx   {
     5         kx     struct dlist *list = NULL;
     5         kx 
     5         kx     directories = tree_lines_sort_bytag( directories );
     5         kx 
     5         kx     list = directories;
     5         kx 
     5         kx     while( list )
     5         kx     {
     5         kx       struct tree_line *line = (struct tree_line *)list->data;
     5         kx 
     5         kx       /**********************
     5         kx         Print tag Reference:
     5         kx        */
     5         kx       strbuf_addf( sb, "                <div class=\"row\">\n" );
     5         kx       if( ctx.env.query_string && *ctx.env.query_string )
     5         kx         strbuf_addf( sb, "                  <div class=\"col-path\"><a href=\"/%s/tags/%s/?%s\"><div class=\"tree-path dir\">%s/</div></a></div>\n", ctx.repo.name, line->name, ctx.env.query_string, line->name );
     5         kx       else
     5         kx         strbuf_addf( sb, "                  <div class=\"col-path\"><a href=\"/%s/tags/%s/\"><div class=\"tree-path dir\">%s/</div></a></div>\n", ctx.repo.name, line->name, line->name );
     5         kx       strbuf_addf( sb, "                  <div class=\"col-size\"><div onclick=\"trunc(this)\" class=\"tree-size trunc\">%ld</div></div>\n", line->size );
     5         kx       strbuf_addf( sb, "                  <div class=\"col-rev\"><div onclick=\"trunc(this)\" class=\"tree-rev trunc\"><span class=\"rev-short\" title=\"%s\">%0.8s</span></div></div>\n", line->revision, line->revision );
     5         kx       if( line->date != -1 )
     5         kx       {
     5         kx         strbuf_addf( sb, "                  <div class=\"col-date\"><div onclick=\"trunc(this)\" class=\"tree-date trunc\">" );
     5         kx         cgit_print_age( sb, (time_t)line->date, line->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=\"tree-date trunc\">unknown</div></div>\n" );
     5         kx       }
     5         kx       strbuf_addf( sb, "                  <div class=\"col-links\">\n" );
     5         kx       strbuf_addf( sb, "                    <div onclick=\"trunc(this)\" class=\"tree-links trunc\">\n" );
     5         kx       if( query_string && *query_string )
     5         kx       {
     5         kx         strbuf_addf( sb, "                      <a title=\"Log from: %s\" href=\"/%s/tags/%s/?op=log&rev=%s&%s\">log</a> &nbsp;\n", line->revision, ctx.repo.name, line->name, line->revision, query_string );
     5         kx         strbuf_addf( sb, "                      <a title=\"Compare with Previous\" href=\"/%s/tags/%s/?op=diff&rev=%s&%s\">diff</a>\n", ctx.repo.name, line->name, line->revision, query_string );
     5         kx       }
     5         kx       else
     5         kx       {
     5         kx         strbuf_addf( sb, "                      <a title=\"Log from: %s\" href=\"/%s/tags/%s/?op=log&rev=%s\">log</a> &nbsp;\n", line->revision, ctx.repo.name, line->name, line->revision );
     5         kx         strbuf_addf( sb, "                      <a title=\"Compare with Previous\" href=\"/%s/tags/%s/?op=diff&rev=%s\">diff</a>\n", ctx.repo.name, line->name, line->revision );
     5         kx       }
     5         kx       strbuf_addf( sb, "                    </div>\n" );
     5         kx       strbuf_addf( sb, "                  </div>\n" );
     5         kx       strbuf_addf( sb, "                </div>\n\n" );
     5         kx       /*
     5         kx         End of printing.
     5         kx        **********************/
     5         kx 
     5         kx       list = dlist_next( list );
     5         kx     }
     5         kx     directories = tree_lines_free( directories );
     5         kx   }
     5         kx 
     5         kx   strbuf_addf( sb, "              </div> <!-- End of Tree -->\n\n" );
     5         kx }
     5         kx 
     5         kx 
     5         kx static void cgit_print_tree( struct strbuf *sb, const char *relative_path, const char *revision )
     5         kx {
     5         kx   struct cgit_info info = CGIT_INFO_INIT;
     5         kx   const char *query_string = NULL;
     5         kx 
     5         kx   if( !sb || !relative_path || !*relative_path ) return;
     5         kx 
     5         kx   if( !strcmp( relative_path, "tags"     ) ) return cgit_print_tags( sb, relative_path );
     5         kx   if( !strcmp( relative_path, "branches" ) ) return cgit_print_branches( sb, relative_path );
     5         kx 
     5         kx   if( revision && strcmp( revision, (const char *)&ctx.repo.relative_info.revision[0] ) )
     5         kx   {
     5         kx     cgit_rpath_info( &info, relative_path, revision );
     5         kx     if( info.kind != GIT_OBJECT_BLOB )
     5         kx     {
     5         kx       strbuf_addf( sb, "              <h1>Requested resource cannot be shown</h1>\n" );
     5         kx       strbuf_addf( sb, "              <p class='leading'>The Tree with specified revision '%s' cannot be shown.</p>\n", revision );
     5         kx       return;
     5         kx     }
     5         kx   }
     5         kx   else
     5         kx   {
     5         kx     memcpy( (void *)&info, (void *)&ctx.repo.relative_info, sizeof( struct cgit_info ) );
     5         kx   }
     5         kx 
     5         kx   query_string = ctx_remove_query_param( ctx.env.query_string, "rev" );
     5         kx   query_string = ctx_remove_query_param( query_string, "op" );
     5         kx 
     5         kx   strbuf_addstr( sb, "\n" );
     5         kx   strbuf_addf( sb, "              <div class=\"repo-tree-header\">\n" );
     5         kx   strbuf_addf( sb, "                <div class=\"row\">\n" );
     5         kx   strbuf_addf( sb, "                  <div class=\"col-path\">Path</div>\n" );
     5         kx   strbuf_addf( sb, "                  <div class=\"col-size\"><div class=\"tree-size trunc\">Size</div></div>\n" );
     5         kx   strbuf_addf( sb, "                  <div class=\"col-rev\"><div class=\"tree-rev trunc\">Rev</div></div>\n" );
     5         kx   strbuf_addf( sb, "                  <div class=\"col-date\"><div class=\"tree-date trunc\">Date</div></div>\n" );
     5         kx   strbuf_addf( sb, "                  <div class=\"col-links\"><div class=\"tree-links trunc\">Links</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=\"tree\">\n\n" );
     5         kx 
     5         kx   if( info.kind == GIT_OBJECT_TREE )
     5         kx   {
     5         kx     git_repository *repo = NULL;
     5         kx     git_tree       *tree = NULL;
     5         kx     git_oid         oid;
     5         kx     size_t          i, count;
     5         kx     char rpath[PATH_MAX] = { 0 };
     5         kx 
     5         kx     if( git_oid_fromstr( &oid, (const char *)&info.oid[0] ) < 0 )
     5         kx     {
     5         kx       return;
     5         kx     }
     5         kx 
     5         kx     if( !(repo = cgit_open_repository()) )
     5         kx     {
     5         kx       return;
     5         kx     }
     5         kx 
     5         kx     if( git_tree_lookup( &tree, repo, &oid ) < 0 )
     5         kx     {
     5         kx       close_repository( repo );
     5         kx       return;
     5         kx     }
     5         kx 
     5         kx     count = git_tree_entrycount( tree );
     5         kx     for( i = 0; i < count; ++i )
     5         kx     {
     5         kx       struct tree_line *line = NULL;
     5         kx       struct cgit_info entry_info = CGIT_INFO_INIT;
     5         kx       git_object *obj = NULL;
     5         kx       git_off_t   rawsize = 4096;
     5         kx       const git_tree_entry *entry = git_tree_entry_byindex( (const git_tree *)tree, i );
     5         kx       const char *name = git_tree_entry_name( entry ); /* filename */
     5         kx 
     5         kx       git_tree_entry_to_object( &obj, repo, entry );
     5         kx       if( git_object_type( (const git_object *)obj ) == GIT_OBJECT_BLOB )
     5         kx       {
     5         kx         rawsize = git_blob_rawsize( (git_blob *)obj );
     5         kx       }
     5         kx       git_object_free( obj );
     5         kx 
     5         kx       sprintf( (char *)&rpath[0], "%s/%s", relative_path, name );
     5         kx       cgit_rpath_info( &entry_info, (char *)&rpath[0], info.revision );
     5         kx 
     5         kx       line = tree_line_alloc( name, (const char *)&entry_info.revision[0], entry_info.mode, entry_info.date, entry_info.offset, rawsize );
     5         kx 
     5         kx       if( entry_info.kind == GIT_OBJECT_TREE )
     5         kx       {
     5         kx         directories = dlist_append( directories, (void *)line );
     5         kx       }
     5         kx       else
     5         kx       {
     5         kx         files = dlist_append( files, (void *)line );
     5         kx       }
     5         kx     }
     5         kx 
     5         kx     git_tree_free( tree );
     5         kx     close_repository( repo );
     5         kx 
     5         kx 
     5         kx     /*************************
     5         kx       Print directories list:
     5         kx      */
     5         kx     if( directories )
     5         kx     {
     5         kx       struct dlist *list = NULL;
     5         kx 
     5         kx       directories = tree_lines_sort_byname( directories );
     5         kx 
     5         kx       list = directories;
     5         kx 
     5         kx       while( list )
     5         kx       {
     5         kx         const char *mode = "";
     5         kx         struct tree_line *line = (struct tree_line *)list->data;
     5         kx 
     5         kx         if( line->mode == GIT_FILEMODE_LINK )
     5         kx           mode = " link";
     5         kx         else
     5         kx           mode = "";
     5         kx 
     5         kx         /*************************
     5         kx           Print branch Reference:
     5         kx          */
     5         kx         strbuf_addf( sb, "                <div class=\"row\">\n" );
     5         kx         if( ctx.env.query_string && *ctx.env.query_string )
     5         kx           strbuf_addf( sb, "                  <div class=\"col-path\"><a href=\"/%s/%s/%s/?%s\"><div class=\"tree-path dir%s\">%s/</div></a></div>\n", ctx.repo.name, relative_path, line->name, ctx.env.query_string, mode, line->name );
     5         kx         else
     5         kx           strbuf_addf( sb, "                  <div class=\"col-path\"><a href=\"/%s/%s/%s/\"><div class=\"tree-path dir%s\">%s/</div></a></div>\n", ctx.repo.name, relative_path, line->name, mode, line->name );
     5         kx         strbuf_addf( sb, "                  <div class=\"col-size\"><div onclick=\"trunc(this)\" class=\"tree-size trunc\">%ld</div></div>\n", line->size );
     5         kx         strbuf_addf( sb, "                  <div class=\"col-rev\"><div onclick=\"trunc(this)\" class=\"tree-rev trunc\"><span class=\"rev-short\" title=\"%s\">%0.8s</span></div></div>\n", line->revision, line->revision );
     5         kx         if( line->date != -1 )
     5         kx         {
     5         kx           strbuf_addf( sb, "                  <div class=\"col-date\"><div onclick=\"trunc(this)\" class=\"tree-date trunc\">" );
     5         kx           cgit_print_age( sb, (time_t)line->date, line->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=\"tree-date trunc\">unknown</div></div>\n" );
     5         kx         }
     5         kx         strbuf_addf( sb, "                  <div class=\"col-links\">\n" );
     5         kx         strbuf_addf( sb, "                    <div onclick=\"trunc(this)\" class=\"tree-links trunc\">\n" );
     5         kx         if( query_string && *query_string )
     5         kx         {
     5         kx           strbuf_addf( sb, "                      <a title=\"Log from: %s\" href=\"/%s/%s/%s/?op=log&rev=%s&%s\">log</a> &nbsp;\n", line->revision, ctx.repo.name, relative_path, line->name, line->revision, query_string );
     5         kx           strbuf_addf( sb, "                      <a title=\"Compare with Previous\" href=\"/%s/%s/%s/?op=diff&rev=%s&%s\">diff</a>\n", ctx.repo.name, relative_path, line->name, line->revision, query_string );
     5         kx         }
     5         kx         else
     5         kx         {
     5         kx           strbuf_addf( sb, "                      <a title=\"Log from: %s\" href=\"/%s/%s/%s/?op=log&rev=%s\">log</a> &nbsp;\n", line->revision, ctx.repo.name, relative_path, line->name, line->revision );
     5         kx           strbuf_addf( sb, "                      <a title=\"Compare with Previous\" href=\"/%s/%s/%s/?op=diff&rev=%s\">diff</a>\n", ctx.repo.name, relative_path, line->name, line->revision );
     5         kx         }
     5         kx         strbuf_addf( sb, "                    </div>\n" );
     5         kx         strbuf_addf( sb, "                  </div>\n" );
     5         kx         strbuf_addf( sb, "                </div>\n\n" );
     5         kx         /*
     5         kx           End of printing.
     5         kx          *************************/
     5         kx 
     5         kx         list = dlist_next( list );
     5         kx       }
     5         kx       directories = tree_lines_free( directories );
     5         kx     }
     5         kx 
     5         kx     /********************
     5         kx       Print filess list:
     5         kx      */
     5         kx     if( files )
     5         kx     {
     5         kx       struct dlist *list = NULL;
     5         kx 
     5         kx       files = tree_lines_sort_byname( files );
     5         kx 
     5         kx       list = files;
     5         kx 
     5         kx       while( list )
     5         kx       {
     5         kx         const char *mode = "";
     5         kx         const char *mode_prefix = "";
     5         kx         struct tree_line *line = (struct tree_line *)list->data;
     5         kx 
     5         kx         if( line->mode == GIT_FILEMODE_LINK )
     5         kx         {
     5         kx           mode = " link";
     5         kx           mode_prefix = "@";
     5         kx         }
     5         kx         else if( line->mode == GIT_FILEMODE_BLOB_EXECUTABLE )
     5         kx         {
     5         kx           mode = " exec";
     5         kx           mode_prefix = "";
     5         kx         }
     5         kx         else
     5         kx         {
     5         kx           mode = "";
     5         kx           mode_prefix = "";
     5         kx         }
     5         kx 
     5         kx         /*************************
     5         kx           Print branch Reference:
     5         kx          */
     5         kx         strbuf_addf( sb, "                <div class=\"row\">\n" );
     5         kx         if( ctx.env.query_string && *ctx.env.query_string )
     5         kx           strbuf_addf( sb, "                  <div class=\"col-path\"><a href=\"/%s/%s/%s/?%s\"><div class=\"tree-path file%s\">%s%s</div></a></div>\n", ctx.repo.name, relative_path, line->name, ctx.env.query_string, mode, mode_prefix, line->name );
     5         kx         else
     5         kx           strbuf_addf( sb, "                  <div class=\"col-path\"><a href=\"/%s/%s/%s/\"><div class=\"tree-path file%s\">%s%s</div></a></div>\n", ctx.repo.name, relative_path, line->name, mode, mode_prefix, line->name );
     5         kx         strbuf_addf( sb, "                  <div class=\"col-size\"><div onclick=\"trunc(this)\" class=\"tree-size trunc\">%ld</div></div>\n", line->size );
     5         kx         strbuf_addf( sb, "                  <div class=\"col-rev\"><div onclick=\"trunc(this)\" class=\"tree-rev trunc\"><span class=\"rev-short\" title=\"%s\">%0.8s</span></div></div>\n", line->revision, line->revision );
     5         kx         if( line->date != -1 )
     5         kx         {
     5         kx           strbuf_addf( sb, "                  <div class=\"col-date\"><div onclick=\"trunc(this)\" class=\"tree-date trunc\">" );
     5         kx           cgit_print_age( sb, (time_t)line->date, line->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=\"tree-date trunc\">unknown</div></div>\n" );
     5         kx         }
     5         kx         strbuf_addf( sb, "                  <div class=\"col-links\">\n" );
     5         kx         strbuf_addf( sb, "                    <div onclick=\"trunc(this)\" class=\"tree-links trunc\">\n" );
     5         kx         if( query_string && *query_string )
     5         kx         {
     5         kx           strbuf_addf( sb, "                      <a title=\"Log from: %s\" href=\"/%s/%s/%s/?op=log&rev=%s&%s\">log</a> &nbsp;\n", line->revision, ctx.repo.name, relative_path, line->name, line->revision, query_string );
     5         kx           strbuf_addf( sb, "                      <a title=\"Compare with Previous\" href=\"/%s/%s/%s/?op=diff&rev=%s&%s\">diff</a> &nbsp;\n", ctx.repo.name, relative_path, line->name, line->revision, query_string );
     5         kx           strbuf_addf( sb, "                      <a href=\"/%s/%s/%s/?op=blame&rev=%s&%s\">blame</a>\n", ctx.repo.name, relative_path, line->name, line->revision, query_string );
     5         kx         }
     5         kx         else
     5         kx         {
     5         kx           strbuf_addf( sb, "                      <a title=\"Log from: %s\" href=\"/%s/%s/%s/?op=log&rev=%s\">log</a> &nbsp;\n", line->revision, ctx.repo.name, relative_path, line->name, line->revision );
     5         kx           strbuf_addf( sb, "                      <a title=\"Compare with Previous\" href=\"/%s/%s/%s/?op=diff&rev=%s\">diff</a> &nbsp;\n", ctx.repo.name, relative_path, line->name, line->revision );
     5         kx           strbuf_addf( sb, "                      <a href=\"/%s/%s/%s/?op=blame&rev=%s\">blame</a>\n", ctx.repo.name, relative_path, line->name, line->revision );
     5         kx         }
     5         kx         strbuf_addf( sb, "                    </div>\n" );
     5         kx         strbuf_addf( sb, "                  </div>\n" );
     5         kx         strbuf_addf( sb, "                </div>\n\n" );
     5         kx         /*
     5         kx           End of printing.
     5         kx          *************************/
     5         kx 
     5         kx         list = dlist_next( list );
     5         kx       }
     5         kx       files = tree_lines_free( files );
     5         kx     }
     5         kx   }
     5         kx 
     5         kx   strbuf_addf( sb, "              </div> <!-- End of Tree -->\n\n" );
     5         kx 
     5         kx   return;
     5         kx }
     5         kx 
     5         kx static void cgit_print_summary( struct strbuf *sb, const char *relative_path, const char *revision )
     5         kx {
     5         kx   struct cgit_info  info;
     5         kx   const char       *query_string = NULL;
     5         kx 
     5         kx   if( !sb ) return;
     5         kx 
     5         kx   if( relative_path && *relative_path )
     5         kx     return cgit_print_tree( sb, relative_path, revision );
     5         kx 
     5         kx   query_string = ctx_remove_query_param( ctx.env.query_string, "rev" );
     5         kx   query_string = ctx_remove_query_param( query_string, "op" );
     5         kx 
     5         kx   strbuf_addstr( sb, "\n" );
     5         kx   strbuf_addf( sb, "              <div class=\"repo-tree-header\">\n" );
     5         kx   strbuf_addf( sb, "                <div class=\"row\">\n" );
     5         kx   strbuf_addf( sb, "                  <div class=\"col-path\">Refs</div>\n" );
     5         kx   strbuf_addf( sb, "                </div>\n" );
     5         kx   strbuf_addf( sb, "              </div>\n\n" );
     5         kx   strbuf_addf( sb, "              <div class=\"tree\">\n\n" );
     5         kx   strbuf_addf( sb, "                <div class=\"row\">\n" );
     5         kx   strbuf_addf( sb, "                  <div class=\"col-path\"><div class=\"tree-path dir\"><a href=\"/%s/branches/\">branches/</a></div></div>\n", ctx.repo.name );
     5         kx   strbuf_addf( sb, "                </div>\n" );
     5         kx   strbuf_addf( sb, "                <div class=\"row\">\n" );
     5         kx   strbuf_addf( sb, "                  <div class=\"col-path\"><div class=\"tree-path dir\"><a href=\"/%s/tags/\">tags/</a></div></div>\n", ctx.repo.name );
     5         kx   strbuf_addf( sb, "                </div>\n\n" );
     5         kx   strbuf_addf( sb, "              </div> <!-- End of Tree -->\n" );
     5         kx 
     5         kx   cgit_rpath_info( &info, "/", revision );
     5         kx   if( info.kind != GIT_OBJECT_TREE && info.kind != GIT_OBJECT_BLOB )
     5         kx   {
     5         kx     memcpy( (void *)&info, (const void *)&ctx.repo.relative_info, sizeof( struct cgit_info ) );
     5         kx     ctx.query.revision = NULL;
     5         kx     (void)ctx_grab_str_query_param( "rev" );
     5         kx     ctx.query.rev = "0";
     5         kx   }
     5         kx 
     5         kx   strbuf_addstr( sb, "\n" );
     5         kx   strbuf_addf( sb, "              <div class=\"repo-tree-header\">\n" );
     5         kx   strbuf_addf( sb, "                <div class=\"row\">\n" );
     5         kx   strbuf_addf( sb, "                  <div class=\"col-path\">Path</div>\n" );
     5         kx   strbuf_addf( sb, "                  <div class=\"col-size\"><div class=\"tree-size trunc\">Size</div></div>\n" );
     5         kx   strbuf_addf( sb, "                  <div class=\"col-rev\"><div class=\"tree-rev trunc\">Rev</div></div>\n" );
     5         kx   strbuf_addf( sb, "                  <div class=\"col-date\"><div class=\"tree-date trunc\">Date</div></div>\n" );
     5         kx   strbuf_addf( sb, "                  <div class=\"col-links\"><div class=\"tree-links trunc\">Links</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=\"tree\">\n\n" );
     5         kx 
     5         kx   /************************
     5         kx     Print trunk Reference:
     5         kx    */
     5         kx   strbuf_addf( sb, "                <div class=\"row\">\n" );
     5         kx   strbuf_addf( sb, "                  <div class=\"col-path\"><a href=\"/%s/trunk/\"><div class=\"tree-path dir\">trunk/</div></a></div>\n", ctx.repo.name );
     5         kx   strbuf_addf( sb, "                  <div class=\"col-size\"><div onclick=\"trunc(this)\" class=\"tree-size trunc\">%ld</div></div>\n", (git_off_t)4096 );
     5         kx   strbuf_addf( sb, "                  <div class=\"col-rev\"><div onclick=\"trunc(this)\" class=\"tree-rev trunc\"><span class=\"rev-short\" title=\"%s\">%0.8s</span></div></div>\n", info.revision, info.revision );
     5         kx   if( info.date != -1 )
     5         kx   {
     5         kx     strbuf_addf( sb, "                  <div class=\"col-date\"><div onclick=\"trunc(this)\" class=\"tree-date trunc\">" );
     5         kx     cgit_print_age( sb, (time_t)info.date, info.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=\"tree-date trunc\">unknown</div></div>\n" );
     5         kx   }
     5         kx   strbuf_addf( sb, "                  <div class=\"col-links\">\n" );
     5         kx   strbuf_addf( sb, "                    <div onclick=\"trunc(this)\" class=\"tree-links trunc\">\n" );
     5         kx   if( query_string && *query_string )
     5         kx   {
     5         kx     strbuf_addf( sb, "                      <a title=\"Log from: %s\" href=\"/%s/trunk/?op=log&rev=%s&%s\">log</a> &nbsp;\n", info.revision, ctx.repo.name, info.revision, query_string );
     5         kx     strbuf_addf( sb, "                      <a title=\"Compare with Previous\" href=\"/%s/trunk/?op=diff&rev=%s&%s\">diff</a>\n", ctx.repo.name, info.revision, query_string );
     5         kx   }
     5         kx   else
     5         kx   {
     5         kx     strbuf_addf( sb, "                      <a title=\"Log from: %s\" href=\"/%s/trunk/?op=log&rev=%s\">log</a> &nbsp;\n", info.revision, ctx.repo.name, info.revision );
     5         kx     strbuf_addf( sb, "                      <a title=\"Compare with Previous\" href=\"/%s/trunk/?op=diff&rev=%s\">diff</a>\n", ctx.repo.name, info.revision );
     5         kx   }
     5         kx   strbuf_addf( sb, "                    </div>\n" );
     5         kx   strbuf_addf( sb, "                  </div>\n" );
     5         kx   strbuf_addf( sb, "                </div>\n\n" );
     5         kx 
     5         kx   strbuf_addf( sb, "              </div> <!-- End of Tree -->\n\n" );
     5         kx }
     5         kx 
     5         kx 
     5         kx static void cgit_print_clone_urls( struct strbuf *sb, const char *relative_path )
     5         kx {
     5         kx   char  ref[PATH_MAX] = { 0 };
     5         kx   char  rpath[PATH_MAX] = { 0 };
     5         kx   char  branch[PATH_MAX] = { 0 };
     5         kx 
     5         kx   if( !sb ) return;
     5         kx 
     5         kx   parse_relative_path( (char *)&ref[0], (char *)&rpath[0], relative_path );
     5         kx 
     5         kx   if( ref[0] && strlen( (char *)&ref[0] ) > 11 )
     5         kx   {
     5         kx     const char *b = NULL;
     5         kx     if( !strncmp( (char *)&ref[0], "refs/heads/", 11 ) )
     5         kx     {
     5         kx       b = (const char *)&ref[11];
     5         kx       sprintf( (char *)&branch[0], "--branch %s -- ", b );
     5         kx     }
     5         kx 
     5         kx     if( b && !strcmp( b, ctx.repo.trunk ) )
     5         kx     {
     5         kx       branch[0] = '\0';
     5         kx     }
     5         kx   }
     5         kx 
     5         kx   strbuf_addf( sb, "              <div class=\"clone-box\">\n" );
     5         kx   strbuf_addf( sb, "                <div class=\"clone-header\">Clone</div>\n" );
     5         kx   strbuf_addf( sb, "                <div class=\"clone-urls\">\n" );
     5         kx   strbuf_addf( sb, "                  <div class=\"clone-line\">\n" );
     5         kx   strbuf_addf( sb, "                    <div class=\"clone-perms\">ro:</div>\n" );
     5         kx 
     5         kx   strbuf_addf( sb, "                    <div class=\"clone-url\">git clone %s%s/%s</div>\n", branch, ctx.repo.clone_ro_prefix, ctx.repo.name );
     5         kx 
     5         kx   strbuf_addf( sb, "                  </div>\n" );
     5         kx   if( ctx.repo.clone_prefix && *ctx.repo.clone_prefix )
     5         kx   {
     5         kx     strbuf_addf( sb, "                  <div class=\"clone-line\">\n" );
     5         kx     strbuf_addf( sb, "                    <div class=\"clone-perms\">rw:</div>\n" );
     5         kx 
     5         kx     strbuf_addf( sb, "                    <div class=\"clone-url\">git clone %s%s/%s</div>\n", branch, ctx.repo.clone_prefix, ctx.repo.name );
     5         kx 
     5         kx     strbuf_addf( sb, "                  </div>\n" );
     5         kx   }
     5         kx   strbuf_addf( sb, "                </div>\n" );
     5         kx   strbuf_addf( sb, "              </div> <!-- End of Clone -->\n\n" );
     5         kx }
     5         kx 
     5         kx void cgit_print_tree_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.relative_info.kind == GIT_OBJECT_TREE )
     5         kx   {
     5         kx     if( ctx.repo.name )
     5         kx     {
     5         kx       cgit_print_summary( &buf, ctx.repo.relative_path, (!strcmp( ctx.query.rev, "0" )) ? (const char *)&ctx.repo.relative_info.revision[0] : ctx.query.rev );
     5         kx       cgit_print_clone_urls( &buf, ctx.repo.relative_path );
     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   else
     5         kx   {
     5         kx     strbuf_addf( &buf, "              <h1>Requested resource cannot be shown</h1>\n" );
     5         kx     strbuf_addf( &buf, "              <p class='leading'>This page assume the repository tree to be shown.</p>\n" );
     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 }