marcamillion marcamillion - 1 year ago 66
Ruby Question

Why is the Git Diff/Patch info different than the Github representation of that patch?

When you look at a diff of a file, it will show you the diff info at the top, and then it highlights the changes below.

However, in every example I have looked at...the line number that Github highlights with the change, is always different than the line number that Git specified in the Diff/Patch info.

For example this commit (note the diff data says

@@ -362,7 +362,7 @@ def association_instance_set(name, association
, yet Github begins the highlighting at line 365.)


Or this one:

enter image description here

Or this:

enter image description here

Or finally this one:

enter image description here

It seems as if the actual line-number highlighted by Github is usually around 3 lines higher than the patch/diff data from Git specified.

When I check their API, pull down the first file I highlighted and linked above, spit it out into an array and then do a line count in the array using
, I get a different result too.

The line where the diff specifies the change is made i.e.
, comes out using my array conversion method to
and not
as Github highlighted it.

So something is a bit off.

Why is that?

Answer Source

There is nothing wrong with those patches, they look all exactly how they should look.

Unified diff includes 3 lines for context.

Lets look at the hunk in your first example:

@@ -362,7 +362,7 @@

It says that the patch file starts at line 362, and that 7 lines are included in the diff. If we look at the diff, we can see that it indeed starts at line 362 and is 7 lines long.

If we look at the diff in more detail, we see that line 362, 363, 364 are produced verbatim. Line 365 is labeled with a - (respective +) because it got removed and another line re-inserted. This is highlighted by the red/green color in the output. One thing that is not in the actual diff file is GitHubs highlighting of exactly which parts of the line was changed. Thats a custom enhancement of GitHub.

And after that, the three next context lines which are not changed are displayed verbatim.

Unified diff simply provides context lines and includes them in the diff, and GitHub shows it this way, too.

You have 1 changed line (365), and three lines before and after for context. makes 7 lines in total that are included in the patch/diff file (starting at 362).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download