annotate .svn/pristine/80/8016b0f16ed106bd626d09699f9ed97c1561c6f3.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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