annotate .svn/pristine/a7/a71b998ee631f5527b99e3a0df2d62bbccac91fe.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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