Search code examples
gitgit-diff

Unexpected result in git-diff


Why I get this patch:

@@ -129,8 +132,9 @@ sub _preprocess_message {
 sub _process_message {
     my ($self, $message) = @_;

-    my $method = ref($message) eq 'HASH' ? $message->{method} : undef;
+    my $time =  [ gettimeofday ];

+    my $method = ref($message) eq 'HASH' ? $message->{method} : undef;
     return $self->send_error(ERROR_REQUEST_INVALID)
         unless defined($method);

When remove empty line after my $method = ...:

@@ -129,6 +132,8 @@ sub _preprocess_message {
 sub _process_message {
     my ($self, $message) = @_;

+    my $time =  [ gettimeofday ];
+
     my $method = ref($message) eq 'HASH' ? $message->{method} : undef;

     return $self->send_error(ERROR_REQUEST_INVALID)

I expect to see this patch instead of showed first one:

@@ -129,6 +132,8 @@ sub _preprocess_message {
 sub _process_message {
     my ($self, $message) = @_;

+    my $time =  [ gettimeofday ];
+
     my $method = ref($message) eq 'HASH' ? $message->{method} : undef;
-     
     return $self->send_error(ERROR_REQUEST_INVALID)

The my $method = ref($message) eq 'HASH' ? $message->{method} : undef; did not changed at all: there is no change in whitespace and EOL is same

Maybe should I supply some extra options to git to get this behaviour?


Solution

  • Git's diff implements a specific variant of the generalized minimal edit distance or string to string edit problem. We're given some initial set of symbols and some final set, and we are told to come up with the fewest edit instructions out of some limited set of edit commands.

    In our particular case, the only allowed instructions are "delete symbol" and "add symbol" (there is no "move" allowed, but see below). Moreover, we have no knowledge of what each symbol means, but each "symbol" is a source line.

    The two "symbols" are the same if and only if they match exactly, or (with certain end-of-line and/or white-space options turned on) match after stripping some items away (white space or carriage returns, mainly). Our job is to produce the smallest number of "delete" and "insert new" commands.

    The diff you show has two "insert"s and one "delete". The diff Git produces also has two "insert"s and one "delete". As far as Git can tell, this makes them equal. The one it chooses is just a matter of which of several "equal" trace-back paths it chooses through a comparison matrix.

    The code in git blame allows a different algorithm that does allow moves. Solving the problem when allowing moves is much harder, so git diff just doesn't bother. To enable move detection in git blame, use -M.