[PATCH 5/5] pretty-lib: print commits using ref-filters logic

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Hariom Verma <hariom18599@xxxxxxxxx>

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@xxxxxxxxxxxxx>
Mentored-by: Heba Waly <heba.waly@xxxxxxxxx>
Signed-off-by: Hariom Verma <hariom18599@xxxxxxxxx>
---
 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



[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux