Skip to contentSkip to conversation

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

PATCH In review Tobias Preuss 3 participants 5 messages Jun 15, 2016 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

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

#1 lore raw

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

---------- Forwarded message ----------
From: Tobias Preuss <[email protected]>
Date: Tue, Nov 13, 2012 at 9:26 PM
Subject: git-gui / Warning: "No newline at end of file”
To: git <[email protected]>

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

A message that is not in the archive (sent off-list or not yet received): [email protected]

#3 lore raw
Quoted 7 lines · Junio C Hamano
Quoted 2 lines · Tobias Preuss

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.

Quoted 30 lines
Quoted 29 lines

---------- Forwarded message ----------
From: Tobias Preuss <[email protected]>
Date: Tue, Nov 13, 2012 at 9:26 PM
Subject: git-gui / Warning: "No newline at end of file”
To: git <[email protected]>

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

Signature · 2 lines
Pat Thoyts                            http://www.patthoyts.tk/
PGP fingerprint 2C 6E 98 07 2C 59 C8 97  10 CE 11 E6 04 E0 B9 DD
#4 lore raw

PATCH 1/1 git-gui: allow "\ No newline at end of file" for linewise staging

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

---
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(-)
git-gui/lib/diff.tcl +9 -2
index ec44055..30d9a79 100644
@@ -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
Signature · 1 line
1.8.3.rc1.40.gba374ae
#5 lore raw

Re: [PATCH] git-gui: allow "\ No newline at end of file" for linewise staging

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.

Quoted 36 lines · Heiko Voigt

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

---
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(-)
git-gui/lib/diff.tcl +9 -3
index ec44055..30d9a79 100644
@@ -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