branch: add read_branch_desc() helper function

This will be used by various callers that make use of the branch description throughout the system, so that if we need to update the implementation the callers do not have to be modified. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Sep 21, 2011 at 20:19 UTC 6f9a332144cda5f4d7e6e03c37fb17f8ffac1fe3
2 files changed +36
branch.c
+31
@@ -135,6 +135,37 @@ static int setup_tracking(const char *new_ref, const char *orig_ref,
135 return 0;
136 }
137
138 +struct branch_desc_cb {
139 + const char *config_name;
140 + const char *value;
141 +};
142 +
143 +static int read_branch_desc_cb(const char *var, const char *value, void *cb)
144 +{
145 + struct branch_desc_cb *desc = cb;
146 + if (strcmp(desc->config_name, var))
147 + return 0;
148 + free((char *)desc->value);
149 + return git_config_string(&desc->value, var, value);
150 +}
151 +
152 +int read_branch_desc(struct strbuf *buf, const char *branch_name)
153 +{
154 + struct branch_desc_cb cb;
155 + struct strbuf name = STRBUF_INIT;
156 + strbuf_addf(&name, "branch.%s.description", branch_name);
157 + cb.config_name = name.buf;
158 + cb.value = NULL;
159 + if (git_config(read_branch_desc_cb, &cb) < 0) {
160 + strbuf_release(&name);
161 + return -1;
162 + }
163 + if (cb.value)
164 + strbuf_addstr(buf, cb.value);
165 + strbuf_release(&name);
166 + return 0;
167 +}
168 +
169 int validate_new_branchname(const char *name, struct strbuf *ref,
170 int force, int attr_only)
171 {
branch.h
+5
@@ -46,4 +46,9 @@ void remove_branch_state(void);
46 #define BRANCH_CONFIG_VERBOSE 01
47 extern void install_branch_config(int flag, const char *local, const char *origin, const char *remote);
48
49 +/*
50 + * Read branch description
51 + */
52 +extern int read_branch_desc(struct strbuf *, const char *branch_name);
53 +
54 #endif