annotate .svn/pristine/a7/a71b998ee631f5527b99e3a0df2d62bbccac91fe.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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