Skip to content

Commit a22ae75

Browse files
rscharfegitster
authored andcommitted
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. 02962d3 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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 63f0a75 commit a22ae75

File tree

7 files changed

+18
-12
lines changed

7 files changed

+18
-12
lines changed

builtin/fmt-merge-msg.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ static void shortlog(const char *name,
395395

396396
for (i = 0; i < subjects.nr; i++)
397397
if (i >= limit)
398-
strbuf_addf(out, " ...\n");
398+
strbuf_addstr(out, " ...\n");
399399
else
400400
strbuf_addf(out, " %s\n", subjects.items[i].string);
401401

builtin/merge.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ static void write_merge_state(struct commit_list *remoteheads)
940940

941941
strbuf_reset(&buf);
942942
if (fast_forward == FF_NO)
943-
strbuf_addf(&buf, "no-ff");
943+
strbuf_addstr(&buf, "no-ff");
944944
write_file_buf(git_path_merge_mode(), buf.buf, buf.len);
945945
}
946946

builtin/submodule--helper.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,8 +749,9 @@ static int update_clone_get_next_task(struct child_process *child,
749749
ce = suc->failed_clones[index];
750750
if (!prepare_to_clone_next_submodule(ce, child, suc, err)) {
751751
suc->current ++;
752-
strbuf_addf(err, "BUG: submodule considered for cloning,"
753-
"doesn't need cloning any more?\n");
752+
strbuf_addstr(err, "BUG: submodule considered for "
753+
"cloning, doesn't need cloning "
754+
"any more?\n");
754755
return 0;
755756
}
756757
p = xmalloc(sizeof(*p));

contrib/coccinelle/strbuf.cocci

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@@
2+
expression E1, E2;
3+
@@
4+
- strbuf_addf(E1, E2);
5+
+ strbuf_addstr(E1, E2);

merge-recursive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ static void output_commit_title(struct merge_options *o, struct commit *commit)
206206
find_unique_abbrev(commit->object.oid.hash,
207207
DEFAULT_ABBREV));
208208
if (parse_commit(commit) != 0)
209-
strbuf_addf(&o->obuf, _("(bad commit)\n"));
209+
strbuf_addstr(&o->obuf, _("(bad commit)\n"));
210210
else {
211211
const char *title;
212212
const char *msg = get_commit_buffer(commit, NULL);

remote.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,7 +2073,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb)
20732073
_("Your branch is based on '%s', but the upstream is gone.\n"),
20742074
base);
20752075
if (advice_status_hints)
2076-
strbuf_addf(sb,
2076+
strbuf_addstr(sb,
20772077
_(" (use \"git branch --unset-upstream\" to fixup)\n"));
20782078
} else if (!ours && !theirs) {
20792079
strbuf_addf(sb,
@@ -2086,7 +2086,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb)
20862086
ours),
20872087
base, ours);
20882088
if (advice_status_hints)
2089-
strbuf_addf(sb,
2089+
strbuf_addstr(sb,
20902090
_(" (use \"git push\" to publish your local commits)\n"));
20912091
} else if (!ours) {
20922092
strbuf_addf(sb,
@@ -2097,7 +2097,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb)
20972097
theirs),
20982098
base, theirs);
20992099
if (advice_status_hints)
2100-
strbuf_addf(sb,
2100+
strbuf_addstr(sb,
21012101
_(" (use \"git pull\" to update your local branch)\n"));
21022102
} else {
21032103
strbuf_addf(sb,
@@ -2110,7 +2110,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb)
21102110
ours + theirs),
21112111
base, ours, theirs);
21122112
if (advice_status_hints)
2113-
strbuf_addf(sb,
2113+
strbuf_addstr(sb,
21142114
_(" (use \"git pull\" to merge the remote branch into yours)\n"));
21152115
}
21162116
free(base);

wt-status.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,11 +367,11 @@ static void wt_status_print_change_data(struct wt_status *s,
367367
if (d->new_submodule_commits || d->dirty_submodule) {
368368
strbuf_addstr(&extra, " (");
369369
if (d->new_submodule_commits)
370-
strbuf_addf(&extra, _("new commits, "));
370+
strbuf_addstr(&extra, _("new commits, "));
371371
if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
372-
strbuf_addf(&extra, _("modified content, "));
372+
strbuf_addstr(&extra, _("modified content, "));
373373
if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
374-
strbuf_addf(&extra, _("untracked content, "));
374+
strbuf_addstr(&extra, _("untracked content, "));
375375
strbuf_setlen(&extra, extra.len - 2);
376376
strbuf_addch(&extra, ')');
377377
}

0 commit comments

Comments
 (0)