You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

repository.rb 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2011 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. class ScmFetchError < Exception; end
  18. class Repository < ActiveRecord::Base
  19. include Redmine::Ciphering
  20. belongs_to :project
  21. has_many :changesets, :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC"
  22. has_many :changes, :through => :changesets
  23. serialize :extra_info
  24. before_save :check_default
  25. # Raw SQL to delete changesets and changes in the database
  26. # has_many :changesets, :dependent => :destroy is too slow for big repositories
  27. before_destroy :clear_changesets
  28. validates_length_of :password, :maximum => 255, :allow_nil => true
  29. validates_length_of :identifier, :maximum => 255, :allow_blank => true
  30. validates_presence_of :identifier, :unless => Proc.new { |r| r.is_default? || r.set_as_default? }
  31. validates_uniqueness_of :identifier, :scope => :project_id, :allow_blank => true
  32. validates_exclusion_of :identifier, :in => %w(show entry raw changes annotate diff show stats graph)
  33. # donwcase letters, digits, dashes but not digits only
  34. validates_format_of :identifier, :with => /^(?!\d+$)[a-z0-9\-]*$/, :allow_blank => true
  35. # Checks if the SCM is enabled when creating a repository
  36. validate :repo_create_validation, :on => :create
  37. def repo_create_validation
  38. unless Setting.enabled_scm.include?(self.class.name.demodulize)
  39. errors.add(:type, :invalid)
  40. end
  41. end
  42. def self.human_attribute_name(attribute_key_name, *args)
  43. attr_name = attribute_key_name.to_s
  44. if attr_name == "log_encoding"
  45. attr_name = "commit_logs_encoding"
  46. end
  47. super(attr_name, *args)
  48. end
  49. alias :attributes_without_extra_info= :attributes=
  50. def attributes=(new_attributes)
  51. return if new_attributes.nil?
  52. attributes = new_attributes.dup
  53. attributes.stringify_keys!
  54. p = {}
  55. p_extra = {}
  56. attributes.each do |k, v|
  57. if k =~ /^extra_/
  58. p_extra[k] = v
  59. else
  60. p[k] = v
  61. end
  62. end
  63. send :attributes_without_extra_info=, p
  64. if p_extra.keys.any?
  65. merge_extra_info(p_extra)
  66. end
  67. end
  68. # Removes leading and trailing whitespace
  69. def url=(arg)
  70. write_attribute(:url, arg ? arg.to_s.strip : nil)
  71. end
  72. # Removes leading and trailing whitespace
  73. def root_url=(arg)
  74. write_attribute(:root_url, arg ? arg.to_s.strip : nil)
  75. end
  76. def password
  77. read_ciphered_attribute(:password)
  78. end
  79. def password=(arg)
  80. write_ciphered_attribute(:password, arg)
  81. end
  82. def scm_adapter
  83. self.class.scm_adapter_class
  84. end
  85. def scm
  86. unless @scm
  87. @scm = self.scm_adapter.new(url, root_url,
  88. login, password, path_encoding)
  89. if root_url.blank? && @scm.root_url.present?
  90. update_attribute(:root_url, @scm.root_url)
  91. end
  92. end
  93. @scm
  94. end
  95. def scm_name
  96. self.class.scm_name
  97. end
  98. def name
  99. if identifier.present?
  100. identifier
  101. elsif is_default?
  102. l(:field_repository_is_default)
  103. else
  104. scm_name
  105. end
  106. end
  107. def identifier_param
  108. if is_default?
  109. nil
  110. elsif identifier.present?
  111. identifier
  112. else
  113. id.to_s
  114. end
  115. end
  116. def <=>(repository)
  117. if is_default?
  118. -1
  119. elsif repository.is_default?
  120. 1
  121. else
  122. identifier <=> repository.identifier
  123. end
  124. end
  125. def self.find_by_identifier_param(param)
  126. if param.to_s =~ /^\d+$/
  127. find_by_id(param)
  128. else
  129. find_by_identifier(param)
  130. end
  131. end
  132. def merge_extra_info(arg)
  133. h = extra_info || {}
  134. return h if arg.nil?
  135. h.merge!(arg)
  136. write_attribute(:extra_info, h)
  137. end
  138. def report_last_commit
  139. true
  140. end
  141. def supports_cat?
  142. scm.supports_cat?
  143. end
  144. def supports_annotate?
  145. scm.supports_annotate?
  146. end
  147. def supports_all_revisions?
  148. true
  149. end
  150. def supports_directory_revisions?
  151. false
  152. end
  153. def supports_revision_graph?
  154. false
  155. end
  156. def entry(path=nil, identifier=nil)
  157. scm.entry(path, identifier)
  158. end
  159. def entries(path=nil, identifier=nil)
  160. scm.entries(path, identifier)
  161. end
  162. def branches
  163. scm.branches
  164. end
  165. def tags
  166. scm.tags
  167. end
  168. def default_branch
  169. nil
  170. end
  171. def properties(path, identifier=nil)
  172. scm.properties(path, identifier)
  173. end
  174. def cat(path, identifier=nil)
  175. scm.cat(path, identifier)
  176. end
  177. def diff(path, rev, rev_to)
  178. scm.diff(path, rev, rev_to)
  179. end
  180. def diff_format_revisions(cs, cs_to, sep=':')
  181. text = ""
  182. text << cs_to.format_identifier + sep if cs_to
  183. text << cs.format_identifier if cs
  184. text
  185. end
  186. # Returns a path relative to the url of the repository
  187. def relative_path(path)
  188. path
  189. end
  190. # Finds and returns a revision with a number or the beginning of a hash
  191. def find_changeset_by_name(name)
  192. return nil if name.blank?
  193. s = name.to_s
  194. changesets.find(:first, :conditions => (s.match(/^\d*$/) ?
  195. ["revision = ?", s] : ["revision LIKE ?", s + '%']))
  196. end
  197. def latest_changeset
  198. @latest_changeset ||= changesets.find(:first)
  199. end
  200. # Returns the latest changesets for +path+
  201. # Default behaviour is to search in cached changesets
  202. def latest_changesets(path, rev, limit=10)
  203. if path.blank?
  204. changesets.find(
  205. :all,
  206. :include => :user,
  207. :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC",
  208. :limit => limit)
  209. else
  210. changes.find(
  211. :all,
  212. :include => {:changeset => :user},
  213. :conditions => ["path = ?", path.with_leading_slash],
  214. :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC",
  215. :limit => limit
  216. ).collect(&:changeset)
  217. end
  218. end
  219. def scan_changesets_for_issue_ids
  220. self.changesets.each(&:scan_comment_for_issue_ids)
  221. end
  222. # Returns an array of committers usernames and associated user_id
  223. def committers
  224. @committers ||= Changeset.connection.select_rows(
  225. "SELECT DISTINCT committer, user_id FROM #{Changeset.table_name} WHERE repository_id = #{id}")
  226. end
  227. # Maps committers username to a user ids
  228. def committer_ids=(h)
  229. if h.is_a?(Hash)
  230. committers.each do |committer, user_id|
  231. new_user_id = h[committer]
  232. if new_user_id && (new_user_id.to_i != user_id.to_i)
  233. new_user_id = (new_user_id.to_i > 0 ? new_user_id.to_i : nil)
  234. Changeset.update_all(
  235. "user_id = #{ new_user_id.nil? ? 'NULL' : new_user_id }",
  236. ["repository_id = ? AND committer = ?", id, committer])
  237. end
  238. end
  239. @committers = nil
  240. @found_committer_users = nil
  241. true
  242. else
  243. false
  244. end
  245. end
  246. # Returns the Redmine User corresponding to the given +committer+
  247. # It will return nil if the committer is not yet mapped and if no User
  248. # with the same username or email was found
  249. def find_committer_user(committer)
  250. unless committer.blank?
  251. @found_committer_users ||= {}
  252. return @found_committer_users[committer] if @found_committer_users.has_key?(committer)
  253. user = nil
  254. c = changesets.find(:first, :conditions => {:committer => committer}, :include => :user)
  255. if c && c.user
  256. user = c.user
  257. elsif committer.strip =~ /^([^<]+)(<(.*)>)?$/
  258. username, email = $1.strip, $3
  259. u = User.find_by_login(username)
  260. u ||= User.find_by_mail(email) unless email.blank?
  261. user = u
  262. end
  263. @found_committer_users[committer] = user
  264. user
  265. end
  266. end
  267. def repo_log_encoding
  268. encoding = log_encoding.to_s.strip
  269. encoding.blank? ? 'UTF-8' : encoding
  270. end
  271. # Fetches new changesets for all repositories of active projects
  272. # Can be called periodically by an external script
  273. # eg. ruby script/runner "Repository.fetch_changesets"
  274. def self.fetch_changesets
  275. Project.active.has_module(:repository).all.each do |project|
  276. project.repositories.each do |repository|
  277. begin
  278. repository.fetch_changesets
  279. rescue Redmine::Scm::Adapters::CommandFailed => e
  280. logger.error "scm: error during fetching changesets: #{e.message}"
  281. end
  282. end
  283. end
  284. end
  285. # scan changeset comments to find related and fixed issues for all repositories
  286. def self.scan_changesets_for_issue_ids
  287. find(:all).each(&:scan_changesets_for_issue_ids)
  288. end
  289. def self.scm_name
  290. 'Abstract'
  291. end
  292. def self.available_scm
  293. subclasses.collect {|klass| [klass.scm_name, klass.name]}
  294. end
  295. def self.factory(klass_name, *args)
  296. klass = "Repository::#{klass_name}".constantize
  297. klass.new(*args)
  298. rescue
  299. nil
  300. end
  301. def self.scm_adapter_class
  302. nil
  303. end
  304. def self.scm_command
  305. ret = ""
  306. begin
  307. ret = self.scm_adapter_class.client_command if self.scm_adapter_class
  308. rescue Exception => e
  309. logger.error "scm: error during get command: #{e.message}"
  310. end
  311. ret
  312. end
  313. def self.scm_version_string
  314. ret = ""
  315. begin
  316. ret = self.scm_adapter_class.client_version_string if self.scm_adapter_class
  317. rescue Exception => e
  318. logger.error "scm: error during get version string: #{e.message}"
  319. end
  320. ret
  321. end
  322. def self.scm_available
  323. ret = false
  324. begin
  325. ret = self.scm_adapter_class.client_available if self.scm_adapter_class
  326. rescue Exception => e
  327. logger.error "scm: error during get scm available: #{e.message}"
  328. end
  329. ret
  330. end
  331. def set_as_default?
  332. new_record? && project && !Repository.first(:conditions => {:project_id => project.id})
  333. end
  334. protected
  335. def check_default
  336. if !is_default? && set_as_default?
  337. self.is_default = true
  338. end
  339. if is_default? && is_default_changed?
  340. Repository.update_all(["is_default = ?", false], ["project_id = ?", project_id])
  341. end
  342. end
  343. private
  344. # Deletes repository data
  345. def clear_changesets
  346. cs = Changeset.table_name
  347. ch = Change.table_name
  348. ci = "#{table_name_prefix}changesets_issues#{table_name_suffix}"
  349. cp = "#{table_name_prefix}changeset_parents#{table_name_suffix}"
  350. connection.delete("DELETE FROM #{ch} WHERE #{ch}.changeset_id IN (SELECT #{cs}.id FROM #{cs} WHERE #{cs}.repository_id = #{id})")
  351. connection.delete("DELETE FROM #{ci} WHERE #{ci}.changeset_id IN (SELECT #{cs}.id FROM #{cs} WHERE #{cs}.repository_id = #{id})")
  352. connection.delete("DELETE FROM #{cp} WHERE #{cp}.changeset_id IN (SELECT #{cs}.id FROM #{cs} WHERE #{cs}.repository_id = #{id})")
  353. connection.delete("DELETE FROM #{cs} WHERE #{cs}.repository_id = #{id}")
  354. end
  355. end