use strbuf_addstr() for adding constant strings to a strbuf, part 2

Replace uses of strbuf_addf() for adding strings with more lightweight
strbuf_addstr() calls.  This makes the intent clearer and avoids
potential issues with printf format specifiers.

02962d3684 already converted six cases,
this patch covers eleven more.

A semantic patch for Coccinelle is included for easier checking for
new cases that might be introduced in the future.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
René Scharfe 2016-09-15 20:31:00 +02:00 committed by Junio C Hamano
parent 63f0a758a0
commit a22ae753cb
7 changed files with 18 additions and 12 deletions

View File

@ -395,7 +395,7 @@ static void shortlog(const char *name,
for (i = 0; i < subjects.nr; i++) for (i = 0; i < subjects.nr; i++)
if (i >= limit) if (i >= limit)
strbuf_addf(out, " ...\n"); strbuf_addstr(out, " ...\n");
else else
strbuf_addf(out, " %s\n", subjects.items[i].string); strbuf_addf(out, " %s\n", subjects.items[i].string);

View File

@ -940,7 +940,7 @@ static void write_merge_state(struct commit_list *remoteheads)
strbuf_reset(&buf); strbuf_reset(&buf);
if (fast_forward == FF_NO) if (fast_forward == FF_NO)
strbuf_addf(&buf, "no-ff"); strbuf_addstr(&buf, "no-ff");
write_file_buf(git_path_merge_mode(), buf.buf, buf.len); write_file_buf(git_path_merge_mode(), buf.buf, buf.len);
} }

View File

@ -749,8 +749,9 @@ static int update_clone_get_next_task(struct child_process *child,
ce = suc->failed_clones[index]; ce = suc->failed_clones[index];
if (!prepare_to_clone_next_submodule(ce, child, suc, err)) { if (!prepare_to_clone_next_submodule(ce, child, suc, err)) {
suc->current ++; suc->current ++;
strbuf_addf(err, "BUG: submodule considered for cloning," strbuf_addstr(err, "BUG: submodule considered for "
"doesn't need cloning any more?\n"); "cloning, doesn't need cloning "
"any more?\n");
return 0; return 0;
} }
p = xmalloc(sizeof(*p)); p = xmalloc(sizeof(*p));

View File

@ -0,0 +1,5 @@
@@
expression E1, E2;
@@
- strbuf_addf(E1, E2);
+ strbuf_addstr(E1, E2);

View File

@ -206,7 +206,7 @@ static void output_commit_title(struct merge_options *o, struct commit *commit)
find_unique_abbrev(commit->object.oid.hash, find_unique_abbrev(commit->object.oid.hash,
DEFAULT_ABBREV)); DEFAULT_ABBREV));
if (parse_commit(commit) != 0) if (parse_commit(commit) != 0)
strbuf_addf(&o->obuf, _("(bad commit)\n")); strbuf_addstr(&o->obuf, _("(bad commit)\n"));
else { else {
const char *title; const char *title;
const char *msg = get_commit_buffer(commit, NULL); const char *msg = get_commit_buffer(commit, NULL);

View File

@ -2073,7 +2073,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb)
_("Your branch is based on '%s', but the upstream is gone.\n"), _("Your branch is based on '%s', but the upstream is gone.\n"),
base); base);
if (advice_status_hints) if (advice_status_hints)
strbuf_addf(sb, strbuf_addstr(sb,
_(" (use \"git branch --unset-upstream\" to fixup)\n")); _(" (use \"git branch --unset-upstream\" to fixup)\n"));
} else if (!ours && !theirs) { } else if (!ours && !theirs) {
strbuf_addf(sb, strbuf_addf(sb,
@ -2086,7 +2086,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb)
ours), ours),
base, ours); base, ours);
if (advice_status_hints) if (advice_status_hints)
strbuf_addf(sb, strbuf_addstr(sb,
_(" (use \"git push\" to publish your local commits)\n")); _(" (use \"git push\" to publish your local commits)\n"));
} else if (!ours) { } else if (!ours) {
strbuf_addf(sb, strbuf_addf(sb,
@ -2097,7 +2097,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb)
theirs), theirs),
base, theirs); base, theirs);
if (advice_status_hints) if (advice_status_hints)
strbuf_addf(sb, strbuf_addstr(sb,
_(" (use \"git pull\" to update your local branch)\n")); _(" (use \"git pull\" to update your local branch)\n"));
} else { } else {
strbuf_addf(sb, strbuf_addf(sb,
@ -2110,7 +2110,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb)
ours + theirs), ours + theirs),
base, ours, theirs); base, ours, theirs);
if (advice_status_hints) if (advice_status_hints)
strbuf_addf(sb, strbuf_addstr(sb,
_(" (use \"git pull\" to merge the remote branch into yours)\n")); _(" (use \"git pull\" to merge the remote branch into yours)\n"));
} }
free(base); free(base);

View File

@ -367,11 +367,11 @@ static void wt_status_print_change_data(struct wt_status *s,
if (d->new_submodule_commits || d->dirty_submodule) { if (d->new_submodule_commits || d->dirty_submodule) {
strbuf_addstr(&extra, " ("); strbuf_addstr(&extra, " (");
if (d->new_submodule_commits) if (d->new_submodule_commits)
strbuf_addf(&extra, _("new commits, ")); strbuf_addstr(&extra, _("new commits, "));
if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED) if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
strbuf_addf(&extra, _("modified content, ")); strbuf_addstr(&extra, _("modified content, "));
if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
strbuf_addf(&extra, _("untracked content, ")); strbuf_addstr(&extra, _("untracked content, "));
strbuf_setlen(&extra, extra.len - 2); strbuf_setlen(&extra, extra.len - 2);
strbuf_addch(&extra, ')'); strbuf_addch(&extra, ')');
} }