From: whydoubt at gmail.com (Jeff Smith)
Subject: [RFCv2 PATCH 2/7] ui-blame: create framework
Date: Fri, 22 Sep 2017 22:38:43 -0500 [thread overview]
Message-ID: <20170923033848.5922-3-whydoubt@gmail.com> (raw)
In-Reply-To: <20170923033848.5922-1-whydoubt@gmail.com>
Create framework for a page that will contain the 'blame' for a file
in the repository.
Signed-off-by: Jeff Smith <whydoubt at gmail.com>
---
ui-blame.c | 160 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ui-blame.h | 7 +++
2 files changed, 167 insertions(+)
create mode 100644 ui-blame.c
create mode 100644 ui-blame.h
diff --git a/ui-blame.c b/ui-blame.c
new file mode 100644
index 0000000..901ca89
--- /dev/null
+++ b/ui-blame.c
@@ -0,0 +1,160 @@
+/* ui-blame.c: functions for blame output
+ *
+ * Copyright (C) 2006-2017 cgit Development Team <cgit at 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"
+
+struct walk_tree_context {
+ char *curr_rev;
+ int match_baselen;
+ int state;
+};
+
+static void set_title_from_path(const char *path)
+{
+ size_t path_len, path_index, path_last_end;
+ char *new_title;
+
+ if (!path)
+ return;
+
+ path_len = strlen(path);
+ new_title = xmalloc(path_len + 3 + strlen(ctx.page.title) + 1);
+ new_title[0] = '\0';
+
+ for (path_index = path_len, path_last_end = path_len; path_index-- > 0;) {
+ if (path[path_index] == '/') {
+ if (path_index == path_len - 1) {
+ path_last_end = path_index - 1;
+ continue;
+ }
+ strncat(new_title, &path[path_index + 1], path_last_end - path_index - 1);
+ strcat(new_title, "\\");
+ path_last_end = path_index;
+ }
+ }
+ if (path_last_end)
+ strncat(new_title, path, path_last_end);
+
+ strcat(new_title, " - ");
+ strcat(new_title, ctx.page.title);
+ ctx.page.title = new_title;
+}
+static void print_object(const unsigned char *sha1, const char *path, const char *basename, const char *rev)
+{
+ enum object_type type;
+ unsigned long size;
+
+ 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;
+ }
+
+ /* Read in applicable data here */
+
+ 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;
+ }
+
+ /* Output data here */
+
+ 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
next prev parent reply other threads:[~2017-09-23 3:38 UTC|newest]
Thread overview: 63+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-06-08 2:18 [RFC PATCH 0/4] Add ui-blame whydoubt
2017-06-08 2:18 ` [RFC PATCH 1/4] git: update to v2.14 whydoubt
2017-07-22 11:25 ` john
2017-06-08 2:18 ` [RFC PATCH 2/4] ui-blame: create placeholder and links whydoubt
2017-07-22 11:31 ` john
2017-06-08 2:18 ` [RFC PATCH 3/4] ui-blame: create needed html_ntxt_noellipsis function whydoubt
2017-07-22 11:36 ` john
2017-06-08 2:18 ` [RFC PATCH 4/4] ui-blame: fill in the contents whydoubt
2017-07-05 8:32 ` list
2017-07-22 11:47 ` john
2017-06-08 9:00 ` [RFC PATCH 0/4] Add ui-blame list
2017-07-22 12:02 ` john
2017-08-05 0:23 ` dlcampbell
2017-08-05 0:57 ` whydoubt
2017-08-24 18:14 ` list
2017-08-31 13:05 ` whydoubt
2017-09-23 3:38 ` [RFCv2 PATCH 0/7] " whydoubt
2017-09-23 3:38 ` [RFCv2 PATCH 1/7] ui-blame: create enable-blame config item whydoubt
2017-09-23 15:46 ` john
2017-09-24 3:12 ` whydoubt
2017-09-23 3:38 ` whydoubt [this message]
2017-09-23 15:47 ` [RFCv2 PATCH 2/7] ui-blame: create framework john
2017-09-24 3:24 ` whydoubt
2017-09-23 3:38 ` [RFCv2 PATCH 3/7] ui-blame: create links whydoubt
2017-09-23 15:47 ` john
2017-09-24 20:25 ` whydoubt
2017-09-23 3:38 ` [RFCv2 PATCH 4/7] ui-blame: html_ntxt with no ellipsis whydoubt
2017-09-23 15:47 ` john
2017-09-23 3:38 ` [RFCv2 PATCH 5/7] ui-blame: pull blame info from libgit whydoubt
2017-09-23 15:47 ` john
2017-09-24 19:06 ` whydoubt
2017-09-24 20:09 ` whydoubt
2017-09-24 20:52 ` john
2017-09-23 3:38 ` [RFCv2 PATCH 6/7] ui-blame: begin building whydoubt
2017-09-23 3:38 ` [RFCv2 PATCH 7/7] ui-blame: generate blame page when requested whydoubt
2017-09-23 15:53 ` [RFCv2 PATCH 0/7] Add ui-blame john
2017-09-24 3:05 ` whydoubt
2017-09-27 22:43 ` [PATCH 0/5] " whydoubt
2017-09-27 22:43 ` [PATCH 1/5] html: html_ntxt with no ellipsis whydoubt
2017-09-30 11:55 ` john
2017-09-27 22:43 ` [PATCH 2/5] ui-tree: move set_title_from_path to ui-shared whydoubt
2017-09-30 11:56 ` john
2017-09-27 22:43 ` [PATCH 3/5] ui-shared: make a char* parameter const whydoubt
2017-09-30 12:00 ` john
2017-09-27 22:43 ` [PATCH 4/5] ui-blame: add blame UI whydoubt
2017-09-30 12:07 ` john
2017-09-27 22:43 ` [PATCH 5/5] ui-tree: link to blame UI if enabled whydoubt
2017-09-30 12:08 ` john
2017-09-30 12:10 ` [PATCH 0/5] Add ui-blame john
2017-10-02 1:17 ` Jason
2017-10-02 5:34 ` list
2017-10-02 22:35 ` whydoubt
2017-10-02 23:29 ` list
2017-10-03 18:22 ` john
2017-10-03 18:23 ` Jason
2017-10-03 18:36 ` Jason
2017-10-03 19:06 ` whydoubt
2017-10-02 4:39 ` [PATCHv2 " whydoubt
2017-10-02 4:39 ` [PATCHv2 1/5] html: html_ntxt with no ellipsis whydoubt
2017-10-02 4:39 ` [PATCHv2 2/5] ui-tree: move set_title_from_path to ui-shared whydoubt
2017-10-02 4:39 ` [PATCHv2 3/5] ui-shared: make a char* parameter const whydoubt
2017-10-02 4:39 ` [PATCHv2 4/5] ui-blame: add blame UI whydoubt
2017-10-02 4:39 ` [PATCHv2 5/5] ui-tree: link to blame UI if enabled whydoubt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170923033848.5922-3-whydoubt@gmail.com \
--to=cgit@lists.zx2c4.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).