Re: [RFC/PATCH 1/9] ref-filter: add %(refname:lalignX) option
- From
Christian Couder <christian.couder@gmail.com>
- Date
- Jun 27, 2015, 20:02 UTC
- Message-ID
- <CAP8UFD1gT_2RKYc1vvBO9goGwPzFxhu9uMan5+cLO7eWxwwB_w@mail.gmail.com>
- In-Reply-To
- <1435232596-27466-1-git-send-email-karthik.188@gmail.com>
On Thu, Jun 25, 2015 at 1:43 PM, Karthik Nayak <karthik.188@gmail.com> wrote:
Show 28 quoted lines
> Add support for %(refname:lalignX) where X is a number.
> This will print a shortened refname aligned to the left
> followed by spaces for a total length of X characters.
> If X is less than the shortened refname size, the entire
> shortened refname is printed.
>
> Mentored-by: Christian Couder <christian.couder@gmail.com>
> Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr>
> Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
> ---
> ref-filter.c | 17 ++++++++++++++++-
> 1 file changed, 16 insertions(+), 1 deletion(-)
>
> diff --git a/ref-filter.c b/ref-filter.c
> index 00d06bf..299b048 100644
> --- a/ref-filter.c
> +++ b/ref-filter.c
> @@ -695,7 +695,22 @@ static void populate_value(struct ref_array_item *ref)
> int num_ours, num_theirs;
>
> formatp++;
> - if (!strcmp(formatp, "short"))
> + if (starts_with(formatp, "lalign")) {
> + const char *valp;
> + int val;
> +
> + skip_prefix(formatp, "lalign", &valp);
> + val = atoi(valp);After thinking about such code, I wonder if it would be better to support %(refname:lalign=X) instead of %(refname:lalignX).
The reason why it might be interesting to require an = sign between "align" and the number X is that if we later want to introduce another option with a name that starts with "lalign", for example %(refname:lalignall=X) that would truncate the refname if it is bigger than X), we might be more backward compatible with old git versions that implement %(refname:lalign=X) but not %(refname:lalignall=X).
We will be more backward compatible because the above call to starts_with() would probably be something like:
if (starts_with(formatp, "lalign=")) {which means that old git versions would ignore something like "lalignall=X".
Show 9 quoted lines
> + refname = shorten_unambiguous_ref(refname,
> + warn_ambiguous_refs);
> + if (val > strlen(refname)) {
> + struct strbuf buf = STRBUF_INIT;
> + strbuf_addstr(&buf, refname);
> + strbuf_addchars(&buf, ' ', val - strlen(refname));
> + free((char *)refname);
> + refname = strbuf_detach(&buf, NULL);
> + }Thanks, Christian.