Thread (1 message) 1 message, 1 author, 2016-06-15

Re: [PATCH v2 11/11] branch.c: replace `git_config()` with `git_config_get_string()

From: Matthieu Moy <hidden>
Date: 2016-06-15 23:02:10

Tanay Abhra [off-list ref] writes:
quoted hunk
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 <redacted>
---
 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/
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help