Commit 95cf5a09 authored by Douwe Maan's avatar Douwe Maan Committed by Ruben Davila

Merge branch '20895-inline-comments-for-first-commit' into 'master'

Fix line commenting for the initial commit

## What does this MR do?

Support line positions on the initial commit, where we can't compare because there's no parent commit.

## Are there points in the code the reviewer needs to double check?

I chose to use the blank SHA to represent the initial commit, but it could as easily be the same SHA. I just thought this was clearer.

## Why was this MR needed?

People couldn't add line comments to the initial commit!

## What are the relevant issue numbers?

Closes #20895.

## Screenshots (if relevant)

![Comment_on_initial_commit](/uploads/1164448462ff55d133f3ff51c98044df/Comment_on_initial_commit.gif)

## Does this MR meet the acceptance criteria?

- [x] [CHANGELOG](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/CHANGELOG) entry added (N/A, regression)
- [x] [Documentation created/updated](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/development/doc_styleguide.md) (N/A)
- [x] API support added (N/A)
- Tests
  - [x] Added for this feature/bug
  - [x] All builds are passing
- [x] Conform by the [style guides](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/CONTRIBUTING.md#style-guides)
- [x] Branch has no merge conflicts with `master` (if you do - rebase it please)
- [x] [Squashed related commits together](https://git-scm.com/book/en/Git-Tools-Rewriting-History#Squashing-Commits)

See merge request !5900
parent af3026c7
...@@ -6,6 +6,7 @@ v 8.11.0 (unreleased) ...@@ -6,6 +6,7 @@ v 8.11.0 (unreleased)
- Ability to specify branches for Pivotal Tracker integration (Egor Lynko) - Ability to specify branches for Pivotal Tracker integration (Egor Lynko)
- Fix don't pass a local variable called `i` to a partial. !20510 (herminiotorres) - Fix don't pass a local variable called `i` to a partial. !20510 (herminiotorres)
- Fix rename `add_users_into_project` and `projects_ids`. !20512 (herminiotorres) - Fix rename `add_users_into_project` and `projects_ids`. !20512 (herminiotorres)
- Fix adding line comments on the initial commit to a repo !5900
- Fix the title of the toggle dropdown button. !5515 (herminiotorres) - Fix the title of the toggle dropdown button. !5515 (herminiotorres)
- Rename `markdown_preview` routes to `preview_markdown`. (Christopher Bartz) - Rename `markdown_preview` routes to `preview_markdown`. (Christopher Bartz)
- Update to Ruby 2.3.1. !4948 - Update to Ruby 2.3.1. !4948
......
...@@ -229,7 +229,7 @@ class Commit ...@@ -229,7 +229,7 @@ class Commit
def diff_refs def diff_refs
Gitlab::Diff::DiffRefs.new( Gitlab::Diff::DiffRefs.new(
base_sha: self.parent_id || self.sha, base_sha: self.parent_id || Gitlab::Git::BLANK_SHA,
head_sha: self.sha head_sha: self.sha
) )
end end
......
...@@ -139,13 +139,19 @@ module Gitlab ...@@ -139,13 +139,19 @@ module Gitlab
private private
def find_diff_file(repository) def find_diff_file(repository)
diffs = Gitlab::Git::Compare.new( # We're at the initial commit, so just get that as we can't compare to anything.
repository.raw_repository, if Gitlab::Git.blank_ref?(start_sha)
start_sha, compare = Gitlab::Git::Commit.find(repository.raw_repository, head_sha)
head_sha else
).diffs(paths: paths) compare = Gitlab::Git::Compare.new(
repository.raw_repository,
start_sha,
head_sha
)
end
diff = compare.diffs(paths: paths).first
diff = diffs.first
return unless diff return unless diff
Gitlab::Diff::File.new(diff, repository: repository, diff_refs: diff_refs) Gitlab::Diff::File.new(diff, repository: repository, diff_refs: diff_refs)
......
...@@ -339,6 +339,48 @@ describe Gitlab::Diff::Position, lib: true do ...@@ -339,6 +339,48 @@ describe Gitlab::Diff::Position, lib: true do
end end
end end
describe "position for a file in the initial commit" do
let(:commit) { project.commit("1a0b36b3cdad1d2ee32457c102a8c0b7056fa863") }
subject do
described_class.new(
old_path: "README.md",
new_path: "README.md",
old_line: nil,
new_line: 1,
diff_refs: commit.diff_refs
)
end
describe "#diff_file" do
it "returns the correct diff file" do
diff_file = subject.diff_file(project.repository)
expect(diff_file.new_file).to be true
expect(diff_file.new_path).to eq(subject.new_path)
expect(diff_file.diff_refs).to eq(subject.diff_refs)
end
end
describe "#diff_line" do
it "returns the correct diff line" do
diff_line = subject.diff_line(project.repository)
expect(diff_line.added?).to be true
expect(diff_line.new_line).to eq(subject.new_line)
expect(diff_line.text).to eq("+testme")
end
end
describe "#line_code" do
it "returns the correct line code" do
line_code = Gitlab::Diff::LineCode.generate(subject.file_path, subject.new_line, 0)
expect(subject.line_code(project.repository)).to eq(line_code)
end
end
end
describe "#to_json" do describe "#to_json" do
let(:hash) do let(:hash) do
{ {
......
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