Skip to content

Commit 787cb8a

Browse files
calvin-wan-googlegitster
authored andcommitted
strbuf: remove global variable
As a library that only interacts with other primitives, strbuf should not utilize the comment_line_char global variable within its functions. Therefore, add an additional parameter for functions that use comment_line_char and refactor callers to pass it in instead. strbuf_stripspace() removes the skip_comments boolean and checks if comment_line_char is a non-NUL character to determine whether to skip comments or not. Signed-off-by: Calvin Wan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent aba0706 commit 787cb8a

16 files changed

+90
-64
lines changed

add-patch.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,10 +1105,11 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
11051105
size_t i;
11061106

11071107
strbuf_reset(&s->buf);
1108-
strbuf_commented_addf(&s->buf, _("Manual hunk edit mode -- see bottom for "
1109-
"a quick guide.\n"));
1108+
strbuf_commented_addf(&s->buf, comment_line_char,
1109+
_("Manual hunk edit mode -- see bottom for "
1110+
"a quick guide.\n"));
11101111
render_hunk(s, hunk, 0, 0, &s->buf);
1111-
strbuf_commented_addf(&s->buf,
1112+
strbuf_commented_addf(&s->buf, comment_line_char,
11121113
_("---\n"
11131114
"To remove '%c' lines, make them ' ' lines "
11141115
"(context).\n"
@@ -1117,12 +1118,13 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
11171118
s->mode->is_reverse ? '+' : '-',
11181119
s->mode->is_reverse ? '-' : '+',
11191120
comment_line_char);
1120-
strbuf_commented_addf(&s->buf, "%s", _(s->mode->edit_hunk_hint));
1121+
strbuf_commented_addf(&s->buf, comment_line_char, "%s",
1122+
_(s->mode->edit_hunk_hint));
11211123
/*
11221124
* TRANSLATORS: 'it' refers to the patch mentioned in the previous
11231125
* messages.
11241126
*/
1125-
strbuf_commented_addf(&s->buf,
1127+
strbuf_commented_addf(&s->buf, comment_line_char,
11261128
_("If it does not apply cleanly, you will be "
11271129
"given an opportunity to\n"
11281130
"edit again. If all lines of the hunk are "

builtin/am.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1283,7 +1283,7 @@ static int parse_mail(struct am_state *state, const char *mail)
12831283

12841284
strbuf_addstr(&msg, "\n\n");
12851285
strbuf_addbuf(&msg, &mi.log_message);
1286-
strbuf_stripspace(&msg, 0);
1286+
strbuf_stripspace(&msg, '\0');
12871287

12881288
assert(!state->author_name);
12891289
state->author_name = strbuf_detach(&author_name, NULL);

builtin/branch.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ static int edit_branch_description(const char *branch_name)
674674
exists = !read_branch_desc(&buf, branch_name);
675675
if (!buf.len || buf.buf[buf.len-1] != '\n')
676676
strbuf_addch(&buf, '\n');
677-
strbuf_commented_addf(&buf,
677+
strbuf_commented_addf(&buf, comment_line_char,
678678
_("Please edit the description for the branch\n"
679679
" %s\n"
680680
"Lines starting with '%c' will be stripped.\n"),
@@ -685,7 +685,7 @@ static int edit_branch_description(const char *branch_name)
685685
strbuf_release(&buf);
686686
return -1;
687687
}
688-
strbuf_stripspace(&buf, 1);
688+
strbuf_stripspace(&buf, comment_line_char);
689689

690690
strbuf_addf(&name, "branch.%s.description", branch_name);
691691
if (buf.len || exists)

builtin/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
893893
s->hints = 0;
894894

895895
if (clean_message_contents)
896-
strbuf_stripspace(&sb, 0);
896+
strbuf_stripspace(&sb, '\0');
897897

898898
if (signoff)
899899
append_signoff(&sb, ignore_non_trailer(sb.buf, sb.len), 0);

builtin/merge.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -879,13 +879,15 @@ static void prepare_to_commit(struct commit_list *remoteheads)
879879
strbuf_addch(&msg, '\n');
880880
if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
881881
wt_status_append_cut_line(&msg);
882-
strbuf_commented_addf(&msg, "\n");
882+
strbuf_commented_addf(&msg, comment_line_char, "\n");
883883
}
884-
strbuf_commented_addf(&msg, _(merge_editor_comment));
884+
strbuf_commented_addf(&msg, comment_line_char,
885+
_(merge_editor_comment));
885886
if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
886-
strbuf_commented_addf(&msg, _(scissors_editor_comment));
887+
strbuf_commented_addf(&msg, comment_line_char,
888+
_(scissors_editor_comment));
887889
else
888-
strbuf_commented_addf(&msg,
890+
strbuf_commented_addf(&msg, comment_line_char,
889891
_(no_scissors_editor_comment), comment_line_char);
890892
}
891893
if (signoff)

builtin/notes.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "config.h"
1212
#include "builtin.h"
1313
#include "editor.h"
14+
#include "environment.h"
1415
#include "gettext.h"
1516
#include "hex.h"
1617
#include "notes.h"
@@ -157,7 +158,7 @@ static void write_commented_object(int fd, const struct object_id *object)
157158

158159
if (strbuf_read(&buf, show.out, 0) < 0)
159160
die_errno(_("could not read 'show' output"));
160-
strbuf_add_commented_lines(&cbuf, buf.buf, buf.len);
161+
strbuf_add_commented_lines(&cbuf, buf.buf, buf.len, comment_line_char);
161162
write_or_die(fd, cbuf.buf, cbuf.len);
162163

163164
strbuf_release(&cbuf);
@@ -185,9 +186,10 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
185186
copy_obj_to_fd(fd, old_note);
186187

187188
strbuf_addch(&buf, '\n');
188-
strbuf_add_commented_lines(&buf, "\n", strlen("\n"));
189-
strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)));
190-
strbuf_add_commented_lines(&buf, "\n", strlen("\n"));
189+
strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
190+
strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)),
191+
comment_line_char);
192+
strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
191193
write_or_die(fd, buf.buf, buf.len);
192194

