# [PATCH/RFC] pretty.c: add %O format specifier to format_commit_one()

5 messages from 2010-05-01 to 2010-05-02. Participants: Alexander Shishkin, Junio C Hamano, Jakub Narebski, Jeff King.
Thread: https://gitlist.dev/t/23650

## Alexander Shishkin, 2010-05-01 10:56

Subject: [PATCH/RFC] pretty.c: add %O format specifier to format_commit_one()
Message-ID: <1272711401-29005-1-git-send-email-ash@koowaldah.org>
URL: https://gitlist.dev/e/1272711401-29005-1-git-send-email-ash%40koowaldah.org

```
This specifier represents the number of each commit in the output
stream.

Signed-off-by: Alexander Shishkin <ash@koowaldah.org>
---
 Documentation/pretty-formats.txt |    3 ++-
 pretty.c                         |    5 +++++
 2 files changed, 7 insertions(+), 1 deletions(-)

diff --git a/Documentation/pretty-formats.txt b/Documentation/pretty-formats.txt
index 1686a54..7b0cab1 100644
--- a/Documentation/pretty-formats.txt
+++ b/Documentation/pretty-formats.txt
@@ -137,7 +137,8 @@ The placeholders are:
 - '%%': a raw '%'
 - '%x00': print a byte from a hex code
 - '%w([<w>[,<i1>[,<i2>]]])': switch line wrapping, like the -w option of
-  linkgit:git-shortlog[1].
+  linkgit:git-shortlog[1]
+- '%O': number of the record in the output.
 
 NOTE: Some placeholders may depend on other options given to the
 revision traversal engine. For example, the `%g*` reflog options will
diff --git a/pretty.c b/pretty.c
index 7cb3a2a..3e3d399 100644
--- a/pretty.c
+++ b/pretty.c
@@ -11,6 +11,7 @@
 #include "reflog-walk.h"
 
 static char *user_format;
+static int record_number;
 
 static void save_user_format(struct rev_info *rev, const char *cp, int is_tformat)
 {
@@ -639,6 +640,9 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
 
 	/* these are independent of the commit */
 	switch (placeholder[0]) {
+	case 'O':
+		strbuf_addf(sb, "%d", record_number);
+		return 1;
 	case 'C':
 		if (placeholder[1] == '(') {
 			const char *end = strchr(placeholder + 2, ')');
@@ -899,6 +903,7 @@ void format_commit_message(const struct commit *commit,
 	context.wrap_start = sb->len;
 	strbuf_expand(sb, format, format_commit_item, &context);
 	rewrap_message_tail(sb, &context, 0, 0, 0);
+	record_number++;
 }
 
 static void pp_header(enum cmit_fmt fmt,
-- 
1.7.0.4

```

## Junio C Hamano, 2010-05-01 17:18

Subject: Re: [PATCH/RFC] pretty.c: add %O format specifier to format_commit_one()
Message-ID: <7vsk6b5yms.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vsk6b5yms.fsf%40alter.siamese.dyndns.org
In-Reply-To: <1272711401-29005-1-git-send-email-ash@koowaldah.org>

```
Alexander Shishkin <ash@koowaldah.org> writes:

> This specifier represents the number of each commit in the output
> stream.

I don't like this.  What does such a number _mean_ in a non-linear
history?

Also the next person who sees this will inevitably ask for %TOTAL to so
that the output can say [N/M], but that would mean the list has to be
limited and we cannot stream the output anymore.

```

## Alexander Shishkin, 2010-05-01 17:56

Subject: Re: [PATCH/RFC] pretty.c: add %O format specifier to format_commit_one()
Message-ID: <20100501175601.GB27062@shisha.kicks-ass.net>
URL: https://gitlist.dev/e/20100501175601.GB27062%40shisha.kicks-ass.net
In-Reply-To: <7vsk6b5yms.fsf@alter.siamese.dyndns.org>

