Unexpected result in git-diff

不想你离开。 提交于 2019-12-29 08:10:57

问题


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?


回答1:


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.




回答2:


git diff --patience produces the result that you expect. The method tries hard not to mark lines as added or removed when they did not change. It is not the default because it is computationally expensive. (And when the purpose is to generate a patch that is applied later, it does not matter most of the time whether a diff looks the one way or the other.)



来源:https://stackoverflow.com/questions/40550751/unexpected-result-in-git-diff

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!