All of lore.kernel.org
 help / color / mirror / Atom feed
From: Cyril Roelandt <tipecaml@gmail.com>
To: git@vger.kernel.org
Cc: Cyril Roelandt <tipecaml@gmail.com>
Subject: [PATCH] Allow --pretty to be passed to git-describe.
Date: Tue, 25 Mar 2014 02:04:04 +0100	[thread overview]
Message-ID: <1395709444-11220-1-git-send-email-tipecaml@gmail.com> (raw)

In some cases, ony may want to find the the most recent tag that is reachable
from a commit and have it pretty printed, using the formatting options available
in git-log and git-show.

Signed-off-by: Cyril Roelandt <tipecaml@gmail.com>
---
 Documentation/git-describe.txt |  4 ++++
 builtin/describe.c             | 39 ++++++++++++++++++++++++++++++++++-----
 2 files changed, 38 insertions(+), 5 deletions(-)

diff --git a/Documentation/git-describe.txt b/Documentation/git-describe.txt
index d20ca40..fae4713 100644
--- a/Documentation/git-describe.txt
+++ b/Documentation/git-describe.txt
@@ -93,6 +93,10 @@ OPTIONS
 	This is useful when you wish to not match tags on branches merged
 	in the history of the target commit.
 
+include::pretty-options.txt[]
+
+include::pretty-formats.txt[]
+
 EXAMPLES
 --------
 
diff --git a/builtin/describe.c b/builtin/describe.c
index 24d740c..4c0ebae 100644
--- a/builtin/describe.c
+++ b/builtin/describe.c
@@ -8,8 +8,8 @@
 #include "diff.h"
 #include "hashmap.h"
 #include "argv-array.h"
+#include "revision.h"
 
-#define SEEN		(1u << 0)
 #define MAX_TAGS	(FLAG_BITS - 1)
 
 static const char * const describe_usage[] = {
@@ -30,6 +30,8 @@ static int have_util;
 static const char *pattern;
 static int always;
 static const char *dirty;
+static const char *fmt_pretty;
+static enum cmit_fmt commit_format;
 
 /* diff-index command arguments to check if working tree is dirty. */
 static const char *diff_index_args[] = {
@@ -266,8 +268,14 @@ static void describe(const char *arg, int last_one)
 		 * Exact match to an existing ref.
 		 */
 		display_name(n);
-		if (longformat)
+		if (longformat) {
 			show_suffix(0, n->tag ? n->tag->tagged->sha1 : sha1);
+		} else if (fmt_pretty) {
+			struct strbuf buf = STRBUF_INIT;
+			pp_commit_easy(commit_format, cmit, &buf);
+			printf("%s", buf.buf);
+			strbuf_release(&buf);
+		}
 		if (dirty)
 			printf("%s", dirty);
 		printf("\n");
@@ -386,9 +394,16 @@ static void describe(const char *arg, int last_one)
 		}
 	}
 
-	display_name(all_matches[0].name);
-	if (abbrev)
-		show_suffix(all_matches[0].depth, cmit->object.sha1);
+	if (fmt_pretty) {
+		struct strbuf buf = STRBUF_INIT;
+		pp_commit_easy(commit_format, cmit, &buf);
+		printf("%s", buf.buf);
+		strbuf_release(&buf);
+	} else {
+		display_name(all_matches[0].name);
+		if (abbrev)
+			show_suffix(all_matches[0].depth, cmit->object.sha1);
+	}
 	if (dirty)
 		printf("%s", dirty);
 	printf("\n");
@@ -419,6 +434,10 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
 		{OPTION_STRING, 0, "dirty",  &dirty, N_("mark"),
 			N_("append <mark> on dirty working tree (default: \"-dirty\")"),
 			PARSE_OPT_OPTARG, NULL, (intptr_t) "-dirty"},
+		OPT_STRING(0, "pretty",      &fmt_pretty, N_("pattern"),
+			   N_("pretty print")),
+		OPT_STRING(0, "format",      &fmt_pretty, N_("pattern"),
+			   N_("pretty print")),
 		OPT_END(),
 	};
 
@@ -437,6 +456,9 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
 	if (longformat && abbrev == 0)
 		die(_("--long is incompatible with --abbrev=0"));
 
+	if (longformat && fmt_pretty)
+		die(_("--long is incompatible with --pretty"));
+
 	if (contains) {
 		struct argv_array args;
 
@@ -458,6 +480,13 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
 		return cmd_name_rev(args.argc, args.argv, prefix);
 	}
 
+	if (fmt_pretty) {
+		struct rev_info rev;
+		init_revisions(&rev, prefix);
+		get_commit_format(fmt_pretty, &rev);
+		commit_format = rev.commit_format;
+	}
+
 	hashmap_init(&names, (hashmap_cmp_fn) commit_name_cmp, 0);
 	for_each_rawref(get_name, NULL);
 	if (!names.size && !always)
-- 
1.9.1

             reply	other threads:[~2014-03-25  1:09 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-03-25  1:04 Cyril Roelandt [this message]
2014-03-25  5:04 ` [PATCH] Allow --pretty to be passed to git-describe Junio C Hamano
2014-03-25  5:50 ` Eric Sunshine

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=1395709444-11220-1-git-send-email-tipecaml@gmail.com \
    --to=tipecaml@gmail.com \
    --cc=git@vger.kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.