193195
write_commented_object(fd, object);
@@ -199,7 +201,7 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
199201
if (launch_editor(d->edit_path, &d->buf, NULL)) {
200202
die(_("please supply the note contents using either -m or -F option"));
201203
}
202-
strbuf_stripspace(&d->buf, 1);
204+
strbuf_stripspace(&d->buf, comment_line_char);
203205
}
204206
}
205207

@@ -225,7 +227,7 @@ static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
225227
if (d->buf.len)
226228
strbuf_addch(&d->buf, '\n');
227229
strbuf_addstr(&d->buf, arg);
228-
strbuf_stripspace(&d->buf, 0);
230+
strbuf_stripspace(&d->buf, '\0');
229231

230232
d->given = 1;
231233
return 0;
@@ -244,7 +246,7 @@ static int parse_file_arg(const struct option *opt, const char *arg, int unset)
244246
die_errno(_("cannot read '%s'"), arg);
245247
} else if (strbuf_read_file(&d->buf, arg, 1024) < 0)
246248
die_errno(_("could not open or read '%s'"), arg);
247-
strbuf_stripspace(&d->buf, 0);
249+
strbuf_stripspace(&d->buf, '\0');
248250

249251
d->given = 1;
250252
return 0;

builtin/rebase.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ static int edit_todo_file(unsigned flags)
209209
if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
210210
return error_errno(_("could not read '%s'."), todo_file);
211211

212-
strbuf_stripspace(&todo_list.buf, 1);
212+
strbuf_stripspace(&todo_list.buf, comment_line_char);
213213
res = edit_todo_list(the_repository, &todo_list, &new_todo, NULL, NULL, flags);
214214
if (!res && todo_list_write_to_file(the_repository, &new_todo, todo_file,
215215
NULL, NULL, -1, flags & ~(TODO_LIST_SHORTEN_IDS)))

