#ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #ifdef HAVE_INTTYPES_H #include #else #include #endif #include /* offsetof(3) */ #include #include /* chmod(2) */ #include #include #include #include #include /* strdup(3) */ #include /* basename(3) */ #include /* tolower(3) */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /******************************** 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, "
\n" ); strbuf_addf( sb, "
\n" ); strbuf_addf( sb, "
Path
\n" ); strbuf_addf( sb, "
Size
\n" ); strbuf_addf( sb, "
Rev
\n" ); strbuf_addf( sb, "
Date
\n" ); strbuf_addf( sb, "
\n" ); strbuf_addf( sb, "
\n" ); strbuf_addf( sb, "
\n\n" ); strbuf_addf( sb, "
\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, "
\n" ); if( ctx.env.query_string && *ctx.env.query_string ) strbuf_addf( sb, " \n", ctx.repo.name, line->name, ctx.env.query_string, line->name ); else strbuf_addf( sb, " \n", ctx.repo.name, line->name, line->name ); strbuf_addf( sb, "
%ld
\n", line->size ); strbuf_addf( sb, "
%0.8s
\n", line->revision, line->revision ); if( line->date != -1 ) { strbuf_addf( sb, "
" ); cgit_print_age( sb, (time_t)line->date, line->offset, 0 ); strbuf_addf( sb, "
\n" ); } else { strbuf_addf( sb, "
unknown
\n" ); } strbuf_addf( sb, "
\n" ); strbuf_addf( sb, " \n" ); strbuf_addf( sb, "
\n" ); strbuf_addf( sb, "
\n\n" ); /* End of printing. *************************/ } list = dlist_next( list ); } directories = tree_lines_free( directories ); } strbuf_addf( sb, "
\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, "
\n" ); strbuf_addf( sb, "
\n" ); strbuf_addf( sb, "
Path
\n" ); strbuf_addf( sb, "
Size
\n" ); strbuf_addf( sb, "
Rev
\n" ); strbuf_addf( sb, "
Date
\n" ); strbuf_addf( sb, "
\n" ); strbuf_addf( sb, "
\n" ); strbuf_addf( sb, "
\n\n" ); strbuf_addf( sb, "
\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, "
\n" ); if( ctx.env.query_string && *ctx.env.query_string ) strbuf_addf( sb, " \n", ctx.repo.name, line->name, ctx.env.query_string, line->name ); else strbuf_addf( sb, " \n", ctx.repo.name, line->name, line->name ); strbuf_addf( sb, "
%ld
\n", line->size ); strbuf_addf( sb, "
%0.8s
\n", line->revision, line->revision ); if( line->date != -1 ) { strbuf_addf( sb, "
" ); cgit_print_age( sb, (time_t)line->date, line->offset, 0 ); strbuf_addf( sb, "
\n" ); } else { strbuf_addf( sb, "
unknown
\n" ); } strbuf_addf( sb, "
\n" ); strbuf_addf( sb, " \n" ); strbuf_addf( sb, "
\n" ); strbuf_addf( sb, "
\n\n" ); /* End of printing. **********************/ list = dlist_next( list ); } directories = tree_lines_free( directories ); } strbuf_addf( sb, "
\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, "

Requested resource cannot be shown

\n" ); strbuf_addf( sb, "

The Tree with specified revision '%s' cannot be shown.

