git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Hariom Verma via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Hariom Verma <hariom18599@gmail.com>,
	Hariom Verma <hariom18599@gmail.com>
Subject: [PATCH 5/5] pretty-lib: print commits using ref-filters logic
Date: Mon, 15 Jun 2020 10:57:42 +0000	[thread overview]
Message-ID: <a83270485be2bebb1ce77be55ff73d136b735922.1592218662.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.658.git.1592218662.gitgitgadget@gmail.com>

From: Hariom Verma <hariom18599@gmail.com>

This change intends to use ref-filters logic to print commits.

Add `ref_pretty_print_commit()` which might be a future possible replacement
for `pretty_print_commit()`.

This is an introductory commit. Some features of `git log` might not work.

Mentored-by: Christian Couder <chriscool@tuxfamily.org>
Mentored-by: Heba Waly <heba.waly@gmail.com>
Signed-off-by: Hariom Verma <hariom18599@gmail.com>
---
 Makefile     |  1 +
 log-tree.c   |  7 ++++-
 pretty-lib.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 pretty-lib.h | 21 +++++++++++++
 4 files changed, 112 insertions(+), 1 deletion(-)
 create mode 100644 pretty-lib.c
 create mode 100644 pretty-lib.h

diff --git a/Makefile b/Makefile
index 372139f1f24..bcc65e87827 100644
--- a/Makefile
+++ b/Makefile
@@ -943,6 +943,7 @@ LIB_OBJS += pathspec.o
 LIB_OBJS += pkt-line.o
 LIB_OBJS += preload-index.o
 LIB_OBJS += pretty.o
+LIB_OBJS += pretty-lib.o
 LIB_OBJS += prio-queue.o
 LIB_OBJS += progress.o
 LIB_OBJS += promisor-remote.o
diff --git a/log-tree.c b/log-tree.c
index 55a68d0c610..663056664f9 100644
--- a/log-tree.c
+++ b/log-tree.c
@@ -17,6 +17,7 @@
 #include "help.h"
 #include "interdiff.h"
 #include "range-diff.h"
+#include "pretty-lib.h"
 
 static struct decoration name_decoration = { "object names" };
 static int decoration_loaded;
@@ -756,7 +757,11 @@ void show_log(struct rev_info *opt)
 		ctx.from_ident = &opt->from_ident;
 	if (opt->graph)
 		ctx.graph_width = graph_width(opt->graph);
-	pretty_print_commit(&ctx, commit, &msgbuf);
+
+	if (opt->use_ref_filter)
+		ref_pretty_print_commit(&ctx, commit, &msgbuf);
+	else
+		pretty_print_commit(&ctx, commit, &msgbuf);
 
 	if (opt->add_signoff)
 		append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
