Avoid passing global comment_line_char repeatedly
cover Avoid passing global comment_line_char repeatedly
Two strbuf functions used to produce commented lines take the comment_line_char as their parameter, but in practice, all callers feed the global variable comment_line_char from environment.[ch].
Dropping the parameter from the callchain will make the interface less flexible, and less error prone. If we choose to change the implementation of the customizable comment line character (e.g., we may want to stop referencing the global variable and instead use a getter function), we will have fewer places we need to modify.
Junio C Hamano (2):
strbuf_commented_addf(): drop the comment_line_char parameter strbuf_add_commented_lines(): drop the comment_line_char parameter
add-patch.c | 8 ++++---- builtin/branch.c | 2 +- builtin/merge.c | 8 ++++---- builtin/notes.c | 9 ++++----- builtin/stripspace.c | 2 +- builtin/tag.c | 4 ++-- fmt-merge-msg.c | 9 +++------ rebase-interactive.c | 8 ++++---- sequencer.c | 14 ++++++-------- strbuf.c | 9 +++++---- strbuf.h | 7 +++---- wt-status.c | 6 +++--- 12 files changed, 40 insertions(+), 46 deletions(-)
Signature · 1 line
2.42.0-526-g3130c155df
PATCH 1/2 strbuf_commented_addf(): drop the comment_line_char parameter
All the callers of this function supply the global variable comment_line_char as an argument to its second parameter. Remove the parameter to allow us in the future to change the reference to the global variable with something else, like a function call.
--- add-patch.c | 8 ++++---- builtin/branch.c | 2 +- builtin/merge.c | 8 ++++---- builtin/tag.c | 4 ++-- rebase-interactive.c | 2 +- sequencer.c | 4 ++-- strbuf.c | 3 ++- strbuf.h | 4 ++-- wt-status.c | 2 +- 9 files changed, 19 insertions(+), 18 deletions(-)
add-patch.c +4 -4
@@ -1106,11 +1106,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_char, +strbuf_commented_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_char, + strbuf_commented_addf(&s->buf, _("---\n" "To remove '%c' lines, make them ' ' lines " "(context).\n"
@@ -1119,13 +1119,13 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk) s->mode->is_reverse ?'+' :'-' , s->mode->is_reverse ?'-' :'+' , comment_line_char); -strbuf_commented_addf (&s->buf, comment_line_char,"%s" , +strbuf_commented_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_char, + strbuf_commented_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 "
builtin/branch.c +1 -1
@@ -668,7 +668,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_char, + strbuf_commented_addf(&buf, _("Please edit the description for the branch\n" " %s\n" "Lines starting with '%c' will be stripped.\n"),
builtin/merge.c +4 -4
@@ -857,15 +857,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_char,"\n" ); +strbuf_commented_addf (&msg,"\n" ); } -strbuf_commented_addf (&msg, comment_line_char, +strbuf_commented_addf (&msg,_ (merge_editor_comment));if (cleanup_mode ==COMMIT_MSG_CLEANUP_SCISSORS ) -strbuf_commented_addf (&msg, comment_line_char, +strbuf_commented_addf (&msg,_ (scissors_editor_comment));else -strbuf_commented_addf (&msg, comment_line_char, +strbuf_commented_addf (&msg,_ (no_scissors_editor_comment), comment_line_char); }if (signoff)
builtin/tag.c +2 -2
@@ -314,10 +314,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_char, +strbuf_commented_addf (&buf,_ (tag_template), tag, comment_line_char);else -strbuf_commented_addf (&buf, comment_line_char, +strbuf_commented_addf (&buf,_ (tag_template_nocleanup), tag, comment_line_char);write_or_die (fd, buf.buf, buf.len);strbuf_release (&buf);
rebase-interactive.c +1 -1
@@ -71,7 +71,7 @@ void append_todo_help(int command_count,if (!edit_todo) {strbuf_addch (buf,'\n' ); - strbuf_commented_addf(buf, comment_line_char, + strbuf_commented_addf(buf, Q_("Rebase %s onto %s (%d command)", "Rebase %s onto %s (%d commands)", command_count),
sequencer.c +2 -2
@@ -675,11 +675,11 @@ void append_conflicts_hint(struct index_state *istate, }strbuf_addch (msgbuf,'\n' ); -strbuf_commented_addf (msgbuf, comment_line_char,"Conflicts:\n" ); +strbuf_commented_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_char, +strbuf_commented_addf (msgbuf,"\t%s\n" , ce->name);while (i < istate->cache_nr && !strcmp (ce->name, istate->cache[i]->name))
strbuf.c +2 -1
@@ -1,4 +1,5 @@#include "git-compat-util.h" +#include "environment.h" #include "gettext.h" #include "hex-ll.h" #include "strbuf.h"
@@ -372,7 +373,7 @@ void strbuf_add_commented_lines(struct strbuf *out, const char *buf,add_lines (out, prefix1, prefix2, buf, size); } -void strbuf_commented_addf (struct strbuf *sb,char comment_line_char, +void strbuf_commented_addf (struct strbuf *sb,const char *fmt, ...) {va_list params;
strbuf.h +2 -2
@@ -378,8 +378,8 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...);* Add a formatted string prepended by a comment character and a * blank to the buffer. */ -__attribute__((format (printf,3 ,4 ))) -void strbuf_commented_addf (struct strbuf *sb,char comment_line_char,const char *fmt, ...); +__attribute__((format (printf,2 ,3 ))) +void strbuf_commented_addf (struct strbuf *sb,const char *fmt, ...); __attribute__((format (printf,2 ,0 )))void strbuf_vaddf (struct strbuf *sb,const char *fmt,va_list ap);
wt-status.c +1 -1
@@ -1102,7 +1102,7 @@ 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_char,"%s" , cut_line); +strbuf_commented_addf (buf,"%s" , cut_line);strbuf_add_commented_lines (buf, explanation,strlen (explanation), comment_line_char); }
Signature · 1 line
2.42.0-526-g3130c155df
PATCH 2/2 strbuf_add_commented_lines(): drop the comment_line_char parameter
In reply to Junio C HamanoAll the callers of this function supply the global variable comment_line_char as an argument to its last parameter. Remove the parameter to allow us in the future to change the reference to the global variable with something else, like a function call.
--- builtin/notes.c | 9 ++++----- builtin/stripspace.c | 2 +- fmt-merge-msg.c | 9 +++------ rebase-interactive.c | 6 +++--- sequencer.c | 10 ++++------ strbuf.c | 6 +++--- strbuf.h | 3 +-- wt-status.c | 4 ++-- 8 files changed, 21 insertions(+), 28 deletions(-)
builtin/notes.c +4 -5
@@ -181,7 +181,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_char); +strbuf_add_commented_lines (&cbuf, buf.buf, buf.len);write_or_die (fd, cbuf.buf, cbuf.len);strbuf_release (&cbuf);
@@ -209,10 +209,9 @@ 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_char); -strbuf_add_commented_lines (&buf,_ (note_template),strlen (_ (note_template)), - comment_line_char); -strbuf_add_commented_lines (&buf,"\n" ,strlen ("\n" ), comment_line_char); +strbuf_add_commented_lines (&buf,"\n" ,strlen ("\n" )); +strbuf_add_commented_lines (&buf,_ (note_template),strlen (_ (note_template))); +strbuf_add_commented_lines (&buf,"\n" ,strlen ("\n" ));write_or_die (fd, buf.buf, buf.len);write_commented_object (fd, object);
builtin/stripspace.c +1 -1
@@ -13,7 +13,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_char); +strbuf_add_commented_lines (buf, msg, len);free (msg); }
fmt-merge-msg.c +3 -6
@@ -509,8 +509,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_char); +strbuf_add_commented_lines (tagbuf, sig->buf, sig->len); } }
@@ -556,8 +555,7 @@ static void fmt_merge_msg_sigs(struct strbuf *out)strbuf_addch (&tagline,'\n' );strbuf_add_commented_lines (&tagline, origins.items[first_tag].string, -strlen (origins.items[first_tag].string), - comment_line_char); +strlen (origins.items[first_tag].string));strbuf_insert (&tagbuf,0 , tagline.buf, tagline.len);strbuf_release (&tagline);
@@ -565,8 +563,7 @@ static void fmt_merge_msg_sigs(struct strbuf *out)strbuf_addch (&tagbuf,'\n' );strbuf_add_commented_lines (&tagbuf, origins.items[i].string, -strlen (origins.items[i].string), - comment_line_char); +strlen (origins.items[i].string));fmt_tag_signature (&tagbuf, &sig, buf, len); }strbuf_release (&payload);
rebase-interactive.c +3 -3
@@ -78,7 +78,7 @@ void append_todo_help(int command_count, shortrevisions, shortonto, command_count); } -strbuf_add_commented_lines (buf, msg,strlen (msg), comment_line_char); +strbuf_add_commented_lines (buf, msg,strlen (msg));if (get_missing_commit_check_level () ==MISSING_COMMIT_CHECK_ERROR ) msg = _("\nDo not remove any line. Use 'drop' "
@@ -87,7 +87,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_char); +strbuf_add_commented_lines (buf, msg,strlen (msg));if (edit_todo) msg = _("\nYou are editing the todo file "
@@ -98,7 +98,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_char); +strbuf_add_commented_lines (buf, msg,strlen (msg)); } int edit_todo_list(struct repository *r, struct todo_list *todo_list,
sequencer.c +4 -6
@@ -1859,7 +1859,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_char); +strbuf_add_commented_lines (buf, s, len); }/* Does the current fixup chain contain a squash command? */
@@ -1958,7 +1958,7 @@ static int append_squash_message(struct strbuf *buf, const char *body,strbuf_addf (buf,_ (nth_commit_msg_fmt), ++opts->current_fixup_count +1 );strbuf_addstr (buf,"\n\n" ); -strbuf_add_commented_lines (buf, body, commented_len, comment_line_char); +strbuf_add_commented_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);
@@ -2048,8 +2048,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_char); +strbuf_add_commented_lines (&buf, body,strlen (body));else strbuf_addstr (&buf, body);
@@ -2068,8 +2067,7 @@ static int update_squash_messages(struct repository *r,strbuf_addf (&buf,_ (skip_nth_commit_msg_fmt), ++opts->current_fixup_count +1 );strbuf_addstr (&buf,"\n\n" ); -strbuf_add_commented_lines (&buf, body,strlen (body), - comment_line_char); +strbuf_add_commented_lines (&buf, body,strlen (body)); }else return error (_ ("unknown command: %d" ), command);repo_unuse_commit_buffer (r, commit, message);
strbuf.c +3 -3
@@ -360,8 +360,8 @@ 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,char comment_line_char) +void strbuf_add_commented_lines (struct strbuf *out, +const char *buf,size_t size) {static char prefix1[3 ];static char prefix2[2 ];
@@ -384,7 +384,7 @@ void strbuf_commented_addf(struct strbuf *sb,strbuf_vaddf (&buf, fmt, params);va_end (params); -strbuf_add_commented_lines (sb, buf.buf, buf.len, comment_line_char); +strbuf_add_commented_lines (sb, buf.buf, buf.len);if (incomplete_line) sb->buf[--sb->len] ='\0' ;
strbuf.h +1 -2
@@ -287,8 +287,7 @@ void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,* by a comment character and a blank. */ void strbuf_add_commented_lines (struct strbuf *out, -const char *buf,size_t size, -char comment_line_char); +const char *buf,size_t size);/**
wt-status.c +2 -2
@@ -1027,7 +1027,7 @@ static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncomif (s->display_comment_prefix) {size_t len; summary_content =strbuf_detach (&summary, &len); -strbuf_add_commented_lines (&summary, summary_content, len, comment_line_char); +strbuf_add_commented_lines (&summary, summary_content, len);free (summary_content); }
@@ -1103,7 +1103,7 @@ 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,"%s" , cut_line); -strbuf_add_commented_lines (buf, explanation,strlen (explanation), comment_line_char); +strbuf_add_commented_lines (buf, explanation,strlen (explanation)); }void wt_status_add_cut_line (FILE *fp)
Signature · 1 line
2.42.0-526-g3130c155df
Quoted 10 lines · Junio C Hamano
Two strbuf functions used to produce commented lines take the
comment_line_charas their parameter, but in practice, all callers feed the global variablecomment_line_charfrom environment.[ch].Dropping the parameter from the callchain will make the interface less flexible, and less error prone. If we choose to change the implementation of the customizable comment line character (e.g., we may want to stop referencing the global variable and instead use a getter function), we will have fewer places we need to modify.
Quoted 3 lines
Junio C Hamano (2):
strbuf_commented_addf(): drop thecomment_line_charparameterstrbuf_add_commented_lines(): drop thecomment_line_charparameter
This series looks good to me. It removes unnecessary complexity that provided pretty much no value.
Quoted 13 lines
add-patch.c | 8 ++++---- builtin/branch.c | 2 +- builtin/merge.c | 8 ++++---- builtin/notes.c | 9 ++++----- builtin/stripspace.c | 2 +- builtin/tag.c | 4 ++-- fmt-merge-msg.c | 9 +++------ rebase-interactive.c | 8 ++++---- sequencer.c | 14 ++++++-------- strbuf.c | 9 +++++---- strbuf.h | 7 +++---- wt-status.c | 6 +++--- 12 files changed, 40 insertions(+), 46 deletions(-)
Hi Junio
Quoted 9 lines · Junio C Hamano
Two strbuf functions used to produce commented lines take the
comment_line_charas their parameter, but in practice, all callers feed the global variablecomment_line_charfrom environment.[ch].Dropping the parameter from the callchain will make the interface less flexible, and less error prone. If we choose to change the implementation of the customizable comment line character (e.g., we may want to stop referencing the global variable and instead use a getter function), we will have fewer places we need to modify.
While I agree with your reasoning here, I think that parameter was recently added as part of the libification effort - I can't remember exactly why and am too lazy to look it up so I've cc'd Calvin and Johathan instead.
Best Wishes
Phillip
Quoted 18 lines
Junio C Hamano (2):
strbuf_commented_addf(): drop thecomment_line_charparameterstrbuf_add_commented_lines(): drop thecomment_line_charparameteradd-patch.c | 8 ++++---- builtin/branch.c | 2 +- builtin/merge.c | 8 ++++---- builtin/notes.c | 9 ++++----- builtin/stripspace.c | 2 +- builtin/tag.c | 4 ++-- fmt-merge-msg.c | 9 +++------ rebase-interactive.c | 8 ++++---- sequencer.c | 14 ++++++-------- strbuf.c | 9 +++++---- strbuf.h | 7 +++---- wt-status.c | 6 +++--- 12 files changed, 40 insertions(+), 46 deletions(-)
cover Avoid passing global comment_line_char repeatedly
Quoted 4 lines
While I agree with your reasoning here, I think that parameter was recently added as part of the libification effort - I can't remember exactly why and am too lazy to look it up so I've cc'd Calvin and Johathan instead.
Thanks Phillip for noticing this. Putting on my libification hat, this was probably because we wanted to remove strbuf's dependency on environment, so that we wouldn't need to include it in git-std-lib. If we were to merge these patches, libification would probably still be doable if we stubbed the global comment_line_char.
Removing my libification hat, I think it's better to solve this issue by moving the functions into environment.{c,h} instead, following the example of functions like strbuf_worktree_ref() in worktree.h and strbuf_utf8_align() in utf8.h that, when operating on both strbuf and a specific domain, are placed in the domain's header file, not in strbuf.h. This avoids a situation in which strbuf.h contains everything string-related.
The main issue with this is that by not centralizing all strbuf-related functionality, some strbuf-related helper functions that could have been private now need to be made public, but I think that a similar issue would be faced if we don't centralize, say, all environment-related functionality (some environment-related helper functions would have to be made public, although I didn't encounter this problem with this patch set).
I've attached some patches to illustrate what I've described above.
Jonathan Tan (1):
strbuf: make add_lines() public
Junio C Hamano (2):
strbuf_commented_addf(): drop the comment_line_char parameter strbuf_add_commented_lines(): drop the comment_line_char parameter
add-patch.c | 8 ++--- branch.c | 3 +- builtin/branch.c | 2 +- builtin/merge.c | 8 ++--- builtin/notes.c | 9 +++--- builtin/stripspace.c | 2 +- builtin/tag.c | 4 +-- commit.c | 2 +- environment.c | 31 ++++++++++++++++++++ environment.h | 14 +++++++++ fmt-merge-msg.c | 9 ++---- rebase-interactive.c | 8 ++--- sequencer.c | 14 ++++----- strbuf.c | 69 ++++++++++---------------------------------- strbuf.h | 19 ++---------- wt-status.c | 6 ++-- 16 files changed, 98 insertions(+), 110 deletions(-)
Signature · 1 line
2.42.0.820.g83a721a137-goog
RFC PATCH 1/3 strbuf: make add_lines() public
Subsequent patches will require the ability to add different prefixes to different lines (depending on their contents), so make this functionality available from outside strbuf.c.
--- branch.c | 3 ++- commit.c | 2 +- strbuf.c | 39 ++++++++++++++++----------------------- strbuf.h | 3 ++- 4 files changed, 21 insertions(+), 26 deletions(-)
branch.c +2 -1
@@ -721,7 +721,8 @@ static int submodule_create_branch(struct repository *r,return ret; ret =finish_command (&child);strbuf_read (&child_err, child.err,0 ); -strbuf_add_lines (&out_buf, out_prefix, child_err.buf, child_err.len); +strbuf_add_lines (&out_buf, out_prefix, out_prefix, + child_err.buf, child_err.len);if (ret)fprintf (stderr,"%s" , out_buf.buf);
commit.c +1 -1
@@ -1361,7 +1361,7 @@ static void add_extra_header(struct strbuf *buffer, {strbuf_addstr (buffer, extra->key);if (extra->len) -strbuf_add_lines (buffer," " , extra->value, extra->len); +strbuf_add_lines (buffer," " ," " , extra->value, extra->len);else strbuf_addch (buffer,'\n' ); }
strbuf.c +16 -23
@@ -339,26 +339,6 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...)va_end (ap); } -static void add_lines (struct strbuf *out, -const char *prefix1, -const char *prefix2, -const char *buf,size_t size) -{ -while (size) { -const char *prefix; -const char *next =memchr (buf,'\n' , size); - next = next ? (next +1 ) : (buf + size); - - prefix = ((prefix2 && (buf[0 ] =='\n' || buf[0 ] =='\t' )) - ? prefix2 : prefix1); -strbuf_addstr (out, prefix); -strbuf_add (out, buf, next - buf); - size -= next - buf; - buf = next; - } -strbuf_complete_line (out); -} -void strbuf_add_commented_lines (struct strbuf *out,const char *buf,size_t size,char comment_line_char) {
@@ -369,7 +349,7 @@ void strbuf_add_commented_lines(struct strbuf *out, const char *buf,xsnprintf (prefix1,sizeof (prefix1),"%c " , comment_line_char);xsnprintf (prefix2,sizeof (prefix2),"%c" , comment_line_char); } -add_lines (out, prefix1, prefix2, buf, size); +strbuf_add_lines (out, prefix1, prefix2, buf, size); } void strbuf_commented_addf(struct strbuf *sb, char comment_line_char,
@@ -747,10 +727,23 @@ ssize_t strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)return len; } -void strbuf_add_lines (struct strbuf *out,const char *prefix, +void strbuf_add_lines (struct strbuf *out,const char *default_prefix, +const char *tab_or_nl_prefix,const char *buf,size_t size) { -add_lines (out, prefix,NULL , buf, size); +while (size) { +const char *prefix; +const char *next =memchr (buf,'\n' , size); + next = next ? (next +1 ) : (buf + size); + + prefix = (buf[0 ] =='\n' || buf[0 ] =='\t' ) + ? tab_or_nl_prefix : default_prefix; +strbuf_addstr (out, prefix); +strbuf_add (out, buf, next - buf); + size -= next - buf; + buf = next; + } +strbuf_complete_line (out); }void strbuf_addstr_xml_quoted (struct strbuf *buf,const char *s)
strbuf.h +2 -1
@@ -599,7 +599,8 @@ void strbuf_list_free(struct strbuf **list);void strbuf_strip_file_from_path (struct strbuf *sb);void strbuf_add_lines (struct strbuf *sb, -const char *prefix, +const char *default_prefix, +const char *tab_or_nl_prefix,const char *buf,size_t size);
Signature · 1 line
2.42.0.820.g83a721a137-goog
Re: [RFC PATCH 1/3] strbuf: make add_lines() public
Quoted 3 lines · Jonathan Tan
Subsequent patches will require the ability to add different prefixes to different lines (depending on their contents), so make this functionality available from outside
strbuf.c.
I do not think it is a good idea to force almost everybody to repeat themselves. As we can see here, all but just a single caller of strbuf_add_lines() with this patch pass the same prefix for both parameters. If we need to make the current strbuf.c:add_lines() also available to some specific callers, that is fine, but let's keep the simpler version that almost everybody uses as-is, and give the more complex and featureful one that is used only by selected callers a longer and more cumbersome name.
Thanks.
Re: [RFC PATCH 1/3] strbuf: make add_lines() public
Quoted 15 lines · Junio C Hamano
Quoted 3 lines · Jonathan Tan
Subsequent patches will require the ability to add different prefixes to different lines (depending on their contents), so make this functionality available from outside
strbuf.c.I do not think it is a good idea to force almost everybody to repeat themselves. As we can see here, all but just a single caller of
strbuf_add_lines()with this patch pass the same prefix for both parameters. If we need to make the current strbuf.c:add_lines() also available to some specific callers, that is fine, but let's keep the simpler version that almost everybody uses as-is, and give the more complex and featureful one that is used only by selected callers a longer and more cumbersome name.
Quoted 1 line
Thanks.
Another practical downside of this patch is that it breaks other in-flight topics that adds new users of strbuf_add_lines(), and that breakage is totally unnecessary.
RFC PATCH 2/3 strbuf_commented_addf(): drop the comment_line_char parameter
In reply to Jonathan TanFrom: Junio C Hamano <[email protected]>
All the callers of this function supply the global variable comment_line_char as an argument to its second parameter. Remove the parameter to allow us in the future to change the reference to the global variable with something else, like a function call.
--- add-patch.c | 8 ++++---- builtin/branch.c | 2 +- builtin/merge.c | 8 ++++---- builtin/tag.c | 4 ++-- environment.c | 18 ++++++++++++++++++ environment.h | 7 +++++++ rebase-interactive.c | 2 +- sequencer.c | 4 ++-- strbuf.c | 19 +------------------ strbuf.h | 7 ------- wt-status.c | 2 +- 11 files changed, 41 insertions(+), 40 deletions(-)
add-patch.c +4 -4
@@ -1106,11 +1106,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_char, +strbuf_commented_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_char, + strbuf_commented_addf(&s->buf, _("---\n" "To remove '%c' lines, make them ' ' lines " "(context).\n"
@@ -1119,13 +1119,13 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk) s->mode->is_reverse ?'+' :'-' , s->mode->is_reverse ?'-' :'+' , comment_line_char); -strbuf_commented_addf (&s->buf, comment_line_char,"%s" , +strbuf_commented_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_char, + strbuf_commented_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 "
builtin/branch.c +1 -1
@@ -668,7 +668,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_char, + strbuf_commented_addf(&buf, _("Please edit the description for the branch\n" " %s\n" "Lines starting with '%c' will be stripped.\n"),
builtin/merge.c +4 -4
@@ -857,15 +857,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_char,"\n" ); +strbuf_commented_addf (&msg,"\n" ); } -strbuf_commented_addf (&msg, comment_line_char, +strbuf_commented_addf (&msg,_ (merge_editor_comment));if (cleanup_mode ==COMMIT_MSG_CLEANUP_SCISSORS ) -strbuf_commented_addf (&msg, comment_line_char, +strbuf_commented_addf (&msg,_ (scissors_editor_comment));else -strbuf_commented_addf (&msg, comment_line_char, +strbuf_commented_addf (&msg,_ (no_scissors_editor_comment), comment_line_char); }if (signoff)
builtin/tag.c +2 -2
@@ -314,10 +314,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_char, +strbuf_commented_addf (&buf,_ (tag_template), tag, comment_line_char);else -strbuf_commented_addf (&buf, comment_line_char, +strbuf_commented_addf (&buf,_ (tag_template_nocleanup), tag, comment_line_char);write_or_die (fd, buf.buf, buf.len);strbuf_release (&buf);
environment.c +18 -0
@@ -416,3 +416,21 @@ int print_sha1_ellipsis(void) }return cached_result; } + +void strbuf_commented_addf (struct strbuf *sb, +const char *fmt, ...) +{ +va_list params; +struct strbuf buf =STRBUF_INIT ; +int incomplete_line = sb->len && sb->buf[sb->len -1 ] !='\n' ; + +va_start (params, fmt); +strbuf_vaddf (&buf, fmt, params); +va_end (params); + +strbuf_add_commented_lines (sb, buf.buf, buf.len, comment_line_char); +if (incomplete_line) + sb->buf[--sb->len] ='\0' ; + +strbuf_release (&buf); +}
environment.h +7 -0
@@ -229,4 +229,11 @@ extern const char *excludes_file;*/ int print_sha1_ellipsis (void ); +/** +* Add a formatted string prepended by a comment character and a +* blank to the buffer. +*/ +__attribute__((format (printf,2 ,3 ))) +void strbuf_commented_addf (struct strbuf *sb,const char *fmt, ...); +#endif
rebase-interactive.c +1 -1
@@ -71,7 +71,7 @@ void append_todo_help(int command_count,if (!edit_todo) {strbuf_addch (buf,'\n' ); - strbuf_commented_addf(buf, comment_line_char, + strbuf_commented_addf(buf, Q_("Rebase %s onto %s (%d command)", "Rebase %s onto %s (%d commands)", command_count),
sequencer.c +2 -2
@@ -675,11 +675,11 @@ void append_conflicts_hint(struct index_state *istate, }strbuf_addch (msgbuf,'\n' ); -strbuf_commented_addf (msgbuf, comment_line_char,"Conflicts:\n" ); +strbuf_commented_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_char, +strbuf_commented_addf (msgbuf,"\t%s\n" , ce->name);while (i < istate->cache_nr && !strcmp (ce->name, istate->cache[i]->name))
strbuf.c +1 -18
@@ -1,4 +1,5 @@#include "git-compat-util.h" +#include "environment.h" #include "gettext.h" #include "hex-ll.h" #include "strbuf.h"
@@ -352,24 +353,6 @@ void strbuf_add_commented_lines(struct strbuf *out, const char *buf,strbuf_add_lines (out, prefix1, prefix2, buf, size); } -void strbuf_commented_addf (struct strbuf *sb,char comment_line_char, -const char *fmt, ...) -{ -va_list params; -struct strbuf buf =STRBUF_INIT ; -int incomplete_line = sb->len && sb->buf[sb->len -1 ] !='\n' ; - -va_start (params, fmt); -strbuf_vaddf (&buf, fmt, params); -va_end (params); - -strbuf_add_commented_lines (sb, buf.buf, buf.len, comment_line_char); -if (incomplete_line) - sb->buf[--sb->len] ='\0' ; - -strbuf_release (&buf); -} -void strbuf_vaddf (struct strbuf *sb,const char *fmt,va_list ap) {int len;
strbuf.h +0 -7
@@ -374,13 +374,6 @@ void strbuf_humanise_rate(struct strbuf *buf, off_t bytes); __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 -* blank to the buffer. -*/ -__attribute__((format (printf,3 ,4 ))) -void strbuf_commented_addf (struct strbuf *sb,char comment_line_char,const char *fmt, ...); - __attribute__((format (printf,2 ,0 )))void strbuf_vaddf (struct strbuf *sb,const char *fmt,va_list ap);
wt-status.c +1 -1
@@ -1102,7 +1102,7 @@ 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_char,"%s" , cut_line); +strbuf_commented_addf (buf,"%s" , cut_line);strbuf_add_commented_lines (buf, explanation,strlen (explanation), comment_line_char); }
Signature · 1 line
2.42.0.820.g83a721a137-goog
Re: [RFC PATCH 2/3] strbuf_commented_addf(): drop the comment_line_char parameter
Quoted 27 lines · Jonathan Tan
From: Junio C Hamano <[email protected]>
All the callers of this function supply the global variable
comment_line_charas an argument to its second parameter. Remove the parameter to allow us in the future to change the reference to the global variable with something else, like a function call.Signed-off-by Junio C Hamano <[email protected]>Signed-off-by Jonathan Tan <[email protected]>---
environment.c +8
-0@@ -416,3 +416,21 @@ int print_sha1_ellipsis(void) }return cached_result; } + +void strbuf_commented_addf (struct strbuf *sb, +const char *fmt, ...) +{ +va_list params; +struct strbuf buf =STRBUF_INIT ; +int incomplete_line = sb->len && sb->buf[sb->len -1 ] !='\n' ; +
environment.c int print_sha1_ellipsis(void) · line 436
Quoted 10 lines
environment.c +10
-0+va_start (params, fmt); +strbuf_vaddf (&buf, fmt, params); +va_end (params); + +strbuf_add_commented_lines (sb, buf.buf, buf.len, comment_line_char); +if (incomplete_line) + sb->buf[--sb->len] ='\0' ; + +strbuf_release (&buf); +}
This moving of the helper function does not belong to the "fix commented_addf() not to take the comment_line_char" step.
The series should be restructured to have the two patches from me first, and then your moving some stuff to environment.c, probably.
Re: [RFC PATCH 2/3] strbuf_commented_addf(): drop the comment_line_char parameter
Quoted 5 lines · Junio C Hamano
This moving of the helper function does not belong to the "fix
commented_addf()not to take thecomment_line_char" step.The series should be restructured to have the two patches from me first, and then your moving some stuff to
environment.c, probably.
This means that #include "environment.h" will be added and then removed in the same series, but I don't feel too strongly about that. I'll send an updated set of patches.
Re: [RFC PATCH 2/3] strbuf_commented_addf(): drop the comment_line_char parameter
Quoted 9 lines · Jonathan Tan
Quoted 5 lines · Junio C Hamano
This moving of the helper function does not belong to the "fix
commented_addf()not to take thecomment_line_char" step.The series should be restructured to have the two patches from me first, and then your moving some stuff to
environment.c, probably.This means that #include "
environment.h" will be added and then removed in the same series,
I do prefer it that way, because that is exactly what we are doing. First we fix the duplicated parameter in the API by relying on the global, and then we move things around to hide the dependence on the global.
Thanks.
RFC PATCH 3/3 strbuf_add_commented_lines(): drop the comment_line_char parameter
In reply to Jonathan TanFrom: Junio C Hamano <[email protected]>
All the callers of this function supply the global variable comment_line_char as an argument to its last parameter. Remove the parameter to allow us in the future to change the reference to the global variable with something else, like a function call.
--- builtin/notes.c | 9 ++++----- builtin/stripspace.c | 2 +- environment.c | 15 ++++++++++++++- environment.h | 7 +++++++ fmt-merge-msg.c | 9 +++------ rebase-interactive.c | 6 +++--- sequencer.c | 10 ++++------ strbuf.c | 13 ------------- strbuf.h | 9 --------- wt-status.c | 4 ++-- 10 files changed, 38 insertions(+), 46 deletions(-)
builtin/notes.c +4 -5
@@ -181,7 +181,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_char); +strbuf_add_commented_lines (&cbuf, buf.buf, buf.len);write_or_die (fd, cbuf.buf, cbuf.len);strbuf_release (&cbuf);
@@ -209,10 +209,9 @@ 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_char); -strbuf_add_commented_lines (&buf,_ (note_template),strlen (_ (note_template)), - comment_line_char); -strbuf_add_commented_lines (&buf,"\n" ,strlen ("\n" ), comment_line_char); +strbuf_add_commented_lines (&buf,"\n" ,strlen ("\n" )); +strbuf_add_commented_lines (&buf,_ (note_template),strlen (_ (note_template))); +strbuf_add_commented_lines (&buf,"\n" ,strlen ("\n" ));write_or_die (fd, buf.buf, buf.len);write_commented_object (fd, object);
builtin/stripspace.c +1 -1
@@ -13,7 +13,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_char); +strbuf_add_commented_lines (buf, msg, len);free (msg); }
environment.c +14 -1
@@ -428,9 +428,22 @@ void strbuf_commented_addf(struct strbuf *sb,strbuf_vaddf (&buf, fmt, params);va_end (params); -strbuf_add_commented_lines (sb, buf.buf, buf.len, comment_line_char); +strbuf_add_commented_lines (sb, buf.buf, buf.len);if (incomplete_line) sb->buf[--sb->len] ='\0' ;strbuf_release (&buf); } + +void strbuf_add_commented_lines (struct strbuf *out, +const char *buf,size_t size) +{ +static char prefix1[3 ]; +static char prefix2[2 ]; + +if (prefix1[0 ] != comment_line_char) { +xsnprintf (prefix1,sizeof (prefix1),"%c " , comment_line_char); +xsnprintf (prefix2,sizeof (prefix2),"%c" , comment_line_char); + } +strbuf_add_lines (out, prefix1, prefix2, buf, size); +}
environment.h +7 -0
@@ -236,4 +236,11 @@ int print_sha1_ellipsis(void); __attribute__((format (printf,2 ,3 )))void strbuf_commented_addf (struct strbuf *sb,const char *fmt, ...); +/** +* Add a NUL-terminated string to the buffer. Each line will be prepended +* by a comment character and a blank. +*/ +void strbuf_add_commented_lines (struct strbuf *out, +const char *buf,size_t size); +#endif
fmt-merge-msg.c +3 -6
@@ -509,8 +509,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_char); +strbuf_add_commented_lines (tagbuf, sig->buf, sig->len); } }
@@ -556,8 +555,7 @@ static void fmt_merge_msg_sigs(struct strbuf *out)strbuf_addch (&tagline,'\n' );strbuf_add_commented_lines (&tagline, origins.items[first_tag].string, -strlen (origins.items[first_tag].string), - comment_line_char); +strlen (origins.items[first_tag].string));strbuf_insert (&tagbuf,0 , tagline.buf, tagline.len);strbuf_release (&tagline);
@@ -565,8 +563,7 @@ static void fmt_merge_msg_sigs(struct strbuf *out)strbuf_addch (&tagbuf,'\n' );strbuf_add_commented_lines (&tagbuf, origins.items[i].string, -strlen (origins.items[i].string), - comment_line_char); +strlen (origins.items[i].string));fmt_tag_signature (&tagbuf, &sig, buf, len); }strbuf_release (&payload);
rebase-interactive.c +3 -3
@@ -78,7 +78,7 @@ void append_todo_help(int command_count, shortrevisions, shortonto, command_count); } -strbuf_add_commented_lines (buf, msg,strlen (msg), comment_line_char); +strbuf_add_commented_lines (buf, msg,strlen (msg));if (get_missing_commit_check_level () ==MISSING_COMMIT_CHECK_ERROR ) msg = _("\nDo not remove any line. Use 'drop' "
@@ -87,7 +87,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_char); +strbuf_add_commented_lines (buf, msg,strlen (msg));if (edit_todo) msg = _("\nYou are editing the todo file "
@@ -98,7 +98,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_char); +strbuf_add_commented_lines (buf, msg,strlen (msg)); } int edit_todo_list(struct repository *r, struct todo_list *todo_list,
sequencer.c +4 -6
@@ -1859,7 +1859,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_char); +strbuf_add_commented_lines (buf, s, len); }/* Does the current fixup chain contain a squash command? */
@@ -1958,7 +1958,7 @@ static int append_squash_message(struct strbuf *buf, const char *body,strbuf_addf (buf,_ (nth_commit_msg_fmt), ++opts->current_fixup_count +1 );strbuf_addstr (buf,"\n\n" ); -strbuf_add_commented_lines (buf, body, commented_len, comment_line_char); +strbuf_add_commented_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);
@@ -2048,8 +2048,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_char); +strbuf_add_commented_lines (&buf, body,strlen (body));else strbuf_addstr (&buf, body);
@@ -2068,8 +2067,7 @@ static int update_squash_messages(struct repository *r,strbuf_addf (&buf,_ (skip_nth_commit_msg_fmt), ++opts->current_fixup_count +1 );strbuf_addstr (&buf,"\n\n" ); -strbuf_add_commented_lines (&buf, body,strlen (body), - comment_line_char); +strbuf_add_commented_lines (&buf, body,strlen (body)); }else return error (_ ("unknown command: %d" ), command);repo_unuse_commit_buffer (r, commit, message);
strbuf.c +0 -13
@@ -340,19 +340,6 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...)va_end (ap); } -void strbuf_add_commented_lines (struct strbuf *out,const char *buf, -size_t size,char comment_line_char) -{ -static char prefix1[3 ]; -static char prefix2[2 ]; - -if (prefix1[0 ] != comment_line_char) { -xsnprintf (prefix1,sizeof (prefix1),"%c " , comment_line_char); -xsnprintf (prefix2,sizeof (prefix2),"%c" , comment_line_char); - } -strbuf_add_lines (out, prefix1, prefix2, buf, size); -} -void strbuf_vaddf (struct strbuf *sb,const char *fmt,va_list ap) {int len;
strbuf.h +0 -9
@@ -282,15 +282,6 @@ void strbuf_remove(struct strbuf *sb, size_t pos, size_t len);void strbuf_splice (struct strbuf *sb,size_t pos,size_t len,const void *data,size_t data_len); -/** -* Add a NUL-terminated string to the buffer. Each line will be prepended -* by a comment character and a blank. -*/ -void strbuf_add_commented_lines (struct strbuf *out, -const char *buf,size_t size, -char comment_line_char); - -/** * Add data of given length to the buffer. */
wt-status.c +2 -2
@@ -1027,7 +1027,7 @@ static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncomif (s->display_comment_prefix) {size_t len; summary_content =strbuf_detach (&summary, &len); -strbuf_add_commented_lines (&summary, summary_content, len, comment_line_char); +strbuf_add_commented_lines (&summary, summary_content, len);free (summary_content); }
@@ -1103,7 +1103,7 @@ 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,"%s" , cut_line); -strbuf_add_commented_lines (buf, explanation,strlen (explanation), comment_line_char); +strbuf_add_commented_lines (buf, explanation,strlen (explanation)); }void wt_status_add_cut_line (FILE *fp)
Signature · 1 line
2.42.0.820.g83a721a137-goog
v2 cover Avoid passing global comment_line_char repeatedly
In reply to Phillip WoodHere's an updated patchset. The first 2 are the exact same as what Junio has sent.
Jonathan Tan (2):
strbuf: make add_lines() public
strbuf: move env-using functions to environment.c
Junio C Hamano (2):
strbuf_commented_addf(): drop the comment_line_char parameter strbuf_add_commented_lines(): drop the comment_line_char parameter
add-patch.c | 8 +++---- builtin/branch.c | 2 +- builtin/merge.c | 8 +++---- builtin/notes.c | 9 ++++---- builtin/stripspace.c | 2 +- builtin/tag.c | 4 ++-- environment.c | 32 +++++++++++++++++++++++++++ environment.h | 14 ++++++++++++ fmt-merge-msg.c | 9 +++----- rebase-interactive.c | 8 +++---- sequencer.c | 14 ++++++------ strbuf.c | 51 +++++++++----------------------------------- strbuf.h | 20 ++++------------- wt-status.c | 6 +++--- 14 files changed, 92 insertions(+), 95 deletions(-)
Signature · 1 line
2.42.0.820.g83a721a137-goog
PATCH v2 1/4 strbuf_commented_addf(): drop the comment_line_char parameter
From: Junio C Hamano <[email protected]>
All the callers of this function supply the global variable comment_line_char as an argument to its second parameter. Remove the parameter to allow us in the future to change the reference to the global variable with something else, like a function call.
--- add-patch.c | 8 ++++---- builtin/branch.c | 2 +- builtin/merge.c | 8 ++++---- builtin/tag.c | 4 ++-- rebase-interactive.c | 2 +- sequencer.c | 4 ++-- strbuf.c | 3 ++- strbuf.h | 4 ++-- wt-status.c | 2 +- 9 files changed, 19 insertions(+), 18 deletions(-)
add-patch.c +4 -4
@@ -1106,11 +1106,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_char, +strbuf_commented_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_char, + strbuf_commented_addf(&s->buf, _("---\n" "To remove '%c' lines, make them ' ' lines " "(context).\n"
@@ -1119,13 +1119,13 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk) s->mode->is_reverse ?'+' :'-' , s->mode->is_reverse ?'-' :'+' , comment_line_char); -strbuf_commented_addf (&s->buf, comment_line_char,"%s" , +strbuf_commented_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_char, + strbuf_commented_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 "
builtin/branch.c +1 -1
@@ -668,7 +668,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_char, + strbuf_commented_addf(&buf, _("Please edit the description for the branch\n" " %s\n" "Lines starting with '%c' will be stripped.\n"),
builtin/merge.c +4 -4
@@ -857,15 +857,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_char,"\n" ); +strbuf_commented_addf (&msg,"\n" ); } -strbuf_commented_addf (&msg, comment_line_char, +strbuf_commented_addf (&msg,_ (merge_editor_comment));if (cleanup_mode ==COMMIT_MSG_CLEANUP_SCISSORS ) -strbuf_commented_addf (&msg, comment_line_char, +strbuf_commented_addf (&msg,_ (scissors_editor_comment));else -strbuf_commented_addf (&msg, comment_line_char, +strbuf_commented_addf (&msg,_ (no_scissors_editor_comment), comment_line_char); }if (signoff)
builtin/tag.c +2 -2
@@ -314,10 +314,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_char, +strbuf_commented_addf (&buf,_ (tag_template), tag, comment_line_char);else -strbuf_commented_addf (&buf, comment_line_char, +strbuf_commented_addf (&buf,_ (tag_template_nocleanup), tag, comment_line_char);write_or_die (fd, buf.buf, buf.len);strbuf_release (&buf);
rebase-interactive.c +1 -1
@@ -71,7 +71,7 @@ void append_todo_help(int command_count,if (!edit_todo) {strbuf_addch (buf,'\n' ); - strbuf_commented_addf(buf, comment_line_char, + strbuf_commented_addf(buf, Q_("Rebase %s onto %s (%d command)", "Rebase %s onto %s (%d commands)", command_count),
sequencer.c +2 -2
@@ -675,11 +675,11 @@ void append_conflicts_hint(struct index_state *istate, }strbuf_addch (msgbuf,'\n' ); -strbuf_commented_addf (msgbuf, comment_line_char,"Conflicts:\n" ); +strbuf_commented_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_char, +strbuf_commented_addf (msgbuf,"\t%s\n" , ce->name);while (i < istate->cache_nr && !strcmp (ce->name, istate->cache[i]->name))
strbuf.c +2 -1
@@ -1,4 +1,5 @@#include "git-compat-util.h" +#include "environment.h" #include "gettext.h" #include "hex-ll.h" #include "strbuf.h"
@@ -372,7 +373,7 @@ void strbuf_add_commented_lines(struct strbuf *out, const char *buf,add_lines (out, prefix1, prefix2, buf, size); } -void strbuf_commented_addf (struct strbuf *sb,char comment_line_char, +void strbuf_commented_addf (struct strbuf *sb,const char *fmt, ...) {va_list params;
strbuf.h +2 -2
@@ -378,8 +378,8 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...);* Add a formatted string prepended by a comment character and a * blank to the buffer. */ -__attribute__((format (printf,3 ,4 ))) -void strbuf_commented_addf (struct strbuf *sb,char comment_line_char,const char *fmt, ...); +__attribute__((format (printf,2 ,3 ))) +void strbuf_commented_addf (struct strbuf *sb,const char *fmt, ...); __attribute__((format (printf,2 ,0 )))void strbuf_vaddf (struct strbuf *sb,const char *fmt,va_list ap);
wt-status.c +1 -1
@@ -1102,7 +1102,7 @@ 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_char,"%s" , cut_line); +strbuf_commented_addf (buf,"%s" , cut_line);strbuf_add_commented_lines (buf, explanation,strlen (explanation), comment_line_char); }
Signature · 1 line
2.42.0.820.g83a721a137-goog
PATCH v2 2/4 strbuf_add_commented_lines(): drop the comment_line_char parameter
In reply to Jonathan TanFrom: Junio C Hamano <[email protected]>
All the callers of this function supply the global variable comment_line_char as an argument to its last parameter. Remove the parameter to allow us in the future to change the reference to the global variable with something else, like a function call.
--- builtin/notes.c | 9 ++++----- builtin/stripspace.c | 2 +- fmt-merge-msg.c | 9 +++------ rebase-interactive.c | 6 +++--- sequencer.c | 10 ++++------ strbuf.c | 6 +++--- strbuf.h | 3 +-- wt-status.c | 4 ++-- 8 files changed, 21 insertions(+), 28 deletions(-)
builtin/notes.c +4 -5
@@ -181,7 +181,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_char); +strbuf_add_commented_lines (&cbuf, buf.buf, buf.len);write_or_die (fd, cbuf.buf, cbuf.len);strbuf_release (&cbuf);
@@ -209,10 +209,9 @@ 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_char); -strbuf_add_commented_lines (&buf,_ (note_template),strlen (_ (note_template)), - comment_line_char); -strbuf_add_commented_lines (&buf,"\n" ,strlen ("\n" ), comment_line_char); +strbuf_add_commented_lines (&buf,"\n" ,strlen ("\n" )); +strbuf_add_commented_lines (&buf,_ (note_template),strlen (_ (note_template))); +strbuf_add_commented_lines (&buf,"\n" ,strlen ("\n" ));write_or_die (fd, buf.buf, buf.len);write_commented_object (fd, object);
builtin/stripspace.c +1 -1
@@ -13,7 +13,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_char); +strbuf_add_commented_lines (buf, msg, len);free (msg); }
fmt-merge-msg.c +3 -6
@@ -509,8 +509,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_char); +strbuf_add_commented_lines (tagbuf, sig->buf, sig->len); } }
@@ -556,8 +555,7 @@ static void fmt_merge_msg_sigs(struct strbuf *out)strbuf_addch (&tagline,'\n' );strbuf_add_commented_lines (&tagline, origins.items[first_tag].string, -strlen (origins.items[first_tag].string), - comment_line_char); +strlen (origins.items[first_tag].string));strbuf_insert (&tagbuf,0 , tagline.buf, tagline.len);strbuf_release (&tagline);
@@ -565,8 +563,7 @@ static void fmt_merge_msg_sigs(struct strbuf *out)strbuf_addch (&tagbuf,'\n' );strbuf_add_commented_lines (&tagbuf, origins.items[i].string, -strlen (origins.items[i].string), - comment_line_char); +strlen (origins.items[i].string));fmt_tag_signature (&tagbuf, &sig, buf, len); }strbuf_release (&payload);
rebase-interactive.c +3 -3
@@ -78,7 +78,7 @@ void append_todo_help(int command_count, shortrevisions, shortonto, command_count); } -strbuf_add_commented_lines (buf, msg,strlen (msg), comment_line_char); +strbuf_add_commented_lines (buf, msg,strlen (msg));if (get_missing_commit_check_level () ==MISSING_COMMIT_CHECK_ERROR ) msg = _("\nDo not remove any line. Use 'drop' "
@@ -87,7 +87,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_char); +strbuf_add_commented_lines (buf, msg,strlen (msg));if (edit_todo) msg = _("\nYou are editing the todo file "
@@ -98,7 +98,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_char); +strbuf_add_commented_lines (buf, msg,strlen (msg)); } int edit_todo_list(struct repository *r, struct todo_list *todo_list,
sequencer.c +4 -6
@@ -1859,7 +1859,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_char); +strbuf_add_commented_lines (buf, s, len); }/* Does the current fixup chain contain a squash command? */
@@ -1958,7 +1958,7 @@ static int append_squash_message(struct strbuf *buf, const char *body,strbuf_addf (buf,_ (nth_commit_msg_fmt), ++opts->current_fixup_count +1 );strbuf_addstr (buf,"\n\n" ); -strbuf_add_commented_lines (buf, body, commented_len, comment_line_char); +strbuf_add_commented_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);
@@ -2048,8 +2048,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_char); +strbuf_add_commented_lines (&buf, body,strlen (body));else strbuf_addstr (&buf, body);
@@ -2068,8 +2067,7 @@ static int update_squash_messages(struct repository *r,strbuf_addf (&buf,_ (skip_nth_commit_msg_fmt), ++opts->current_fixup_count +1 );strbuf_addstr (&buf,"\n\n" ); -strbuf_add_commented_lines (&buf, body,strlen (body), - comment_line_char); +strbuf_add_commented_lines (&buf, body,strlen (body)); }else return error (_ ("unknown command: %d" ), command);repo_unuse_commit_buffer (r, commit, message);
strbuf.c +3 -3
@@ -360,8 +360,8 @@ 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,char comment_line_char) +void strbuf_add_commented_lines (struct strbuf *out, +const char *buf,size_t size) {static char prefix1[3 ];static char prefix2[2 ];
@@ -384,7 +384,7 @@ void strbuf_commented_addf(struct strbuf *sb,strbuf_vaddf (&buf, fmt, params);va_end (params); -strbuf_add_commented_lines (sb, buf.buf, buf.len, comment_line_char); +strbuf_add_commented_lines (sb, buf.buf, buf.len);if (incomplete_line) sb->buf[--sb->len] ='\0' ;
strbuf.h +1 -2
@@ -287,8 +287,7 @@ void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,* by a comment character and a blank. */ void strbuf_add_commented_lines (struct strbuf *out, -const char *buf,size_t size, -char comment_line_char); +const char *buf,size_t size);/**
wt-status.c +2 -2
@@ -1027,7 +1027,7 @@ static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncomif (s->display_comment_prefix) {size_t len; summary_content =strbuf_detach (&summary, &len); -strbuf_add_commented_lines (&summary, summary_content, len, comment_line_char); +strbuf_add_commented_lines (&summary, summary_content, len);free (summary_content); }
@@ -1103,7 +1103,7 @@ 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,"%s" , cut_line); -strbuf_add_commented_lines (buf, explanation,strlen (explanation), comment_line_char); +strbuf_add_commented_lines (buf, explanation,strlen (explanation)); }void wt_status_add_cut_line (FILE *fp)
Signature · 1 line
2.42.0.820.g83a721a137-goog
PATCH v2 3/4 strbuf: make add_lines() public
In reply to Jonathan TanA subsequent patch will require the ability to add different prefixes to different lines (depending on their contents), so make this functionality available from outside strbuf.c. The function name is chosen to avoid a conflict with the existing function named strbuf_add_lines().
--- strbuf.c | 22 +++++++++++----------- strbuf.h | 4 ++++ 2 files changed, 15 insertions(+), 11 deletions(-)
strbuf.c +11 -11
@@ -340,24 +340,24 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...)va_end (ap); } -static void add_lines (struct strbuf *out, -const char *prefix1, -const char *prefix2, -const char *buf,size_t size) +void strbuf_add_lines_varied_prefix (struct strbuf *sb, +const char *default_prefix, +const char *tab_nl_prefix, +const char *buf,size_t size) {while (size) {const char *prefix;const char *next =memchr (buf,'\n' , size); next = next ? (next +1 ) : (buf + size); - prefix = ((prefix2 && (buf[0 ] =='\n' || buf[0 ] =='\t' )) - ? prefix2 : prefix1); -strbuf_addstr (out, prefix); -strbuf_add (out, buf, next - buf); + prefix = (buf[0 ] =='\n' || buf[0 ] =='\t' ) + ? tab_nl_prefix : default_prefix; +strbuf_addstr (sb, prefix); +strbuf_add (sb, buf, next - buf); size -= next - buf; buf = next; } -strbuf_complete_line (out); +strbuf_complete_line (sb); } void strbuf_add_commented_lines(struct strbuf *out,
@@ -370,7 +370,7 @@ void strbuf_add_commented_lines(struct strbuf *out,xsnprintf (prefix1,sizeof (prefix1),"%c " , comment_line_char);xsnprintf (prefix2,sizeof (prefix2),"%c" , comment_line_char); } -add_lines (out, prefix1, prefix2, buf, size); +strbuf_add_lines_varied_prefix (out, prefix1, prefix2, buf, size); } void strbuf_commented_addf(struct strbuf *sb,
@@ -751,7 +751,7 @@ ssize_t strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)void strbuf_add_lines (struct strbuf *out,const char *prefix,const char *buf,size_t size) { -add_lines (out, prefix,NULL , buf, size); +strbuf_add_lines_varied_prefix (out, prefix, prefix, buf, size); }void strbuf_addstr_xml_quoted (struct strbuf *buf,const char *s)
strbuf.h +4 -0
@@ -601,6 +601,10 @@ void strbuf_add_lines(struct strbuf *sb,const char *prefix, const char *buf, size_t size); +void strbuf_add_lines_varied_prefix (struct strbuf *sb, +const char *default_prefix, +const char *tab_nl_prefix, +const char *buf,size_t size);/** * Append s to sb, with the characters '<', '>', '&' and '"' converted
Signature · 1 line
2.42.0.820.g83a721a137-goog
Re: [PATCH v2 3/4] strbuf: make add_lines() public
Quoted 12 lines · Jonathan Tan
strbuf.c +4
-4-static void add_lines (struct strbuf *out, -const char *prefix1, -const char *prefix2, -const char *buf,size_t size) +void strbuf_add_lines_varied_prefix (struct strbuf *sb, +const char *default_prefix, +const char *tab_nl_prefix, +const char *buf,size_t size) {while (size) {const char *prefix;const char *next =memchr (buf,'\n' , size);
strbuf.c void strbuf_addf(struct strbuf *sb, const char *fmt, ...) · line 356
Quoted 10 lines
strbuf.c +4
-4next = next ? (next +1 ) : (buf + size); - prefix = ((prefix2 && (buf[0 ] =='\n' || buf[0 ] =='\t' )) - ? prefix2 : prefix1); -strbuf_addstr (out, prefix); -strbuf_add (out, buf, next - buf); + prefix = (buf[0 ] =='\n' || buf[0 ] =='\t' ) + ? tab_nl_prefix : default_prefix; +strbuf_addstr (sb, prefix); +strbuf_add (sb, buf, next - buf);
The original allowed callers to pass NULL for the second prefix when they want to use the same prefix, even for commenting out an empty line or a line that begins with a tab. The new one does not allow the callers to do so. As long as updating the existing callers are done carefully, the difference would not matter, but would it help new callers in the future to rid the usability feature like this patch does while performing a refactoring? The loss of feature is not even documented, by the way.
While "tab_nl" sound a bit more specific than "2", I am not sure if we made it better. It does not make it clear why it makes sense to (and it is necessary to) special case HT and LF. A developer who is writing a new caller would not know why there are two prefixes supported, or why the function is named "varied prefix", with these names.
Giving a name that explains the reason might help the readability. I've been thinking what the best name for this function would be but not successfully.
It may be that we shouldn't take two prefixes in the first place. The ONLY case callers want to pass prefix2 that is different from prefix1 is when prefix1 ends with a space, and prefix2 is identical to prefix1 without the trailing space. The reason they use such a pair of prefixes is to avoid leaving a trailing whitespace (when buf[0] == '\n') or having a space before tab (when buf[0] == '\t') on the generated lines.
So eventually we may want to have something like this as the final interface given to the public callers, simply because ...
strbuf_add_lines_as_comments(struct strbuf *sb,
const char *comment_prefix,
const char *buf, size_t size)
{
while (size) {
const char *next = memchr(buf, '\n', size);
next = next ? (next + 1) : (buf + size);
strbuf_addstr(sb, comment_prefix);
/* avoid trailing-whitespace and space-before-tab */
if (buf[0] != '\n' && buf[0] != '\t')
strbuf_addch(sb, ' ');
strbuf_add(sb, buf, next - buf);
... loop control ...
}
... strbuf completion ...
}... there is no need for totally unrelated two prefix variants. And both the function name and the parameter name would be a bit easier to understand than your version (and far easier than the original). The function is about commenting out all the lines in buf with the comment prefix, and most of the time we add a space between the comment character and the commented out text, but in some cases we do not want to add the space.
But as I said already, I'd prefer to see a patch that claims to be a refactoring to do as little as necessary. Giving it a name better than add_lines() is inevitable, because you are making it extern. But I'd prefer to see the parameter naems and the function body left untouched and kept the same as the original. It should be left to a separate step to improve the interface and the implementation.
Thanks.
PATCH v2 4/4 strbuf: move env-using functions to environment.c
In reply to Jonathan TanThis eliminates the dependency from strbuf to environment.
--- environment.c | 32 ++++++++++++++++++++++++++++++++ environment.h | 14 ++++++++++++++ strbuf.c | 32 -------------------------------- strbuf.h | 15 --------------- 4 files changed, 46 insertions(+), 47 deletions(-)
environment.c +32 -0
@@ -18,6 +18,7 @@#include "refs.h" #include "fmt-merge-msg.h" #include "commit.h" +#include "strbuf.h" #include "strvec.h" #include "object-file.h" #include "object-store-ll.h"
@@ -416,3 +417,34 @@ int print_sha1_ellipsis(void) }return cached_result; } + +void strbuf_commented_addf (struct strbuf *sb, +const char *fmt, ...) +{ +va_list params; +struct strbuf buf =STRBUF_INIT ; +int incomplete_line = sb->len && sb->buf[sb->len -1 ] !='\n' ; + +va_start (params, fmt); +strbuf_vaddf (&buf, fmt, params); +va_end (params); + +strbuf_add_commented_lines (sb, buf.buf, buf.len); +if (incomplete_line) + sb->buf[--sb->len] ='\0' ; + +strbuf_release (&buf); +} + +void strbuf_add_commented_lines (struct strbuf *out, +const char *buf,size_t size) +{ +static char prefix1[3 ]; +static char prefix2[2 ]; + +if (prefix1[0 ] != comment_line_char) { +xsnprintf (prefix1,sizeof (prefix1),"%c " , comment_line_char); +xsnprintf (prefix2,sizeof (prefix2),"%c" , comment_line_char); + } +strbuf_add_lines_varied_prefix (out, prefix1, prefix2, buf, size); +}
environment.h +14 -0
@@ -229,4 +229,18 @@ extern const char *excludes_file;*/ int print_sha1_ellipsis (void ); +/** +* Add a formatted string prepended by a comment character and a +* blank to the buffer. +*/ +__attribute__((format (printf,2 ,3 ))) +void strbuf_commented_addf (struct strbuf *sb,const char *fmt, ...); + +/** +* Add a NUL-terminated string to the buffer. Each line will be prepended +* by a comment character and a blank. +*/ +void strbuf_add_commented_lines (struct strbuf *out, +const char *buf,size_t size); +#endif
strbuf.c +0 -32
@@ -1,5 +1,4 @@#include "git-compat-util.h" -#include "environment.h" #include "gettext.h" #include "hex-ll.h" #include "strbuf.h"
@@ -360,37 +359,6 @@ void strbuf_add_lines_varied_prefix(struct strbuf *sb,strbuf_complete_line (sb); } -void strbuf_add_commented_lines (struct strbuf *out, -const char *buf,size_t size) -{ -static char prefix1[3 ]; -static char prefix2[2 ]; - -if (prefix1[0 ] != comment_line_char) { -xsnprintf (prefix1,sizeof (prefix1),"%c " , comment_line_char); -xsnprintf (prefix2,sizeof (prefix2),"%c" , comment_line_char); - } -strbuf_add_lines_varied_prefix (out, prefix1, prefix2, buf, size); -} - -void strbuf_commented_addf (struct strbuf *sb, -const char *fmt, ...) -{ -va_list params; -struct strbuf buf =STRBUF_INIT ; -int incomplete_line = sb->len && sb->buf[sb->len -1 ] !='\n' ; - -va_start (params, fmt); -strbuf_vaddf (&buf, fmt, params); -va_end (params); - -strbuf_add_commented_lines (sb, buf.buf, buf.len); -if (incomplete_line) - sb->buf[--sb->len] ='\0' ; - -strbuf_release (&buf); -} -void strbuf_vaddf (struct strbuf *sb,const char *fmt,va_list ap) {int len;
strbuf.h +0 -15
@@ -282,14 +282,6 @@ void strbuf_remove(struct strbuf *sb, size_t pos, size_t len);void strbuf_splice (struct strbuf *sb,size_t pos,size_t len,const void *data,size_t data_len); -/** -* Add a NUL-terminated string to the buffer. Each line will be prepended -* by a comment character and a blank. -*/ -void strbuf_add_commented_lines (struct strbuf *out, -const char *buf,size_t size); - -/** * Add data of given length to the buffer. */
@@ -373,13 +365,6 @@ void strbuf_humanise_rate(struct strbuf *buf, off_t bytes); __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 -* blank to the buffer. -*/ -__attribute__((format (printf,2 ,3 ))) -void strbuf_commented_addf (struct strbuf *sb,const char *fmt, ...); - __attribute__((format (printf,2 ,0 )))void strbuf_vaddf (struct strbuf *sb,const char *fmt,va_list ap);
Signature · 1 line
2.42.0.820.g83a721a137-goog
Re: [PATCH v2 4/4] strbuf: move env-using functions to environment.c
Quoted 12 lines · Jonathan Tan
environment.h +4
-0@@ -229,4 +229,18 @@ extern const char *excludes_file;*/ int print_sha1_ellipsis (void ); +/** +* Add a formatted string prepended by a comment character and a +* blank to the buffer. +*/
environment.h extern const char *excludes_file; · line 245
Quoted 10 lines
environment.h +10
-0+__attribute__((format (printf,2 ,3 ))) +void strbuf_commented_addf (struct strbuf *sb,const char *fmt, ...); + +/** +* Add a NUL-terminated string to the buffer. Each line will be prepended +* by a comment character and a blank. +*/ +void strbuf_add_commented_lines (struct strbuf *out, +const char *buf,size_t size); +
What's your plans for globals kept in ident.c for example?
The reason why I ask is because I do not quite see how making the use of the global comment-line-char variable hidden like this patch does would help your libification effort. There are many settings that are reasonably expected to be used by many places, and if you want to avoid them, it appears to me that your only way forward after applying this patch would be to recreate the implementation the public git has in environment.[ch] in your version of Git. You'd have to do something similar for what is in ident.c for the same reason.
The relative size of the logic necessary to split the original into lines and prefix the comment prefix character (which is much larger) and the idea that there is a system wide setting of what the comment prefix character should be (which is miniscule) makes me wonder if this is going in the right direction.
Thanks.