Skip to content

Commit

Permalink
Merge branch 'jc/strbuf-commented-something' into seen
Browse files Browse the repository at this point in the history
Update two functions whose callers always pass the same global
variable to omit the redundant parameter and use the global in the
callee themselves.

* jc/strbuf-commented-something:
  strbuf: retire strbuf_commented_lines()
  strbuf: retire strbuf_commented_addf()
  • Loading branch information
gitster committed Sep 19, 2024
2 parents b6310a6 + 19e27e2 commit cf50d3d
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 57 deletions.
8 changes: 4 additions & 4 deletions add-patch.c
Original file line number Diff line number Diff line change
Expand Up @@ -1116,11 +1116,11 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
size_t i;

strbuf_reset(&s->buf);
strbuf_commented_addf(&s->buf, comment_line_str,
strbuf_comment_addf(&s->buf,
_("Manual hunk edit mode -- see bottom for "
"a quick guide.\n"));
render_hunk(s, hunk, 0, 0, &s->buf);
strbuf_commented_addf(&s->buf, comment_line_str,
strbuf_comment_addf(&s->buf,
_("---\n"
"To remove '%c' lines, make them ' ' lines "
"(context).\n"
Expand All @@ -1129,13 +1129,13 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
s->mode->is_reverse ? '+' : '-',
s->mode->is_reverse ? '-' : '+',
comment_line_str);
strbuf_commented_addf(&s->buf, comment_line_str, "%s",
strbuf_comment_addf(&s->buf, "%s",
_(s->mode->edit_hunk_hint));
/*
* TRANSLATORS: 'it' refers to the patch mentioned in the previous
* messages.
*/
strbuf_commented_addf(&s->buf, comment_line_str,
strbuf_comment_addf(&s->buf,
_("If it does not apply cleanly, you will be "
"given an opportunity to\n"
"edit again. If all lines of the hunk are "
Expand Down
2 changes: 1 addition & 1 deletion builtin/branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ static int edit_branch_description(const char *branch_name)
exists = !read_branch_desc(&buf, branch_name);
if (!buf.len || buf.buf[buf.len-1] != '\n')
strbuf_addch(&buf, '\n');
strbuf_commented_addf(&buf, comment_line_str,
strbuf_comment_addf(&buf,
_("Please edit the description for the branch\n"
" %s\n"
"Lines starting with '%s' will be stripped.\n"),
Expand Down
8 changes: 4 additions & 4 deletions builtin/merge.c
Original file line number Diff line number Diff line change
Expand Up @@ -868,15 +868,15 @@ static void prepare_to_commit(struct commit_list *remoteheads)
strbuf_addch(&msg, '\n');
if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
wt_status_append_cut_line(&msg);
strbuf_commented_addf(&msg, comment_line_str, "\n");
strbuf_comment_addf(&msg, "\n");
}
strbuf_commented_addf(&msg, comment_line_str,
strbuf_comment_addf(&msg,
_(merge_editor_comment));
if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
strbuf_commented_addf(&msg, comment_line_str,
strbuf_comment_addf(&msg,
_(scissors_editor_comment));
else
strbuf_commented_addf(&msg, comment_line_str,
strbuf_comment_addf(&msg,
_(no_scissors_editor_comment), comment_line_str);
}
if (signoff)
Expand Down
10 changes: 5 additions & 5 deletions builtin/notes.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ static void write_commented_object(int fd, const struct object_id *object)

if (strbuf_read(&buf, show.out, 0) < 0)
die_errno(_("could not read 'show' output"));
strbuf_add_commented_lines(&cbuf, buf.buf, buf.len, comment_line_str);
strbuf_add_comment_lines(&cbuf, buf.buf, buf.len);
write_or_die(fd, cbuf.buf, cbuf.len);

strbuf_release(&cbuf);
Expand Down Expand Up @@ -206,10 +206,10 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
copy_obj_to_fd(fd, old_note);

strbuf_addch(&buf, '\n');
strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_str);
strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)),
comment_line_str);
strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_str);
strbuf_add_comment_lines(&buf, "\n", strlen("\n"));
strbuf_add_comment_lines(&buf, _(note_template),
strlen(_(note_template)));
strbuf_add_comment_lines(&buf, "\n", strlen("\n"));
write_or_die(fd, buf.buf, buf.len);

write_commented_object(fd, object);
Expand Down
2 changes: 1 addition & 1 deletion builtin/stripspace.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ static void comment_lines(struct strbuf *buf)
size_t len;

msg = strbuf_detach(buf, &len);
strbuf_add_commented_lines(buf, msg, len, comment_line_str);
strbuf_add_comment_lines(buf, msg, len);
free(msg);
}

Expand Down
4 changes: 2 additions & 2 deletions builtin/tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,10 @@ static void create_tag(const struct object_id *object, const char *object_ref,
struct strbuf buf = STRBUF_INIT;
strbuf_addch(&buf, '\n');
if (opt->cleanup_mode == CLEANUP_ALL)
strbuf_commented_addf(&buf, comment_line_str,
strbuf_comment_addf(&buf,
_(tag_template), tag, comment_line_str);
else
strbuf_commented_addf(&buf, comment_line_str,
strbuf_comment_addf(&buf,
_(tag_template_nocleanup), tag, comment_line_str);
write_or_die(fd, buf.buf, buf.len);
strbuf_release(&buf);
Expand Down
17 changes: 7 additions & 10 deletions fmt-merge-msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,7 @@ static void fmt_tag_signature(struct strbuf *tagbuf,
strbuf_complete_line(tagbuf);
if (sig->len) {
strbuf_addch(tagbuf, '\n');
strbuf_add_commented_lines(tagbuf, sig->buf, sig->len,
comment_line_str);
strbuf_add_comment_lines(tagbuf, sig->buf, sig->len);
}
}

Expand Down Expand Up @@ -556,19 +555,17 @@ static void fmt_merge_msg_sigs(struct strbuf *out)
if (tag_number == 2) {
struct strbuf tagline = STRBUF_INIT;
strbuf_addch(&tagline, '\n');
strbuf_add_commented_lines(&tagline,
origins.items[first_tag].string,
strlen(origins.items[first_tag].string),
comment_line_str);
strbuf_add_comment_lines(&tagline,
origins.items[first_tag].string,
strlen(origins.items[first_tag].string));
strbuf_insert(&tagbuf, 0, tagline.buf,
tagline.len);
strbuf_release(&tagline);
}
strbuf_addch(&tagbuf, '\n');
strbuf_add_commented_lines(&tagbuf,
origins.items[i].string,
strlen(origins.items[i].string),
comment_line_str);
strbuf_add_comment_lines(&tagbuf,
origins.items[i].string,
strlen(origins.items[i].string));
fmt_tag_signature(&tagbuf, &sig, buf, len);
}
strbuf_release(&payload);
Expand Down
8 changes: 4 additions & 4 deletions rebase-interactive.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ void append_todo_help(int command_count,

if (!edit_todo) {
strbuf_addch(buf, '\n');
strbuf_commented_addf(buf, comment_line_str,
strbuf_comment_addf(buf,
Q_("Rebase %s onto %s (%d command)",
"Rebase %s onto %s (%d commands)",
command_count),
shortrevisions, shortonto, command_count);
}

strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_str);
strbuf_add_comment_lines(buf, msg, strlen(msg));

if (get_missing_commit_check_level() == MISSING_COMMIT_CHECK_ERROR)
msg = _("\nDo not remove any line. Use 'drop' "
Expand All @@ -89,7 +89,7 @@ void append_todo_help(int command_count,
msg = _("\nIf you remove a line here "
"THAT COMMIT WILL BE LOST.\n");

strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_str);
strbuf_add_comment_lines(buf, msg, strlen(msg));

if (edit_todo)
msg = _("\nYou are editing the todo file "
Expand All @@ -100,7 +100,7 @@ void append_todo_help(int command_count,
msg = _("\nHowever, if you remove everything, "
"the rebase will be aborted.\n\n");

strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_str);
strbuf_add_comment_lines(buf, msg, strlen(msg));
}

int edit_todo_list(struct repository *r, struct replay_opts *opts,
Expand Down
16 changes: 7 additions & 9 deletions sequencer.c
Original file line number Diff line number Diff line change
Expand Up @@ -732,11 +732,11 @@ void append_conflicts_hint(struct index_state *istate,
}

strbuf_addch(msgbuf, '\n');
strbuf_commented_addf(msgbuf, comment_line_str, "Conflicts:\n");
strbuf_comment_addf(msgbuf, "Conflicts:\n");
for (i = 0; i < istate->cache_nr;) {
const struct cache_entry *ce = istate->cache[i++];
if (ce_stage(ce)) {
strbuf_commented_addf(msgbuf, comment_line_str,
strbuf_comment_addf(msgbuf,
"\t%s\n", ce->name);
while (i < istate->cache_nr &&
!strcmp(ce->name, istate->cache[i]->name))
Expand Down Expand Up @@ -1912,7 +1912,7 @@ static int is_fixup_flag(enum todo_command command, unsigned flag)
}

/*
* Wrapper around strbuf_add_commented_lines() which avoids double
* Wrapper around strbuf_add_comment_lines() which avoids double
* commenting commit subjects.
*/
static void add_commented_lines(struct strbuf *buf, const void *str, size_t len)
Expand All @@ -1929,7 +1929,7 @@ static void add_commented_lines(struct strbuf *buf, const void *str, size_t len)
s += count;
len -= count;
}
strbuf_add_commented_lines(buf, s, len, comment_line_str);
strbuf_add_comment_lines(buf, s, len);
}

/* Does the current fixup chain contain a squash command? */
Expand Down Expand Up @@ -2029,7 +2029,7 @@ static int append_squash_message(struct strbuf *buf, const char *body,
strbuf_addf(buf, _(nth_commit_msg_fmt),
++ctx->current_fixup_count + 1);
strbuf_addstr(buf, "\n\n");
strbuf_add_commented_lines(buf, body, commented_len, comment_line_str);
strbuf_add_comment_lines(buf, body, commented_len);
/* buf->buf may be reallocated so store an offset into the buffer */
fixup_off = buf->len;
strbuf_addstr(buf, body + commented_len);
Expand Down Expand Up @@ -2120,8 +2120,7 @@ static int update_squash_messages(struct repository *r,
_(first_commit_msg_str));
strbuf_addstr(&buf, "\n\n");
if (is_fixup_flag(command, flag))
strbuf_add_commented_lines(&buf, body, strlen(body),
comment_line_str);
strbuf_add_comment_lines(&buf, body, strlen(body));
else
strbuf_addstr(&buf, body);

Expand All @@ -2140,8 +2139,7 @@ static int update_squash_messages(struct repository *r,
strbuf_addf(&buf, _(skip_nth_commit_msg_fmt),
++ctx->current_fixup_count + 1);
strbuf_addstr(&buf, "\n\n");
strbuf_add_commented_lines(&buf, body, strlen(body),
comment_line_str);
strbuf_add_comment_lines(&buf, body, strlen(body));
} else
return error(_("unknown command: %d"), command);
repo_unuse_commit_buffer(r, commit, message);
Expand Down
19 changes: 13 additions & 6 deletions strbuf.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
#include "git-compat-util.h"
#include "environment.h"
#include "gettext.h"
#include "hex-ll.h"
#include "strbuf.h"
#include "string-list.h"
#include "utf8.h"
#include "date.h"

/*
* We do not need the_repository at all, but the comment_line_str
* variable is hidden behind USE_THE_REPOSITORY_VARIABLE CPP macro.
* We could #define it before including "envirnoment.h". Or we can
* make an external declaration ourselves here.
*/
extern const char *comment_line_str;

int starts_with(const char *str, const char *prefix)
{
for (; ; str++, prefix++)
Expand Down Expand Up @@ -385,14 +394,12 @@ static void add_lines(struct strbuf *out,
strbuf_complete_line(out);
}

void strbuf_add_commented_lines(struct strbuf *out, const char *buf,
size_t size, const char *comment_prefix)
void strbuf_add_comment_lines(struct strbuf *out, const char *buf, size_t size)
{
add_lines(out, comment_prefix, buf, size, 1);
add_lines(out, comment_line_str, buf, size, 1);
}

void strbuf_commented_addf(struct strbuf *sb, const char *comment_prefix,
const char *fmt, ...)
void strbuf_comment_addf(struct strbuf *sb, const char *fmt, ...)
{
va_list params;
struct strbuf buf = STRBUF_INIT;
Expand All @@ -402,7 +409,7 @@ void strbuf_commented_addf(struct strbuf *sb, const char *comment_prefix,
strbuf_vaddf(&buf, fmt, params);
va_end(params);

strbuf_add_commented_lines(sb, buf.buf, buf.len, comment_prefix);
strbuf_add_comment_lines(sb, buf.buf, buf.len);
if (incomplete_line)
sb->buf[--sb->len] = '\0';

Expand Down
13 changes: 5 additions & 8 deletions strbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,9 @@ void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,

/**
* Add a NUL-terminated string to the buffer. Each line will be prepended
* by a comment character and a blank.
* by the comment character and a blank.
*/
void strbuf_add_commented_lines(struct strbuf *out,
const char *buf, size_t size,
const char *comment_prefix);

void strbuf_add_comment_lines(struct strbuf *out, const char *buf, size_t size);

/**
* Add data of given length to the buffer.
Expand Down Expand Up @@ -388,11 +385,11 @@ __attribute__((format (printf,2,3)))
void strbuf_addf(struct strbuf *sb, const char *fmt, ...);

/**
* Add a formatted string prepended by a comment character and a
* Add a formatted string prepended by the comment character and a
* blank to the buffer.
*/
__attribute__((format (printf, 3, 4)))
void strbuf_commented_addf(struct strbuf *sb, const char *comment_prefix, const char *fmt, ...);
__attribute__((format (printf,2,3)))
void strbuf_comment_addf(struct strbuf *sb, const char *fmt, ...);

__attribute__((format (printf,2,0)))
void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap);
Expand Down
6 changes: 3 additions & 3 deletions wt-status.c
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,7 @@ static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncom
if (s->display_comment_prefix) {
size_t len;
summary_content = strbuf_detach(&summary, &len);
strbuf_add_commented_lines(&summary, summary_content, len, comment_line_str);
strbuf_add_comment_lines(&summary, summary_content, len);
free(summary_content);
}

Expand Down Expand Up @@ -1112,8 +1112,8 @@ void wt_status_append_cut_line(struct strbuf *buf)
{
const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored.");

strbuf_commented_addf(buf, comment_line_str, "%s", cut_line);
strbuf_add_commented_lines(buf, explanation, strlen(explanation), comment_line_str);
strbuf_comment_addf(buf, "%s", cut_line);
strbuf_add_comment_lines(buf, explanation, strlen(explanation));
}

void wt_status_add_cut_line(struct wt_status *s)
Expand Down

0 comments on commit cf50d3d

Please sign in to comment.