author | Lars Hjemli <hjemli@gmail.com> | 2007-06-16 18:20:42 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2007-06-16 18:20:42 (UTC) |
commit | ffc69736a606fd8f9a10468cce48b9a37b428944 (patch) (side-by-side diff) | |
tree | b080c702391244a525fa0e7c97758f7f7fa0c383 | |
parent | 849f0f0f02f858e21fbe59295846e51628f82915 (diff) | |
download | cgit-ffc69736a606fd8f9a10468cce48b9a37b428944.zip cgit-ffc69736a606fd8f9a10468cce48b9a37b428944.tar.gz cgit-ffc69736a606fd8f9a10468cce48b9a37b428944.tar.bz2 |
ui-tree: unify with ui-view, use path to select tree/blob
This teaches ui-tree to show both trees and blobs, thereby making ui-view
superfluous. At the same time, ui-tree is extended to honour the specified
path instead of requiering a tree/blob sha1.
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | cgit.c | 5 | ||||
-rw-r--r-- | cgit.css | 16 | ||||
-rw-r--r-- | cgit.h | 8 | ||||
-rw-r--r-- | shared.c | 3 | ||||
-rw-r--r-- | ui-commit.c | 3 | ||||
-rw-r--r-- | ui-tree.c | 192 | ||||
-rw-r--r-- | ui-view.c | 55 |
8 files changed, 173 insertions, 111 deletions
@@ -1,41 +1,41 @@ CGIT_VERSION = 0.5 prefix = /var/www/htdocs/cgit SHA1_HEADER = <openssl/sha.h> CACHE_ROOT = /var/cache/cgit CGIT_CONFIG = /etc/cgitrc CGIT_SCRIPT_NAME = cgit.cgi # # Let the user override the above settings. # -include cgit.conf EXTLIBS = git/libgit.a git/xdiff/lib.a -lz -lcrypto OBJECTS = shared.o cache.o parsing.o html.o ui-shared.o ui-repolist.o \ - ui-summary.o ui-log.o ui-view.o ui-tree.o ui-commit.o ui-diff.o \ + ui-summary.o ui-log.o ui-tree.o ui-commit.o ui-diff.o \ ui-snapshot.o ui-blob.o CFLAGS += -Wall ifdef DEBUG CFLAGS += -g endif CFLAGS += -Igit CFLAGS += -DSHA1_HEADER='$(SHA1_HEADER)' CFLAGS += -DCGIT_VERSION='"$(CGIT_VERSION)"' CFLAGS += -DCGIT_CONFIG='"$(CGIT_CONFIG)"' CFLAGS += -DCGIT_SCRIPT_NAME='"$(CGIT_SCRIPT_NAME)"' # # If make is run on a nongit platform, get the git sources as a tarball. # GITVER = $(shell git version 2>/dev/null || echo nogit) ifeq ($(GITVER),nogit) GITURL = http://www.kernel.org/pub/software/scm/git/git-1.5.2.tar.bz2 INITGIT = test -e git/git.c || ((curl "$(GITURL)" | tar -xj) && mv git-1.5.2 git) else INITGIT = ./submodules.sh -i @@ -76,56 +76,53 @@ static void cgit_print_repo_page(struct cacheitem *item) if (cgit_cmd == CMD_BLOB) { cgit_print_blob(item, cgit_query_sha1, cgit_query_path); return; } show_search = (cgit_cmd == CMD_LOG); cgit_print_docstart(title, item); if (!cgit_cmd) { cgit_print_pageheader("summary", show_search); cgit_print_summary(); cgit_print_docend(); return; } cgit_print_pageheader(cgit_query_page, show_search); switch(cgit_cmd) { case CMD_LOG: cgit_print_log(cgit_query_head, cgit_query_ofs, cgit_max_commit_count, cgit_query_search, cgit_query_path, 1); break; case CMD_TREE: - cgit_print_tree(cgit_query_head, cgit_query_sha1, cgit_query_path); + cgit_print_tree(cgit_query_sha1, cgit_query_path); break; case CMD_COMMIT: cgit_print_commit(cgit_query_head); break; - case CMD_VIEW: - cgit_print_view(cgit_query_sha1, cgit_query_path); - break; case CMD_DIFF: cgit_print_diff(cgit_query_head, cgit_query_sha1, cgit_query_sha2, cgit_query_path); break; default: cgit_print_error("Invalid request"); } cgit_print_docend(); } static void cgit_fill_cache(struct cacheitem *item, int use_cache) { static char buf[PATH_MAX]; int stdout2; getcwd(buf, sizeof(buf)); item->st.st_mtime = time(NULL); if (use_cache) { stdout2 = chk_positive(dup(STDOUT_FILENO), "Preserving STDOUT"); chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); } @@ -178,52 +178,64 @@ td.ls-blob, td.ls-dir, td.ls-mod { font-family: monospace; } div.ls-dir a { font-weight: bold; } th.filesize, td.filesize { text-align: right; } td.filesize { font-family: monospace; } td.links { font-size: 80%; padding-left: 2em; } td.filemode { font-family: monospace; } -td.blob { +table.blob { + margin-top: 0.5em; + border-top: solid 1px black; +} + +table.blob td.no { + border-right: solid 1px black; + color: black; + background-color: #eee; + text-align: right; +} + +table.blob td.txt { white-space: pre; font-family: monospace; - background-color: white; + padding-left: 0.5em; } table.nowrap td { white-space: nowrap; } table.commit-info { border-collapse: collapse; margin-top: 1.5em; } table.commit-info th { text-align: left; font-weight: normal; padding: 0.1em 1em 0.1em 0.1em; } table.commit-info td { font-weight: normal; padding: 0.1em 1em 0.1em 0.1em; } div.commit-subject { font-weight: bold; @@ -4,51 +4,50 @@ #include <git-compat-util.h> #include <cache.h> #include <grep.h> #include <object.h> #include <tree.h> #include <commit.h> #include <tag.h> #include <diff.h> #include <diffcore.h> #include <refs.h> #include <revision.h> #include <log-tree.h> #include <archive.h> #include <xdiff/xdiff.h> /* * The valid cgit repo-commands */ #define CMD_LOG 1 #define CMD_COMMIT 2 #define CMD_DIFF 3 #define CMD_TREE 4 -#define CMD_VIEW 5 -#define CMD_BLOB 6 -#define CMD_SNAPSHOT 7 +#define CMD_BLOB 5 +#define CMD_SNAPSHOT 6 /* * Dateformats used on misc. pages */ #define FMT_LONGDATE "%Y-%m-%d %H:%M:%S" #define FMT_SHORTDATE "%Y-%m-%d" /* * Limits used for relative dates */ #define TM_MIN 60 #define TM_HOUR (TM_MIN * 60) #define TM_DAY (TM_HOUR * 24) #define TM_WEEK (TM_DAY * 7) #define TM_YEAR (TM_DAY * 365) #define TM_MONTH (TM_YEAR / 12.0) typedef void (*configfn)(const char *name, const char *value); typedef void (*filepair_fn)(struct diff_filepair *pair); typedef void (*linediff_fn)(char *line, int len); @@ -194,35 +193,34 @@ extern void cgit_parse_url(const char *url); extern char *cache_safe_filename(const char *unsafe); extern int cache_lock(struct cacheitem *item); extern int cache_unlock(struct cacheitem *item); extern int cache_cancel_lock(struct cacheitem *item); extern int cache_exist(struct cacheitem *item); extern int cache_expired(struct cacheitem *item); extern char *cgit_repourl(const char *reponame); extern char *cgit_pageurl(const char *reponame, const char *pagename, const char *query); extern void cgit_print_error(char *msg); extern void cgit_print_date(time_t secs, char *format); extern void cgit_print_age(time_t t, time_t max_relative, char *format); extern void cgit_print_docstart(char *title, struct cacheitem *item); extern void cgit_print_docend(); extern void cgit_print_pageheader(char *title, int show_search); extern void cgit_print_snapshot_start(const char *mimetype, const char *filename, struct cacheitem *item); extern void cgit_print_repolist(struct cacheitem *item); extern void cgit_print_summary(); extern void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *path, int pager); -extern void cgit_print_view(const char *hex, char *path); extern void cgit_print_blob(struct cacheitem *item, const char *hex, char *path); -extern void cgit_print_tree(const char *rev, const char *hex, char *path); +extern void cgit_print_tree(const char *rev, char *path); extern void cgit_print_commit(const char *hex); extern void cgit_print_diff(const char *head, const char *old_hex, const char *new_hex, char *path); extern void cgit_print_snapshot(struct cacheitem *item, const char *hex, const char *format, const char *prefix, const char *filename); #endif /* CGIT_H */ @@ -38,49 +38,50 @@ int cgit_summary_log = 0; int cgit_max_msg_len = 60; int cgit_max_repodesc_len = 60; int cgit_max_commit_count = 50; int cgit_query_has_symref = 0; int cgit_query_has_sha1 = 0; char *cgit_querystring = NULL; char *cgit_query_repo = NULL; char *cgit_query_page = NULL; char *cgit_query_head = NULL; char *cgit_query_search = NULL; char *cgit_query_sha1 = NULL; char *cgit_query_sha2 = NULL; char *cgit_query_path = NULL; char *cgit_query_name = NULL; int cgit_query_ofs = 0; int htmlfd = 0; int cgit_get_cmd_index(const char *cmd) { - static char *cmds[] = {"log", "commit", "diff", "tree", "view", "blob", "snapshot", NULL}; + static char *cmds[] = {"log", "commit", "diff", "tree", "blob", + "snapshot", NULL}; int i; for(i = 0; cmds[i]; i++) if (!strcmp(cmd, cmds[i])) return i + 1; return 0; } int chk_zero(int result, char *msg) { if (result != 0) die("%s: %s", msg, strerror(errno)); return result; } int chk_positive(int result, char *msg) { if (result <= 0) die("%s: %s", msg, strerror(errno)); return result; } struct repoinfo *add_repo(const char *url) { diff --git a/ui-commit.c b/ui-commit.c index 1d12bbb..ed5384d 100644 --- a/ui-commit.c +++ b/ui-commit.c @@ -161,50 +161,49 @@ void cgit_print_commit(const char *hex) } commit = lookup_commit_reference(sha1); if (!commit) { cgit_print_error(fmt("Bad commit reference: %s", hex)); return; } info = cgit_parse_commit(commit); html("<table class='commit-info'>\n"); html("<tr><th>author</th><td>"); html_txt(info->author); html(" "); html_txt(info->author_email); html("</td><td class='right'>"); cgit_print_date(info->author_date, FMT_LONGDATE); html("</td></tr>\n"); html("<tr><th>committer</th><td>"); html_txt(info->committer); html(" "); html_txt(info->committer_email); html("</td><td class='right'>"); cgit_print_date(info->committer_date, FMT_LONGDATE); html("</td></tr>\n"); html("<tr><th>tree</th><td colspan='2' class='sha1'><a href='"); - query = fmt("h=%s&id=%s", sha1_to_hex(commit->object.sha1), - sha1_to_hex(commit->tree->object.sha1)); + query = fmt("h=%s", sha1_to_hex(commit->object.sha1)); html_attr(cgit_pageurl(cgit_query_repo, "tree", query)); htmlf("'>%s</a></td></tr>\n", sha1_to_hex(commit->tree->object.sha1)); for (p = commit->parents; p ; p = p->next) { parent = lookup_commit_reference(p->item->object.sha1); if (!parent) { html("<tr><td colspan='3'>"); cgit_print_error("Error reading parent commit"); html("</td></tr>"); continue; } html("<tr><th>parent</th>" "<td colspan='2' class='sha1'>" "<a href='"); query = fmt("h=%s", sha1_to_hex(p->item->object.sha1)); html_attr(cgit_pageurl(cgit_query_repo, "commit", query)); htmlf("'>%s</a> (<a href='", sha1_to_hex(p->item->object.sha1)); query = fmt("id=%s&id2=%s", sha1_to_hex(parent->tree->object.sha1), sha1_to_hex(commit->tree->object.sha1)); html_attr(cgit_pageurl(cgit_query_repo, "diff", query)); html("'>diff</a>)</td></tr>"); } if (cgit_repo->snapshots) { htmlf("<tr><th>download</th><td colspan='2' class='sha1'><a href='"); @@ -1,104 +1,214 @@ /* ui-tree.c: functions for tree output * * Copyright (C) 2006 Lars Hjemli * * Licensed under GNU General Public License v2 * (see COPYING for full license text) */ #include "cgit.h" char *curr_rev; +char *match_path; +int header = 0; -static int print_entry(const unsigned char *sha1, const char *base, - int baselen, const char *pathname, unsigned int mode, - int stage) +static void print_object(const unsigned char *sha1, char *path) +{ + enum object_type type; + unsigned char *buf; + unsigned long size, lineno, start, idx; + + type = sha1_object_info(sha1, &size); + if (type == OBJ_BAD) { + cgit_print_error(fmt("Bad object name: %s", + sha1_to_hex(sha1))); + return; + } + + buf = read_sha1_file(sha1, &type, &size); + if (!buf) { + cgit_print_error(fmt("Error reading object %s", + sha1_to_hex(sha1))); + return; + } + + html("<table class='blob'>\n"); + idx = 0; + start = 0; + lineno = 0; + while(idx < size) { + if (buf[idx] == '\n') { + buf[idx] = '\0'; + htmlf("<tr><td class='no'>%d</td><td class='txt'>", + ++lineno); + html_txt(buf + start); + html("</td></tr>\n"); + start = idx + 1; + } + idx++; + } + html("\n</td></tr>\n"); + html("</table>\n"); +} + + +static int ls_item(const unsigned char *sha1, const char *base, int baselen, + const char *pathname, unsigned int mode, int stage) { char *name; enum object_type type; unsigned long size = 0; + char *url, *qry; name = xstrdup(pathname); type = sha1_object_info(sha1, &size); if (type == OBJ_BAD && !S_ISDIRLNK(mode)) { htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>", name, sha1_to_hex(sha1)); return 0; } + qry = fmt("h=%s&path=%s%s%s", curr_rev, + cgit_query_path ? cgit_query_path : "", + cgit_query_path ? "/" : "", pathname); + url = cgit_pageurl(cgit_query_repo, "tree", qry); html("<tr><td class='filemode'>"); html_filemode(mode); html("</td><td "); if (S_ISDIRLNK(mode)) { htmlf("class='ls-mod'><a href='"); html_attr(fmt(cgit_repo->module_link, name, sha1_to_hex(sha1))); } else if (S_ISDIR(mode)) { html("class='ls-dir'><a href='"); - html_attr(cgit_pageurl(cgit_query_repo, "tree", - fmt("h=%s&id=%s&path=%s%s/", - curr_rev, - sha1_to_hex(sha1), - cgit_query_path ? cgit_query_path : "", - pathname))); + html_attr(url); } else { html("class='ls-blob'><a href='"); - html_attr(cgit_pageurl(cgit_query_repo, "view", - fmt("h=%s&id=%s&path=%s%s", curr_rev, - sha1_to_hex(sha1), - cgit_query_path ? cgit_query_path : "", - pathname))); + html_attr(url); } htmlf("'>%s</a></td>", name); htmlf("<td class='filesize'>%li</td>", size); html("<td class='links'><a href='"); - html_attr(cgit_pageurl(cgit_query_repo, "log", - fmt("h=%s&path=%s%s", - curr_rev, + qry = fmt("h=%s&path=%s%s%s", curr_rev, cgit_query_path ? cgit_query_path : "", - pathname))); - html("'>history</a></td>"); + cgit_query_path ? "/" : "", pathname); + url = cgit_pageurl(cgit_query_repo, "log", qry); + html_attr(url); + html("' class='button'>H</a></td>"); html("</tr>\n"); free(name); return 0; } -void cgit_print_tree(const char *rev, const char *hex, char *path) +static void ls_head() +{ + html("<table class='list'>\n"); + html("<tr class='nohover'>"); + html("<th class='left'>Mode</th>"); + html("<th class='left'>Name</th>"); + html("<th class='right'>Size</th>"); + html("<th/>"); + html("</tr>\n"); + header = 1; +} + +static void ls_tail() +{ + if (!header) + return; + html("</table>\n"); + header = 0; +} + +static void ls_tree(const unsigned char *sha1, char *path) { struct tree *tree; + + tree = parse_tree_indirect(sha1); + if (!tree) { + cgit_print_error(fmt("Not a tree object: %s", + sha1_to_hex(sha1))); + return; + } + + ls_head(); + read_tree_recursive(tree, "", 0, 1, NULL, ls_item); + ls_tail(); +} + + +static int walk_tree(const unsigned char *sha1, const char *base, int baselen, + const char *pathname, unsigned mode, int stage) +{ + static int state; + static char buffer[PATH_MAX]; + char *url; + + if (state == 0) { + memcpy(buffer, base, baselen); + strcpy(buffer+baselen, pathname); + url = cgit_pageurl(cgit_query_repo, "tree", + fmt("h=%s&path=%s", curr_rev, buffer)); + htmlf(" / <a href='"); + html_attr(url); + html("'>"); + html_txt(xstrdup(pathname)); + html("</a>"); + + if (strcmp(match_path, buffer)) + return READ_TREE_RECURSIVE; + + if (S_ISDIR(mode)) { + state = 1; + ls_head(); + return READ_TREE_RECURSIVE; + } else { + print_object(sha1, buffer); + return 0; + } + } + ls_item(sha1, base, baselen, pathname, mode, stage); + return 0; +} + + +/* + * Show a tree or a blob + * rev: the commit pointing at the root tree object + * path: path to tree or blob + */ +void cgit_print_tree(const char *rev, char *path) +{ unsigned char sha1[20]; struct commit *commit; + const char *paths[] = {path, NULL}; + + if (!rev) + rev = cgit_query_head; curr_rev = xstrdup(rev); - get_sha1(rev, sha1); + if (get_sha1(rev, sha1)) { + cgit_print_error(fmt("Invalid revision name: %s", rev)); + return; + } commit = lookup_commit_reference(sha1); if (!commit || parse_commit(commit)) { - cgit_print_error(fmt("Invalid head: %s", rev)); + cgit_print_error(fmt("Invalid commit reference: %s", rev)); return; } - if (!hex) - hex = sha1_to_hex(commit->tree->object.sha1); - if (get_sha1_hex(hex, sha1)) { - cgit_print_error(fmt("Invalid object id: %s", hex)); - return; - } - tree = parse_tree_indirect(sha1); - if (!tree) { - cgit_print_error(fmt("Not a tree object: %s", hex)); + html("path: <a href='"); + html_attr(cgit_pageurl(cgit_query_repo, "tree", fmt("h=%s", rev))); + html("'>root</a>"); + + if (path == NULL) { + ls_tree(commit->tree->object.sha1, NULL); return; } - html_txt(path); - html("<table class='list'>\n"); - html("<tr class='nohover'>"); - html("<th class='left'>Mode</th>"); - html("<th class='left'>Name</th>"); - html("<th class='right'>Size</th>"); - html("<th/>"); - html("</tr>\n"); - read_tree_recursive(tree, "", 0, 1, NULL, print_entry); - html("</table>\n"); + match_path = path; + read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree); + ls_tail(); } diff --git a/ui-view.c b/ui-view.c deleted file mode 100644 index 8873415..0000000 --- a/ui-view.c +++ b/dev/null @@ -1,55 +0,0 @@ -/* ui-view.c: functions to output _any_ object, given it's sha1 - * - * Copyright (C) 2006 Lars Hjemli - * - * Licensed under GNU General Public License v2 - * (see COPYING for full license text) - */ - -#include "cgit.h" - -void cgit_print_view(const char *hex, char *path) -{ - unsigned char sha1[20]; - enum object_type type; - unsigned char *buf; - unsigned long size; - - if (get_sha1_hex(hex, sha1)){ - cgit_print_error(fmt("Bad hex value: %s", hex)); - return; - } - - type = sha1_object_info(sha1, &size); - if (type == OBJ_BAD) { - cgit_print_error(fmt("Bad object name: %s", hex)); - return; - } - - buf = read_sha1_file(sha1, &type, &size); - if (!buf) { - cgit_print_error(fmt("Error reading object %s", hex)); - return; - } - - buf[size] = '\0'; - html("<table class='list'>\n"); - html("<tr class='nohover'><th class='left'>"); - if (path) - htmlf("%s (", path); - htmlf("%s %s, %li bytes", typename(type), hex, size); - if (path) - html(")"); - - html(" <a href='"); - html_attr(cgit_pageurl(cgit_query_repo, "blob", - fmt("id=%s&path=%s", - hex, - path))); - html("'>download</a>"); - html("</th></tr>\n"); - html("<tr><td class='blob'>\n"); - html_txt(buf); - html("\n</td></tr>\n"); - html("</table>\n"); -} |