Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Léo-Paul Géneau
gitlab-ce
Commits
09fa0139
Commit
09fa0139
authored
Jul 27, 2016
by
Dmitriy Zaporozhets
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor merge request diff
Signed-off-by:
Dmitriy Zaporozhets
<
dmitriy.zaporozhets@gmail.com
>
parent
61902165
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
51 deletions
+38
-51
app/models/merge_request_diff.rb
app/models/merge_request_diff.rb
+38
-51
No files found.
app/models/merge_request_diff.rb
View file @
09fa0139
...
...
@@ -8,7 +8,8 @@ class MergeRequestDiff < ActiveRecord::Base
belongs_to
:merge_request
delegate
:source_branch_sha
,
:target_branch_sha
,
:target_branch
,
:source_branch
,
to: :merge_request
,
prefix:
nil
delegate
:source_branch_sha
,
:target_branch_sha
,
:target_branch
,
:source_branch
,
to: :merge_request
,
prefix:
nil
state_machine
:state
,
initial: :empty
do
state
:collected
...
...
@@ -24,9 +25,16 @@ class MergeRequestDiff < ActiveRecord::Base
serialize
:st_commits
serialize
:st_diffs
after_initialize
:set_diff_range
after_create
:reload_content
,
unless: :importing?
after_save
:keep_around_commits
,
unless: :importing?
def
set_diff_range
self
.
start_commit_sha
||=
target_branch_sha
self
.
head_commit_sha
||=
source_branch_sha
self
.
base_commit_sha
||=
branch_base_sha
end
def
reload_content
reload_commits
reload_diffs
...
...
@@ -41,8 +49,8 @@ class MergeRequestDiff < ActiveRecord::Base
@diffs_no_whitespace
||=
begin
compare
=
Gitlab
::
Git
::
Compare
.
new
(
repository
.
raw_repository
,
s
elf
.
start_commit_sha
||
self
.
target_branch
_sha
,
self
.
head_commit_sha
||
self
.
source_branch_sha
,
s
tart_commit
_sha
,
head_commit_sha
)
compare
.
diffs
(
options
)
end
...
...
@@ -65,35 +73,21 @@ class MergeRequestDiff < ActiveRecord::Base
end
def
base_commit
return
unless
self
.
base_commit_sha
return
unless
base_commit_sha
project
.
commit
(
self
.
base_commit_sha
)
project
.
commit
(
base_commit_sha
)
end
def
start_commit
return
unless
s
elf
.
s
tart_commit_sha
return
unless
start_commit_sha
project
.
commit
(
s
elf
.
s
tart_commit_sha
)
project
.
commit
(
start_commit_sha
)
end
def
head_commit
return
last_commit
unless
self
.
head_commit_sha
return
last_commit
unless
head_commit_sha
project
.
commit
(
self
.
head_commit_sha
)
end
def
compare
@compare
||=
begin
# Update ref for merge request
merge_request
.
fetch_ref
Gitlab
::
Git
::
Compare
.
new
(
repository
.
raw_repository
,
self
.
target_branch_sha
,
self
.
source_branch_sha
)
end
project
.
commit
(
head_commit_sha
)
end
def
diff_refs
...
...
@@ -108,16 +102,18 @@ class MergeRequestDiff < ActiveRecord::Base
private
# Collect array of Git::Commit objects
# between target and source branches
def
unmerged_commits
commits
=
compare
.
commits
if
commits
.
present?
commits
=
Commit
.
decorate
(
commits
,
merge_request
.
source_project
).
reverse
end
def
compare
@compare
||=
begin
# Update ref for merge request
merge_request
.
fetch_ref
commits
Gitlab
::
Git
::
Compare
.
new
(
repository
.
raw_repository
,
start_commit_sha
,
head_commit_sha
)
end
end
def
dump_commits
(
commits
)
...
...
@@ -133,21 +129,16 @@ class MergeRequestDiff < ActiveRecord::Base
def
reload_commits
new_attributes
=
{}
commit
_objects
=
unmerged_
commits
commit
s
=
compare
.
commits
if
commit_objects
.
present?
new_attributes
[
:st_commits
]
=
dump_commits
(
commit_objects
)
if
commits
.
present?
commits
=
Commit
.
decorate
(
commits
,
merge_request
.
source_project
).
reverse
new_attributes
[
:st_commits
]
=
dump_commits
(
commits
)
end
update_columns_serialized
(
new_attributes
)
end
# Collect array of Git::Diff objects
# between target and source branches
def
unmerged_diffs
compare
.
diffs
(
Commit
.
max_diff_options
)
end
def
dump_diffs
(
diffs
)
if
diffs
.
respond_to?
(
:map
)
diffs
.
map
(
&
:to_hash
)
...
...
@@ -177,7 +168,7 @@ class MergeRequestDiff < ActiveRecord::Base
if
commits
.
size
.
zero?
new_attributes
[
:state
]
=
:empty
else
diff_collection
=
unmerged_diffs
diff_collection
=
compare
.
diffs
(
Commit
.
max_diff_options
)
if
diff_collection
.
overflow?
# Set our state to 'overflow' to make the #empty? and #collected?
...
...
@@ -195,10 +186,6 @@ class MergeRequestDiff < ActiveRecord::Base
new_attributes
[
:st_diffs
]
=
new_diffs
new_attributes
[
:start_commit_sha
]
=
self
.
target_branch_sha
new_attributes
[
:head_commit_sha
]
=
self
.
source_branch_sha
new_attributes
[
:base_commit_sha
]
=
branch_base_sha
update_columns_serialized
(
new_attributes
)
keep_around_commits
...
...
@@ -213,9 +200,9 @@ class MergeRequestDiff < ActiveRecord::Base
end
def
branch_base_commit
return
unless
s
elf
.
source_branch_sha
&&
self
.
target_branch_sha
return
unless
s
ource_branch_sha
&&
target_branch_sha
project
.
merge_base_commit
(
s
elf
.
source_branch_sha
,
self
.
target_branch_sha
)
project
.
merge_base_commit
(
s
ource_branch_sha
,
target_branch_sha
)
end
def
branch_base_sha
...
...
@@ -254,8 +241,8 @@ class MergeRequestDiff < ActiveRecord::Base
end
def
keep_around_commits
repository
.
keep_around
(
target_branch
_sha
)
repository
.
keep_around
(
source_branch
_sha
)
repository
.
keep_around
(
b
ranch_base
_sha
)
repository
.
keep_around
(
start_commit
_sha
)
repository
.
keep_around
(
head_commit
_sha
)
repository
.
keep_around
(
b
ase_commit
_sha
)
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment