# git-gui / Warning: "No newline at end of file”

5 messages from 2012-11-13 to 2013-05-10. Participants: Tobias Preuss, Pat Thoyts, Heiko Voigt.
Thread: https://gitlist.dev/t/32104

## Tobias Preuss, 2012-11-13 20:26

Subject: git-gui / Warning: "No newline at end of file”
Message-ID: <CADEaiE-J4nEwyK4WSiH=zzaH6cb85mw15O3wxrWrTXJtZfJixQ@mail.gmail.com>
URL: https://gitlist.dev/e/CADEaiE-J4nEwyK4WSiH%3DzzaH6cb85mw15O3wxrWrTXJtZfJixQ%40mail.gmail.com

```
Hello.
I noticed a problem when working with git-gui which might be a bug.
The issue only affects you when you are visually trying to stage
changes line by line. Here are the steps to reproduce the problem:

1. Initialize a new repository.
2. Create a file with three lines of content each with the word
"Hello". Do not put a new line at the end of the file.
3. Add and commit the file.
4. Edit the same file putting words inbetween the three lines.
5. Open git-gui and try to stage the changes line by line.

The editor will append the warning "No newline at end of file” to the
end of the diff. When you are trying to stage a line an error occurs.
The problem is also illustrated in a question on Stackoverflow [1].

Please let me know if you need more information or if I should send
this problem to some other mailing list.
Thank you, Tobias

____________
[1] http://stackoverflow.com/questions/13223868/how-to-stage-line-by-line-in-git-gui-although-no-newline-at-end-of-file-warnin

```

## Tobias Preuss, 2013-01-03 12:26

Subject: Fwd: git-gui / Warning: "No newline at end of file”
Message-ID: <CADEaiE8GKfSQ5u+0bb8qtBe-3OTYJThGb-UFLU_pM-T9ZqvkiA@mail.gmail.com>
URL: https://gitlist.dev/e/CADEaiE8GKfSQ5u%2B0bb8qtBe-3OTYJThGb-UFLU_pM-T9ZqvkiA%40mail.gmail.com
In-Reply-To: <CADEaiE-J4nEwyK4WSiH=zzaH6cb85mw15O3wxrWrTXJtZfJixQ@mail.gmail.com>

```
Hello. I never got a response. Did my email pass the distribution
list? Best, Tobias


---------- Forwarded message ----------
From: Tobias Preuss <tobias.preuss@googlemail.com>
Date: Tue, Nov 13, 2012 at 9:26 PM
Subject: git-gui / Warning: "No newline at end of file”
To: git <git@vger.kernel.org>


Hello.
I noticed a problem when working with git-gui which might be a bug.
The issue only affects you when you are visually trying to stage
changes line by line. Here are the steps to reproduce the problem:

1. Initialize a new repository.
2. Create a file with three lines of content each with the word
"Hello". Do not put a new line at the end of the file.
3. Add and commit the file.
4. Edit the same file putting words inbetween the three lines.
5. Open git-gui and try to stage the changes line by line.

The editor will append the warning "No newline at end of file” to the
end of the diff. When you are trying to stage a line an error occurs.
The problem is also illustrated in a question on Stackoverflow [1].

Please let me know if you need more information or if I should send
this problem to some other mailing list.
Thank you, Tobias

____________
[1] http://stackoverflow.com/questions/13223868/how-to-stage-line-by-line-in-git-gui-although-no-newline-at-end-of-file-warnin

```

## Pat Thoyts, 2013-01-09 14:49

Subject: Re: Fwd: git-gui / Warning: "No newline at end of file”
Message-ID: <87vcb6bee5.fsf@fox.patthoyts.tk>
URL: https://gitlist.dev/e/87vcb6bee5.fsf%40fox.patthoyts.tk
In-Reply-To: <7vzk0qw8ts.fsf@alter.siamese.dyndns.org>

```
Junio C Hamano <gitster@pobox.com> writes:

>Tobias Preuss <tobias.preuss@googlemail.com> writes:
>
>> Hello. I never got a response. Did my email pass the distribution
>> list? Best, Tobias
>
>Pat?
>

I did have a brief look at this but I don't have a solution at the
moment. The "\ No newline at end of file" is emitted by git at we don't
appear to handle it well in the lib/diff.tcl apply_range_or_line
function.

>> ---------- Forwarded message ----------
>> From: Tobias Preuss <tobias.preuss@googlemail.com>
>> Date: Tue, Nov 13, 2012 at 9:26 PM
>> Subject: git-gui / Warning: "No newline at end of file”
>> To: git <git@vger.kernel.org>
>>
>>
>> Hello.
>> I noticed a problem when working with git-gui which might be a bug.
>> The issue only affects you when you are visually trying to stage
>> changes line by line. Here are the steps to reproduce the problem:
>>
>> 1. Initialize a new repository.
>> 2. Create a file with three lines of content each with the word
>> "Hello". Do not put a new line at the end of the file.
>> 3. Add and commit the file.
>> 4. Edit the same file putting words inbetween the three lines.
>> 5. Open git-gui and try to stage the changes line by line.
>>
>> The editor will append the warning "No newline at end of file” to the
>> end of the diff. When you are trying to stage a line an error occurs.
>> The problem is also illustrated in a question on Stackoverflow [1].
>>
>> Please let me know if you need more information or if I should send
>> this problem to some other mailing list.
>> Thank you, Tobias
>>
>> ____________
>> [1] http://stackoverflow.com/questions/13223868/how-to-stage-line-by-line-in-git-gui-although-no-newline-at-end-of-file-warnin
>

-- 
Pat Thoyts                            http://www.patthoyts.tk/
PGP fingerprint 2C 6E 98 07 2C 59 C8 97  10 CE 11 E6 04 E0 B9 DD

```