builtin/stripspace.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "builtin.h"
22
#include "cache.h"
33
#include "config.h"
4+
#include "environment.h"
45
#include "gettext.h"
56
#include "parse-options.h"
67
#include "setup.h"
@@ -13,7 +14,7 @@ static void comment_lines(struct strbuf *buf)
1314
size_t len;
1415

1516
msg = strbuf_detach(buf, &len);
16-
strbuf_add_commented_lines(buf, msg, len);
17+
strbuf_add_commented_lines(buf, msg, len, comment_line_char);
1718
free(msg);
1819
}
1920

@@ -58,7 +59,8 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix)
5859
die_errno("could not read the input");
5960

6061
if (mode == STRIP_DEFAULT || mode == STRIP_COMMENTS)
61-
strbuf_stripspace(&buf, mode == STRIP_COMMENTS);
62+
strbuf_stripspace(&buf,
63+
mode == STRIP_COMMENTS ? comment_line_char : '\0');
6264
else
6365
comment_lines(&buf);
6466

builtin/tag.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,11 @@ static void create_tag(const struct object_id *object, const char *object_ref,
311311
struct strbuf buf = STRBUF_INIT;
312312
strbuf_addch(&buf, '\n');
313313
if (opt->cleanup_mode == CLEANUP_ALL)
314-
strbuf_commented_addf(&buf, _(tag_template), tag, comment_line_char);
314+
strbuf_commented_addf(&buf, comment_line_char,
315+
_(tag_template), tag, comment_line_char);
315316
else
316-
strbuf_commented_addf(&buf, _(tag_template_nocleanup), tag, comment_line_char);
317+
strbuf_commented_addf(&buf, comment_line_char,
318+
_(tag_template_nocleanup), tag, comment_line_char);
317319
write_or_die(fd, buf.buf, buf.len);
318320
strbuf_release(&buf);
319321
}
@@ -327,7 +329,8 @@ static void create_tag(const struct object_id *object, const char *object_ref,
327329
}
328330

329331
if (opt->cleanup_mode != CLEANUP_NONE)
330-
strbuf_stripspace(buf, opt->cleanup_mode == CLEANUP_ALL);
332+
strbuf_stripspace(buf,
333+
opt->cleanup_mode == CLEANUP_ALL ? comment_line_char : '\0');
331334

332335
if (!opt->message_given && !buf->len)
333336
die(_("no tag message?"));

fmt-merge-msg.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,8 @@ static void fmt_tag_signature(struct strbuf *tagbuf,
508508
strbuf_complete_line(tagbuf);
509509
if (sig->len) {
510510
strbuf_addch(tagbuf, '\n');
511-
strbuf_add_commented_lines(tagbuf, sig->buf, sig->len);
511+
strbuf_add_commented_lines(tagbuf, sig->buf, sig->len,
512+
comment_line_char);
512513
}
513514
}
514515

@@ -554,15 +555,17 @@ static void fmt_merge_msg_sigs(struct strbuf *out)
554555
strbuf_addch(&tagline, '\n');
555556
strbuf_add_commented_lines(&tagline,
556557
origins.items[first_tag].string,
557-
strlen(origins.items[first_tag].string));
558+
strlen(origins.items[first_tag].string),
559+
comment_line_char);
558560
strbuf_insert(&tagbuf, 0, tagline.buf,
559561
tagline.len);
560562
strbuf_release(&tagline);
561563
}
562564
strbuf_addch(&tagbuf, '\n');
563565
strbuf_add_commented_lines(&tagbuf,
564566
origins.items[i].string,
565-
strlen(origins.items[i].string));
567+
strlen(origins.items[i].string),
568+
comment_line_char);
566569
fmt_tag_signature(&tagbuf, &sig, buf, len);
567570
}
568571
strbuf_release(&payload);

0 commit comments

Comments
 (0)