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
Boxiang Sun
gitlab-ce
Commits
98535fc1
Commit
98535fc1
authored
Aug 08, 2017
by
blackst0ne
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add checks for branch existence before changing HEAD
parent
4490af8c
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
29 additions
and
8 deletions
+29
-8
app/models/project.rb
app/models/project.rb
+12
-7
app/services/projects/update_service.rb
app/services/projects/update_service.rb
+1
-1
changelogs/unreleased/36010-api-v4-allows-setting-a-branch-that-doesn-t-exist-as-the-default-one.yml
...etting-a-branch-that-doesn-t-exist-as-the-default-one.yml
+4
-0
spec/models/project_spec.rb
spec/models/project_spec.rb
+5
-0
spec/services/projects/update_service_spec.rb
spec/services/projects/update_service_spec.rb
+7
-0
No files found.
app/models/project.rb
View file @
98535fc1
...
@@ -1046,13 +1046,18 @@ class Project < ActiveRecord::Base
...
@@ -1046,13 +1046,18 @@ class Project < ActiveRecord::Base
end
end
def
change_head
(
branch
)
def
change_head
(
branch
)
repository
.
before_change_head
if
repository
.
branch_exists?
(
branch
)
repository
.
rugged
.
references
.
create
(
'HEAD'
,
repository
.
before_change_head
"refs/heads/
#{
branch
}
"
,
repository
.
rugged
.
references
.
create
(
'HEAD'
,
force:
true
)
"refs/heads/
#{
branch
}
"
,
repository
.
copy_gitattributes
(
branch
)
force:
true
)
repository
.
after_change_head
repository
.
copy_gitattributes
(
branch
)
reload_default_branch
repository
.
after_change_head
reload_default_branch
else
errors
.
add
(
:base
,
"Could not change HEAD: branch '
#{
branch
}
' does not exist"
)
false
end
end
end
def
forked_from?
(
project
)
def
forked_from?
(
project
)
...
...
app/services/projects/update_service.rb
View file @
98535fc1
...
@@ -10,7 +10,7 @@ module Projects
...
@@ -10,7 +10,7 @@ module Projects
end
end
if
changing_default_branch?
if
changing_default_branch?
project
.
change_head
(
params
[
:default_branch
])
return
error
(
"Could not set the default branch"
)
unless
project
.
change_head
(
params
[
:default_branch
])
end
end
if
project
.
update_attributes
(
params
.
except
(
:default_branch
))
if
project
.
update_attributes
(
params
.
except
(
:default_branch
))
...
...
changelogs/unreleased/36010-api-v4-allows-setting-a-branch-that-doesn-t-exist-as-the-default-one.yml
0 → 100644
View file @
98535fc1
---
title
:
Add checks for branch existence before changing HEAD
merge_request
:
13359
author
:
Vitaliy @blackst0ne Klachkov
spec/models/project_spec.rb
View file @
98535fc1
...
@@ -1832,6 +1832,11 @@ describe Project do
...
@@ -1832,6 +1832,11 @@ describe Project do
describe
'#change_head'
do
describe
'#change_head'
do
let
(
:project
)
{
create
(
:project
,
:repository
)
}
let
(
:project
)
{
create
(
:project
,
:repository
)
}
it
'returns error if branch does not exist'
do
expect
(
project
.
change_head
(
'unexisted-branch'
)).
to
be
false
expect
(
project
.
errors
.
size
).
to
eq
(
1
)
end
it
'calls the before_change_head and after_change_head methods'
do
it
'calls the before_change_head and after_change_head methods'
do
expect
(
project
.
repository
).
to
receive
(
:before_change_head
)
expect
(
project
.
repository
).
to
receive
(
:before_change_head
)
expect
(
project
.
repository
).
to
receive
(
:after_change_head
)
expect
(
project
.
repository
).
to
receive
(
:after_change_head
)
...
...
spec/services/projects/update_service_spec.rb
View file @
98535fc1
...
@@ -101,6 +101,13 @@ describe Projects::UpdateService, '#execute' do
...
@@ -101,6 +101,13 @@ describe Projects::UpdateService, '#execute' do
expect
(
Project
.
find
(
project
.
id
).
default_branch
).
to
eq
'feature'
expect
(
Project
.
find
(
project
.
id
).
default_branch
).
to
eq
'feature'
end
end
it
'does not change a default branch'
do
# The branch 'unexisted-branch' does not exist.
update_project
(
project
,
admin
,
default_branch:
'unexisted-branch'
)
expect
(
Project
.
find
(
project
.
id
).
default_branch
).
to
eq
'master'
end
end
end
context
'when updating a project that contains container images'
do
context
'when updating a project that contains container images'
do
...
...
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