Project

General

Profile

Feature #6515 » mercurial_hgrc_support.diff

the code - Alessio Caiazza, 2010-09-28 10:13

View differences:

app/models/repository/mercurial.rb
16 16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 17

  
18 18
require 'redmine/scm/adapters/mercurial_adapter'
19
require 'redmine/scm/adapters/mercurial/hgrc_support'
19 20

  
20 21
class Repository::Mercurial < Repository
22
  include HgrcSupport
21 23
  # sort changesets by revision number
22 24
  has_many :changesets, :order => "#{Changeset.table_name}.id DESC", :foreign_key => 'repository_id'
23 25

  
lib/redmine/scm/adapters/mercurial/hgrc_support.rb
1
require 'ini'
2

  
3
module HgrcSupport
4
  def self.included(base)
5
    base.after_save :save_hgrc
6
  end
7
  
8
  attr_reader :hgrc_hooks_issues_update
9
  attr_accessor :hgrc_web_contact
10
  attr_accessor :hgrc_web_description
11
  attr_accessor :hgrc_web_style
12
  
13
  def hgrc_hooks_issues_update=(value)
14
    if ['0', '1'].include? value.to_s
15
      @hgrc_hooks_issues_update = value
16
    elsif value == true
17
      @hgrc_hooks_issues_update = 1
18
    else
19
      @hgrc_hooks_issues_update = 0
20
    end
21
  end
22
  
23
  def load_hgrc
24
    ini = Ini.new(File.join(url, '.hg', 'hgrc'))
25
    #web
26
    ini['web'] = {} if ini['web'].nil?
27
    @hgrc_web_contact = ini['web']['contact']
28
    @hgrc_web_description = ini['web']['description']
29
    @hgrc_web_style = ini['web']['style']
30
    #hooks
31
    ini['hooks'] = {} if ini['hooks'].nil?
32
    if !ini['hooks']['changegroup.redmine'].nil? && !ini['hooks']['changegroup.redmine'].empty?
33
      @hgrc_hooks_issues_update = 1
34
    else
35
      @hgrc_hooks_issues_update = 0
36
    end
37
  end
38
  
39
  private
40
  def save_hgrc
41
    ini = Ini.new(File.join(url, '.hg', 'hgrc'))
42
    #web
43
    ini['web'] = {} if ini['web'].nil?
44
    ini['web']['contact'] = @hgrc_web_contact
45
    ini['web']['description'] = @hgrc_web_description
46
    ini['web']['style'] = @hgrc_web_style
47
    #hooks
48
    ini['hooks'] = {} if ini['hooks'].nil?
49
    if @hgrc_hooks_issues_update.to_i == 1
50
      ini['hooks']['changegroup.redmine'] = "cd #{RAILS_ROOT} && ruby script/runner \"Repository.find(#{self[:id]}).fetch_changesets\" -e #{RAILS_ENV}"
51
    elsif !ini['hooks']['changegroup.redmine'].nil? && !ini['hooks']['changegroup.redmine'].empty?
52
      ini['hooks'].delete('changegroup.redmine')
53
    end
54
    ini.comment = "Generated with Redmine.\n"
55
    #save
56
    ini.update()
57
  end
58
end
(4-4/4)