annotate app/models/repository/git.rb @ 1172:60d42b9850d2 bug_367

Close obsolete branch bug_367
author Chris Cannam
date Fri, 03 Feb 2012 15:20:50 +0000
parents 851510f1b535
children 5e80956cc792
rev   line source
Chris@441 1 # Redmine - project management software
Chris@441 2 # Copyright (C) 2006-2011 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@245 25 def self.human_attribute_name(attribute_key_name)
Chris@441 26 attr_name = attribute_key_name
Chris@441 27 if attr_name == "url"
Chris@441 28 attr_name = "path_to_repository"
Chris@441 29 end
Chris@441 30 super(attr_name)
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@245 56 def repo_log_encoding
Chris@245 57 'UTF-8'
Chris@245 58 end
Chris@245 59
Chris@117 60 # Returns the identifier for the given git changeset
Chris@117 61 def self.changeset_identifier(changeset)
Chris@117 62 changeset.scmid
Chris@117 63 end
Chris@117 64
Chris@117 65 # Returns the readable identifier for the given git changeset
Chris@117 66 def self.format_changeset_identifier(changeset)
Chris@117 67 changeset.revision[0, 8]
Chris@117 68 end
Chris@117 69
Chris@0 70 def branches
Chris@0 71 scm.branches
Chris@0 72 end
Chris@0 73
Chris@0 74 def tags
Chris@0 75 scm.tags
Chris@0 76 end
Chris@0 77
Chris@507 78 def default_branch
Chris@507 79 scm.default_branch
Chris@507 80 end
Chris@507 81
Chris@245 82 def find_changeset_by_name(name)
Chris@245 83 return nil if name.nil? || name.empty?
Chris@245 84 e = changesets.find(:first, :conditions => ['revision = ?', name.to_s])
Chris@245 85 return e if e
Chris@245 86 changesets.find(:first, :conditions => ['scmid LIKE ?', "#{name}%"])
Chris@245 87 end
Chris@245 88
Chris@441 89 def entries(path=nil, identifier=nil)
Chris@441 90 scm.entries(path,
Chris@441 91 identifier,
Chris@441 92 options = {:report_last_commit => extra_report_last_commit})
Chris@441 93 end
Chris@441 94
Chris@441 95 # In Git and Mercurial, revisions are not in date order.
Chris@441 96 # Redmine Mercurial fixed issues.
Chris@441 97 # * Redmine Takes Too Long On Large Mercurial Repository
Chris@441 98 # http://www.redmine.org/issues/3449
Chris@441 99 # * Sorting for changesets might go wrong on Mercurial repos
Chris@441 100 # http://www.redmine.org/issues/3567
Chris@441 101 #
Chris@441 102 # Database revision column is text, so Redmine can not sort by revision.
Chris@441 103 # Mercurial has revision number, and revision number guarantees revision order.
Chris@441 104 # Redmine Mercurial model stored revisions ordered by database id to database.
Chris@441 105 # So, Redmine Mercurial model can use correct ordering revisions.
Chris@441 106 #
Chris@441 107 # Redmine Mercurial adapter uses "hg log -r 0:tip --limit 10"
Chris@441 108 # to get limited revisions from old to new.
Chris@441 109 # But, Git 1.7.3.4 does not support --reverse with -n or --skip.
Chris@441 110 #
Chris@0 111 # The repository can still be fully reloaded by calling #clear_changesets
Chris@0 112 # before fetching changesets (eg. for offline resync)
Chris@0 113 def fetch_changesets
Chris@441 114 scm_brs = branches
Chris@441 115 return if scm_brs.nil? || scm_brs.empty?
Chris@441 116 h1 = extra_info || {}
Chris@441 117 h = h1.dup
Chris@441 118 h["branches"] ||= {}
Chris@441 119 h["db_consistent"] ||= {}
Chris@441 120 if changesets.count == 0
Chris@441 121 h["db_consistent"]["ordering"] = 1
Chris@441 122 merge_extra_info(h)
Chris@441 123 self.save
Chris@441 124 elsif ! h["db_consistent"].has_key?("ordering")
Chris@441 125 h["db_consistent"]["ordering"] = 0
Chris@441 126 merge_extra_info(h)
Chris@441 127 self.save
Chris@441 128 end
Chris@441 129 scm_brs.each do |br|
Chris@441 130 from_scmid = nil
Chris@441 131 from_scmid = h["branches"][br]["last_scmid"] if h["branches"][br]
Chris@441 132 h["branches"][br] ||= {}
Chris@441 133 scm.revisions('', from_scmid, br, {:reverse => true}) do |rev|
Chris@441 134 db_rev = find_changeset_by_name(rev.revision)
Chris@245 135 transaction do
Chris@441 136 if db_rev.nil?
Chris@441 137 save_revision(rev)
Chris@245 138 end
Chris@441 139 h["branches"][br]["last_scmid"] = rev.scmid
Chris@441 140 merge_extra_info(h)
Chris@441 141 self.save
Chris@245 142 end
Chris@245 143 end
Chris@245 144 end
Chris@0 145 end
Chris@0 146
Chris@441 147 def save_revision(rev)
Chris@441 148 changeset = Changeset.new(
Chris@441 149 :repository => self,
Chris@441 150 :revision => rev.identifier,
Chris@441 151 :scmid => rev.scmid,
Chris@441 152 :committer => rev.author,
Chris@441 153 :committed_on => rev.time,
Chris@441 154 :comments => rev.message
Chris@441 155 )
Chris@441 156 if changeset.save
Chris@441 157 rev.paths.each do |file|
Chris@441 158 Change.create(
Chris@441 159 :changeset => changeset,
Chris@441 160 :action => file[:action],
Chris@441 161 :path => file[:path])
Chris@441 162 end
Chris@441 163 end
Chris@441 164 end
Chris@441 165 private :save_revision
Chris@441 166
Chris@0 167 def latest_changesets(path,rev,limit=10)
Chris@0 168 revisions = scm.revisions(path, nil, rev, :limit => limit, :all => false)
Chris@0 169 return [] if revisions.nil? || revisions.empty?
Chris@0 170
Chris@0 171 changesets.find(
Chris@441 172 :all,
Chris@0 173 :conditions => [
Chris@441 174 "scmid IN (?)",
Chris@0 175 revisions.map!{|c| c.scmid}
Chris@0 176 ],
Chris@0 177 :order => 'committed_on DESC'
Chris@0 178 )
Chris@0 179 end
Chris@0 180 end