aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeff Smith <whydoubt@gmail.com>2017-10-01 23:39:08 -0500
committerJohn Keeping <john@keeping.me.uk>2017-10-03 19:19:34 +0100
commitc1cd290d1f83d3d1c2d081d734e8d213f12cc06b (patch)
treead5027076d139912140c45b6baec2d390389f7a1
parentf6ffe40d1a2c985494e48dc2d36663ffde1e6044 (diff)
downloadcgit-c1cd290d1f83d3d1c2d081d734e8d213f12cc06b.tar.gz
cgit-c1cd290d1f83d3d1c2d081d734e8d213f12cc06b.tar.bz2
cgit-c1cd290d1f83d3d1c2d081d734e8d213f12cc06b.zip
ui-blame: add blame UI
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 <whydoubt@gmail.com> Reviewed-by: John Keeping <john@keeping.me.uk>
-rw-r--r--cgit.c2
-rw-r--r--cgit.css8
-rw-r--r--cgit.h1
-rw-r--r--cgit.mk1
-rw-r--r--cgitrc.5.txt9
-rw-r--r--cmd.c12
-rw-r--r--ui-blame.c227
-rw-r--r--ui-blame.h6
8 files changed, 265 insertions, 1 deletions
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 <cgit@lists.zx2c4.com>
+ * Copyright (C) 2006-2017 cgit Development Team <cgit@lists.zx2c4.com>
*
* 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..62cf431
--- /dev/null
+++ b/ui-blame.c
@@ -0,0 +1,227 @@
+/* ui-blame.c: functions for blame output
+ *
+ * Copyright (C) 2006-2017 cgit Development Team <cgit@lists.zx2c4.com>
+ *
+ * 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_suspect_detail(struct blame_origin *suspect)
+{
+ 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;
+ struct object_id *oid = &suspect->commit->object.oid;
+ const char *numberfmt = "<a id='n%1$d' href='#n%1$d'>%1$d</a>\n";
+ const char *cp, *cpend;
+
+ char *detail = emit_suspect_detail(suspect);
+
+ html("<tr><td class='sha1 lines'>");
+ cgit_commit_link(find_unique_abbrev(oid->hash, DEFAULT_ABBREV), detail,
+ NULL, ctx.qry.head, oid_to_hex(oid), suspect->path);
+ html("</td>\n");
+
+ free(detail);
+
+ if (ctx.cfg.enable_tree_linenumbers) {
+ unsigned long lineno = ent->lno;
+ html("<td class='linenumbers'><pre>");
+ while (lineno < ent->lno + ent->num_lines)
+ htmlf(numberfmt, ++lineno);
+ html("</pre></td>\n");
+ }
+
+ cp = blame_nth_line(sb, ent->lno);
+ cpend = blame_nth_line(sb, ent->lno + ent->num_lines);
+
+ html("<td class='lines'><pre><code>");
+ html_ntxt(cp, cpend - cp);
+ html("</code></pre></td></tr>\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);
+
+ cgit_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("<div class='error'>blob size (%ldKB)"
+ " exceeds display size limit (%dKB).</div>",
+ size / 1024, ctx.cfg.max_blob_size);
+ return;
+ }
+
+ html("<table class='blame blob'>");
+ for (ent = sb.ent; ent; ) {
+ struct blame_entry *e = ent->next;
+ emit_blame_entry(&sb, ent);
+ free(ent);
+ ent = e;
+ }
+ html("</table>\n");
+ free((void *)sb.final_buf);
+
+ cgit_print_layout_end();
+}
+
+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..5b97e03
--- /dev/null
+++ b/ui-blame.h
@@ -0,0 +1,6 @@
+#ifndef UI_BLAME_H
+#define UI_BLAME_H
+
+extern void cgit_print_blame(void);
+
+#endif /* UI_BLAME_H */