diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2012-09-05 00:04:56 -0700 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2012-09-05 00:04:56 -0700 |
commit | 6e5cd8e0810c63e8744c23cdb887fea78ea37fb4 (patch) | |
tree | 10c5d3887040acf9bd8493479b3a550c34ad1e58 /app | |
parent | 7c76c8d34803faaf606f9a2387f37529b3e42d41 (diff) | |
parent | 5e1c63d3f0f0729a1d9d9e19c64b2fef65cc30fb (diff) | |
download | gitlab-ce-6e5cd8e0810c63e8744c23cdb887fea78ea37fb4.tar.gz |
Merge pull request #1375 from tsigo/discover_default_branch
Default branch auto-discovery
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/application_controller.rb | 10 | ||||
-rw-r--r-- | app/controllers/commits_controller.rb | 14 | ||||
-rw-r--r-- | app/models/project.rb | 2 | ||||
-rw-r--r-- | app/roles/push_observer.rb | 16 | ||||
-rw-r--r-- | app/roles/repository.rb | 24 |
5 files changed, 47 insertions, 19 deletions
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d53b23bb246..7e53b8fe5ff 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/controllers/commits_controller.rb b/app/controllers/commits_controller.rb index 717912d9e92..5e10a1b6ee7 100644 --- a/app/controllers/commits_controller.rb +++ b/app/controllers/commits_controller.rb @@ -59,7 +59,7 @@ class CommitsController < ApplicationController def patch @commit = project.commit(params[:id]) - + send_data( @commit.to_patch, type: "text/plain", @@ -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 diff --git a/app/models/project.rb b/app/models/project.rb index a7735a42137..fc18ad55528 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -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 diff --git a/app/roles/push_observer.rb b/app/roles/push_observer.rb index 1067404d5af..947ed42345d 100644 --- a/app/roles/push_observer.rb +++ b/app/roles/push_observer.rb @@ -1,3 +1,6 @@ +# 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 diff --git a/app/roles/repository.rb b/app/roles/repository.rb index 5f6c35414e1..a77de4ad5f0 100644 --- a/app/roles/repository.rb +++ b/app/roles/repository.rb @@ -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 |