From mboxrd@z Thu Jan 1 00:00:00 1970 From: whydoubt at gmail.com (Jeff Smith) Date: Wed, 27 Sep 2017 17:43:30 -0500 Subject: [PATCH 4/5] ui-blame: add blame UI In-Reply-To: <20170927224331.26494-1-whydoubt@gmail.com> References: <20170923033848.5922-1-whydoubt@gmail.com> <20170927224331.26494-1-whydoubt@gmail.com> Message-ID: <20170927224331.26494-5-whydoubt@gmail.com> Implement a page which provides the blame view of a specified file. This feature is controlled by a new config variable, "enable-blame", which is disabled by default. Signed-off-by: Jeff Smith --- cgit.c | 2 + cgit.css | 8 +++ cgit.h | 1 + cgit.mk | 1 + cgitrc.5.txt | 9 +++ cmd.c | 12 +++- ui-blame.c | 224 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ui-blame.h | 7 ++ 8 files changed, 263 insertions(+), 1 deletion(-) create mode 100644 ui-blame.c create mode 100644 ui-blame.h diff --git a/cgit.c b/cgit.c index 1dae4b8..972a67e 100644 --- a/cgit.c +++ b/cgit.c @@ -167,6 +167,8 @@ static void config_cb(const char *name, const char *value) ctx.cfg.enable_index_links = atoi(value); else if (!strcmp(name, "enable-index-owner")) ctx.cfg.enable_index_owner = atoi(value); + else if (!strcmp(name, "enable-blame")) + ctx.cfg.enable_blame = atoi(value); else if (!strcmp(name, "enable-commit-graph")) ctx.cfg.enable_commit_graph = atoi(value); else if (!strcmp(name, "enable-log-filecount")) diff --git a/cgit.css b/cgit.css index 1dc2c11..836f8ae 100644 --- a/cgit.css +++ b/cgit.css @@ -329,6 +329,14 @@ div#cgit table.ssdiff td.lineno a:hover { color: black; } +div#cgit table.blame tr:nth-child(even) { + background: #eee; +} + +div#cgit table.blame tr:nth-child(odd) { + background: white; +} + div#cgit table.bin-blob { margin-top: 0.5em; border: solid 1px black; diff --git a/cgit.h b/cgit.h index fbc6c6a..0b88dcd 100644 --- a/cgit.h +++ b/cgit.h @@ -228,6 +228,7 @@ struct cgit_config { int enable_http_clone; int enable_index_links; int enable_index_owner; + int enable_blame; int enable_commit_graph; int enable_log_filecount; int enable_log_linecount; diff --git a/cgit.mk b/cgit.mk index 90a2346..3fcc1ca 100644 --- a/cgit.mk +++ b/cgit.mk @@ -77,6 +77,7 @@ CGIT_OBJ_NAMES += parsing.o CGIT_OBJ_NAMES += scan-tree.o CGIT_OBJ_NAMES += shared.o CGIT_OBJ_NAMES += ui-atom.o +CGIT_OBJ_NAMES += ui-blame.o CGIT_OBJ_NAMES += ui-blob.o CGIT_OBJ_NAMES += ui-clone.o CGIT_OBJ_NAMES += ui-commit.o diff --git a/cgitrc.5.txt b/cgitrc.5.txt index 9fcf445..4da166c 100644 --- a/cgitrc.5.txt +++ b/cgitrc.5.txt @@ -141,6 +141,11 @@ embedded:: suitable for embedding in other html pages. Default value: none. See also: "noheader". +enable-blame:: + Flag which, when set to "1", will allow cgit to provide a "blame" page + for files, and will make it generate links to that page in appropriate + places. Default value: "0". + enable-commit-graph:: Flag which, when set to "1", will make cgit print an ASCII-art commit history graph to the left of the commit messages in the repository @@ -799,6 +804,10 @@ enable-http-clone=1 enable-index-links=1 +# Enable blame page and create links to it from tree page +enable-blame=1 + + # Enable ASCII art commit history graph on the log pages enable-commit-graph=1 diff --git a/cmd.c b/cmd.c index d280e95..63f0ae5 100644 --- a/cmd.c +++ b/cmd.c @@ -1,6 +1,6 @@ /* cmd.c: the cgit command dispatcher * - * Copyright (C) 2006-2014 cgit Development Team + * Copyright (C) 2006-2017 cgit Development Team * * Licensed under GNU General Public License v2 * (see COPYING for full license text) @@ -11,6 +11,7 @@ #include "cache.h" #include "ui-shared.h" #include "ui-atom.h" +#include "ui-blame.h" #include "ui-blob.h" #include "ui-clone.h" #include "ui-commit.h" @@ -63,6 +64,14 @@ static void about_fn(void) cgit_print_site_readme(); } +static void blame_fn(void) +{ + if (ctx.cfg.enable_blame) + cgit_print_blame(); + else + cgit_print_error_page(403, "Forbidden", "Blame is disabled"); +} + static void blob_fn(void) { cgit_print_blob(ctx.qry.sha1, ctx.qry.path, ctx.qry.head, 0); @@ -164,6 +173,7 @@ struct cgit_cmd *cgit_get_cmd(void) def_cmd(HEAD, 1, 0, 1), def_cmd(atom, 1, 0, 0), def_cmd(about, 0, 0, 0), + def_cmd(blame, 1, 1, 0), def_cmd(blob, 1, 0, 0), def_cmd(commit, 1, 1, 0), def_cmd(diff, 1, 1, 0), diff --git a/ui-blame.c b/ui-blame.c new file mode 100644 index 0000000..40f695a --- /dev/null +++ b/ui-blame.c @@ -0,0 +1,224 @@ +/* ui-blame.c: functions for blame output + * + * Copyright (C) 2006-2017 cgit Development Team + * + * Licensed under GNU General Public License v2 + * (see COPYING for full license text) + */ + +#include "cgit.h" +#include "ui-blame.h" +#include "html.h" +#include "ui-shared.h" +#include "argv-array.h" +#include "blame.h" + + +static char *emit_one_suspect_detail(struct blame_origin *suspect, const char *hex) +{ + struct commitinfo *info; + struct strbuf detail = STRBUF_INIT; + + info = cgit_parse_commit(suspect->commit); + + strbuf_addf(&detail, "author %s", info->author); + if (!ctx.cfg.noplainemail) + strbuf_addf(&detail, " %s", info->author_email); + strbuf_addf(&detail, " %s\n", + show_date(info->author_date, info->author_tz, + cgit_date_mode(DATE_ISO8601))); + + strbuf_addf(&detail, "committer %s", info->committer); + if (!ctx.cfg.noplainemail) + strbuf_addf(&detail, " %s", info->committer_email); + strbuf_addf(&detail, " %s\n\n", + show_date(info->committer_date, info->committer_tz, + cgit_date_mode(DATE_ISO8601))); + + strbuf_addstr(&detail, info->subject); + + cgit_free_commitinfo(info); + return strbuf_detach(&detail, NULL); +} + +static void emit_blame_entry(struct blame_scoreboard *sb, struct blame_entry *ent) +{ + struct blame_origin *suspect = ent->suspect; + char hex[GIT_SHA1_HEXSZ + 1]; + char *detail; + const char *abbrev; + unsigned long lineno; + const char *numberfmt = "%1$d\n"; + const char *cp, *cpend; + + oid_to_hex_r(hex, &suspect->commit->object.oid); + detail = emit_one_suspect_detail(suspect, hex); + abbrev = find_unique_abbrev(suspect->commit->object.oid.hash, DEFAULT_ABBREV); + + html(""); + cgit_commit_link(abbrev, detail, NULL, ctx.qry.head, hex, suspect->path); + html("\n"); + + free(detail); + + if (ctx.cfg.enable_tree_linenumbers) { + html("
");
+		lineno = ent->lno;
+		while (lineno < ent->lno + ent->num_lines)
+			htmlf(numberfmt, ++lineno);
+		html("
\n"); + } + + cp = blame_nth_line(sb, ent->lno); + cpend = blame_nth_line(sb, ent->lno + ent->num_lines); + + html("
");
+	html_ntxt(cpend - cp, cp);
+	html("
\n"); +} + +struct walk_tree_context { + char *curr_rev; + int match_baselen; + int state; +}; + +static void print_object(const unsigned char *sha1, const char *path, const char *basename, const char *rev) +{ + enum object_type type; + unsigned long size; + struct argv_array rev_argv = ARGV_ARRAY_INIT; + struct rev_info revs; + struct blame_scoreboard sb; + struct blame_origin *o; + struct blame_entry *ent = NULL; + + type = sha1_object_info(sha1, &size); + if (type == OBJ_BAD) { + cgit_print_error_page(404, "Not found", "Bad object name: %s", + sha1_to_hex(sha1)); + return; + } + + argv_array_push(&rev_argv, "blame"); + argv_array_push(&rev_argv, rev); + init_revisions(&revs, NULL); + DIFF_OPT_SET(&revs.diffopt, ALLOW_TEXTCONV); + setup_revisions(rev_argv.argc, rev_argv.argv, &revs, NULL); + init_scoreboard(&sb); + sb.revs = &revs; + setup_scoreboard(&sb, path, &o); + o->suspects = blame_entry_prepend(NULL, 0, sb.num_lines, o); + prio_queue_put(&sb.commits, o->commit); + blame_origin_decref(o); + sb.ent = NULL; + sb.path = path; + assign_blame(&sb, 0); + blame_sort_final(&sb); + blame_coalesce(&sb); + + set_title_from_path(path); + + cgit_print_layout_start(); + htmlf("blob: %s (", sha1_to_hex(sha1)); + cgit_plain_link("plain", NULL, NULL, ctx.qry.head, rev, path); + html(") ("); + cgit_tree_link("tree", NULL, NULL, ctx.qry.head, rev, path); + html(")\n"); + + if (ctx.cfg.max_blob_size && size / 1024 > ctx.cfg.max_blob_size) { + htmlf("
blob size (%ldKB) exceeds display size limit (%dKB).
", + size / 1024, ctx.cfg.max_blob_size); + return; + } + + html(""); + for (ent = sb.ent; ent; ) { + struct blame_entry *e = ent->next; + emit_blame_entry(&sb, ent); + free(ent); + ent = e; + } + html("
\n"); + free((void *)sb.final_buf); + + cgit_print_layout_end(); + return; +} + +static int walk_tree(const unsigned char *sha1, struct strbuf *base, + const char *pathname, unsigned mode, int stage, void *cbdata) +{ + struct walk_tree_context *walk_tree_ctx = cbdata; + + if (base->len == walk_tree_ctx->match_baselen) { + if (S_ISREG(mode)) { + struct strbuf buffer = STRBUF_INIT; + strbuf_addbuf(&buffer, base); + strbuf_addstr(&buffer, pathname); + print_object(sha1, buffer.buf, pathname, walk_tree_ctx->curr_rev); + strbuf_release(&buffer); + walk_tree_ctx->state = 1; + } else if (S_ISDIR(mode)) { + walk_tree_ctx->state = 2; + } + } else if (base->len < INT_MAX && (int)base->len > walk_tree_ctx->match_baselen) { + walk_tree_ctx->state = 2; + } else if (S_ISDIR(mode)) { + return READ_TREE_RECURSIVE; + } + return 0; +} + +static int basedir_len(const char *path) +{ + char *p = strrchr(path, '/'); + if (p) + return p - path + 1; + return 0; +} + +void cgit_print_blame(void) +{ + const char *rev = ctx.qry.sha1; + struct object_id oid; + struct commit *commit; + struct pathspec_item path_items = { + .match = ctx.qry.path, + .len = ctx.qry.path ? strlen(ctx.qry.path) : 0 + }; + struct pathspec paths = { + .nr = 1, + .items = &path_items + }; + struct walk_tree_context walk_tree_ctx = { + .state = 0 + }; + + if (!rev) + rev = ctx.qry.head; + + if (get_oid(rev, &oid)) { + cgit_print_error_page(404, "Not found", + "Invalid revision name: %s", rev); + return; + } + commit = lookup_commit_reference(&oid); + if (!commit || parse_commit(commit)) { + cgit_print_error_page(404, "Not found", + "Invalid commit reference: %s", rev); + return; + } + + walk_tree_ctx.curr_rev = xstrdup(rev); + walk_tree_ctx.match_baselen = (path_items.match) ? + basedir_len(path_items.match) : -1; + + read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx); + if (!walk_tree_ctx.state) + cgit_print_error_page(404, "Not found", "Not found"); + else if (walk_tree_ctx.state == 2) + cgit_print_error_page(404, "No blame for folders", "Blame is not available for folders."); + + free(walk_tree_ctx.curr_rev); +} diff --git a/ui-blame.h b/ui-blame.h new file mode 100644 index 0000000..8a438e9 --- /dev/null +++ b/ui-blame.h @@ -0,0 +1,7 @@ +#ifndef UI_BLAME_H +#define UI_BLAME_H + +extern void cgit_print_blame(void); + +#endif /* UI_BLAME_H */ + -- 2.9.4