Commit c9920c42 authored by Douwe Maan's avatar Douwe Maan

Merge branch 'fix-backslashes-inline-diff' into 'master'

Fix bug where backslashes in inline diffs could be dropped

This MR fixes a bug in inline diff generation causing backslashes to be dropped. For example, the input:

```
input.to_s.sub(/[\r\n].+/,'').sub(/\\[rn].+/, '').strip
```

The second backslash is dropped in the second `sub` statement:

![image](https://gitlab.com/gitlab-org/gitlab-ce/uploads/16e513894f7ecda1b111fe7d43e7f388/image.png)

With this fix, it looks like:

![image](https://gitlab.com/gitlab-org/gitlab-ce/uploads/90fd635a937a68f1b2403740ebc75e3a/image.png)

Closes #2253

See merge request !1143
parents f4cb6e43 c36adb98
......@@ -2,6 +2,7 @@ Please view this file on the master branch, on stable branches it's out of date.
v 7.14.0 (unreleased)
- Provide more feedback what went wrong if HipChat service failed test (Stan Hu)
- Fix bug where backslashes in inline diffs could be dropped (Stan Hu)
- Disable turbolinks when linking to Bitbucket import status (Stan Hu)
- Fix broken code import and display error messages if something went wrong with creating project (Stan Hu)
- Fix corrupted binary files when using API files endpoint (Stan Hu)
......
......@@ -46,8 +46,11 @@ module Gitlab
end
last_the_same_symbols += 1
last_token = first_line[last_the_same_symbols..-1]
diff_arr[index+1].sub!(/#{Regexp.escape(last_token)}$/, FINISH + last_token)
diff_arr[index+2].sub!(/#{Regexp.escape(last_token)}$/, FINISH + last_token)
# This is tricky: escape backslashes so that `sub` doesn't interpret them
# as backreferences. Regexp.escape does NOT do the right thing.
replace_token = FINISH + last_token.gsub(/\\/, '\&\&')
diff_arr[index+1].sub!(/#{Regexp.escape(last_token)}$/, replace_token)
diff_arr[index+2].sub!(/#{Regexp.escape(last_token)}$/, replace_token)
end
diff_arr
end
......
require 'spec_helper'
describe Gitlab::InlineDiff do
describe '#processing' do
let(:diff) do
<<eos
--- a/test.rb
+++ b/test.rb
@@ -1,6 +1,6 @@
class Test
def cleanup_string(input)
return nil if input.nil?
- input.sub(/[\\r\\n].+/,'').sub(/\\\\[rn].+/, '').strip
+ input.to_s.sub(/[\\r\\n].+/,'').sub(/\\\\[rn].+/, '').strip
end
end
eos
end
let(:expected) do
["--- a/test.rb\n",
"+++ b/test.rb\n",
"@@ -1,6 +1,6 @@\n",
" class Test\n",
" def cleanup_string(input)\n",
" return nil if input.nil?\n",
"- input.#!idiff-start!##!idiff-finish!#sub(/[\\r\\n].+/,'').sub(/\\\\[rn].+/, '').strip\n",
"+ input.#!idiff-start!#to_s.#!idiff-finish!#sub(/[\\r\\n].+/,'').sub(/\\\\[rn].+/, '').strip\n",
" end\n",
" end\n"]
end
let(:subject) { Gitlab::InlineDiff.processing(diff.lines) }
it 'should retain backslashes' do
expect(subject).to eq(expected)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment