From fa2bb34477120f18d0834a545ac777e6295650d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sun, 1 Oct 2017 16:44:06 +0200 Subject: [PATCH 1/3] path: use strbuf_add_real_path() Avoid a string copy to a static buffer by using strbuf_add_real_path() instead of combining strbuf_addstr() and real_path(). Patch generated by Coccinelle and contrib/coccinelle/strbuf.cocci. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- path.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/path.c b/path.c index b533ec938d..bf096c411c 100644 --- a/path.c +++ b/path.c @@ -717,7 +717,7 @@ char *expand_user_path(const char *path, int real_home) if (!home) goto return_null; if (real_home) - strbuf_addstr(&user_path, real_path(home)); + strbuf_add_real_path(&user_path, home); else strbuf_addstr(&user_path, home); #ifdef GIT_WINDOWS_NATIVE From 72d4a9a721d519982cbc97c36f3fcf33b4103ed4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sun, 1 Oct 2017 16:44:20 +0200 Subject: [PATCH 2/3] use strbuf_addstr() for adding strings to strbufs Use strbuf_addstr() instead of strbuf_addf() for adding strings. That's simpler and makes the intent clearer. Patch generated by Coccinelle and contrib/coccinelle/strbuf.cocci; adjusted indentation in refs/packed-backend.c manually. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- builtin/branch.c | 2 +- refs/packed-backend.c | 4 ++-- sequencer.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/builtin/branch.c b/builtin/branch.c index 355f9ef5da..a2b7c55427 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -352,7 +352,7 @@ static char *build_format(struct ref_filter *filter, int maxwidth, const char *r strbuf_addf(&obname, "%%(objectname:short=%d)", filter->abbrev); strbuf_addf(&local, "%%(align:%d,left)%%(refname:lstrip=2)%%(end)", maxwidth); - strbuf_addf(&local, "%s", branch_get_color(BRANCH_COLOR_RESET)); + strbuf_addstr(&local, branch_get_color(BRANCH_COLOR_RESET)); strbuf_addf(&local, " %s ", obname.buf); if (filter->verbose > 1) diff --git a/refs/packed-backend.c b/refs/packed-backend.c index 3bc47ffd5e..2951514f25 100644 --- a/refs/packed-backend.c +++ b/refs/packed-backend.c @@ -729,8 +729,8 @@ static int write_with_updates(struct packed_ref_store *refs, } if (ok != ITER_DONE) { - strbuf_addf(err, "unable to write packed-refs file: " - "error iterating over old contents"); + strbuf_addstr(err, "unable to write packed-refs file: " + "error iterating over old contents"); goto error; } diff --git a/sequencer.c b/sequencer.c index 60636ce54b..ba95cefc7d 100644 --- a/sequencer.c +++ b/sequencer.c @@ -203,7 +203,7 @@ int sequencer_remove_state(struct replay_opts *opts) free(opts->xopts[i]); free(opts->xopts); - strbuf_addf(&dir, "%s", get_dir(opts)); + strbuf_addstr(&dir, get_dir(opts)); remove_dir_recursively(&dir, 0); strbuf_release(&dir); From 38bdf62b738bb93f7e1a6af8058dc31f27c91d4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sun, 1 Oct 2017 16:45:45 +0200 Subject: [PATCH 3/3] graph: use strbuf_addchars() to add spaces strbuf_addf() can be used to add a specific number of space characters by using the format "%*s" with an empty string and specifying the desired width. Use strbuf_addchars() instead as it's shorter, makes the intent clearer and is a bit more efficient. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- graph.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/graph.c b/graph.c index e7e20650da..e1f6d3bddb 100644 --- a/graph.c +++ b/graph.c @@ -696,12 +696,8 @@ static void graph_pad_horizontally(struct git_graph *graph, struct strbuf *sb, * This way, fields printed to the right of the graph will remain * aligned for the entire commit. */ - int extra; - if (chars_written >= graph->width) - return; - - extra = graph->width - chars_written; - strbuf_addf(sb, "%*s", (int) extra, ""); + if (chars_written < graph->width) + strbuf_addchars(sb, ' ', graph->width - chars_written); } static void graph_output_padding_line(struct git_graph *graph, @@ -787,7 +783,7 @@ static void graph_output_pre_commit_line(struct git_graph *graph, if (col->commit == graph->commit) { seen_this = 1; strbuf_write_column(sb, col, '|'); - strbuf_addf(sb, "%*s", graph->expansion_row, ""); + strbuf_addchars(sb, ' ', graph->expansion_row); chars_written += 1 + graph->expansion_row; } else if (seen_this && (graph->expansion_row == 0)) { /*