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
iv
gitlab-ce
Commits
6e5cd8e0
Commit
6e5cd8e0
authored
Sep 05, 2012
by
Dmitriy Zaporozhets
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1375 from tsigo/discover_default_branch
Default branch auto-discovery
parents
7c76c8d3
5e1c63d3
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
118 additions
and
28 deletions
+118
-28
app/controllers/application_controller.rb
app/controllers/application_controller.rb
+0
-10
app/controllers/commits_controller.rb
app/controllers/commits_controller.rb
+13
-1
app/models/project.rb
app/models/project.rb
+1
-1
app/roles/push_observer.rb
app/roles/push_observer.rb
+12
-4
app/roles/repository.rb
app/roles/repository.rb
+21
-3
db/migrate/20120905043334_set_default_branch_default_to_nil.rb
...grate/20120905043334_set_default_branch_default_to_nil.rb
+12
-0
db/schema.rb
db/schema.rb
+9
-9
spec/roles/repository_spec.rb
spec/roles/repository_spec.rb
+50
-0
No files found.
app/controllers/application_controller.rb
View file @
6e5cd8e0
...
...
@@ -120,16 +120,6 @@ class ApplicationController < ActionController::Base
end
end
def
load_refs
if
params
[
:ref
].
blank?
@branch
=
params
[
:branch
].
blank?
?
nil
:
params
[
:branch
]
@tag
=
params
[
:tag
].
blank?
?
nil
:
params
[
:tag
]
@ref
=
@branch
||
@tag
||
@project
.
try
(
:default_branch
)
||
Repository
.
default_ref
else
@ref
=
params
[
:ref
]
end
end
def
render_404
render
file:
File
.
join
(
Rails
.
root
,
"public"
,
"404"
),
layout:
false
,
status:
"404"
end
...
...
app/controllers/commits_controller.rb
View file @
6e5cd8e0
...
...
@@ -67,4 +67,16 @@ class CommitsController < ApplicationController
filename:
(
@commit
.
id
.
to_s
+
".patch"
)
)
end
protected
def
load_refs
if
params
[
:ref
].
blank?
@branch
=
params
[
:branch
].
blank?
?
nil
:
params
[
:branch
]
@tag
=
params
[
:tag
].
blank?
?
nil
:
params
[
:tag
]
@ref
=
@branch
||
@tag
||
@project
.
try
(
:default_branch
)
||
'master'
else
@ref
=
params
[
:ref
]
end
end
end
app/models/project.rb
View file @
6e5cd8e0
...
...
@@ -187,7 +187,7 @@ end
# private_flag :boolean(1) default(TRUE), not null
# code :string(255)
# owner_id :integer(4)
# default_branch :string(255)
default("master"), not null
# default_branch :string(255)
# issues_enabled :boolean(1) default(TRUE), not null
# wall_enabled :boolean(1) default(TRUE), not null
# merge_requests_enabled :boolean(1) default(TRUE), not null
...
...
app/roles/push_observer.rb
View file @
6e5cd8e0
# Includes methods for handling Git Push events
#
# Triggered by PostReceive job
module
PushObserver
def
observe_push
(
oldrev
,
newrev
,
ref
,
user
)
data
=
post_receive_data
(
oldrev
,
newrev
,
ref
,
user
)
...
...
@@ -84,11 +87,10 @@ module PushObserver
data
end
# This method will be called after each post receive
# and only if user present in gitlab.
# All callbacks for post receive should be placed here
# This method will be called after each post receive and only if the provided
# user is present in GitLab.
#
# All callbacks for post receive should be placed here.
def
trigger_post_receive
(
oldrev
,
newrev
,
ref
,
user
)
# Create push event
self
.
observe_push
(
oldrev
,
newrev
,
ref
,
user
)
...
...
@@ -101,5 +103,11 @@ module PushObserver
# Create satellite
self
.
satellite
.
create
unless
self
.
satellite
.
exists?
# Discover the default branch, but only if it hasn't already been set to
# something else
if
default_branch
.
nil?
update_attributes
(
default_branch:
discover_default_branch
)
end
end
end
app/roles/repository.rb
View file @
6e5cd8e0
...
...
@@ -94,6 +94,24 @@ module Repository
end
.
sort_by
(
&
:name
)
end
# Discovers the default branch based on the repository's available branches
#
# - If no branches are present, returns nil
# - If one branch is present, returns its name
# - If two or more branches are present, returns the one that has a name
# matching root_ref (default_branch or 'master' if default_branch is nil)
def
discover_default_branch
branches
=
heads
.
collect
(
&
:name
)
if
branches
.
length
==
0
nil
elsif
branches
.
length
==
1
branches
.
first
else
branches
.
select
{
|
v
|
v
==
root_ref
}.
first
end
end
def
has_commits?
!!
commit
end
...
...
@@ -102,7 +120,7 @@ module Repository
default_branch
||
"master"
end
def
root_ref?
branch
def
root_ref?
(
branch
)
root_ref
==
branch
end
...
...
@@ -111,7 +129,7 @@ module Repository
# Already packed repo archives stored at
# app_root/tmp/repositories/project_name/project_name-commit-id.tag.gz
#
def
archive_repo
ref
def
archive_repo
(
ref
)
ref
=
ref
||
self
.
root_ref
commit
=
self
.
commit
(
ref
)
return
nil
unless
commit
...
...
@@ -138,6 +156,6 @@ module Repository
end
def
http_url_to_repo
http_url
=
[
Gitlab
.
config
.
url
,
"/"
,
path
,
".git"
].
join
()
http_url
=
[
Gitlab
.
config
.
url
,
"/"
,
path
,
".git"
].
join
(
''
)
end
end
db/migrate/20120905043334_set_default_branch_default_to_nil.rb
0 → 100644
View file @
6e5cd8e0
class
SetDefaultBranchDefaultToNil
<
ActiveRecord
::
Migration
def
up
# Set the default_branch to allow nil, and default it to nil
change_column_null
(
:projects
,
:default_branch
,
true
)
change_column_default
(
:projects
,
:default_branch
,
nil
)
end
def
down
change_column_null
(
:projects
,
:default_branch
,
false
)
change_column_default
(
:projects
,
:default_branch
,
'master'
)
end
end
db/schema.rb
View file @
6e5cd8e0
...
...
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord
::
Schema
.
define
(
:version
=>
20120
729131232
)
do
ActiveRecord
::
Schema
.
define
(
:version
=>
20120
905043334
)
do
create_table
"events"
,
:force
=>
true
do
|
t
|
t
.
string
"target_type"
...
...
@@ -103,7 +103,7 @@ ActiveRecord::Schema.define(:version => 20120729131232) do
t
.
boolean
"private_flag"
,
:default
=>
true
,
:null
=>
false
t
.
string
"code"
t
.
integer
"owner_id"
t
.
string
"default_branch"
,
:default
=>
"master"
,
:null
=>
false
t
.
string
"default_branch"
t
.
boolean
"issues_enabled"
,
:default
=>
true
,
:null
=>
false
t
.
boolean
"wall_enabled"
,
:default
=>
true
,
:null
=>
false
t
.
boolean
"merge_requests_enabled"
,
:default
=>
true
,
:null
=>
false
...
...
spec/roles/repository_spec.rb
View file @
6e5cd8e0
...
...
@@ -19,4 +19,54 @@ describe Project, "Repository" do
project
.
should_not
be_empty_repo
end
end
describe
"#discover_default_branch"
do
let
(
:master
)
{
double
(
name:
'master'
)
}
let
(
:stable
)
{
double
(
name:
'stable'
)
}
it
"returns 'master' when master exists"
do
project
.
should_receive
(
:heads
).
and_return
([
stable
,
master
])
project
.
discover_default_branch
.
should
==
'master'
end
it
"returns non-master when master exists but default branch is set to something else"
do
project
.
default_branch
=
'stable'
project
.
should_receive
(
:heads
).
and_return
([
stable
,
master
])
project
.
discover_default_branch
.
should
==
'stable'
end
it
"returns a non-master branch when only one exists"
do
project
.
should_receive
(
:heads
).
and_return
([
stable
])
project
.
discover_default_branch
.
should
==
'stable'
end
it
"returns nil when no branch exists"
do
project
.
should_receive
(
:heads
).
and_return
([])
project
.
discover_default_branch
.
should
be_nil
end
end
describe
"#root_ref"
do
it
"returns default_branch when set"
do
project
.
default_branch
=
'stable'
project
.
root_ref
.
should
==
'stable'
end
it
"returns 'master' when default_branch is nil"
do
project
.
default_branch
=
nil
project
.
root_ref
.
should
==
'master'
end
end
describe
"#root_ref?"
do
it
"returns true when branch is root_ref"
do
project
.
default_branch
=
'stable'
project
.
root_ref?
(
'stable'
).
should
be_true
end
it
"returns false when branch is not root_ref"
do
project
.
default_branch
=
nil
project
.
root_ref?
(
'stable'
).
should
be_false
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