\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, "
\n" ); strbuf_addf( sb, "
\n" ); strbuf_addf( sb, "
Path
\n" ); strbuf_addf( sb, "
Size
\n" ); strbuf_addf( sb, "
Rev
\n" ); strbuf_addf( sb, "
Date
\n" ); strbuf_addf( sb, "
\n" ); strbuf_addf( sb, "
\n" ); strbuf_addf( sb, "
\n\n" ); strbuf_addf( sb, "
\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, "
\n" ); if( ctx.env.query_string && *ctx.env.query_string ) strbuf_addf( sb, " \n", ctx.repo.name, relative_path, line->name, ctx.env.query_string, mode, line->name ); else strbuf_addf( sb, " \n", ctx.repo.name, relative_path, line->name, mode, line->name ); strbuf_addf( sb, "
%ld
\n", line->size ); strbuf_addf( sb, "
%0.8s
\n", line->revision, line->revision ); if( line->date != -1 ) { strbuf_addf( sb, "
" ); cgit_print_age( sb, (time_t)line->date, line->offset, 0 ); strbuf_addf( sb, "
\n" ); } else { strbuf_addf( sb, "
unknown
\n" ); } strbuf_addf( sb, "
\n" ); strbuf_addf( sb, " \n" ); strbuf_addf( sb, "
\n" ); strbuf_addf( sb, "
\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, "
\n" ); if( ctx.env.query_string && *ctx.env.query_string ) strbuf_addf( sb, " \n", ctx.repo.name, relative_path, line->name, ctx.env.query_string, mode, mode_prefix, line->name ); else strbuf_addf( sb, " \n", ctx.repo.name, relative_path, line->name, mode, mode_prefix, line->name ); strbuf_addf( sb, "
%ld
\n", line->size ); strbuf_addf( sb, "
%0.8s
\n", line->revision, line->revision ); if( line->date != -1 ) { strbuf_addf( sb, "
" ); cgit_print_age( sb, (time_t)line->date, line->offset, 0 ); strbuf_addf( sb, "
\n" ); } else { strbuf_addf( sb, "
unknown
\n" ); } strbuf_addf( sb, "
\n" ); strbuf_addf( sb, " \n" ); strbuf_addf( sb, "
\n" ); strbuf_addf( sb, "
\n\n" ); /* End of printing. *************************/ list = dlist_next( list ); } files = tree_lines_free( files ); } } strbuf_addf( sb, "
\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, "
\n" ); strbuf_addf( sb, "
\n" ); strbuf_addf( sb, "
Refs
\n" ); strbuf_addf( sb, "
\n" ); strbuf_addf( sb, "
\n\n" ); strbuf_addf( sb, "
\n\n" ); strbuf_addf( sb, "
\n" ); strbuf_addf( sb, " \n", ctx.repo.name ); strbuf_addf( sb, "
\n" ); strbuf_addf( sb, "
\n" ); strbuf_addf( sb, " \n", ctx.repo.name ); strbuf_addf( sb, "
\n\n" ); strbuf_addf( sb, "
\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, "
\n" ); strbuf_addf( sb, "
\n" ); strbuf_addf( sb, "
Path
\n" ); strbuf_addf( sb, "
Size
\n" ); strbuf_addf( sb, "
Rev
\n" ); strbuf_addf( sb, "
Date
\n" ); strbuf_addf( sb, "
\n" ); strbuf_addf( sb, "
\n" ); strbuf_addf( sb, "
\n\n" ); strbuf_addf( sb, "
\n\n" ); /************************ Print trunk Reference: */ strbuf_addf( sb, "
\n" ); strbuf_addf( sb, " \n", ctx.repo.name ); strbuf_addf( sb, "
%ld
\n", (git_off_t)4096 ); strbuf_addf( sb, "
%0.8s
\n", info.revision, info.revision ); if( info.date != -1 ) { strbuf_addf( sb, "
" ); cgit_print_age( sb, (time_t)info.date, info.offset, 0 ); strbuf_addf( sb, "
\n" ); } else { strbuf_addf( sb, "
unknown
\n" ); } strbuf_addf( sb, "
\n" ); strbuf_addf( sb, " \n" ); strbuf_addf( sb, "
\n" ); strbuf_addf( sb, "
\n\n" ); strbuf_addf( sb, "
\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, "
\n" ); strbuf_addf( sb, "
Clone
\n" ); strbuf_addf( sb, "
\n" ); strbuf_addf( sb, "
\n" ); strbuf_addf( sb, "
ro:
\n" ); strbuf_addf( sb, "
git clone %s%s/%s
\n", branch, ctx.repo.clone_ro_prefix, ctx.repo.name ); strbuf_addf( sb, "
\n" ); if( ctx.repo.clone_prefix && *ctx.repo.clone_prefix ) { strbuf_addf( sb, "
\n" ); strbuf_addf( sb, "
rw:
\n" ); strbuf_addf( sb, "
git clone %s%s/%s
\n", branch, ctx.repo.clone_prefix, ctx.repo.name ); strbuf_addf( sb, "
\n" ); } strbuf_addf( sb, "
\n" ); strbuf_addf( sb, "
\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, "
\n" ); strbuf_addf( &buf, "
\n" ); strbuf_addf( &buf, "
\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, "

Requested resource cannot be shown

\n" ); strbuf_addf( &buf, "

Repository '%s' not found.

\n", ctx.repo.name ); } } else { strbuf_addf( &buf, "

Requested resource cannot be shown

\n" ); strbuf_addf( &buf, "

This page assume the repository tree to be shown.

\n" ); } strbuf_addf( &buf, "
\n" ); strbuf_addf( &buf, "
\n" ); strbuf_addf( &buf, "
\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 ); }