Re: [PATCH v3] ref-filter: trim end whitespace in subject

2017-05-22 Thread Johannes Sixt

Am 22.05.2017 um 21:53 schrieb Jeff King:

On Mon, May 22, 2017 at 09:47:59PM +0200, Johannes Sixt wrote:


Am 22.05.2017 um 19:10 schrieb DOAN Tran Cong Danh:

diff --git a/t/t3203-branch-output.sh b/t/t3203-branch-output.sh
index 5778c0afe..fa4441868 100755
--- a/t/t3203-branch-output.sh
+++ b/t/t3203-branch-output.sh
@@ -13,7 +13,8 @@ test_expect_success 'make commits' '
   test_expect_success 'make branches' '
git branch branch-one &&
-   git branch branch-two HEAD^
+   git branch branch-two $(printf "%s\r\n" one " " line3_long line4 |


I didn't notice earlier that there is a blank between the dq here.


+git commit-tree HEAD:)
   '
   test_expect_success 'make remote branches' '



This updated test shows nothing, I am afraid: If I apply only this change
without the rest of the patch, then all test in t3203 still pass. And I do
not see how the code change could make any difference at all. What am I
missing?


And I didn't look carefully enough at t3203. Some tests do check branch 
-v output.




It does for me here on Linux; I wonder if the CRs are being eaten by the
shell expansion.


And I tested on Linux, too, but on the wrong branch. On a branch closer 
to master I see a failure as well. Sorry for the noise.


There are no CRs on the command line, BTW, only on stdin of commit-tree.

-- Hannes


Re: [PATCH v3] ref-filter: trim end whitespace in subject

2017-05-22 Thread Jeff King
On Mon, May 22, 2017 at 09:47:59PM +0200, Johannes Sixt wrote:

> Am 22.05.2017 um 19:10 schrieb DOAN Tran Cong Danh:
> > diff --git a/t/t3203-branch-output.sh b/t/t3203-branch-output.sh
> > index 5778c0afe..fa4441868 100755
> > --- a/t/t3203-branch-output.sh
> > +++ b/t/t3203-branch-output.sh
> > @@ -13,7 +13,8 @@ test_expect_success 'make commits' '
> >   test_expect_success 'make branches' '
> > git branch branch-one &&
> > -   git branch branch-two HEAD^
> > +   git branch branch-two $(printf "%s\r\n" one " " line3_long line4 |
> > +git commit-tree HEAD:)
> >   '
> >   test_expect_success 'make remote branches' '
> > 
> 
> This updated test shows nothing, I am afraid: If I apply only this change
> without the rest of the patch, then all test in t3203 still pass. And I do
> not see how the code change could make any difference at all. What am I
> missing?

It does for me here on Linux; I wonder if the CRs are being eaten by the
shell expansion.

-Peff


Re: [PATCH v3] ref-filter: trim end whitespace in subject

2017-05-22 Thread Johannes Sixt

Am 22.05.2017 um 19:10 schrieb DOAN Tran Cong Danh:

diff --git a/t/t3203-branch-output.sh b/t/t3203-branch-output.sh
index 5778c0afe..fa4441868 100755
--- a/t/t3203-branch-output.sh
+++ b/t/t3203-branch-output.sh
@@ -13,7 +13,8 @@ test_expect_success 'make commits' '
  
  test_expect_success 'make branches' '

git branch branch-one &&
-   git branch branch-two HEAD^
+   git branch branch-two $(printf "%s\r\n" one " " line3_long line4 |
+git commit-tree HEAD:)
  '
  
  test_expect_success 'make remote branches' '




This updated test shows nothing, I am afraid: If I apply only this 
change without the rest of the patch, then all test in t3203 still pass. 
And I do not see how the code change could make any difference at all. 
What am I missing?


-- Hannes


[PATCH v3] ref-filter: trim end whitespace in subject

2017-05-22 Thread DOAN Tran Cong Danh
From: ĐOÀN Trần Công Danh 

Commit 949af0684 ("branch: use ref-filter printing APIs", 2017-01-10)
make `git branch -v` stops trimming end-whitespace in subject,
and it stops treating next all-whitespace-line as an empty line.

