bazaar.rb

Alexander Usenko, 2012-09-14 17:15

Download (3.95 KB)

 
1
# Redmine - project management software
2
# Copyright (C) 2006-2012  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

    
18
require 'redmine/scm/adapters/bazaar_adapter'
19

    
20
class Repository::Bazaar < Repository
21
  attr_protected :root_url
22
#--- Begin: modified by Alexander Usenko ---
23
# No need log_encoding
24
  validates_presence_of :url #, :log_encoding
25
#--- End: modified by Alexander Usenko ---
26

    
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

    
35
  def self.scm_adapter_class
36
    Redmine::Scm::Adapters::BazaarAdapter
37
  end
38

    
39
  def self.scm_name
40
    'Bazaar'
41
  end
42

    
43
  def entries(path=nil, identifier=nil)
44
    entries = scm.entries(path, identifier)
45
    if entries
46
      entries.each do |e|
47
        next if e.lastrev.revision.blank?
48
        # Set the filesize unless browsing a specific revision
49
        if identifier.nil? && e.is_file?
50
          full_path = File.join(root_url, e.path)
51
          e.size = File.stat(full_path).size if File.file?(full_path)
52
        end
53
        c = Change.find(
54
               :first,
55
               :include    => :changeset,
56
               :conditions => [
57
                   "#{Change.table_name}.revision = ? and #{Changeset.table_name}.repository_id = ?",
58
                   e.lastrev.revision,
59
                   id
60
                   ],
61
               :order => "#{Changeset.table_name}.revision DESC")
62
        if c
63
          e.lastrev.identifier = c.changeset.revision
64
          e.lastrev.name       = c.changeset.revision
65
          e.lastrev.author     = c.changeset.committer
66
        end
67
      end
68
    end
69
  end
70

    
71
  def fetch_changesets
72
    scm_info = scm.info
73
    if scm_info
74
      # latest revision found in database
75
      db_revision = latest_changeset ? latest_changeset.revision.to_i : 0
76
      # latest revision in the repository
77
      scm_revision = scm_info.lastrev.identifier.to_i
78
      if db_revision < scm_revision
79
        logger.debug "Fetching changesets for repository #{url}" if logger && logger.debug?
80
        identifier_from = db_revision + 1
81
        while (identifier_from <= scm_revision)
82
          # loads changesets by batches of 200
83
          identifier_to = [identifier_from + 199, scm_revision].min
84
          revisions = scm.revisions('', identifier_to, identifier_from, :with_paths => true)
85
          transaction do
86
            revisions.reverse_each do |revision|
87
              changeset = Changeset.create(:repository   => self,
88
                                           :revision     => revision.identifier,
89
                                           :committer    => revision.author,
90
                                           :committed_on => revision.time,
91
                                           :scmid        => revision.scmid,
92
                                           :comments     => revision.message)
93

    
94
              revision.paths.each do |change|
95
                Change.create(:changeset => changeset,
96
                              :action    => change[:action],
97
                              :path      => change[:path],
98
                              :revision  => change[:revision])
99
              end
100
            end
101
          end unless revisions.nil?
102
          identifier_from = identifier_to + 1
103
        end
104
      end
105
    end
106
  end
107
end