PageRenderTime 46ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/app/models/repository/mercurial.rb

https://bitbucket.org/sharjeelaslam/redmine-tivilon-dev
Ruby | 159 lines | 104 code | 18 blank | 37 comment | 10 complexity | 21b5e493afcf819e820adacad251b460 MD5 | raw file
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2013 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. require 'redmine/scm/adapters/mercurial_adapter'
  18. class Repository::Mercurial < Repository
  19. # sort changesets by revision number
  20. has_many :changesets,
  21. :order => "#{Changeset.table_name}.id DESC",
  22. :foreign_key => 'repository_id'
  23. attr_protected :root_url
  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}"
  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. # Finds and returns a revision with a number or the beginning of a hash
  61. def find_changeset_by_name(name)
  62. return nil if name.blank?
  63. s = name.to_s
  64. if /[^\d]/ =~ s or s.size > 8
  65. cs = changesets.where(:scmid => s).first
  66. else
  67. cs = changesets.where(:revision => s).first
  68. end
  69. return cs if cs
  70. changesets.where('scmid LIKE ?', "#{s}%").first
  71. end
  72. # Returns the latest changesets for +path+; sorted by revision number
  73. #
  74. # Because :order => 'id DESC' is defined at 'has_many',
  75. # there is no need to set 'order'.
  76. # But, MySQL test fails.
  77. # Sqlite3 and PostgreSQL pass.
  78. # Is this MySQL bug?
  79. def latest_changesets(path, rev, limit=10)
  80. changesets.
  81. includes(:user).
  82. where(latest_changesets_cond(path, rev, limit)).
  83. limit(limit).
  84. order("#{Changeset.table_name}.id DESC").
  85. all
  86. end
  87. def latest_changesets_cond(path, rev, limit)
  88. cond, args = [], []
  89. if scm.branchmap.member? rev
  90. # Mercurial named branch is *stable* in each revision.
  91. # So, named branch can be stored in database.
  92. # Mercurial provides *bookmark* which is equivalent with git branch.
  93. # But, bookmark is not implemented.
  94. cond << "#{Changeset.table_name}.scmid IN (?)"
  95. # Revisions in root directory and sub directory are not equal.
  96. # So, in order to get correct limit, we need to get all revisions.
  97. # But, it is very heavy.
  98. # Mercurial does not treat direcotry.
  99. # So, "hg log DIR" is very heavy.
  100. branch_limit = path.blank? ? limit : ( limit * 5 )
  101. args << scm.nodes_in_branch(rev, :limit => branch_limit)
  102. elsif last = rev ? find_changeset_by_name(scm.tagmap[rev] || rev) : nil
  103. cond << "#{Changeset.table_name}.id <= ?"
  104. args << last.id
  105. end
  106. unless path.blank?
  107. cond << "EXISTS (SELECT * FROM #{Change.table_name}
  108. WHERE #{Change.table_name}.changeset_id = #{Changeset.table_name}.id
  109. AND (#{Change.table_name}.path = ?
  110. OR #{Change.table_name}.path LIKE ? ESCAPE ?))"
  111. args << path.with_leading_slash
  112. args << "#{path.with_leading_slash.gsub(%r{[%_\\]}) { |s| "\\#{s}" }}/%" << '\\'
  113. end
  114. [cond.join(' AND '), *args] unless cond.empty?
  115. end
  116. private :latest_changesets_cond
  117. def fetch_changesets
  118. return if scm.info.nil?
  119. scm_rev = scm.info.lastrev.revision.to_i
  120. db_rev = latest_changeset ? latest_changeset.revision.to_i : -1
  121. return unless db_rev < scm_rev # already up-to-date
  122. logger.debug "Fetching changesets for repository #{url}" if logger
  123. (db_rev + 1).step(scm_rev, FETCH_AT_ONCE) do |i|
  124. scm.each_revision('', i, [i + FETCH_AT_ONCE - 1, scm_rev].min) do |re|
  125. transaction do
  126. parents = (re.parents || []).collect{|rp| find_changeset_by_name(rp)}.compact
  127. cs = Changeset.create(:repository => self,
  128. :revision => re.revision,
  129. :scmid => re.scmid,
  130. :committer => re.author,
  131. :committed_on => re.time,
  132. :comments => re.message,
  133. :parents => parents)
  134. unless cs.new_record?
  135. re.paths.each { |e| cs.create_change(e) }
  136. end
  137. end
  138. end
  139. end
  140. end
  141. end