annotate app/models/repository/git.rb @ 1517:dffacf8a6908 redmine-2.5

Update to Redmine SVN revision 13367 on 2.5-stable branch
author Chris Cannam
date Tue, 09 Sep 2014 09:29:00 +0100
parents e248c7af89ec
children a1bdbf8a87d5
rev   line source
Chris@441 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 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@0 19 require '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@119 64 # Returns the identifier for the given git changeset
Chris@119 65 def self.changeset_identifier(changeset)
Chris@119 66 changeset.scmid
Chris@119 67 end
Chris@119 68
Chris@119 69 # Returns the readable identifier for the given git changeset
Chris@119 70 def self.format_changeset_identifier(changeset)
Chris@119 71 changeset.revision[0, 8]
Chris@119 72 end
Chris@119 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@1517 96 def scm_entries(path=nil, identifier=nil)
Chris@1517 97 scm.entries(path, identifier, :report_last_commit => extra_report_last_commit)
Chris@441 98 end
Chris@1517 99 protected :scm_entries
Chris@441 100
Chris@909 101 # With SCMs that have a sequential commit numbering,
Chris@909 102 # such as Subversion and Mercurial,
Chris@909 103 # Redmine is able to be clever and only fetch changesets
Chris@909 104 # going forward from the most recent one it knows about.
Chris@1115 105 #
Chris@909 106 # However, Git does not have a sequential commit numbering.
Chris@909 107 #
Chris@909 108 # In order to fetch only new adding revisions,
Chris@1115 109 # Redmine needs to save "heads".
Chris@909 110 #
Chris@441 111 # In Git and Mercurial, revisions are not in date order.
Chris@441 112 # Redmine Mercurial fixed issues.
Chris@441 113 # * Redmine Takes Too Long On Large Mercurial Repository
Chris@441 114 # http://www.redmine.org/issues/3449
Chris@441 115 # * Sorting for changesets might go wrong on Mercurial repos
Chris@441 116 # http://www.redmine.org/issues/3567
Chris@441 117 #
Chris@441 118 # Database revision column is text, so Redmine can not sort by revision.
Chris@441 119 # Mercurial has revision number, and revision number guarantees revision order.
Chris@441 120 # Redmine Mercurial model stored revisions ordered by database id to database.
Chris@441 121 # So, Redmine Mercurial model can use correct ordering revisions.
Chris@441 122 #
Chris@441 123 # Redmine Mercurial adapter uses "hg log -r 0:tip --limit 10"
Chris@441 124 # to get limited revisions from old to new.
Chris@441 125 # But, Git 1.7.3.4 does not support --reverse with -n or --skip.
Chris@441 126 #
Chris@0 127 # The repository can still be fully reloaded by calling #clear_changesets
Chris@0 128 # before fetching changesets (eg. for offline resync)
Chris@0 129 def fetch_changesets
Chris@441 130 scm_brs = branches
Chris@441 131 return if scm_brs.nil? || scm_brs.empty?
Chris@1115 132
Chris@441 133 h1 = extra_info || {}
Chris@441 134 h = h1.dup
Chris@1115 135 repo_heads = scm_brs.map{ |br| br.scmid }
Chris@1115 136 h["heads"] ||= []
Chris@1115 137 prev_db_heads = h["heads"].dup
Chris@1115 138 if prev_db_heads.empty?
Chris@1115 139 prev_db_heads += heads_from_branches_hash
Chris@1115 140 end
Chris@1115 141 return if prev_db_heads.sort == repo_heads.sort
Chris@1115 142
Chris@441 143 h["db_consistent"] ||= {}
Chris@441 144 if changesets.count == 0
Chris@441 145 h["db_consistent"]["ordering"] = 1
Chris@441 146 merge_extra_info(h)
Chris@441 147 self.save
Chris@441 148 elsif ! h["db_consistent"].has_key?("ordering")
Chris@441 149 h["db_consistent"]["ordering"] = 0
Chris@441 150 merge_extra_info(h)
Chris@441 151 self.save
Chris@441 152 end
Chris@1115 153 save_revisions(prev_db_heads, repo_heads)
Chris@1115 154 end
Chris@1115 155
Chris@1115 156 def save_revisions(prev_db_heads, repo_heads)
Chris@1115 157 h = {}
Chris@1115 158 opts = {}
Chris@1115 159 opts[:reverse] = true
Chris@1115 160 opts[:excludes] = prev_db_heads
Chris@1115 161 opts[:includes] = repo_heads
Chris@1115 162
Chris@1115 163 revisions = scm.revisions('', nil, nil, opts)
Chris@1115 164 return if revisions.blank?
Chris@1115 165
Chris@1115 166 # Make the search for existing revisions in the database in a more sufficient manner
Chris@1115 167 #
Chris@1115 168 # Git branch is the reference to the specific revision.
Chris@1115 169 # Git can *delete* remote branch and *re-push* branch.
Chris@1115 170 #
Chris@1115 171 # $ git push remote :branch
Chris@1115 172 # $ git push remote branch
Chris@1115 173 #
Chris@1115 174 # After deleting branch, revisions remain in repository until "git gc".
Chris@1115 175 # On git 1.7.2.3, default pruning date is 2 weeks.
Chris@1115 176 # So, "git log --not deleted_branch_head_revision" return code is 0.
Chris@1115 177 #
Chris@1115 178 # After re-pushing branch, "git log" returns revisions which are saved in database.
Chris@1115 179 # So, Redmine needs to scan revisions and database every time.
Chris@1115 180 #
Chris@1115 181 # This is replacing the one-after-one queries.
Chris@1517 182 # Find all revisions, that are in the database, and then remove them
Chris@1517 183 # from the revision array.
Chris@1115 184 # Then later we won't need any conditions for db existence.
Chris@1517 185 # Query for several revisions at once, and remove them
Chris@1517 186 # from the revisions array, if they are there.
Chris@1517 187 # Do this in chunks, to avoid eventual memory problems
Chris@1517 188 # (in case of tens of thousands of commits).
Chris@1115 189 # If there are no revisions (because the original code's algorithm filtered them),
Chris@1115 190 # then this part will be stepped over.
Chris@1115 191 # We make queries, just if there is any revision.
Chris@1115 192 limit = 100
Chris@1115 193 offset = 0
Chris@1115 194 revisions_copy = revisions.clone # revisions will change
Chris@1115 195 while offset < revisions_copy.size
Chris@1464 196 scmids = revisions_copy.slice(offset, limit).map{|x| x.scmid}
Chris@1517 197 recent_changesets_slice = changesets.where(:scmid => scmids)
Chris@1115 198 # Subtract revisions that redmine already knows about
Chris@1115 199 recent_revisions = recent_changesets_slice.map{|c| c.scmid}
Chris@1115 200 revisions.reject!{|r| recent_revisions.include?(r.scmid)}
Chris@1115 201 offset += limit
Chris@1115 202 end
Chris@1115 203 revisions.each do |rev|
Chris@1115 204 transaction do
Chris@1115 205 # There is no search in the db for this revision, because above we ensured,
Chris@1115 206 # that it's not in the db.
Chris@1115 207 save_revision(rev)
Chris@245 208 end
Chris@245 209 end
Chris@1115 210 h["heads"] = repo_heads.dup
Chris@1115 211 merge_extra_info(h)
Chris@1115 212 self.save
Chris@0 213 end
Chris@1115 214 private :save_revisions
Chris@0 215
Chris@441 216 def save_revision(rev)
Chris@1115 217 parents = (rev.parents || []).collect{|rp| find_changeset_by_name(rp)}.compact
Chris@1115 218 changeset = Changeset.create(
Chris@441 219 :repository => self,
Chris@441 220 :revision => rev.identifier,
Chris@441 221 :scmid => rev.scmid,
Chris@441 222 :committer => rev.author,
Chris@441 223 :committed_on => rev.time,
Chris@1115 224 :comments => rev.message,
Chris@1115 225 :parents => parents
Chris@441 226 )
Chris@1115 227 unless changeset.new_record?
Chris@1115 228 rev.paths.each { |change| changeset.create_change(change) }
Chris@441 229 end
Chris@909 230 changeset
Chris@441 231 end
Chris@441 232 private :save_revision
Chris@441 233
Chris@1115 234 def heads_from_branches_hash
Chris@1115 235 h1 = extra_info || {}
Chris@1115 236 h = h1.dup
Chris@1115 237 h["branches"] ||= {}
Chris@1115 238 h['branches'].map{|br, hs| hs['last_scmid']}
Chris@1115 239 end
Chris@1115 240
Chris@0 241 def latest_changesets(path,rev,limit=10)
Chris@0 242 revisions = scm.revisions(path, nil, rev, :limit => limit, :all => false)
Chris@0 243 return [] if revisions.nil? || revisions.empty?
Chris@1464 244 changesets.where(:scmid => revisions.map {|c| c.scmid}).all
Chris@0 245 end
Chris@1115 246
Chris@1115 247 def clear_extra_info_of_changesets
Chris@1115 248 return if extra_info.nil?
Chris@1115 249 v = extra_info["extra_report_last_commit"]
Chris@1115 250 write_attribute(:extra_info, nil)
Chris@1115 251 h = {}
Chris@1115 252 h["extra_report_last_commit"] = v
Chris@1115 253 merge_extra_info(h)
Chris@1115 254 self.save
Chris@1115 255 end
Chris@1115 256 private :clear_extra_info_of_changesets
Chris@0 257 end