summaryrefslogtreecommitdiff
path: root/cgitcgi/ui-tree.c
diff options
context:
space:
mode:
authorkx <kx@radix.pro>2023-03-24 03:51:10 +0300
committerkx <kx@radix.pro>2023-03-24 03:51:10 +0300
commit05d292b208dfe01324826b4c87bbc4da3389a0d5 (patch)
treeb10a2269e9320785f3b61189e75f6778fa167986 /cgitcgi/ui-tree.c
parent40ab18a661ff6ada40e73969be293918d346a2f5 (diff)
downloadcgit-ui-05d292b208dfe01324826b4c87bbc4da3389a0d5.tar.xz
Version 0.1.7
Diffstat (limited to 'cgitcgi/ui-tree.c')
-rw-r--r--cgitcgi/ui-tree.c864
1 files changed, 864 insertions, 0 deletions
diff --git a/cgitcgi/ui-tree.c b/cgitcgi/ui-tree.c
new file mode 100644
index 0000000..59f65a1
--- /dev/null
+++ b/cgitcgi/ui-tree.c
@@ -0,0 +1,864 @@
+
+#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>
+
+#include <git2.h>
+
+#include <nls.h>
+
+#include <defs.h>
+
+#include <fatal.h>
+#include <http.h>
+#include <html.h>
+
+#include <cmpvers.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>
+
+
+/********************************
+ Sorted dlist functions:
+ */
+struct tree_line {
+ char *name;
+ char *revision;
+ git_filemode_t mode;
+ git_time_t date;
+ int offset;
+ git_off_t size;
+};
+
+static struct dlist *directories = NULL;
+static struct dlist *files = NULL;
+
+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 )
+{
+ struct tree_line *line = NULL;
+ line = (struct tree_line *)xmalloc( sizeof(struct tree_line) );
+
+ if( name ) { line->name = xstrdup( name ); }
+ if( revision ) { line->revision = xstrdup( revision ); }
+
+ if( mode ) line->mode = mode;
+ else line->mode = GIT_FILEMODE_TREE;
+
+ if( date ) line->date = date;
+ else line->date = -1;
+
+ if( size ) line->size = size;
+ else line->size = 4096;
+
+ return line;
+}
+
+static void __line_free( void *data, void *user_data )
+{
+ struct tree_line *line = (struct tree_line *)data;
+ if( line )
+ {
+ if( line->name ) { free( line->name ); line->name = NULL; }
+ if( line->revision ) { free( line->revision ); line->revision = NULL; }
+
+ free( line );
+ }
+}
+
+static struct dlist *tree_lines_free( struct dlist *lines )
+{
+ if( lines )
+ {
+ dlist_free( lines, __line_free );
+ lines = NULL;
+ }
+ return lines;
+}
+
+static int __cmp_tree_lines_byname( const void *a, const void *b )
+{
+ int ret = -1;
+
+ struct tree_line *line1 = (struct tree_line *)a;
+ struct tree_line *line2 = (struct tree_line *)b;
+
+ if( line1->name && line2->name )
+ {
+ ret = strcmp( line1->name, line2->name );
+ }
+ else
+ return ret;
+
+ return ret;
+}
+
+static int __cmp_tree_lines_bytag( const void *a, const void *b )
+{
+ char *v1 = NULL, *v2 = NULL;
+ int ret = -1;
+
+ struct tree_line *line1 = (struct tree_line *)a;
+ struct tree_line *line2 = (struct tree_line *)b;
+
+ if( line1->name && line2->name )
+ {
+ v1 = line1->name;
+ v2 = line2->name;
+ while( *v1 && !isdigit( *v1 ) ) ++v1;
+ while( *v2 && !isdigit( *v2 ) ) ++v2;
+ ret = strcmp( line1->name, line2->name );
+ }
+ else
+ return ret;
+
+ if( !*v1 || !*v2 )
+ {
+ return ret;
+ }
+
+ /******************************************
+ sort reversive to show newest tag first:
+ */
+ return -cmp_version( (const char *)v1, (const char *)v2 );
+}
+
+static struct dlist *tree_lines_sort_byname( struct dlist *lines )
+{
+ if( lines ) { lines = dlist_sort( lines, __cmp_tree_lines_byname ); }
+ return lines;
+}
+
+static struct dlist *tree_lines_sort_bytag( struct dlist *lines )
+{
+ if( lines ) { lines = dlist_sort( lines, __cmp_tree_lines_bytag ); }
+ return lines;
+}
+/*
+ End of sorted dlist functions.
+ ********************************/
+
+static void cgit_print_branches( struct strbuf *sb, const char *relative_path )
+{
+ git_repository *repo = NULL;
+ git_reference_iterator *iter = NULL;
+ const char *name = NULL;
+ const char *refs = "refs/heads/*";
+
+ const char *query_string = NULL;
+
+ if( !sb || !relative_path || !*relative_path ) return;
+
+ query_string = ctx_remove_query_param( ctx.env.query_string, "rev" );
+ query_string = ctx_remove_query_param( query_string, "op" );
+
+ if( !(repo = cgit_open_repository()) ) return;
+
+ if( git_reference_iterator_glob_new( &iter, repo, refs ) < 0 )
+ {
+ close_repository( repo );
+ return;
+ }
+
+ strbuf_addstr( sb, "\n" );
+ strbuf_addf( sb, " <div class=\"repo-tree-header\">\n" );
+ strbuf_addf( sb, " <div class=\"row\">\n" );
+ strbuf_addf( sb, " <div class=\"col-path\">Path</div>\n" );
+ strbuf_addf( sb, " <div class=\"col-size\"><div class=\"tree-size trunc\">Size</div></div>\n" );
+ strbuf_addf( sb, " <div class=\"col-rev\"><div class=\"tree-rev trunc\">Rev</div></div>\n" );
+ strbuf_addf( sb, " <div class=\"col-date\"><div class=\"tree-date trunc\">Date</div></div>\n" );
+ strbuf_addf( sb, " <div class=\"col-links\"><div class=\"tree-links trunc\">Links</div></div>\n" );
+ strbuf_addf( sb, " </div>\n" );
+ strbuf_addf( sb, " </div>\n\n" );
+ strbuf_addf( sb, " <div class=\"tree\">\n\n" );
+
+ while( !git_reference_next_name( &name, iter ) )
+ {
+ struct tree_line *line = NULL;
+ git_commit *commit = NULL;
+ const git_oid *oid;
+ git_time_t date;
+ int offset;
+ char id[GIT_OID_HEXSZ+1] = { 0 };
+
+ if( !(commit = get_commit_by_ref( repo, name )) ) continue;
+
+ oid = git_commit_id( commit );
+ date = git_commit_time( commit );
+ offset = git_commit_time_offset( commit );
+
+ offset = (offset % 60) + ((offset / 60) * 100);
+
+ if( git_oid_fmt( (char *)&id[0], oid ) < 0 )
+ {
+ git_commit_free( commit );
+ continue;
+ }
+
+ line = tree_line_alloc( (const char *)&name[11], (const char *)&id[0], 0, date, offset, 0 );
+ directories = dlist_append( directories, (void *)line );
+
+ git_commit_free( commit );
+ }
+
+ git_reference_iterator_free( iter );
+ close_repository( repo );
+
+ /*************************
+ Print directories list:
+ */
+ if( directories )
+ {
+ struct dlist *list = NULL;
+
+ directories = tree_lines_sort_byname( directories );
+
+ list = directories;
+
+ while( list )
+ {
+ struct tree_line *line = (struct tree_line *)list->data;
+
+ if( strcmp( line->name, ctx.repo.trunk ) ) /* Do not print Trunk. */
+ {
+ /*************************
+ Print branch Reference:
+ */
+ strbuf_addf( sb, " <div class=\"row\">\n" );
+ if( ctx.env.query_string && *ctx.env.query_string )
+ 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 );
+ else
+ 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 );
+ strbuf_addf( sb, " <div class=\"col-size\"><div onclick=\"trunc(this)\" class=\"tree-size trunc\">%ld</div></div>\n", line->size );
+ 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 );
+ if( line->date != -1 )
+ {
+ strbuf_addf( sb, " <div class=\"col-date\"><div onclick=\"trunc(this)\" class=\"tree-date trunc\">" );
+ cgit_print_age( sb, (time_t)line->date, line->offset, 0 );
+ strbuf_addf( sb, "</div></div>\n" );
+ }
+ else
+ {
+ strbuf_addf( sb, " <div class=\"col-date\"><div onclick=\"trunc(this)\" class=\"tree-date trunc\">unknown</div></div>\n" );
+ }
+ strbuf_addf( sb, " <div class=\"col-links\">\n" );
+ strbuf_addf( sb, " <div onclick=\"trunc(this)\" class=\"tree-links trunc\">\n" );
+ if( query_string && *query_string )
+ {
+ 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 );
+ 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 );
+ }
+ else
+ {
+ 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 );
+ 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 );
+ }
+ strbuf_addf( sb, " </div>\n" );
+ strbuf_addf( sb, " </div>\n" );
+ strbuf_addf( sb, " </div>\n\n" );
+ /*
+ End of printing.
+ *************************/
+ }
+
+ list = dlist_next( list );
+ }
+ directories = tree_lines_free( directories );
+ }
+
+ strbuf_addf( sb, " </div> <!-- End of Tree -->\n\n" );
+}
+
+
+static void cgit_print_tags( struct strbuf *sb, const char *relative_path )
+{
+ git_repository *repo = NULL;
+ git_reference_iterator *iter = NULL;
+ const char *name = NULL;
+ const char *refs = "refs/tags/*";
+
+ const char *query_string = NULL;
+
+ if( !sb || !relative_path || !*relative_path ) return;
+
+ query_string = ctx_remove_query_param( ctx.env.query_string, "rev" );
+ query_string = ctx_remove_query_param( query_string, "op" );
+
+ if( !(repo = cgit_open_repository()) ) return;
+
+ if( git_reference_iterator_glob_new( &iter, repo, refs ) < 0 )
+ {
+ close_repository( repo );
+ return;
+ }
+
+ strbuf_addstr( sb, "\n" );
+ strbuf_addf( sb, " <div class=\"repo-tree-header\">\n" );
+ strbuf_addf( sb, " <div class=\"row\">\n" );
+ strbuf_addf( sb, " <div class=\"col-path\">Path</div>\n" );
+ strbuf_addf( sb, " <div class=\"col-size\"><div class=\"tree-size trunc\">Size</div></div>\n" );
+ strbuf_addf( sb, " <div class=\"col-rev\"><div class=\"tree-rev trunc\">Rev</div></div>\n" );
+ strbuf_addf( sb, " <div class=\"col-date\"><div class=\"tree-date trunc\">Date</div></div>\n" );
+ strbuf_addf( sb, " <div class=\"col-links\"><div class=\"tree-links trunc\">Links</div></div>\n" );
+ strbuf_addf( sb, " </div>\n" );
+ strbuf_addf( sb, " </div>\n\n" );
+ strbuf_addf( sb, " <div class=\"tree\">\n\n" );
+
+ while( !git_reference_next_name( &name, iter ) )
+ {
+ struct tree_line *line = NULL;
+ git_commit *commit = NULL;
+ const git_oid *oid;
+ git_time_t date;
+ int offset;
+ char id[GIT_OID_HEXSZ+1] = { 0 };
+
+ if( !(commit = get_commit_by_ref( repo, name )) ) continue;
+
+ oid = git_commit_id( commit );
+ date = git_commit_time( commit );
+ offset = git_commit_time_offset( commit );
+
+ offset = (offset % 60) + ((offset / 60) * 100);
+
+ if( git_oid_fmt( (char *)&id[0], oid ) < 0 )
+ {
+ git_commit_free( commit );
+ continue;
+ }
+
+ line = tree_line_alloc( (const char *)&name[10], (const char *)&id[0], 0, date, offset, 0 );
+ directories = dlist_append( directories, (void *)line );
+
+ git_commit_free( commit );
+ }
+
+ git_reference_iterator_free( iter );
+ close_repository( repo );
+
+ /*************************
+ Print directories list:
+ */
+ if( directories )
+ {
+ struct dlist *list = NULL;
+
+ directories = tree_lines_sort_bytag( directories );
+
+ list = directories;
+
+ while( list )
+ {
+ struct tree_line *line = (struct tree_line *)list->data;
+
+ /**********************
+ Print tag Reference:
+ */
+ strbuf_addf( sb, " <div class=\"row\">\n" );
+ if( ctx.env.query_string && *ctx.env.query_string )
+ 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 );
+ else
+ 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 );
+ strbuf_addf( sb, " <div class=\"col-size\"><div onclick=\"trunc(this)\" class=\"tree-size trunc\">%ld</div></div>\n", line->size );
+ 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 );
+ if( line->date != -1 )
+ {
+ strbuf_addf( sb, " <div class=\"col-date\"><div onclick=\"trunc(this)\" class=\"tree-date trunc\">" );
+ cgit_print_age( sb, (time_t)line->date, line->offset, 0 );
+ strbuf_addf( sb, "</div></div>\n" );
+ }
+ else
+ {
+ strbuf_addf( sb, " <div class=\"col-date\"><div onclick=\"trunc(this)\" class=\"tree-date trunc\">unknown</div></div>\n" );
+ }
+ strbuf_addf( sb, " <div class=\"col-links\">\n" );
+ strbuf_addf( sb, " <div onclick=\"trunc(this)\" class=\"tree-links trunc\">\n" );
+ if( query_string && *query_string )
+ {
+ 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 );
+ 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 );
+ }
+ else
+ {
+ 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 );
+ 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 );
+ }
+ strbuf_addf( sb, " </div>\n" );
+ strbuf_addf( sb, " </div>\n" );
+ strbuf_addf( sb, " </div>\n\n" );
+ /*
+ End of printing.
+ **********************/
+
+ list = dlist_next( list );
+ }
+ directories = tree_lines_free( directories );
+ }
+
+ strbuf_addf( sb, " </div> <!-- End of Tree -->\n\n" );
+}
+
+
+static void cgit_print_tree( struct strbuf *sb, const char *relative_path, const char *revision )
+{
+ struct cgit_info info = CGIT_INFO_INIT;
+ const char *query_string = NULL;
+
+ if( !sb || !relative_path || !*relative_path ) return;
+
+ if( !strcmp( relative_path, "tags" ) ) return cgit_print_tags( sb, relative_path );
+ if( !strcmp( relative_path, "branches" ) ) return cgit_print_branches( sb, relative_path );
+
+ if( revision && strcmp( revision, (const char *)&ctx.repo.relative_info.revision[0] ) )
+ {
+ cgit_rpath_info( &info, relative_path, revision );
+ if( info.kind != GIT_OBJECT_BLOB )
+ {
+ strbuf_addf( sb, " <h1>Requested resource cannot be shown</h1>\n" );
+ strbuf_addf( sb, " <p class='leading'>The Tree with specified revision '%s' cannot be shown.</p>\n", revision );
+ return;
+ }
+ }
+ else
+ {
+ memcpy( (void *)&info, (void *)&ctx.repo.relative_info, sizeof( struct cgit_info ) );
+ }
+
+ query_string = ctx_remove_query_param( ctx.env.query_string, "rev" );
+ query_string = ctx_remove_query_param( query_string, "op" );
+
+ strbuf_addstr( sb, "\n" );
+ strbuf_addf( sb, " <div class=\"repo-tree-header\">\n" );
+ strbuf_addf( sb, " <div class=\"row\">\n" );
+ strbuf_addf( sb, " <div class=\"col-path\">Path</div>\n" );
+ strbuf_addf( sb, " <div class=\"col-size\"><div class=\"tree-size trunc\">Size</div></div>\n" );
+ strbuf_addf( sb, " <div class=\"col-rev\"><div class=\"tree-rev trunc\">Rev</div></div>\n" );
+ strbuf_addf( sb, " <div class=\"col-date\"><div class=\"tree-date trunc\">Date</div></div>\n" );
+ strbuf_addf( sb, " <div class=\"col-links\"><div class=\"tree-links trunc\">Links</div></div>\n" );
+ strbuf_addf( sb, " </div>\n" );
+ strbuf_addf( sb, " </div>\n\n" );
+ strbuf_addf( sb, " <div class=\"tree\">\n\n" );
+
+ if( info.kind == GIT_OBJECT_TREE )
+ {
+ git_repository *repo = NULL;
+ git_tree *tree = NULL;
+ git_oid oid;
+ size_t i, count;
+ char rpath[PATH_MAX] = { 0 };
+
+ if( git_oid_fromstr( &oid, (const char *)&info.oid[0] ) < 0 )
+ {
+ return;
+ }
+
+ if( !(repo = cgit_open_repository()) )
+ {
+ return;
+ }
+
+ if( git_tree_lookup( &tree, repo, &oid ) < 0 )
+ {
+ close_repository( repo );
+ return;
+ }
+
+ count = git_tree_entrycount( tree );
+ for( i = 0; i < count; ++i )
+ {
+ struct tree_line *line = NULL;
+ struct cgit_info entry_info = CGIT_INFO_INIT;
+ git_object *obj = NULL;
+ git_off_t rawsize = 4096;
+ const git_tree_entry *entry = git_tree_entry_byindex( (const git_tree *)tree, i );
+ const char *name = git_tree_entry_name( entry ); /* filename */
+
+ git_tree_entry_to_object( &obj, repo, entry );
+ if( git_object_type( (const git_object *)obj ) == GIT_OBJECT_BLOB )
+ {
+ rawsize = git_blob_rawsize( (git_blob *)obj );
+ }
+ git_object_free( obj );
+
+ sprintf( (char *)&rpath[0], "%s/%s", relative_path, name );
+ cgit_rpath_info( &entry_info, (char *)&rpath[0], info.revision );
+
+ line = tree_line_alloc( name, (const char *)&entry_info.revision[0], entry_info.mode, entry_info.date, entry_info.offset, rawsize );
+
+ if( entry_info.kind == GIT_OBJECT_TREE )
+ {
+ directories = dlist_append( directories, (void *)line );
+ }
+ else
+ {
+ files = dlist_append( files, (void *)line );
+ }
+ }
+
+ git_tree_free( tree );
+ close_repository( repo );
+
+
+ /*************************
+ Print directories list:
+ */
+ if( directories )
+ {
+ struct dlist *list = NULL;
+
+ directories = tree_lines_sort_byname( directories );
+
+ list = directories;
+
+ while( list )
+ {
+ const char *mode = "";
+ struct tree_line *line = (struct tree_line *)list->data;
+
+ if( line->mode == GIT_FILEMODE_LINK )
+ mode = " link";
+ else
+ mode = "";
+
+ /*************************
+ Print branch Reference:
+ */
+ strbuf_addf( sb, " <div class=\"row\">\n" );
+ if( ctx.env.query_string && *ctx.env.query_string )
+ 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 );
+ else
+ 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 );
+ strbuf_addf( sb, " <div class=\"col-size\"><div onclick=\"trunc(this)\" class=\"tree-size trunc\">%ld</div></div>\n", line->size );
+ 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 );
+ if( line->date != -1 )
+ {
+ strbuf_addf( sb, " <div class=\"col-date\"><div onclick=\"trunc(this)\" class=\"tree-date trunc\">" );
+ cgit_print_age( sb, (time_t)line->date, line->offset, 0 );
+ strbuf_addf( sb, "</div></div>\n" );
+ }
+ else
+ {
+ strbuf_addf( sb, " <div class=\"col-date\"><div onclick=\"trunc(this)\" class=\"tree-date trunc\">unknown</div></div>\n" );
+ }
+ strbuf_addf( sb, " <div class=\"col-links\">\n" );
+ strbuf_addf( sb, " <div onclick=\"trunc(this)\" class=\"tree-links trunc\">\n" );
+ if( query_string && *query_string )
+ {
+ 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 );
+ 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 );
+ }
+ else
+ {
+ 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 );
+ 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 );
+ }
+ strbuf_addf( sb, " </div>\n" );
+ strbuf_addf( sb, " </div>\n" );
+ strbuf_addf( sb, " </div>\n\n" );
+ /*
+ End of printing.
+ *************************/
+
+ list = dlist_next( list );
+ }
+ directories = tree_lines_free( directories );
+ }
+
+ /********************
+ Print filess list:
+ */
+ if( files )
+ {
+ struct dlist *list = NULL;
+
+ files = tree_lines_sort_byname( files );
+
+ list = files;
+
+ while( list )
+ {
+ const char *mode = "";
+ const char *mode_prefix = "";
+ struct tree_line *line = (struct tree_line *)list->data;
+
+ if( line->mode == GIT_FILEMODE_LINK )
+ {
+ mode = " link";
+ mode_prefix = "@";
+ }
+ else if( line->mode == GIT_FILEMODE_BLOB_EXECUTABLE )
+ {
+ mode = " exec";
+ mode_prefix = "";
+ }
+ else
+ {
+ mode = "";
+ mode_prefix = "";
+ }
+
+ /*************************
+ Print branch Reference:
+ */
+ strbuf_addf( sb, " <div class=\"row\">\n" );
+ if( ctx.env.query_string && *ctx.env.query_string )
+ 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 );
+ else
+ 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 );
+ strbuf_addf( sb, " <div class=\"col-size\"><div onclick=\"trunc(this)\" class=\"tree-size trunc\">%ld</div></div>\n", line->size );
+ 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 );
+ if( line->date != -1 )
+ {
+ strbuf_addf( sb, " <div class=\"col-date\"><div onclick=\"trunc(this)\" class=\"tree-date trunc\">" );
+ cgit_print_age( sb, (time_t)line->date, line->offset, 0 );
+ strbuf_addf( sb, "</div></div>\n" );
+ }
+ else
+ {
+ strbuf_addf( sb, " <div class=\"col-date\"><div onclick=\"trunc(this)\" class=\"tree-date trunc\">unknown</div></div>\n" );
+ }
+ strbuf_addf( sb, " <div class=\"col-links\">\n" );
+ strbuf_addf( sb, " <div onclick=\"trunc(this)\" class=\"tree-links trunc\">\n" );
+ if( query_string && *query_string )
+ {
+ 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 );
+ 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 );
+ 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 );
+ }
+ else
+ {
+ 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 );
+ 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 );
+ strbuf_addf( sb, " <a href=\"/%s/%s/%s/?op=blame&rev=%s\">blame</a>\n", ctx.repo.name, relative_path, line->name, line->revision );
+ }
+ strbuf_addf( sb, " </div>\n" );
+ strbuf_addf( sb, " </div>\n" );
+ strbuf_addf( sb, " </div>\n\n" );
+ /*
+ End of printing.
+ *************************/
+
+ list = dlist_next( list );
+ }
+ files = tree_lines_free( files );
+ }
+ }
+
+ strbuf_addf( sb, " </div> <!-- End of Tree -->\n\n" );
+
+ return;
+}
+
+static void cgit_print_summary( struct strbuf *sb, const char *relative_path, const char *revision )
+{
+ struct cgit_info info;
+ const char *query_string = NULL;
+
+ if( !sb ) return;
+
+ if( relative_path && *relative_path )
+ return cgit_print_tree( sb, relative_path, revision );
+
+ query_string = ctx_remove_query_param( ctx.env.query_string, "rev" );
+ query_string = ctx_remove_query_param( query_string, "op" );
+
+ strbuf_addstr( sb, "\n" );
+ strbuf_addf( sb, " <div class=\"repo-tree-header\">\n" );
+ strbuf_addf( sb, " <div class=\"row\">\n" );
+ strbuf_addf( sb, " <div class=\"col-path\">Refs</div>\n" );
+ strbuf_addf( sb, " </div>\n" );
+ strbuf_addf( sb, " </div>\n\n" );
+ strbuf_addf( sb, " <div class=\"tree\">\n\n" );
+ strbuf_addf( sb, " <div class=\"row\">\n" );
+ strbuf_addf( sb, " <div class=\"col-path\"><div class=\"tree-path dir\"><a href=\"/%s/branches/\">branches/</a></div></div>\n", ctx.repo.name );
+ strbuf_addf( sb, " </div>\n" );
+ strbuf_addf( sb, " <div class=\"row\">\n" );
+ strbuf_addf( sb, " <div class=\"col-path\"><div class=\"tree-path dir\"><a href=\"/%s/tags/\">tags/</a></div></div>\n", ctx.repo.name );
+ strbuf_addf( sb, " </div>\n\n" );
+ strbuf_addf( sb, " </div> <!-- End of Tree -->\n" );
+
+ cgit_rpath_info( &info, "/", revision );
+ if( info.kind != GIT_OBJECT_TREE && info.kind != GIT_OBJECT_BLOB )
+ {
+ memcpy( (void *)&info, (const void *)&ctx.repo.relative_info, sizeof( struct cgit_info ) );
+ ctx.query.revision = NULL;
+ (void)ctx_grab_str_query_param( "rev" );
+ ctx.query.rev = "0";
+ }
+
+ strbuf_addstr( sb, "\n" );
+ strbuf_addf( sb, " <div class=\"repo-tree-header\">\n" );
+ strbuf_addf( sb, " <div class=\"row\">\n" );
+ strbuf_addf( sb, " <div class=\"col-path\">Path</div>\n" );
+ strbuf_addf( sb, " <div class=\"col-size\"><div class=\"tree-size trunc\">Size</div></div>\n" );
+ strbuf_addf( sb, " <div class=\"col-rev\"><div class=\"tree-rev trunc\">Rev</div></div>\n" );
+ strbuf_addf( sb, " <div class=\"col-date\"><div class=\"tree-date trunc\">Date</div></div>\n" );
+ strbuf_addf( sb, " <div class=\"col-links\"><div class=\"tree-links trunc\">Links</div></div>\n" );
+ strbuf_addf( sb, " </div>\n" );
+ strbuf_addf( sb, " </div>\n\n" );
+ strbuf_addf( sb, " <div class=\"tree\">\n\n" );
+
+ /************************
+ Print trunk Reference:
+ */
+ strbuf_addf( sb, " <div class=\"row\">\n" );
+ strbuf_addf( sb, " <div class=\"col-path\"><a href=\"/%s/trunk/\"><div class=\"tree-path dir\">trunk/</div></a></div>\n", ctx.repo.name );
+ strbuf_addf( sb, " <div class=\"col-size\"><div onclick=\"trunc(this)\" class=\"tree-size trunc\">%ld</div></div>\n", (git_off_t)4096 );
+ 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 );
+ if( info.date != -1 )
+ {
+ strbuf_addf( sb, " <div class=\"col-date\"><div onclick=\"trunc(this)\" class=\"tree-date trunc\">" );
+ cgit_print_age( sb, (time_t)info.date, info.offset, 0 );
+ strbuf_addf( sb, "</div></div>\n" );
+ }
+ else
+ {
+ strbuf_addf( sb, " <div class=\"col-date\"><div onclick=\"trunc(this)\" class=\"tree-date trunc\">unknown</div></div>\n" );
+ }
+ strbuf_addf( sb, " <div class=\"col-links\">\n" );
+ strbuf_addf( sb, " <div onclick=\"trunc(this)\" class=\"tree-links trunc\">\n" );
+ if( query_string && *query_string )
+ {
+ 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 );
+ 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 );
+ }
+ else
+ {
+ 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 );
+ strbuf_addf( sb, " <a title=\"Compare with Previous\" href=\"/%s/trunk/?op=diff&rev=%s\">diff</a>\n", ctx.repo.name, info.revision );
+ }
+ strbuf_addf( sb, " </div>\n" );
+ strbuf_addf( sb, " </div>\n" );
+ strbuf_addf( sb, " </div>\n\n" );
+
+ strbuf_addf( sb, " </div> <!-- End of Tree -->\n\n" );
+}
+
+
+static void cgit_print_clone_urls( struct strbuf *sb, const char *relative_path )
+{
+ char ref[PATH_MAX] = { 0 };
+ char rpath[PATH_MAX] = { 0 };
+ char branch[PATH_MAX] = { 0 };
+
+ if( !sb ) return;
+
+ parse_relative_path( (char *)&ref[0], (char *)&rpath[0], relative_path );
+
+ if( ref[0] && strlen( (char *)&ref[0] ) > 11 )
+ {
+ const char *b = NULL;
+ if( !strncmp( (char *)&ref[0], "refs/heads/", 11 ) )
+ {
+ b = (const char *)&ref[11];
+ sprintf( (char *)&branch[0], "--branch %s -- ", b );
+ }
+
+ if( b && !strcmp( b, ctx.repo.trunk ) )
+ {
+ branch[0] = '\0';
+ }
+ }
+
+ strbuf_addf( sb, " <div class=\"clone-box\">\n" );
+ strbuf_addf( sb, " <div class=\"clone-header\">Clone</div>\n" );
+ strbuf_addf( sb, " <div class=\"clone-urls\">\n" );
+ strbuf_addf( sb, " <div class=\"clone-line\">\n" );
+ strbuf_addf( sb, " <div class=\"clone-perms\">ro:</div>\n" );
+
+ strbuf_addf( sb, " <div class=\"clone-url\">git clone %s%s/%s</div>\n", branch, ctx.repo.clone_ro_prefix, ctx.repo.name );
+
+ strbuf_addf( sb, " </div>\n" );
+ if( ctx.repo.clone_prefix && *ctx.repo.clone_prefix )
+ {
+ strbuf_addf( sb, " <div class=\"clone-line\">\n" );
+ strbuf_addf( sb, " <div class=\"clone-perms\">rw:</div>\n" );
+
+ strbuf_addf( sb, " <div class=\"clone-url\">git clone %s%s/%s</div>\n", branch, ctx.repo.clone_prefix, ctx.repo.name );
+
+ strbuf_addf( sb, " </div>\n" );
+ }
+ strbuf_addf( sb, " </div>\n" );
+ strbuf_addf( sb, " </div> <!-- End of Clone -->\n\n" );
+}
+
+void cgit_print_tree_page( void )
+{
+ FILE *fp;
+ struct strbuf buf = STRBUF_INIT;
+
+ fp = xfopen( ctx.page.header, "r" );
+ (void)strbuf_env_fread( &buf, fp );
+ fclose( fp );
+
+ strbuf_addf( &buf, " <div class=\"content segment\">\n" );
+ strbuf_addf( &buf, " <div class=\"container\">\n" );
+ strbuf_addf( &buf, " <div class=\"cgit-main-content\">\n" );
+
+ if( ctx.repo.relative_info.kind == GIT_OBJECT_TREE )
+ {
+ if( ctx.repo.name )
+ {
+ cgit_print_summary( &buf, ctx.repo.relative_path, (!strcmp( ctx.query.rev, "0" )) ? (const char *)&ctx.repo.relative_info.revision[0] : ctx.query.rev );
+ cgit_print_clone_urls( &buf, ctx.repo.relative_path );
+ }
+ else
+ {
+ strbuf_addf( &buf, " <h1>Requested resource cannot be shown</h1>\n" );
+ strbuf_addf( &buf, " <p class='leading'>Repository '%s' not found.</p>\n", ctx.repo.name );
+ }
+ }
+ else
+ {
+ strbuf_addf( &buf, " <h1>Requested resource cannot be shown</h1>\n" );
+ strbuf_addf( &buf, " <p class='leading'>This page assume the repository tree to be shown.</p>\n" );
+ }
+
+ 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" );
+
+ fp = xfopen( ctx.page.footer, "r" );
+ (void)strbuf_env_fread( &buf, fp );
+ fclose( fp );
+
+ ctx.page.size = buf.len;
+ cgit_print_http_headers();
+ strbuf_write( &buf, STDOUT_FILENO );
+ strbuf_release( &buf );
+}