Skip to contentSkip to conversation

branch.c: Replace `git_config` with `git_config_get_string`

PATCH/RFC Tanay Abhra 2 participants 3 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

Original implementation uses a callback based approach which has some deficiencies like a convoluted control flow and redundant variables. Use git_config_get_string instead of git_config to take advantage of the config hash-table.

---

This patch builds on top of patch series[1]. It passes all the tests, and this in first of series of patches that aim to replace all git_config calls scattered arund the code base with appropriate non-callback based calls.

There are total 111 calls in total in all of git codebase. How should I send the patches, alphabetically or otherwise?

[1] http://thread.gmane.org/gmane.comp.version-control.git/251704

Cheers,
Tanay Abhra.

 branch.c | 25 +++++++++----------------
 1 file changed, 9 insertions(+), 16 deletions(-)
branch.c +9 -16
index 660097b..257b1bf 100644
@@ -140,33 +140,26 @@ static int setup_tracking(const char *new_ref, const char *orig_ref,
 	return 0;
 }
 
-struct branch_desc_cb {
+struct branch_desc {
 	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;
+	const char *value;
+	struct branch_desc desc;
 	struct strbuf name = STRBUF_INIT;
 	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) {
+	desc.config_name = name.buf;
+	desc.value = NULL;
+	value = git_config_get_string(desc.config_name);
+	git_config_string(&desc.value, desc.config_name, value);
+	if (!desc.value) {
 		strbuf_release(&name);
 		return -1;
 	}
-	if (cb.value)
-		strbuf_addstr(buf, cb.value);
+	strbuf_addstr(buf, desc.value);
 	strbuf_release(&name);
 	return 0;
 }
Signature · 1 line
1.9.0.GIT
#1 lore raw
Quoted 1 line

Subject: Re: [PATCH/RFC] branch.c: Replace git_config with git_config_get_string

Here and elsewhere: usually, no capital after :.

Quoted 2 lines · Tanay Abhra

Original implementation uses a callback based approach which has some deficiencies like a convoluted control flow and redundant variables.

"deficiencies" might be a bit strong (the code did work).

Quoted 2 lines

There are total 111 calls in total in all of git codebase. How should I send the patches, alphabetically or otherwise?

My advice would be: try as much as possible to split according to the complexity of the patch.

As a reviewer, I find it rather easy to review a large number of trivial and similar changes, but I hate having to switch back to "wow, the author did something tricky, let's try to understand this" in the middle of a trivial series.

(we had this discussion about ... Vs $(...) and test -a Vs test ... && series, which were essentially very trivial changes, but with subtle bugs introduced and hidden by the volume of trivial changes).

Quoted 2 lines
 branch.c | 25 +++++++++----------------
 1 file changed, 9 insertions(+), 16 deletions(-)

Removing more lines than it adds. I like the patch already ;-).

Quoted 4 lines
branch.c +0 -0
index 660097b..257b1bf 100644

[...]

Quoted 4 lines

int read_branch_desc(struct strbuf *buf, const char *branch_name) {

branch.c +1 -1
-	struct branch_desc_cb cb;
+	const char *value;

branch.c static int setup_tracking(const char *new_ref, const char *orig_ref, · line 157

Quoted 10 lines
branch.c +5 -3
+	struct branch_desc desc;
 	struct strbuf name = STRBUF_INIT;
 	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) {
+	desc.config_name = name.buf;
+	desc.value = NULL;
+	value = git_config_get_string(desc.config_name);
+	git_config_string(&desc.value, desc.config_name, value);

You're ignoring the return value of git_config_string, which is an error code. It shouldn't harm, because the code is non-zero iff desc.value is set to non-NULL, but you may want to write the code as

if (git_config_string(...)) {
	strbuf_release(...);
	return -1;
}

In any case, the patch sounds good to me.

Signature · 2 lines
Matthieu Moy
http://www-verimag.imag.fr/~moy/
#2 lore raw
Quoted 4 lines · Matthieu Moy
Quoted 1 line

Subject: Re: [PATCH/RFC] branch.c: Replace git_config with git_config_get_string

Here and elsewhere: usually, no capital after :.

Noted.

Quoted 7 lines
Quoted 2 lines · Tanay Abhra

Original implementation uses a callback based approach which has some deficiencies like a convoluted control flow and redundant variables.

"deficiencies" might be a bit strong (the code did work).

Hehe, I did spell out what the "deficiencies" were, nevertheless will revise it in next iteration.

Quoted 6 lines
Quoted 2 lines

There are total 111 calls in total in all of git codebase. How should I send the patches, alphabetically or otherwise?

My advice would be: try as much as possible to split according to the complexity of the patch.

Quoted 9 lines

As a reviewer, I find it rather easy to review a large number of trivial and similar changes, but I hate having to switch back to "wow, the author did something tricky, let's try to understand this" in the middle of a trivial series.

(we had this discussion about ... Vs $(...) and test -a Vs test ... && series, which were essentially very trivial changes, but with subtle bugs introduced and hidden by the volume of trivial changes).

Noted.

Quoted 29 lines
Quoted 2 lines
 branch.c | 25 +++++++++----------------
 1 file changed, 9 insertions(+), 16 deletions(-)

Removing more lines than it adds. I like the patch already ;-).

Quoted 4 lines
branch.c +0 -0
index 660097b..257b1bf 100644

[...]

Quoted 4 lines

int read_branch_desc(struct strbuf *buf, const char *branch_name) {

branch.c +1 -1
-	struct branch_desc_cb cb;
+	const char *value;

branch.c static int setup_tracking(const char *new_ref, const char *orig_ref, · line 157

Quoted 10 lines
branch.c +5 -3
+	struct branch_desc desc;
 	struct strbuf name = STRBUF_INIT;
 	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) {
+	desc.config_name = name.buf;
+	desc.value = NULL;
+	value = git_config_get_string(desc.config_name);
+	git_config_string(&desc.value, desc.config_name, value);

You're ignoring the return value of git_config_string, which is an error code. It shouldn't harm, because the code is non-zero iff desc.value is set to non-NULL, but you may want to write the code as

Quoted 7 lines
if (git_config_string(...)) {
	strbuf_release(...);
	return -1;
}

In any case, the patch sounds good to me.

Yes, for clarity sake, will rewrite the section like that. Thanks for the review.