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