diff.c: refactor pprint_rename() to use strbuf

Instead of passing char* around, let function handle strbuf directly. All callers already use strbuf internally. This helps kill the "not free" exception in free_diffstat_info(). I don't think this code is so critical that we need to avoid some free() calls. The other benefit comes in the next patch, where we append something in pname before returning from fill_print_name(). With strbuf, it's very simple. With "char *" we may have to resort to explicit reallocation and stuff. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Feb 1, 2018 at 20:02 UTC c905cbc49c6455cc8d7c38c24a047396286f0fe6
1 file changed +26 -33
diff.c
+26 -33
@@ -2045,11 +2045,10 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
2045 }
2046 }
2047
2048 -static char *pprint_rename(const char *a, const char *b)
2048 +static void pprint_rename(struct strbuf *name, const char *a, const char *b)
2049 {
2050 const char *old = a;
2051 const char *new = b;
2052 - struct strbuf name = STRBUF_INIT;
2052 int pfx_length, sfx_length;
2053 int pfx_adjust_for_slash;
2054 int len_a = strlen(a);
@@ -2059,10 +2058,10 @@ static char *pprint_rename(const char *a, const char *b)
2058 int qlen_b = quote_c_style(b, NULL, NULL, 0);
2059
2060 if (qlen_a || qlen_b) {
2062 - quote_c_style(a, &name, NULL, 0);
2063 - strbuf_addstr(&name, " => ");
2064 - quote_c_style(b, &name, NULL, 0);
2065 - return strbuf_detach(&name, NULL);
2061 + quote_c_style(a, name, NULL, 0);
2062 + strbuf_addstr(name, " => ");
2063 + quote_c_style(b, name, NULL, 0);
2064 + return;
2065 }
2066
2067 /* Find common prefix */
@@ -2109,19 +2108,18 @@ static char *pprint_rename(const char *a, const char *b)
2108 if (b_midlen < 0)
2109 b_midlen = 0;
2110
2112 - strbuf_grow(&name, pfx_length + a_midlen + b_midlen + sfx_length + 7);
2111 + strbuf_grow(name, pfx_length + a_midlen + b_midlen + sfx_length + 7);
2112 if (pfx_length + sfx_length) {
2114 - strbuf_add(&name, a, pfx_length);
2115 - strbuf_addch(&name, '{');
2113 + strbuf_add(name, a, pfx_length);
2114 + strbuf_addch(name, '{');
2115 }
2117 - strbuf_add(&name, a + pfx_length, a_midlen);
2118 - strbuf_addstr(&name, " => ");
2119 - strbuf_add(&name, b + pfx_length, b_midlen);
2116 + strbuf_add(name, a + pfx_length, a_midlen);
2117 + strbuf_addstr(name, " => ");
2118 + strbuf_add(name, b + pfx_length, b_midlen);
2119 if (pfx_length + sfx_length) {
2121 - strbuf_addch(&name, '}');
2122 - strbuf_add(&name, a + len_a - sfx_length, sfx_length);
2120 + strbuf_addch(name, '}');
2121 + strbuf_add(name, a + len_a - sfx_length, sfx_length);
2122 }
2124 - return strbuf_detach(&name, NULL);
2123 }
2124
2125 struct diffstat_t {
@@ -2197,23 +2195,17 @@ static void show_graph(struct strbuf *out, char ch, int cnt,
2195
2196 static void fill_print_name(struct diffstat_file *file)
2197 {
2200 - char *pname;
2198 + struct strbuf pname = STRBUF_INIT;
2199
2200 if (file->print_name)
2201 return;
2202
2205 - if (!file->is_renamed) {
2206 - struct strbuf buf = STRBUF_INIT;
2207 - if (quote_c_style(file->name, &buf, NULL, 0)) {
2208 - pname = strbuf_detach(&buf, NULL);
2209 - } else {
2210 - pname = file->name;
2211 - strbuf_release(&buf);
2212 - }
2213 - } else {
2214 - pname = pprint_rename(file->from_name, file->name);
2215 - }
2216 - file->print_name = pname;
2203 + if (file->is_renamed)
2204 + pprint_rename(&pname, file->from_name, file->name);
2205 + else
2206 + quote_c_style(file->name, &pname, NULL, 0);
2207 +
2208 + file->print_name = strbuf_detach(&pname, NULL);
2209 }
2210
2211 static void print_stat_summary_inserts_deletes(struct diff_options *options,
@@ -2797,8 +2789,7 @@ static void free_diffstat_info(struct diffstat_t *diffstat)
2789 int i;
2790 for (i = 0; i < diffstat->nr; i++) {
2791 struct diffstat_file *f = diffstat->files[i];
2800 - if (f->name != f->print_name)
2801 - free(f->print_name);
2792 + free(f->print_name);
2793 free(f->name);
2794 free(f->from_name);
2795 free(f);
@@ -5224,10 +5215,12 @@ static void show_rename_copy(struct diff_options *opt, const char *renamecopy,
5215 struct diff_filepair *p)
5216 {
5217 struct strbuf sb = STRBUF_INIT;
5227 - char *names = pprint_rename(p->one->path, p->two->path);
5218 + struct strbuf names = STRBUF_INIT;
5219 +
5220 + pprint_rename(&names, p->one->path, p->two->path);
5221 strbuf_addf(&sb, " %s %s (%d%%)\n",
5229 - renamecopy, names, similarity_index(p));
5230 - free(names);
5222 + renamecopy, names.buf, similarity_index(p));
5223 + strbuf_release(&names);
5224 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
5225 sb.buf, sb.len, 0);
5226 show_mode_change(opt, p, 0);