Quote from git mailing-list:

> Here is a recipe to reproduce the error:
>
>git init
>git commit --allow-empty -m initial
>git branch crlf $(printf '%s\r\n' subject '' line3_long line4 |
>git commit-tree HEAD:)
> The reason for the "bug" is obviously that a line having CR in addition
> to LF is not "an empty line". Consequently, the second line is not
> treated as a separator between subject and body, whereupon Git
> concatenates all line into one large subject line. This strips the LFs
> but leaves the CRS in tact, which, when printed on a terminal move the
> cursor to the beginning of the line, so that text after the CRs
> overwrites what is already in the terminal.

To recover previous behavior, trim all whitespace at the end of
first line, and treat all-white-space line as empty line

Reported-by: Animi Vulpis 
Helped-by: Johannes Sixt 
Signed-off-by: ĐOÀN Trần Công Danh 
---
Sorry for the noise, after sending out v2,
I found that the body is calculated incorrectly.

 ref-filter.c | 40 
 t/t3203-branch-output.sh |  3 ++-
 2 files changed, 34 insertions(+), 9 deletions(-)

diff --git a/ref-filter.c b/ref-filter.c
index 1fc5e9970..4b30edf61 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -942,6 +942,25 @@ static void grab_person(const char *who, struct atom_value 
*val, int deref, stru
}
 }
 
+/*
+ * check if line in range [start, end) is a blank line or not
+ * data in range [start, end) must be valid before calling this function
+ */
+static int is_blank_line(const char *start, const char *end)
+{
+   while (start != end && isspace(*start))
+   start++;
+   return start == end;
+}
+
+static const char* find_next_eol(const char *buf)
+{
+   const char* eol = strchrnul(buf, '\n');
+   if (*eol)
+   eol++;
+   return eol;
+}
+
 static void find_subpos(const char *buf, unsigned long sz,
const char **sub, unsigned long *sublen,
const char **body, unsigned long *bodylen,
@@ -949,6 +968,7 @@ static void find_subpos(const char *buf, unsigned long sz,
const char **sig, unsigned long *siglen)
 {
const char *eol;
+   int has_empty_line = 0;
/* skip past header until we hit empty line */
while (*buf && *buf != '\n') {
eol = strchrnul(buf, '\n');
@@ -967,20 +987,24 @@ static void find_subpos(const char *buf, unsigned long sz,
/* subject is first non-empty line */
*sub = buf;
/* subject goes to first empty line */
-   while (buf < *sig && *buf && *buf != '\n') {
-   eol = strchrnul(buf, '\n');
-   if (*eol)
-   eol++;
+   while (buf < *sig && !has_empty_line) {
+   eol = find_next_eol(buf);
+   has_empty_line = is_blank_line(buf, eol);
buf = eol;
}
*sublen = buf - *sub;
-   /* drop trailing newline, if present */
-   if (*sublen && (*sub)[*sublen - 1] == '\n')
+   /* drop trailing whitespace, if present */
+   while (*sublen && isspace((*sub)[*sublen - 1]))
*sublen -= 1;
 
/* skip any empty lines */
-   while (*buf == '\n')
-   buf++;
+   while (buf < *sig) {
+   eol = find_next_eol(buf);
+   if (is_blank_line(buf, eol))
+   buf = eol;
+   else
+   break;
+   }
*body = buf;
*bodylen = strlen(buf);
*nonsiglen = *sig - buf;
diff --git a/t/t3203-branch-output.sh b/t/t3203-branch-output.sh
index 5778c0afe..fa4441868 100755
--- a/t/t3203-branch-output.sh
+++ b/t/t3203-branch-output.sh
@@ -13,7 +13,8 @@ test_expect_success 'make commits' '
 
 test_expect_success 'make branches' '
git branch branch-one &&
-   git branch branch-two HEAD^
+   git branch branch-two $(printf "%s\r\n" one " " line3_long line4 |
+git commit-tree HEAD:)
 '
 
 test_expect_success 'make remote branches' '
-- 
Danh