No newline at end of file

后端 未结 12 2123

When doing a git diff it says \"No newline at end of file\".

Ok, there is no newline at end of file. What\'s the big deal?

What\'s the

相关标签:
12条回答
  • 2020-11-27 09:39

    There is one thing that I don't see in previous responses. Warning about no end-of-line could be a warning when a portion of a file has been truncated. It could be a symptom of missing data.

    0 讨论(0)
  • 2020-11-27 09:40

    The only reason is that Unix historically had a convention of all human-readable text files ending in a newline. At the time, this avoided extra processing when displaying or joining text files, and avoided treating text files differently to files containing other kinds of data (eg raw binary data which isn't human-readable).

    Because of this convention, many tools from that era expect the ending newline, including text editors, diffing tools, and other text processing tools. Mac OS X was built on BSD Unix, and Linux was developed to be Unix-compatible, so both operating systems have inherited the same convention, behaviour and tools.

    Windows wasn't developed to be Unix-compatible, so it doesn't have the same convention, and most Windows software will deal just fine with no trailing newline.

    But, since Git was developed for Linux first, and a lot of open-source software is built on Unix-compatible systems like Linux, Mac OS X, FreeBSD, etc, most open-source communities and their tools (including programming languages) continue to follow these conventions.

    There are technical reasons which made sense in 1971, but in this era it's mostly convention and maintaining compatibility with existing tools.

    0 讨论(0)
  • 2020-11-27 09:45

    It just indicates that the end of the file doesn't have a newline. It's not a catastrophe it's just a message to make it clearer that there isn't one when looking at a diff in the command line.

    0 讨论(0)
  • 2020-11-27 09:47

    If you add a new line of text at the end of the existing file which does not already have a newline character at the end, the diff will show the old last line as having been modified, even though conceptually it wasn’t.

    This is at least one good reason to add a newline character at the end.

    Example

    A file contains:

    A() {
        // do something
    }
    

    Hexdump:

    00000000: 4128 2920 7b0a 2020 2020 2f2f 2064 6f20  A() {.    // do 
    00000010: 736f 6d65 7468 696e 670a 7d              something.}
    

    You now edit it to

    A() {
        // do something
    }
    // Useful comment
    

    Hexdump:

    00000000: 4128 2920 7b0a 2020 2020 2f2f 2064 6f20  A() {.    // do 
    00000010: 736f 6d65 7468 696e 670a 7d0a 2f2f 2055  something.}.// U
    00000020: 7365 6675 6c20 636f 6d6d 656e 742e 0a    seful comment..
    

    The git diff will show:

    -}
    \ No newline at end of file
    +}
    +// Useful comment.
    

    In other words, it shows a larger diff than conceptually occurred. It shows that you deleted the line } and added the line }\n. This is, in fact, what happened, but it’s not what conceptually happened, so it can be confusing.

    0 讨论(0)
  • 2020-11-27 09:47

    The core problem is what you define line and whether end-on-line character sequence is part of the line or not. UNIX-based editors (such as VIM) or tools (such as Git) use EOL character sequence as line terminator, therefore it's a part of the line. It's similar to use of semicolon (;) in C and Pascal. In C semicolon terminates statements, in Pascal it separates them.

    0 讨论(0)
  • 2020-11-27 09:47

    Source files are often concatenated by tools (C, C++: header files, Javascript: bundlers). If you omit the newline character, you could introduce nasty bugs (where the last line of one source is concatenated with the first line of the next source file). Hopefully all the source code concat tools out there insert a newline between concatenated files anyway but that doesn't always seem to be the case.

    The crux of the issue is - in most languages, newlines have semantic meaning and end-of-file is not a language defined alternative for the newline character. So you ought to terminate every statement/expression with a newline character -- including the last one.

    0 讨论(0)
提交回复
热议问题