To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / app / models / repository / git.rb @ 1298:4f746d8966dd
History | View | Annotate | Download (8.3 KB)
| 1 | 441:cbce1fd3b1b7 | Chris | # Redmine - project management software
|
|---|---|---|---|
| 2 | 1295:622f24f53b42 | Chris | # Copyright (C) 2006-2013 Jean-Philippe Lang
|
| 3 | 0:513646585e45 | Chris | # Copyright (C) 2007 Patrick Aljord patcito@ŋmail.com
|
| 4 | 441:cbce1fd3b1b7 | Chris | #
|
| 5 | 0:513646585e45 | Chris | # This program is free software; you can redistribute it and/or
|
| 6 | # modify it under the terms of the GNU General Public License
|
||
| 7 | # as published by the Free Software Foundation; either version 2
|
||
| 8 | # of the License, or (at your option) any later version.
|
||
| 9 | 441:cbce1fd3b1b7 | Chris | #
|
| 10 | 0:513646585e45 | Chris | # This program is distributed in the hope that it will be useful,
|
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
| 13 | # GNU General Public License for more details.
|
||
| 14 | 441:cbce1fd3b1b7 | Chris | #
|
| 15 | 0:513646585e45 | Chris | # You should have received a copy of the GNU General Public License
|
| 16 | # along with this program; if not, write to the Free Software
|
||
| 17 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
| 18 | |||
| 19 | 1136:51d7f3e06556 | chris | require_dependency 'redmine/scm/adapters/git_adapter'
|
| 20 | 0:513646585e45 | Chris | |
| 21 | class Repository::Git < Repository |
||
| 22 | attr_protected :root_url
|
||
| 23 | validates_presence_of :url
|
||
| 24 | |||
| 25 | 1115:433d4f72a19b | Chris | def self.human_attribute_name(attribute_key_name, *args) |
| 26 | attr_name = attribute_key_name.to_s |
||
| 27 | 441:cbce1fd3b1b7 | Chris | if attr_name == "url" |
| 28 | attr_name = "path_to_repository"
|
||
| 29 | end
|
||
| 30 | 1115:433d4f72a19b | Chris | super(attr_name, *args)
|
| 31 | 245:051f544170fe | Chris | end
|
| 32 | |||
| 33 | def self.scm_adapter_class |
||
| 34 | 0:513646585e45 | Chris | Redmine::Scm::Adapters::GitAdapter |
| 35 | end
|
||
| 36 | 245:051f544170fe | Chris | |
| 37 | 0:513646585e45 | Chris | def self.scm_name |
| 38 | 'Git'
|
||
| 39 | end
|
||
| 40 | |||
| 41 | 441:cbce1fd3b1b7 | Chris | def report_last_commit |
| 42 | extra_report_last_commit |
||
| 43 | end
|
||
| 44 | |||
| 45 | def extra_report_last_commit |
||
| 46 | return false if extra_info.nil? |
||
| 47 | v = extra_info["extra_report_last_commit"]
|
||
| 48 | return false if v.nil? |
||
| 49 | v.to_s != '0'
|
||
| 50 | end
|
||
| 51 | |||
| 52 | def supports_directory_revisions? |
||
| 53 | true
|
||
| 54 | end
|
||
| 55 | |||
| 56 | 909:cbb26bc654de | Chris | def supports_revision_graph? |
| 57 | true
|
||
| 58 | end
|
||
| 59 | |||
| 60 | 245:051f544170fe | Chris | def repo_log_encoding |
| 61 | 'UTF-8'
|
||
| 62 | end
|
||
| 63 | |||
| 64 | 117:af80e5618e9b | Chris | # Returns the identifier for the given git changeset
|
| 65 | def self.changeset_identifier(changeset) |
||
| 66 | changeset.scmid |
||
| 67 | end
|
||
| 68 | |||
| 69 | # Returns the readable identifier for the given git changeset
|
||
| 70 | def self.format_changeset_identifier(changeset) |
||
| 71 | changeset.revision[0, 8] |
||
| 72 | end
|
||
| 73 | |||
| 74 | 0:513646585e45 | Chris | def branches |
| 75 | scm.branches |
||
| 76 | end
|
||
| 77 | |||
| 78 | def tags |
||
| 79 | scm.tags |
||
| 80 | end
|
||
| 81 | |||
| 82 | 507:0c939c159af4 | Chris | def default_branch |
| 83 | scm.default_branch |
||
| 84 | 909:cbb26bc654de | Chris | rescue Exception => e |
| 85 | logger.error "git: error during get default branch: #{e.message}"
|
||
| 86 | nil
|
||
| 87 | 507:0c939c159af4 | Chris | end
|
| 88 | |||
| 89 | 245:051f544170fe | Chris | def find_changeset_by_name(name) |
| 90 | 1115:433d4f72a19b | Chris | if name.present?
|
| 91 | changesets.where(:revision => name.to_s).first ||
|
||
| 92 | changesets.where('scmid LIKE ?', "#{name}%").first |
||
| 93 | end
|
||
| 94 | 245:051f544170fe | Chris | end
|
| 95 | |||
| 96 | 441:cbce1fd3b1b7 | Chris | def entries(path=nil, identifier=nil) |
| 97 | 1115:433d4f72a19b | Chris | entries = scm.entries(path, identifier, :report_last_commit => extra_report_last_commit)
|
| 98 | load_entries_changesets(entries) |
||
| 99 | entries |
||
| 100 | 441:cbce1fd3b1b7 | Chris | end
|
| 101 | |||
| 102 | 909:cbb26bc654de | Chris | # With SCMs that have a sequential commit numbering,
|
| 103 | # such as Subversion and Mercurial,
|
||
| 104 | # Redmine is able to be clever and only fetch changesets
|
||
| 105 | # going forward from the most recent one it knows about.
|
||
| 106 | 1115:433d4f72a19b | Chris | #
|
| 107 | 909:cbb26bc654de | Chris | # However, Git does not have a sequential commit numbering.
|
| 108 | #
|
||
| 109 | # In order to fetch only new adding revisions,
|
||
| 110 | 1115:433d4f72a19b | Chris | # Redmine needs to save "heads".
|
| 111 | 909:cbb26bc654de | Chris | #
|
| 112 | 441:cbce1fd3b1b7 | Chris | # In Git and Mercurial, revisions are not in date order.
|
| 113 | # Redmine Mercurial fixed issues.
|
||
| 114 | # * Redmine Takes Too Long On Large Mercurial Repository
|
||
| 115 | # http://www.redmine.org/issues/3449
|
||
| 116 | # * Sorting for changesets might go wrong on Mercurial repos
|
||
| 117 | # http://www.redmine.org/issues/3567
|
||
| 118 | #
|
||
| 119 | # Database revision column is text, so Redmine can not sort by revision.
|
||
| 120 | # Mercurial has revision number, and revision number guarantees revision order.
|
||
| 121 | # Redmine Mercurial model stored revisions ordered by database id to database.
|
||
| 122 | # So, Redmine Mercurial model can use correct ordering revisions.
|
||
| 123 | #
|
||
| 124 | # Redmine Mercurial adapter uses "hg log -r 0:tip --limit 10"
|
||
| 125 | # to get limited revisions from old to new.
|
||
| 126 | # But, Git 1.7.3.4 does not support --reverse with -n or --skip.
|
||
| 127 | #
|
||
| 128 | 0:513646585e45 | Chris | # The repository can still be fully reloaded by calling #clear_changesets
|
| 129 | # before fetching changesets (eg. for offline resync)
|
||
| 130 | def fetch_changesets |
||
| 131 | 441:cbce1fd3b1b7 | Chris | scm_brs = branches |
| 132 | return if scm_brs.nil? || scm_brs.empty? |
||
| 133 | 1115:433d4f72a19b | Chris | |
| 134 | 441:cbce1fd3b1b7 | Chris | h1 = extra_info || {}
|
| 135 | h = h1.dup |
||
| 136 | 1115:433d4f72a19b | Chris | repo_heads = scm_brs.map{ |br| br.scmid }
|
| 137 | h["heads"] ||= []
|
||
| 138 | prev_db_heads = h["heads"].dup
|
||
| 139 | if prev_db_heads.empty?
|
||
| 140 | prev_db_heads += heads_from_branches_hash |
||
| 141 | end
|
||
| 142 | return if prev_db_heads.sort == repo_heads.sort |
||
| 143 | |||
| 144 | 441:cbce1fd3b1b7 | Chris | h["db_consistent"] ||= {}
|
| 145 | if changesets.count == 0 |
||
| 146 | h["db_consistent"]["ordering"] = 1 |
||
| 147 | merge_extra_info(h) |
||
| 148 | self.save
|
||
| 149 | elsif ! h["db_consistent"].has_key?("ordering") |
||
| 150 | h["db_consistent"]["ordering"] = 0 |
||
| 151 | merge_extra_info(h) |
||
| 152 | self.save
|
||
| 153 | end
|
||
| 154 | 1115:433d4f72a19b | Chris | save_revisions(prev_db_heads, repo_heads) |
| 155 | end
|
||
| 156 | |||
| 157 | def save_revisions(prev_db_heads, repo_heads) |
||
| 158 | h = {}
|
||
| 159 | opts = {}
|
||
| 160 | opts[:reverse] = true |
||
| 161 | opts[:excludes] = prev_db_heads
|
||
| 162 | opts[:includes] = repo_heads
|
||
| 163 | |||
| 164 | revisions = scm.revisions('', nil, nil, opts) |
||
| 165 | return if revisions.blank? |
||
| 166 | |||
| 167 | # Make the search for existing revisions in the database in a more sufficient manner
|
||
| 168 | #
|
||
| 169 | # Git branch is the reference to the specific revision.
|
||
| 170 | # Git can *delete* remote branch and *re-push* branch.
|
||
| 171 | #
|
||
| 172 | # $ git push remote :branch
|
||
| 173 | # $ git push remote branch
|
||
| 174 | #
|
||
| 175 | # After deleting branch, revisions remain in repository until "git gc".
|
||
| 176 | # On git 1.7.2.3, default pruning date is 2 weeks.
|
||
| 177 | # So, "git log --not deleted_branch_head_revision" return code is 0.
|
||
| 178 | #
|
||
| 179 | # After re-pushing branch, "git log" returns revisions which are saved in database.
|
||
| 180 | # So, Redmine needs to scan revisions and database every time.
|
||
| 181 | #
|
||
| 182 | # This is replacing the one-after-one queries.
|
||
| 183 | # Find all revisions, that are in the database, and then remove them from the revision array.
|
||
| 184 | # Then later we won't need any conditions for db existence.
|
||
| 185 | # Query for several revisions at once, and remove them from the revisions array, if they are there.
|
||
| 186 | # Do this in chunks, to avoid eventual memory problems (in case of tens of thousands of commits).
|
||
| 187 | # If there are no revisions (because the original code's algorithm filtered them),
|
||
| 188 | # then this part will be stepped over.
|
||
| 189 | # We make queries, just if there is any revision.
|
||
| 190 | limit = 100
|
||
| 191 | offset = 0
|
||
| 192 | revisions_copy = revisions.clone # revisions will change
|
||
| 193 | while offset < revisions_copy.size
|
||
| 194 | recent_changesets_slice = changesets.find( |
||
| 195 | :all,
|
||
| 196 | :conditions => [
|
||
| 197 | 'scmid IN (?)',
|
||
| 198 | revisions_copy.slice(offset, limit).map{|x| x.scmid}
|
||
| 199 | ] |
||
| 200 | ) |
||
| 201 | # Subtract revisions that redmine already knows about
|
||
| 202 | recent_revisions = recent_changesets_slice.map{|c| c.scmid}
|
||
| 203 | revisions.reject!{|r| recent_revisions.include?(r.scmid)}
|
||
| 204 | offset += limit |
||
| 205 | end
|
||
| 206 | |||
| 207 | revisions.each do |rev|
|
||
| 208 | transaction do
|
||
| 209 | # There is no search in the db for this revision, because above we ensured,
|
||
| 210 | # that it's not in the db.
|
||
| 211 | save_revision(rev) |
||
| 212 | 245:051f544170fe | Chris | end
|
| 213 | end
|
||
| 214 | 1115:433d4f72a19b | Chris | h["heads"] = repo_heads.dup
|
| 215 | merge_extra_info(h) |
||
| 216 | self.save
|
||
| 217 | 0:513646585e45 | Chris | end
|
| 218 | 1115:433d4f72a19b | Chris | private :save_revisions
|
| 219 | 0:513646585e45 | Chris | |
| 220 | 441:cbce1fd3b1b7 | Chris | def save_revision(rev) |
| 221 | 1115:433d4f72a19b | Chris | parents = (rev.parents || []).collect{|rp| find_changeset_by_name(rp)}.compact
|
| 222 | changeset = Changeset.create(
|
||
| 223 | 441:cbce1fd3b1b7 | Chris | :repository => self, |
| 224 | :revision => rev.identifier,
|
||
| 225 | :scmid => rev.scmid,
|
||
| 226 | :committer => rev.author,
|
||
| 227 | :committed_on => rev.time,
|
||
| 228 | 1115:433d4f72a19b | Chris | :comments => rev.message,
|
| 229 | :parents => parents
|
||
| 230 | 441:cbce1fd3b1b7 | Chris | ) |
| 231 | 1115:433d4f72a19b | Chris | unless changeset.new_record?
|
| 232 | rev.paths.each { |change| changeset.create_change(change) }
|
||
| 233 | 441:cbce1fd3b1b7 | Chris | end
|
| 234 | 909:cbb26bc654de | Chris | changeset |
| 235 | 441:cbce1fd3b1b7 | Chris | end
|
| 236 | private :save_revision
|
||
| 237 | |||
| 238 | 1115:433d4f72a19b | Chris | def heads_from_branches_hash |
| 239 | h1 = extra_info || {}
|
||
| 240 | h = h1.dup |
||
| 241 | h["branches"] ||= {}
|
||
| 242 | h['branches'].map{|br, hs| hs['last_scmid']} |
||
| 243 | end
|
||
| 244 | |||
| 245 | 0:513646585e45 | Chris | def latest_changesets(path,rev,limit=10) |
| 246 | revisions = scm.revisions(path, nil, rev, :limit => limit, :all => false) |
||
| 247 | return [] if revisions.nil? || revisions.empty? |
||
| 248 | |||
| 249 | changesets.find( |
||
| 250 | 441:cbce1fd3b1b7 | Chris | :all,
|
| 251 | 0:513646585e45 | Chris | :conditions => [
|
| 252 | 441:cbce1fd3b1b7 | Chris | "scmid IN (?)",
|
| 253 | 0:513646585e45 | Chris | revisions.map!{|c| c.scmid}
|
| 254 | 1295:622f24f53b42 | Chris | ] |
| 255 | 0:513646585e45 | Chris | ) |
| 256 | end
|
||
| 257 | 1115:433d4f72a19b | Chris | |
| 258 | def clear_extra_info_of_changesets |
||
| 259 | return if extra_info.nil? |
||
| 260 | v = extra_info["extra_report_last_commit"]
|
||
| 261 | write_attribute(:extra_info, nil) |
||
| 262 | h = {}
|
||
| 263 | h["extra_report_last_commit"] = v
|
||
| 264 | merge_extra_info(h) |
||
| 265 | self.save
|
||
| 266 | end
|
||
| 267 | private :clear_extra_info_of_changesets
|
||
| 268 | 0:513646585e45 | Chris | end |