## Heiko Voigt, 2013-05-09 16:30

Subject: [PATCH] git-gui: allow "\ No newline at end of file" for linewise staging
Message-ID: <20130509163002.GA3630@book-mint>
URL: https://gitlist.dev/e/20130509163002.GA3630%40book-mint
In-Reply-To: <87vcb6bee5.fsf@fox.patthoyts.tk>

```
Counting of lines did not skip this line when generating the hunk
header.

Signed-off-by: Heiko Voigt <hvoigt@hvoigt.net>
---
Here is an attempt at fixing the no newline issue. I would appreciate
another pair of eyes though.

 git-gui/lib/diff.tcl | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/git-gui/lib/diff.tcl b/git-gui/lib/diff.tcl
index ec44055..30d9a79 100644
--- a/git-gui/lib/diff.tcl
+++ b/git-gui/lib/diff.tcl
@@ -764,8 +764,15 @@ proc apply_range_or_line {x y} {
 				# context line
 				set ln [$ui_diff get $i_l $next_l]
 				set patch "$patch$pre_context$ln"
-				set n [expr $n+1]
-				set m [expr $m+1]
+				# Skip the "\ No newline at end of
+				# file". Depending on the locale setting
+				# we don't know what this line looks
+				# like exactly. The only thing we do
+				# know is that it starts with "\ "
+				if {![string match {\\ *} $ln]} {
+					set n [expr $n+1]
+					set m [expr $m+1]
+				}
 				set pre_context {}
 			} elseif {$c1 eq $to_context} {
 				# turn change line into context line
-- 
1.8.3.rc1.40.gba374ae

```

## Tobias Preuss, 2013-05-10 19:09

Subject: Re: [PATCH] git-gui: allow "\ No newline at end of file" for linewise staging
Message-ID: <CADEaiE_ddTLO0kYL5siN4rmycibuzHA3R1dz9mKyueYd0y_atw@mail.gmail.com>
URL: https://gitlist.dev/e/CADEaiE_ddTLO0kYL5siN4rmycibuzHA3R1dz9mKyueYd0y_atw%40mail.gmail.com
In-Reply-To: <20130509163002.GA3630@book-mint>

```
I tested the patch as I am the same person that bothered about the
behavior a while ago. Please merge.
- Sorry for sending an HTML formatted mail before.

On Thu, May 9, 2013 at 6:30 PM, Heiko Voigt <hvoigt@hvoigt.net> wrote:
> Counting of lines did not skip this line when generating the hunk
> header.
>
> Signed-off-by: Heiko Voigt <hvoigt@hvoigt.net>
> ---
> Here is an attempt at fixing the no newline issue. I would appreciate
> another pair of eyes though.
>
>  git-gui/lib/diff.tcl | 11 +++++++++--
>  1 file changed, 9 insertions(+), 2 deletions(-)
>
> diff --git a/git-gui/lib/diff.tcl b/git-gui/lib/diff.tcl
> index ec44055..30d9a79 100644
> --- a/git-gui/lib/diff.tcl
> +++ b/git-gui/lib/diff.tcl
> @@ -764,8 +764,15 @@ proc apply_range_or_line {x y} {
>                                 # context line
>                                 set ln [$ui_diff get $i_l $next_l]
>                                 set patch "$patch$pre_context$ln"
> -                               set n [expr $n+1]
> -                               set m [expr $m+1]
> +                               # Skip the "\ No newline at end of
> +                               # file". Depending on the locale setting
> +                               # we don't know what this line looks
> +                               # like exactly. The only thing we do
> +                               # know is that it starts with "\ "
> +                               if {![string match {\\ *} $ln]} {
> +                                       set n [expr $n+1]
> +                                       set m [expr $m+1]
> +                               }
>                                 set pre_context {}
>                         } elseif {$c1 eq $to_context} {
>                                 # turn change line into context line
> --
> 1.8.3.rc1.40.gba374ae
>

```
