Re: [PATCH v2 3/9] rebase -i: comment out squash!/fixup! subjects from squash message

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Charvi Mendiratta <charvi077@xxxxxxxxx> writes:

> +static size_t subject_length(const char *body)
> +{
> +	size_t i, len = 0;
> +	char c;
> +	int blank_line = 1;
> +	for (i = 0, c = body[i]; c; c = body[++i]) {
> +		if (c == '\n') {
> +			if (blank_line)
> +				return len;
> +			len = i + 1;
> +			blank_line = 1;
> +		} else if (!isspace(c)) {
> +			blank_line = 0;
> +		}
> +	}
> +	return blank_line ? len : i;
> +}

I cannot quite tell what this loop is trying to compute at the first
glance.

 - If body[0] == '\n', then i==0, c==LF, blank_line==1 and len==0
   so len==0 is returned immediately.

 - If the first line has only SP, HT, CR, etc. whitespace,
   blank_line stays 1 and at the end of the line when we see
   c=='\n', body[i] is pointing at that '\n', blank_line is true, so
   len is returned from the previous iteration (e.g. body="   \n"
   returns 0)

 - If the first line has some non space, blank_line becomes false,
   so at the end of that line when we see c=='\n', body[i] is
   pointing at that '\n', len==i+1 becomes one past that LF and then
   we reset blank_line to true??? and go on to the next line.

So when we see LF, if we have seen any non whitespace byte on that
line, blank_line is false.  Only when we saw LF followed by zero or
more whitespace before seeing another LF, we return len that was set
when we saw the previous LF (which is one past that LF).
    
So... is this trying to find the first paragraph-break-looking line
to find the end of the first paragraph.  OK.

There must be an easier-to-read way to write all this, though, I
would think (or don't we already have an existing code that is
waiting to be factored out?).

In any case, let's keep reading.

>  static void append_squash_message(struct strbuf *buf, const char *body,
>  				  struct replay_opts *opts)
>  {
> +	size_t commented_len = 0;
> +
>  	unlink(rebase_path_fixup_msg());
> +	if (starts_with(body, "squash!") || starts_with(body, "fixup!"))
> +		commented_len = subject_length(body);
>  	strbuf_addf(buf, "\n%c ", comment_line_char);
>  	strbuf_addf(buf, _("This is the commit message #%d:"),
>  		    ++opts->current_fixup_count + 1);
>  	strbuf_addstr(buf, "\n\n");
> -	strbuf_addstr(buf, body);
> +	strbuf_add_commented_lines(buf, body, commented_len);

As add_commented_lines places the comment character at the beginning
of each line, it is OK for body[0..commented_len) to contain more than
one lines.  Good.

> +	strbuf_addstr(buf, body + commented_len);

And we add everything after the beginning of the paragraph-break
looking line.  This code may add a line, immediately after the
previous "commented out" block, bunch of whitespaces and then a LF.
It will be cleaned up with stripspace most of the time, but
depending on the end-user settings, it may be left behind.  I am
guessing that is what we want, but thought it would not hurt to
double check.

> diff --git a/t/t3415-rebase-autosquash.sh b/t/t3415-rebase-autosquash.sh
> index 7bab6000dc..551dc06bc3 100755
> --- a/t/t3415-rebase-autosquash.sh
> +++ b/t/t3415-rebase-autosquash.sh
> @@ -81,8 +81,7 @@ test_auto_squash () {
>  	echo 1 >file1 &&
>  	git add -u &&
>  	test_tick &&
> -	git commit -m "squash! first" &&
> -
> +	git commit -m "squash! first" -m "extra para for first" &&

It is not "extra"; that's the beginning of the "body" ;-).

>  	git tag $1 &&
>  	test_tick &&
>  	git rebase $2 -i HEAD^^^ &&
> @@ -139,7 +138,7 @@ test_expect_success 'auto squash that matches 2 commits' '
>  	echo 1 >file1 &&
>  	git add -u &&
>  	test_tick &&
> -	git commit -m "squash! first" &&
> +	git commit -m "squash! first" -m "extra para for first" &&
>  	git tag final-multisquash &&
>  	test_tick &&
>  	git rebase --autosquash -i HEAD~4 &&
> @@ -192,7 +191,7 @@ test_expect_success 'auto squash that matches a sha1' '
>  	git add -u &&
>  	test_tick &&
>  	oid=$(git rev-parse --short HEAD^) &&
> -	git commit -m "squash! $oid" &&
> +	git commit -m "squash! $oid" -m "extra para" &&
>  	git tag final-shasquash &&
>  	test_tick &&
>  	git rebase --autosquash -i HEAD^^^ &&
> @@ -203,7 +202,8 @@ test_expect_success 'auto squash that matches a sha1' '
>  	git cat-file blob HEAD^:file1 >actual &&
>  	test_cmp expect actual &&
>  	git cat-file commit HEAD^ >commit &&
> -	grep squash commit >actual &&
> +	grep -v "squash" commit &&

This says that the file must have at least one line that does not
say "squash" or the test is a failure.  It does not say "there
should be no line that has "squash" on it".  Intended?

> +	grep "extra para" commit >actual &&

I can tell that you want the "extra para" to still remain, but how
does the grep that is not anchored guarantee that?  Perhaps look for

	grep "^extra para" commit

to ensure that you are not seeing a commented out but somehow failed
to get stripspaced out?

>  	test_line_count = 1 actual
>  '

Thanks.



[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux