Commit 443e23e61a10f34e9eccc3b6e91803a0996f5050
1 parent
f06d98e9
Exists in
master
and in
4 other branches
Add Repository#discover_default_branch and add it to PushObserver
Showing
2 changed files
with
33 additions
and
7 deletions
Show diff stats
app/roles/push_observer.rb
| 1 | +# Includes methods for handling Git Push events | |
| 2 | +# | |
| 3 | +# Triggered by PostReceive job | |
| 1 | 4 | module PushObserver |
| 2 | 5 | def observe_push(oldrev, newrev, ref, user) |
| 3 | 6 | data = post_receive_data(oldrev, newrev, ref, user) |
| ... | ... | @@ -84,11 +87,10 @@ module PushObserver |
| 84 | 87 | data |
| 85 | 88 | end |
| 86 | 89 | |
| 87 | - | |
| 88 | - # This method will be called after each post receive | |
| 89 | - # and only if user present in gitlab. | |
| 90 | - # All callbacks for post receive should be placed here | |
| 90 | + # This method will be called after each post receive and only if the provided | |
| 91 | + # user is present in GitLab. | |
| 91 | 92 | # |
| 93 | + # All callbacks for post receive should be placed here. | |
| 92 | 94 | def trigger_post_receive(oldrev, newrev, ref, user) |
| 93 | 95 | # Create push event |
| 94 | 96 | self.observe_push(oldrev, newrev, ref, user) |
| ... | ... | @@ -101,5 +103,11 @@ module PushObserver |
| 101 | 103 | |
| 102 | 104 | # Create satellite |
| 103 | 105 | self.satellite.create unless self.satellite.exists? |
| 106 | + | |
| 107 | + # Discover the default branch, but only if it hasn't already been set to | |
| 108 | + # something else | |
| 109 | + if default_branch.nil? | |
| 110 | + update_attributes(default_branch: discover_default_branch) | |
| 111 | + end | |
| 104 | 112 | end |
| 105 | 113 | end | ... | ... |
app/roles/repository.rb
| ... | ... | @@ -94,6 +94,24 @@ module Repository |
| 94 | 94 | end.sort_by(&:name) |
| 95 | 95 | end |
| 96 | 96 | |
| 97 | + # Discovers the default branch based on the repository's available branches | |
| 98 | + # | |
| 99 | + # - If no branches are present, returns nil | |
| 100 | + # - If one branch is present, returns its name | |
| 101 | + # - If two or more branches are present, returns the one that has a name | |
| 102 | + # matching root_ref (default_branch or 'master' if default_branch is nil) | |
| 103 | + def discover_default_branch | |
| 104 | + branches = heads.collect(&:name) | |
| 105 | + | |
| 106 | + if branches.length == 0 | |
| 107 | + nil | |
| 108 | + elsif branches.length == 1 | |
| 109 | + branches.first | |
| 110 | + else | |
| 111 | + branches.select { |v| v == root_ref }.first | |
| 112 | + end | |
| 113 | + end | |
| 114 | + | |
| 97 | 115 | def has_commits? |
| 98 | 116 | !!commit |
| 99 | 117 | end |
| ... | ... | @@ -102,7 +120,7 @@ module Repository |
| 102 | 120 | default_branch || "master" |
| 103 | 121 | end |
| 104 | 122 | |
| 105 | - def root_ref? branch | |
| 123 | + def root_ref?(branch) | |
| 106 | 124 | root_ref == branch |
| 107 | 125 | end |
| 108 | 126 | |
| ... | ... | @@ -111,7 +129,7 @@ module Repository |
| 111 | 129 | # Already packed repo archives stored at |
| 112 | 130 | # app_root/tmp/repositories/project_name/project_name-commit-id.tag.gz |
| 113 | 131 | # |
| 114 | - def archive_repo ref | |
| 132 | + def archive_repo(ref) | |
| 115 | 133 | ref = ref || self.root_ref |
| 116 | 134 | commit = self.commit(ref) |
| 117 | 135 | return nil unless commit |
| ... | ... | @@ -138,6 +156,6 @@ module Repository |
| 138 | 156 | end |
| 139 | 157 | |
| 140 | 158 | def http_url_to_repo |
| 141 | - http_url = [Gitlab.config.url, "/", path, ".git"].join() | |
| 159 | + http_url = [Gitlab.config.url, "/", path, ".git"].join('') | |
| 142 | 160 | end |
| 143 | 161 | end | ... | ... |