From: Matthieu Moy <Matthieu.Moy@grenoble-inp.fr>
To: Tanay Abhra <tanayabh@gmail.com>
Cc: git@vger.kernel.org, Ramkumar Ramachandra <artagnon@gmail.com>
Subject: Re: [PATCH v2 11/11] branch.c: replace `git_config()` with `git_config_get_string()
Date: Thu, 07 Aug 2014 19:04:38 +0200 [thread overview]
Message-ID: <vpqvbq4cjtl.fsf@anie.imag.fr> (raw)
In-Reply-To: <1407428486-19049-12-git-send-email-tanayabh@gmail.com> (Tanay Abhra's message of "Thu, 7 Aug 2014 09:21:26 -0700")
Tanay Abhra <tanayabh@gmail.com> writes:
> Use `git_config_get_string()` instead of `git_config()` to take advantage of
> the config-set API which provides a cleaner control flow.
>
> Signed-off-by: Tanay Abhra <tanayabh@gmail.com>
> ---
> branch.c | 27 +++++++--------------------
> 1 file changed, 7 insertions(+), 20 deletions(-)
>
> diff --git a/branch.c b/branch.c
> index 735767d..df6b120 100644
> --- a/branch.c
> +++ b/branch.c
> @@ -140,30 +140,17 @@ static int setup_tracking(const char *new_ref, const char *orig_ref,
> return 0;
> }
>
> -struct branch_desc_cb {
> - const char *config_name;
> - const char *value;
> -};
> -
> -static int read_branch_desc_cb(const char *var, const char *value, void *cb)
> -{
> - struct branch_desc_cb *desc = cb;
> - if (strcmp(desc->config_name, var))
> - return 0;
> - free((char *)desc->value);
> - return git_config_string(&desc->value, var, value);
> -}
> -
> int read_branch_desc(struct strbuf *buf, const char *branch_name)
> {
> - struct branch_desc_cb cb;
> + char *v = NULL;
> struct strbuf name = STRBUF_INIT;
> strbuf_addf(&name, "branch.%s.description", branch_name);
> - cb.config_name = name.buf;
> - cb.value = NULL;
> - git_config(read_branch_desc_cb, &cb);
> - if (cb.value)
> - strbuf_addstr(buf, cb.value);
> + if (git_config_get_string(name.buf, &v)) {
> + strbuf_release(&name);
> + return -1;
> + }
> + strbuf_addstr(buf, v);
> + free(v);
There's a behavior change here, but I think it is the right thing to do.
It lacks a proper commit message though:
As a reminder, your patch "change `git_config()` return value to void"
in the other series did:
--- a/branch.c
+++ b/branch.c
@@ -161,10 +161,7 @@ int read_branch_desc(struct strbuf *buf, const char *branch_name)
strbuf_addf(&name, "branch.%s.description", branch_name);
cb.config_name = name.buf;
cb.value = NULL;
- if (git_config(read_branch_desc_cb, &cb) < 0) {
- strbuf_release(&name);
- return -1;
- }
+ git_config(read_branch_desc_cb, &cb);
if (cb.value)
strbuf_addstr(buf, cb.value);
strbuf_release(&name);
So, before it, read_branch_desc() was returning -1 iff git_config()
failed, which essentially never happened.
Now, you're retoring a similar "if", but you strbuf_release and return
-1 if no value is found for the variable.
There are 3 callers of read_branch_desc:
builtin/branch.c: read_branch_desc(&buf, branch_name);
builtin/fmt-merge-msg.c: if (!read_branch_desc(&desc, name)) {
builtin/log.c: read_branch_desc(&desc, branch_name);
Only the one in fmt-merge-msg.c uses the return value:
static void add_branch_desc(struct strbuf *out, const char *name)
{
struct strbuf desc = STRBUF_INIT;
if (!read_branch_desc(&desc, name)) {
const char *bp = desc.buf;
while (*bp) { /* (1) */
const char *ep = strchrnul(bp, '\n');
if (*ep)
ep++;
strbuf_addf(out, " : %.*s", (int)(ep - bp), bp);
bp = ep;
}
if (out->buf[out->len - 1] != '\n') /* (2) */
strbuf_addch(out, '\n');
}
strbuf_release(&desc);
}
the (1) part is a no-op if no value is found, but the old code was still
adding a \n in the (2) part, even when no value was found.
So, the new code is better than the old one, but your patch does a bit
more than the commit message claims.
--
Matthieu Moy
http://www-verimag.imag.fr/~moy/
next prev parent reply other threads:[~2014-08-07 17:04 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-08-07 16:21 [PATCH v2 00/11] git_config callers rewritten with the new config-set API Tanay Abhra
2014-08-07 16:21 ` [PATCH v2 01/11] daemon.c: replace `git_config()` with `git_config_get_bool()` family Tanay Abhra
2014-08-07 16:21 ` [PATCH v2 02/11] http-backend.c: " Tanay Abhra
2014-08-07 16:21 ` [PATCH v2 03/11] read-cache.c: replace `git_config()` with `git_config_get_*()` family Tanay Abhra
2014-08-07 16:21 ` [PATCH v2 04/11] archive.c: replace `git_config()` with `git_config_get_bool()` family Tanay Abhra
2014-08-07 16:21 ` [PATCH v2 05/11] fetchpack.c: replace `git_config()` with `git_config_get_*()` family Tanay Abhra
2014-08-07 16:21 ` [PATCH v2 06/11] rerere.c: " Tanay Abhra
2014-08-07 16:21 ` [PATCH v2 07/11] builtin/gc.c: " Tanay Abhra
2014-08-07 16:21 ` [PATCH v2 08/11] pager.c: replace `git_config()` with `git_config_get_value()` Tanay Abhra
2014-08-07 16:21 ` [PATCH v2 09/11] imap-send.c: replace `git_config()` with `git_config_get_*()` family Tanay Abhra
2014-08-07 16:21 ` [PATCH v2 10/11] alias.c: replace `git_config()` with `git_config_get_string()` Tanay Abhra
2014-08-07 16:21 ` [PATCH v2 11/11] branch.c: replace `git_config()` with `git_config_get_string() Tanay Abhra
2014-08-07 17:04 ` Matthieu Moy [this message]
2014-08-07 17:56 ` [PATCH v3 " Tanay Abhra
2014-08-07 18:36 ` [PATCH v2 00/11] git_config callers rewritten with the new config-set API Matthieu Moy
2014-08-07 20:10 ` Junio C Hamano
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=vpqvbq4cjtl.fsf@anie.imag.fr \
--to=matthieu.moy@grenoble-inp.fr \
--cc=artagnon@gmail.com \
--cc=git@vger.kernel.org \
--cc=tanayabh@gmail.com \
/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.