branch.c: replace `git_config()` with `git_config_get_string()
Use `git_config_get_string()` instead of `git_config()` to take advantage of the config-set API which provides a cleaner control flow. While we are at it, return -1 if we find no value for the queried variable. Original code returned 0 for all cases, which was checked by `add_branch_desc()` in fmt-merge-msg.c resulting in addition of a spurious newline to the `out` strbuf. Now, the newline addition is skipped as -1 is returned to the caller if no value is found. Signed-off-by: Tanay Abhra <tanayabh@gmail.com> Reviewed-by: Matthieu Moy <Matthieu.Moy@imag.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Tanay Abhra committed
Aug 7, 2014 at 23:26 UTC
540b0f497701fca2749de86c58bc16f44fcb30b5
1 file changed
+7
-20
branch.c
+7
-20
@@ -140,30 +140,17 @@ static int setup_tracking(const char *new_ref, const char *orig_ref,
140
return 0;
141
}
142
143
-struct branch_desc_cb {
144
- const char *config_name;
145
- const char *value;
146
-};
147
-
148
-static int read_branch_desc_cb(const char *var, const char *value, void *cb)
149
-{
150
- struct branch_desc_cb *desc = cb;
151
- if (strcmp(desc->config_name, var))
152
- return 0;
153
- free((char *)desc->value);
154
- return git_config_string(&desc->value, var, value);
155
-}
156
-
143
int read_branch_desc(struct strbuf *buf, const char *branch_name)
144
{
159
- struct branch_desc_cb cb;
145
+ char *v = NULL;
146
struct strbuf name = STRBUF_INIT;
147
strbuf_addf(&name, "branch.%s.description", branch_name);
162
- cb.config_name = name.buf;
163
- cb.value = NULL;
164
- git_config(read_branch_desc_cb, &cb);
165
- if (cb.value)
166
- strbuf_addstr(buf, cb.value);
148
+ if (git_config_get_string(name.buf, &v)) {
149
+ strbuf_release(&name);
150
+ return -1;
151
+ }
152
+ strbuf_addstr(buf, v);
153
+ free(v);
154
strbuf_release(&name);
155
return 0;
156
}