diff --git a/pretty-lib.c b/pretty-lib.c
new file mode 100644
index 00000000000..abe4228290b
--- /dev/null
+++ b/pretty-lib.c
@@ -0,0 +1,84 @@
+#include "commit.h"
+#include "ref-filter.h"
+#include "pretty-lib.h"
+
+static size_t convert_format(struct strbuf *sb, const char *start, void *data)
+{
+	/* TODO - Add support for more formatting options */
+	switch (*start) {
+	case 'H':
+		strbuf_addstr(sb, "%(objectname)");
+		return 1;
+	case 'h':
+		strbuf_addstr(sb, "%(objectname:short)");
+		return 1;
+	case 'T':
+		strbuf_addstr(sb, "%(tree)");
+		return 1;
+	case 'P':
+		strbuf_addstr(sb, "%(parent)");
+		return 1;
+	case 'a':
+		if (start[1] == 'n')
+			strbuf_addstr(sb, "%(authorname)");
+		else if (start[1] == 'e')
+			strbuf_addstr(sb, "%(authoremail)");
+		else if (start[1] == 'd')
+			strbuf_addstr(sb, "%(authordate)");
+		else
+			die(_("invalid formatting option '%c'"), *start);
+		return 2;
+	case 'c':
+		if (start[1] == 'n')
+			strbuf_addstr(sb, "%(committername)");
+		else if (start[1] == 'e')
+			strbuf_addstr(sb, "%(committeremail)");
+		else if (start[1] == 'd')
+			strbuf_addstr(sb, "%(committerdate)");
+		else
+			die(_("invalid formatting option '%c'"), *start);
+		return 2;
+	case 's':
+		strbuf_addstr(sb, "%(subject)");
+		return 1;
+	case 'b':
+		strbuf_addstr(sb, "%(body)");
+		return 1;
+	case 'n':
+		strbuf_addstr(sb, "\n");
+		return 1;
+	default:
+		die(_("invalid formatting option '%c'"), *start);
+	}
+}
+
+void ref_pretty_print_commit(struct pretty_print_context *pp,
+			 const struct commit *commit,
+			 struct strbuf *sb)
+{
+	struct ref_format format = REF_FORMAT_INIT;
+	struct strbuf sb_fmt = STRBUF_INIT;
+	const char *name = "refs";
+	const char *usr_fmt = get_user_format();
+
+	if (pp->fmt == CMIT_FMT_USERFORMAT) {
+		strbuf_expand(&sb_fmt, usr_fmt, convert_format, NULL);
+		format.format = sb_fmt.buf;
+	} else if (pp->fmt == CMIT_FMT_DEFAULT || pp->fmt == CMIT_FMT_MEDIUM) {
+		format.format = "Author: %(authorname) %(authoremail)\nDate:\t%(authordate)\n\n%(subject)\n\n%(body)";
+	} else if (pp->fmt == CMIT_FMT_ONELINE) {
+		format.format = "%(subject)";
+	} else if (pp->fmt == CMIT_FMT_SHORT) {
+		format.format = "Author: %(authorname) %(authoremail)\n\n\t%(subject)\n";
+	} else if (pp->fmt == CMIT_FMT_FULL) {
+		format.format = "Author: %(authorname) %(authoremail)\nCommit: %(committername) %(committeremail)\n\n%(subject)\n\n%(body)";
+	} else if (pp->fmt == CMIT_FMT_FULLER) {
+		format.format = "Author:\t\t%(authorname) %(authoremail)\nAuthorDate:\t%(authordate)\nCommit:\t\t%(committername) %(committeremail)\nCommitDate:\t%(committerdate)\n\n%(subject)\n\n%(body)";
+	}
+
+	format.need_newline_at_eol = 0;
+
+	verify_ref_format(&format);
+	pretty_print_ref(name, &commit->object.oid, &format);
+	strbuf_release(&sb_fmt);
+}
diff --git a/pretty-lib.h b/pretty-lib.h
new file mode 100644
index 00000000000..324499b1150
--- /dev/null
+++ b/pretty-lib.h
@@ -0,0 +1,21 @@
+#ifndef PRETTY_LIB_H
+#define PRETTY_LIB_H
+
+/**
+ * This is a possibly temporary interface between
+ * ref-filter and pretty. This interface may disappear in the
+ * future if a way to use ref-filter directly is found.
+ * In the meantime, this interface would enable us to
+ * step by step replace the formatting code in pretty by the
+ * ref-filter code.
+*/
+
+/**
+ * Possible future replacement for "pretty_print_commit()".
+ * Uses ref-filter's logic.
+*/
+void ref_pretty_print_commit(struct pretty_print_context *pp,
+			const struct commit *commit,
+			struct strbuf *sb);
+
+#endif /* PRETTY_LIB_H */
-- 
gitgitgadget

      parent reply	other threads:[~2020-06-15 10:57 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-15 10:57 [PATCH 0/5] [GSoC][RFC] print commits using ref-filter's logic Hariom Verma via GitGitGadget
2020-06-15 10:57 ` [PATCH 1/5] builtin/log: new config log.useRefFilter Hariom Verma via GitGitGadget
2020-06-15 10:57 ` [PATCH 2/5] revision: add `use_ref_filter` in struct rev_info Hariom Verma via GitGitGadget
2020-06-15 10:57 ` [PATCH 3/5] pretty: introduce `get_user_format()` Hariom Verma via GitGitGadget
2020-06-15 10:57 ` [PATCH 4/5] ref_format: add option to skip `\n` at eol Hariom Verma via GitGitGadget
2020-06-15 10:57 ` Hariom Verma via GitGitGadget [this message]

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

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a83270485be2bebb1ce77be55ff73d136b735922.1592218662.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=hariom18599@gmail.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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).