BigW Consortium Gitlab
Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gitlab-ce
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Forest Godfrey
gitlab-ce
Commits
ca8a44be
Commit
ca8a44be
authored
Mar 02, 2016
by
Douwe Maan
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'issue_13716' into 'master'
Check for conflicts before creating new revert branch Fixes #13716 See merge request !2953
parents
8eadb373
d9e01915
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
62 additions
and
27 deletions
+62
-27
repository.rb
app/models/repository.rb
+20
-12
revert_service.rb
app/services/commits/revert_service.rb
+11
-11
repository_spec.rb
spec/models/repository_spec.rb
+31
-4
No files found.
app/models/repository.rb
View file @
ca8a44be
...
...
@@ -654,30 +654,38 @@ class Repository
end
end
def
revert
(
user
,
commit
,
base_branch
,
target_branch
=
nil
)
source_sha
=
find_branch
(
base_branch
).
target
target_branch
||=
base_branch
args
=
[
commit
.
id
,
source_sha
]
args
<<
{
mainline:
1
}
if
commit
.
merge_commit?
def
revert
(
user
,
commit
,
base_branch
,
revert_tree_id
=
nil
)
source_sha
=
find_branch
(
base_branch
).
target
revert_tree_id
||=
check_revert_content
(
commit
,
base_branch
)
revert_index
=
rugged
.
revert_commit
(
*
args
)
return
false
if
revert_index
.
conflicts?
tree_id
=
revert_index
.
write_tree
(
rugged
)
return
false
unless
diff_exists?
(
source_sha
,
tree_id
)
return
false
unless
revert_tree_id
commit_with_hooks
(
user
,
target
_branch
)
do
|
ref
|
commit_with_hooks
(
user
,
base
_branch
)
do
|
ref
|
committer
=
user_to_committer
(
user
)
source_sha
=
Rugged
::
Commit
.
create
(
rugged
,
message:
commit
.
revert_message
,
author:
committer
,
committer:
committer
,
tree:
tree_id
,
tree:
revert_
tree_id
,
parents:
[
rugged
.
lookup
(
source_sha
)],
update_ref:
ref
)
end
end
def
check_revert_content
(
commit
,
base_branch
)
source_sha
=
find_branch
(
base_branch
).
target
args
=
[
commit
.
id
,
source_sha
]
args
<<
{
mainline:
1
}
if
commit
.
merge_commit?
revert_index
=
rugged
.
revert_commit
(
*
args
)
return
false
if
revert_index
.
conflicts?
tree_id
=
revert_index
.
write_tree
(
rugged
)
return
false
unless
diff_exists?
(
source_sha
,
tree_id
)
tree_id
end
def
diff_exists?
(
sha1
,
sha2
)
rugged
.
diff
(
sha1
,
sha2
).
size
>
0
end
...
...
app/services/commits/revert_service.rb
View file @
ca8a44be
...
...
@@ -17,28 +17,28 @@ module Commits
def
commit
revert_into
=
@create_merge_request
?
@commit
.
revert_branch_name
:
@target_branch
revert_tree_id
=
repository
.
check_revert_content
(
@commit
,
@target_branch
)
if
@create_merge_request
# Temporary branch exists and contains the revert commit
return
success
if
repository
.
find_branch
(
revert_into
)
if
revert_tree_id
create_target_branch
(
revert_into
)
if
@create_merge_request
create_target_branch
end
unless
repository
.
revert
(
current_user
,
@commit
,
revert_into
)
repository
.
revert
(
current_user
,
@commit
,
revert_into
,
revert_tree_id
)
success
else
error_msg
=
"Sorry, we cannot revert this
#{
params
[
:revert_type_title
]
}
automatically.
It may have already been reverted, or a more recent commit may have updated some of its content."
raise
ReversionError
,
error_msg
end
success
end
private
def
create_target_branch
def
create_target_branch
(
new_branch
)
# Temporary branch exists and contains the revert commit
return
success
if
repository
.
find_branch
(
new_branch
)
result
=
CreateBranchService
.
new
(
@project
,
current_user
)
.
execute
(
@commit
.
revert_branch_name
,
@target_branch
,
source_project:
@source_project
)
.
execute
(
new_branch
,
@target_branch
,
source_project:
@source_project
)
if
result
[
:status
]
==
:error
raise
ReversionError
,
"There was an error creating the source branch:
#{
result
[
:message
]
}
"
...
...
spec/models/repository_spec.rb
View file @
ca8a44be
...
...
@@ -457,11 +457,38 @@ describe Repository, models: true do
end
end
describe
'#revert
_merge
'
do
it
'should revert the changes'
do
repository
.
revert
(
user
,
merge_commit
,
'master'
)
describe
'#revert'
do
let
(
:new_image_commit
)
{
repository
.
commit
(
'33f3729a45c02fc67d00adb1b8bca394b0e761d9'
)
}
let
(
:update_image_commit
)
{
repository
.
commit
(
'2f63565e7aac07bcdadb654e253078b727143ec4'
)
}
expect
(
repository
.
blob_at_branch
(
'master'
,
'files/ruby/feature.rb'
)).
not_to
be_present
context
'when there is a conflict'
do
it
'should abort the operation'
do
expect
(
repository
.
revert
(
user
,
new_image_commit
,
'master'
)).
to
eq
(
false
)
end
end
context
'when commit was already reverted'
do
it
'should abort the operation'
do
repository
.
revert
(
user
,
update_image_commit
,
'master'
)
expect
(
repository
.
revert
(
user
,
update_image_commit
,
'master'
)).
to
eq
(
false
)
end
end
context
'when commit can be reverted'
do
it
'should revert the changes'
do
expect
(
repository
.
revert
(
user
,
update_image_commit
,
'master'
)).
to
be_truthy
end
end
context
'reverting a merge commit'
do
it
'should revert the changes'
do
merge_commit
expect
(
repository
.
blob_at_branch
(
'master'
,
'files/ruby/feature.rb'
)).
to
be_present
repository
.
revert
(
user
,
merge_commit
,
'master'
)
expect
(
repository
.
blob_at_branch
(
'master'
,
'files/ruby/feature.rb'
)).
not_to
be_present
end
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