annotate .svn/pristine/4b/4b92ab107a3c108b2ff86abcfa2bd776a779be36.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

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