Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

mercurial.rb 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2017 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require 'redmine/scm/adapters/mercurial_adapter'
  19. class Repository::Mercurial < Repository
  20. # sort changesets by revision number
  21. has_many :changesets,
  22. lambda {order("#{Changeset.table_name}.id DESC")},
  23. :foreign_key => 'repository_id'
  24. validates_presence_of :url
  25. # number of changesets to fetch at once
  26. FETCH_AT_ONCE = 100
  27. def self.human_attribute_name(attribute_key_name, *args)
  28. attr_name = attribute_key_name.to_s
  29. if attr_name == "url"
  30. attr_name = "path_to_repository"
  31. end
  32. super(attr_name, *args)
  33. end
  34. def self.scm_adapter_class
  35. Redmine::Scm::Adapters::MercurialAdapter
  36. end
  37. def self.scm_name
  38. 'Mercurial'
  39. end
  40. def supports_directory_revisions?
  41. true
  42. end
  43. def supports_revision_graph?
  44. true
  45. end
  46. def repo_log_encoding
  47. 'UTF-8'
  48. end
  49. # Returns the readable identifier for the given mercurial changeset
  50. def self.format_changeset_identifier(changeset)
  51. "#{changeset.revision}:#{changeset.scmid[0, 12]}"
  52. end
  53. # Returns the identifier for the given Mercurial changeset
  54. def self.changeset_identifier(changeset)
  55. changeset.scmid
  56. end
  57. def diff_format_revisions(cs, cs_to, sep=':')
  58. super(cs, cs_to, ' ')
  59. end
  60. def modify_entry_lastrev_identifier(entry)
  61. if entry.lastrev && entry.lastrev.identifier
  62. entry.lastrev.identifier = scmid_for_inserting_db(entry.lastrev.identifier)
  63. end
  64. end
  65. private :modify_entry_lastrev_identifier
  66. def entry(path=nil, identifier=nil)
  67. entry = scm.entry(path, identifier)
  68. return nil if entry.nil?
  69. modify_entry_lastrev_identifier(entry)
  70. entry
  71. end
  72. def scm_entries(path=nil, identifier=nil)
  73. entries = scm.entries(path, identifier)
  74. return nil if entries.nil?
  75. entries.each {|entry| modify_entry_lastrev_identifier(entry)}
  76. entries
  77. end
  78. protected :scm_entries
  79. # Finds and returns a revision with a number or the beginning of a hash
  80. def find_changeset_by_name(name)
  81. return nil if name.blank?
  82. s = name.to_s
  83. if /[^\d]/.match?(s) || s.size > 8
  84. cs = changesets.where(:scmid => s).first
  85. else
  86. cs = changesets.find_by(:revision => s)
  87. end
  88. return cs if cs
  89. changesets.where('scmid LIKE ?', "#{s}%").first
  90. end
  91. # Returns the latest changesets for +path+; sorted by revision number
  92. #
  93. # Because :order => 'id DESC' is defined at 'has_many',
  94. # there is no need to set 'order'.
  95. # But, MySQL test fails.
  96. # Sqlite3 and PostgreSQL pass.
  97. # Is this MySQL bug?
  98. def latest_changesets(path, rev, limit=10)
  99. changesets.
  100. includes(:user).
  101. where(latest_changesets_cond(path, rev, limit)).
  102. references(:user).
  103. limit(limit).
  104. order("#{Changeset.table_name}.id DESC").
  105. to_a
  106. end
  107. def is_short_id_in_db?
  108. return @is_short_id_in_db unless @is_short_id_in_db.nil?
  109. cs = changesets.first
  110. @is_short_id_in_db = (!cs.nil? && cs.scmid.length != 40)
  111. end
  112. private :is_short_id_in_db?
  113. def scmid_for_inserting_db(scmid)
  114. is_short_id_in_db? ? scmid[0, 12] : scmid
  115. end
  116. def nodes_in_branch(rev, branch_limit)
  117. scm.nodes_in_branch(rev, :limit => branch_limit).collect do |b|
  118. scmid_for_inserting_db(b)
  119. end
  120. end
  121. def tag_scmid(rev)
  122. scmid = scm.tagmap[rev]
  123. scmid.nil? ? nil : scmid_for_inserting_db(scmid)
  124. end
  125. def latest_changesets_cond(path, rev, limit)
  126. cond, args = [], []
  127. if scm.branchmap.member? rev
  128. # Mercurial named branch is *stable* in each revision.
  129. # So, named branch can be stored in database.
  130. # Mercurial provides *bookmark* which is equivalent with git branch.
  131. # But, bookmark is not implemented.
  132. cond << "#{Changeset.table_name}.scmid IN (?)"
  133. # Revisions in root directory and sub directory are not equal.
  134. # So, in order to get correct limit, we need to get all revisions.
  135. # But, it is very heavy.
  136. # Mercurial does not treat directory.
  137. # So, "hg log DIR" is very heavy.
  138. branch_limit = path.blank? ? limit : ( limit * 5 )
  139. args << nodes_in_branch(rev, branch_limit)
  140. elsif last = rev ? find_changeset_by_name(tag_scmid(rev) || rev) : nil
  141. cond << "#{Changeset.table_name}.id <= ?"
  142. args << last.id
  143. end
  144. unless path.blank?
  145. cond << "EXISTS (SELECT * FROM #{Change.table_name}
  146. WHERE #{Change.table_name}.changeset_id = #{Changeset.table_name}.id
  147. AND (#{Change.table_name}.path = ?
  148. OR #{Change.table_name}.path LIKE ? ESCAPE ?))"
  149. args << path.with_leading_slash
  150. args << "#{path.with_leading_slash.gsub(%r{[%_\\]}) { |s| "\\#{s}" }}/%" << '\\'
  151. end
  152. [cond.join(' AND '), *args] unless cond.empty?
  153. end
  154. private :latest_changesets_cond
  155. def fetch_changesets
  156. return if scm.info.nil?
  157. scm_rev = scm.info.lastrev.revision.to_i
  158. db_rev = latest_changeset ? latest_changeset.revision.to_i : -1
  159. return unless db_rev < scm_rev # already up-to-date
  160. logger.debug "Fetching changesets for repository #{url}" if logger
  161. (db_rev + 1).step(scm_rev, FETCH_AT_ONCE) do |i|
  162. scm.each_revision('', i, [i + FETCH_AT_ONCE - 1, scm_rev].min) do |re|
  163. transaction do
  164. parents = (re.parents || []).collect do |rp|
  165. find_changeset_by_name(scmid_for_inserting_db(rp))
  166. end.compact
  167. cs = Changeset.create(:repository => self,
  168. :revision => re.revision,
  169. :scmid => scmid_for_inserting_db(re.scmid),
  170. :committer => re.author,
  171. :committed_on => re.time,
  172. :comments => re.message,
  173. :parents => parents)
  174. unless cs.new_record?
  175. re.paths.each do |e|
  176. if from_revision = e[:from_revision]
  177. e[:from_revision] = scmid_for_inserting_db(from_revision)
  178. end
  179. cs.create_change(e)
  180. end
  181. end
  182. end
  183. end
  184. end
  185. end
  186. end