```
On Sat, May 01, 2010 at 10:18:35 -0700, Junio C Hamano wrote:
> Alexander Shishkin <ash@koowaldah.org> writes:
> 
> > This specifier represents the number of each commit in the output
> > stream.
> 
> I don't like this.  What does such a number _mean_ in a non-linear
> history?

My idea is to have something like

$ git log --pretty=oneline | nl | less -R

This is generally useful when my local topic branch contains more than 5
(or so) commits and I can't immediately see which one's which if I want
to do a

$ git show HEAD~$n

to see what's inside, which is useful when reordering and/or squashing
commits by an interactive rebase. So this %O gives me this $n quicker
than counting.

> Also the next person who sees this will inevitably ask for %TOTAL to so
> that the output can say [N/M], but that would mean the list has to be
> limited and we cannot stream the output anymore.

Well, for me this kind of numbering only makes sense on a short part of
the topmost history.

Regards,
--
Alex

```

## Jakub Narebski, 2010-05-01 19:39

Subject: Re: [PATCH/RFC] pretty.c: add %O format specifier to format_commit_one()
Message-ID: <m3fx2bcsze.fsf@localhost.localdomain>
URL: https://gitlist.dev/e/m3fx2bcsze.fsf%40localhost.localdomain
In-Reply-To: <20100501175601.GB27062@shisha.kicks-ass.net>

```
Alexander Shishkin <ash@koowaldah.org> writes:

> On Sat, May 01, 2010 at 10:18:35 -0700, Junio C Hamano wrote:
> > Alexander Shishkin <ash@koowaldah.org> writes:
> > 
> > > This specifier represents the number of each commit in the output
> > > stream.
> > 
> > I don't like this.  What does such a number _mean_ in a non-linear
> > history?
> 
> My idea is to have something like
> 
> $ git log --pretty=oneline | nl | less -R
> 
> This is generally useful when my local topic branch contains more than 5
> (or so) commits and I can't immediately see which one's which if I want
> to do a
> 
> $ git show HEAD~$n
> 
> to see what's inside, which is useful when reordering and/or squashing
> commits by an interactive rebase. So this %O gives me this $n quicker
> than counting.

Wouldn't somthing like

  git log --pretty=oneline | git name-rev --stdin | less -R

be good enough?

-- 
Jakub Narebski
Poland
ShadeHawk on #git

```

## Jeff King, 2010-05-02 04:55

Subject: Re: [PATCH/RFC] pretty.c: add %O format specifier to format_commit_one()
Message-ID: <20100502045552.GC14776@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20100502045552.GC14776%40coredump.intra.peff.net
In-Reply-To: <7vsk6b5yms.fsf@alter.siamese.dyndns.org>

```
On Sat, May 01, 2010 at 10:18:35AM -0700, Junio C Hamano wrote:

> Alexander Shishkin <ash@koowaldah.org> writes:
> 
> > This specifier represents the number of each commit in the output
> > stream.
> 
> I don't like this.  What does such a number _mean_ in a non-linear
> history?
> 
> Also the next person who sees this will inevitably ask for %TOTAL to so
> that the output can say [N/M], but that would mean the list has to be
> limited and we cannot stream the output anymore.

Hmph. I started on a patch a while ago (but never finished) that would
allow an [N/M] output. My intent was to allow pretty-format specifiers
for generating cover letters[1].

Of course you can't stream when asking for %TOTAL, but that is already
the case with format-patch, which does such a calculation when numbering
patch subjects. We could use userformat_find_requirements to do the
lookup when needed, and then only formats which use the placeholder
would have to pay the penalty.

-Peff

[1] These days I do:

  git format-patch --stdout "$@" |
    sed -ne 's/^Subject: //p' |
    sed -e 's/\[PATCH /[/' \
        -e 's/]/]:/' \
        -e 's/^/  /'

and pull the result into a cover letter that I write manually.

```
