gpg-interface.c: trim CR only before LF
PATCH v2 1/1 gpg-interface.c: trim CR only before LF
Problem:
The function remove_cr_after() stripped CRs blindly. The comment suggested
NEEDSWORK: trim only CRs before LF. This caused potential confusion.
Solution:
Rename remove_cr_after() to trim_cr_before_lf() and update the comment:
"Trim CR characters only when they appear before LF (\r\n) line endings." This keeps lone CRs intact and documents intent clearly.
Also improved formatting.
--- gpg-interface.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-)
gpg-interface.c +24 -10
@@ -964,23 +964,37 @@ int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *sigreturn use_format->sign_buffer (buffer, signature, signing_key); } -/* -* Trim CR characters only when they appear before LF (\r\n) line endings. -* This avoids removing legitimate lone CRs from teh content. -*/ +/* Convert CRLF to LF, in case we are on Windows */ static void trim_cr_before_lf (struct strbuf *buffer,size_t offset) {size_t i, j; +for (i = j = offset; i < buffer->len; i++) { +/* Skip CR only if it comes right before LF */ +if (buffer->buf[i] =='\r' && i +1 < buffer->len && + buffer->buf[i +1 ] =='\n' ) +continue ; + +if (i != j) + buffer->buf[j] = buffer->buf[i]; + j++; + } +strbuf_setlen (buffer, j); +} + +static void trim_cr_before_lf (struct strbuf *buffer,size_t offset) +{ +size_t i, j; +for (i = j = offset; i < buffer->len; i++) {/* skip CR only if it comes right before LF */ -if (buffer->buf[i] =='\r' && i +1 < buffer->len && buffer->buf[i+1 ] =='\n' ) -continue ; +if (buffer->buf[i] =='\r' && i +1 < buffer->len && + buffer->buf[i+1 ] =='\n' ) +continue ; -if (i != j) - buffer->buf[j] = buffer->buf[i]; - j++; - +if (i != j) + buffer->buf[j] = buffer->buf[i]; + j++; }strbuf_setlen (buffer, j); }
Signature · 1 line
2.43.0
Quoted 4 lines · Okhuomon Ajayi
Problem:
The functionremove_cr_after()stripped CRs blindly. The comment suggested
NEEDSWORK: trim only CRs before LF.
We use the present tense to talk about the current situation. In "Documentation/SubmittingPatches" there is:
"[[present-tense]]
The problem statement that describes the status quo is written in the present tense. Write "The code does X when it is given input Y", instead of "The code used to do Y when given input X". You do not have to say "Currently"---the status quo in the problem statement is about the code _without_ your change, by project convention."
Also you don't need to prefix this part with "Problem:". We should understand from the description of the status quo that the situation is not good and should be improved.
Quoted 1 line
This caused potential confusion.
It's not clear what caused potential confusion. Is it the "NEEDSWORK:
..." comment, or the fact that remove_cr_after() stripped CRs blindly, or both?
Also it's not clear what the confusion is about. Is there confusion because a reader can wonder if stripping CR blindly could be a bug?
What about something like:
"The remove_cr_after() function removes any CR it finds in a buffer after an offset, but a 'NEEDSWORK' code comment in front of it says that it should only remove a CR that is before an LF. This can make readers wonder if stripping CRs blindly could result in bugs."
Quoted 2 lines
Solution:
Renameremove_cr_after()totrim_cr_before_lf()and update the comment:
Here also, from the description of what the patch does, we should understand that it will improve things, so no need to prefix it with "Solution:".
The issue is that to know what should be done about the current situation, it would help to know if stripping CRs blindly could result in bugs or not. So there should be an analysis part before the part describing what the patch does. For example the analysis part could say something like:
"As the remove_cr_after() function is only used to replace CR LF sequences (generated by software on Windows) with a single LF, the 'NEEDSWORK' code comment seems to be correct. It seems safer to only remove a CR when it is before an LF even if the buffer is not likely to contain any other CR."
(Then you could even further clarify the goal of the patch when starting to describe what the patch does with something like:
"To implement this safe solution suggested by the NEEDSWORK comment, rename remove_cr_after() to trim_cr_before_lf() ..."
It might not be necessary here, but I mention it so that you can see how to smoothly transition from the problem description.)
By the way you mention renaming remove_cr_after() to trim_cr_before_lf() and updating the comment before it, but you don't mention actually changing the implementation of the function so that it only removes a CR when it's before a LF.
Quoted 1 line
"Trim CR characters only when they appear before LF (\r\n) line endings."
No need to duplicate the new code comment in the commit message. We can see it in the patch.
Quoted 1 line
This keeps lone CRs intact and documents intent clearly.
This sentence is fine.
Quoted 1 line
Also improved formatting.
It's not clear what formatting is improved. And this should use an imperative tone, like the above did with "Rename remove_cr_after() ... and update ..."
Quoted 10 lines
Signed-off-by Okhuomon Ajayi <[email protected]>--- gpg-interface.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-)gpg-interface.c +0
-0
gpg-interface.c int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *sig · line 967
Quoted 8 lines
gpg-interface.c +1
-4@@ -964,23 +964,37 @@ int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *sigreturn use_format->sign_buffer (buffer, signature, signing_key); } -/* -* Trim CR characters only when they appear before LF (\r\n) line endings. -* This avoids removing legitimate lone CRs from teh content. -*/ +/* Convert CRLF to LF, in case we are on Windows */
I don't see any "NEEDSWORKS" here. It looks like this is a patch that was made against the version 1 of the patch you sent earlier. Instead, all the versions of your patches should be made against a relatively recent version of the 'master' branch.
This way if your patch is accepted, only your patch needs to be merged. Also that makes it easier for reviewers to see that the commit message (which starts by describing the current situation in 'master') is correct.
Quoted 17 lines · Christian Couder
Quoted 4 lines · Okhuomon Ajayi
Problem:
The functionremove_cr_after()stripped CRs blindly. The comment suggested
NEEDSWORK: trim only CRs before LF.We use the present tense to talk about the current situation. In "
Documentation/SubmittingPatches" there is:"[[present-tense]]
The problem statement that describes the status quo is written in the present tense. Write "The code does X when it is given input Y", instead of "The code used to do Y when given input X". You do not have to say "Currently"---the status quo in the problem statement is about the code _without_ your change, by project convention."
Quoted 3 lines
Also you don't need to prefix this part with "Problem:". We should understand from the description of the status quo that the situation is not good and should be improved.
Thanks for the above two pieces of advice. The latter follows if messages of all commits follow a simple convention that we have been following, which is that the usual way to compose a log message of this project is to
- Give an observation on how the current system works in the present tense (so no need to say "Currently X is Y", or "Previously X was Y" to describe the state before your change; just "X is Y" is enough), and discuss what you perceive as a problem in it.
- Propose a solution (optional---often, problem description trivially leads to an obvious solution in reader's minds).
- Give commands to somebody editing the codebase to "make it so", instead of saying "This commit does X".
in this order.
Perhaps we should write it somewhere in the introductory text designed to help applicants of mentoring programs like Outreachy and GSoC?
Thanks.