threads / discuss / 28866

pretty placeholders for reflog entries

Subject: pretty placeholders for reflog entries

## tl;dr

7 messages between Nov 7, 2011 and Nov 10, 2011.

replies: 6people: 3as markdown or json

Jack Nagel· Nov 7, 2011, 05:49 UTC · lore

I have the reflog enabled on a bare repo so that I can have a record of "who pushed what, when". I'd like to define a custom pretty format for use with "git log -g" for reading it, but unfortunately the placeholders for reflog information are somewhat limited. In particular, I'd like to be able to access the identity (i.e., name and email) and date from each reflog entry.

Is it possible to extract this information in current git? Perhaps I overlooked something.

I suppose it would be simple enough to just write a script to parse the reflog file(s), but it would be nice to be able to do this with git itself.

Thanks.
Jack
Jeff King· Nov 7, 2011, 21:13 UTC · re: Jack Nagel · lore

Re: pretty placeholders for reflog entries

On Sun, Nov 06, 2011 at 11:49:48PM -0600, Jack Nagel wrote:
Show 9 quoted lines
> I have the reflog enabled on a bare repo so that I can have a record of
> "who pushed what, when". I'd like to define a custom pretty format for
> use with "git log -g" for reading it, but unfortunately the placeholders
> for reflog information are somewhat limited. In particular, I'd like to
> be able to access the identity (i.e., name and email) and date from each
> reflog entry.
> 
> Is it possible to extract this information in current git? Perhaps I
> overlooked something.

Sorry, but there aren't convenient placeholders for that. There probably should be.

As a workaround, you can get the reflog selector ("%gD"), and then cross-reference it with the output of "git log -g". But obviously that's inefficient and a giant pain. We really should have "%gn" and "%ge" for the reflog name and email. And related date options, though annoyingly "%gd" is already taken for the default format.

I doubt it would be a very big patch. Want to get your feet wet in git development? :)

-Peff
Jack Nagel· Nov 7, 2011, 21:41 UTC · re: Jeff King · lore

Re: pretty placeholders for reflog entries

On Mon, Nov 7, 2011 at 3:13 PM, Jeff King <peff@peff.net> wrote:
> I doubt it would be a very big patch. Want to get your feet wet in git
> development? :)

I'll certainly give it a shot if I find some free time to do so. Though if someone else felt like doing it in the meantime, my feelings wouldn't be hurt. =)

Jack
Jeff King· Nov 7, 2011, 22:45 UTC · re: Jack Nagel · lore

Re: pretty placeholders for reflog entries

On Mon, Nov 07, 2011 at 03:41:29PM -0600, Jack Nagel wrote:
Show 7 quoted lines
> On Mon, Nov 7, 2011 at 3:13 PM, Jeff King <peff@peff.net> wrote:
> > I doubt it would be a very big patch. Want to get your feet wet in git
> > development? :)
> 
> I'll certainly give it a shot if I find some free time to do so. Though if
> someone else felt like doing it in the meantime, my feelings wouldn't be
> hurt. =)

Actually, it needed to touch a few ugly corners of the code, so I just went ahead and did it. The result is below.

-- >8 --
Subject: [PATCH] pretty: give placeholders to reflog identity

When doing a reflog walk, you can get some information about the reflog (such as the subject line), but not the identity information (i.e., name and email).

Let's make those available, mimicing the options for author and committer identity.

Signed-off-by: Jeff King <peff@peff.net>
---
We're still missing date specifiers. However, if you specify a "--date"
flag, then the reflog selector ("%gd") will have the date inside the
curly braces instead of the reflog counter.

It would be a little nicer to provide explicit date placeholders, but we can't quite make them match the author-date specifiers, because "gd" is taken. We could add %gt, %gr, etc. But in the long run, I'd like to move to considering most of %ar, %at, etc to be historical, and have something like "%ad(short)" be the official way of picking different date formats[1]. And then the reflog placeholders could learn "%gt(short)". So making more reflog placeholders right now just feels like cluttering a namespace I'd like to get changed eventually.

So I think in the meantime, this is a good start.
[1] I floated some patches about 8 months ago, and Will Palmer had some,
    too. I really need to take a look at them and push the topic forward.
 Documentation/pretty-formats.txt |    4 ++++
 pretty.c                         |   25 +++++++++++++++++++++++++
 reflog-walk.c                    |   12 ++++++++++++
 reflog-walk.h                    |    1 +
 t/t6006-rev-list-format.sh       |    6 ++++++
 5 files changed, 48 insertions(+), 0 deletions(-)
diff --git a/Documentation/pretty-formats.txt b/Documentation/pretty-formats.txt
index 561cc9f..880b6f2 100644
--- a/Documentation/pretty-formats.txt
+++ b/Documentation/pretty-formats.txt
@@ -132,6 +132,10 @@ The placeholders are:
 - '%N': commit notes
 - '%gD': reflog selector, e.g., `refs/stash@\{1\}`
 - '%gd': shortened reflog selector, e.g., `stash@\{1\}`
+- '%gn': reflog identity name
+- '%gN': reflog identity name (respecting .mailmap, see linkgit:git-shortlog[1] or linkgit:git-blame[1])
+- '%ge': reflog identity email
+- '%gE': reflog identity email (respecting .mailmap, see linkgit:git-shortlog[1] or linkgit:git-blame[1])
 - '%gs': reflog subject
 - '%Cred': switch color to red
 - '%Cgreen': switch color to green
diff --git a/pretty.c b/pretty.c
index 230fe1c..1580299 100644
--- a/pretty.c
+++ b/pretty.c
@@ -822,6 +822,23 @@ static void rewrap_message_tail(struct strbuf *sb,
 	c->indent2 = new_indent2;
 }
 
+static int format_reflog_person(struct strbuf *sb,
+				char part,
+				struct reflog_walk_info *log,
+				enum date_mode dmode)
+{
+	const char *ident;
+
+	if (!log)
+		return 2;
+
+	ident = get_reflog_ident(log);
+	if (!ident)
+		return 2;
+
+	return format_person_part(sb, part, ident, strlen(ident), dmode);
+}
+
 static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
 				void *context)
 {
@@ -963,6 +980,14 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
 			if (c->pretty_ctx->reflog_info)
 				get_reflog_message(sb, c->pretty_ctx->reflog_info);
 			return 2;
+		case 'n':
+		case 'N':
+		case 'e':
+		case 'E':
+			return format_reflog_person(sb,
+						    placeholder[1],
+						    c->pretty_ctx->reflog_info,
+						    c->pretty_ctx->date_mode);
 		}
 		return 0;	/* unknown %g placeholder */
 	case 'N':
diff --git a/reflog-walk.c b/reflog-walk.c
index 5d81d39..590737e 100644
--- a/reflog-walk.c
+++ b/reflog-walk.c
@@ -291,6 +291,18 @@ void get_reflog_message(struct strbuf *sb,
 	strbuf_add(sb, info->message, len);
 }
 
+const char *get_reflog_ident(struct reflog_walk_info *reflog_info)
+{
+	struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
+	struct reflog_info *info;
+
+	if (!commit_reflog)
+		return NULL;
+
+	info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
+	return info->email;
+}
+
 void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
 	enum date_mode dmode)
 {
diff --git a/reflog-walk.h b/reflog-walk.h
index 7bd2cd4..afb1ae3 100644
--- a/reflog-walk.h
+++ b/reflog-walk.h
@@ -14,6 +14,7 @@ extern void show_reflog_message(struct reflog_walk_info *info, int,
 		enum date_mode);
 extern void get_reflog_message(struct strbuf *sb,
 		struct reflog_walk_info *reflog_info);
+extern const char *get_reflog_ident(struct reflog_walk_info *reflog_info);
 extern void get_reflog_selector(struct strbuf *sb,
 		struct reflog_walk_info *reflog_info,
 		enum date_mode dmode,
diff --git a/t/t6006-rev-list-format.sh b/t/t6006-rev-list-format.sh
index d918cc0..4442790 100755
--- a/t/t6006-rev-list-format.sh
+++ b/t/t6006-rev-list-format.sh
@@ -267,6 +267,12 @@ test_expect_success '%gd shortens ref name' '
 	test_cmp expect.gd-short actual.gd-short
 '
 
+test_expect_success 'reflog identity' '
+	echo "C O Mitter:committer@example.com" >expect &&
+	git log -g -1 --format="%gn:%ge" >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success 'oneline with empty message' '
 	git commit -m "dummy" --allow-empty &&
 	git commit -m "dummy" --allow-empty &&
-- 
1.7.7.2.7.g9f96f.dirty
Junio C Hamano· Nov 7, 2011, 23:41 UTC · re: Jeff King · lore

Re: pretty placeholders for reflog entries

Jeff King <peff@peff.net> writes:
Show 8 quoted lines
> It would be a little nicer to provide explicit date placeholders, but we
> can't quite make them match the author-date specifiers, because "gd" is
> taken.  We could add %gt, %gr, etc. But in the long run, I'd like to
> move to considering most of %ar, %at, etc to be historical, and have
> something like "%ad(short)" be the official way of picking different
> date formats[1]. And then the reflog placeholders could learn
> "%gt(short)". So making more reflog placeholders right now just feels
> like cluttering a namespace I'd like to get changed eventually.
I tend to agree with the direction.

As we are not adding anything new before the 1.7.8 final, I'd rather ask you to hold onto this and other changes in your footnote, instead of having me to carry them in 'pu', which is an integration branch even less official than it would otherwise be during the freeze period.

Jeff King· Nov 8, 2011, 05:47 UTC · re: Junio C Hamano · lore

Re: pretty placeholders for reflog entries

On Mon, Nov 07, 2011 at 03:41:19PM -0800, Junio C Hamano wrote:
Show 17 quoted lines
> Jeff King <peff@peff.net> writes:
> 
> > It would be a little nicer to provide explicit date placeholders, but we
> > can't quite make them match the author-date specifiers, because "gd" is
> > taken.  We could add %gt, %gr, etc. But in the long run, I'd like to
> > move to considering most of %ar, %at, etc to be historical, and have
> > something like "%ad(short)" be the official way of picking different
> > date formats[1]. And then the reflog placeholders could learn
> > "%gt(short)". So making more reflog placeholders right now just feels
> > like cluttering a namespace I'd like to get changed eventually.
> 
> I tend to agree with the direction.
> 
> As we are not adding anything new before the 1.7.8 final, I'd rather ask
> you to hold onto this and other changes in your footnote, instead of
> having me to carry them in 'pu', which is an integration branch even less
> official than it would otherwise be during the freeze period.

Sure. I'll re-send the patch once the release is out. That will also give Jack and any other interested parties time to comment and test.

-Peff
Jack Nagel· Nov 10, 2011, 01:56 UTC · re: Jeff King · lore

Re: pretty placeholders for reflog entries

On Mon, Nov 7, 2011 at 11:47 PM, Jeff King <peff@peff.net> wrote:
> Sure. I'll re-send the patch once the release is out. That will also
> give Jack and any other interested parties time to comment and test.

Thanks. I used this day, and combined with "--date=", it gets me pretty close to where I wanted to be. But your comments about reorganizing the date placeholders make sense, so this is perfectly fine for the time being.

Thanks again.
Jack

← back to recent threads