I'm looking for a part-time remote job.

Hire me


I'm the author of:

Mastering Redmine is a comprehensive guide with tips, tricks and best practices, and an easy-to-learn structure.

Check the book's project or

Buy the book

Social pages of the book:

By buying this book you also donate to Redmine (see this page).


Follow me:

rm1.4-multirepo.patch

Takashi Okamoto, 23 May 2012 08:50

Download (12.9 KB)

View differences:

lib/scm_repositories_controller_patch.rb
8 8
        base.class_eval do
9 9
            unloadable
10 10
            before_filter :delete_scm, :only => :destroy
11
            alias_method :edit, :edit_with_add
11
            alias_method :create, :create_with_add
12
            alias_method_chain :edit, :add
12 13
        end
13 14
    end
14 15

  
......
24 25
            end
25 26
        end
26 27

  
28
        def create_repository (params)
29
            if @repository.valid? && params[:operation].present? && params[:operation] == 'add'
30
                attributes = params[:repository]
31
                attrs = {}
32
                extra = {}
33
                attributes.each do |name, value|
34
                   if name =~ %r{^extra_}
35
                       extra[name] = value
36
                   else
37
                       attrs[name] = value
38
                   end
39
                end
40

  
41
                if attrs
42

  
43
                    begin
44
                        interface = Object.const_get("#{params[:repository_scm]}Creator")
45
 
46
                        name = interface.repository_name(attrs['url'])
47
                        if name
48
                            path = interface.path(name)
49
                            if File.directory?(path)
50
                                @repository.errors.add(:url, :already_exists)
51
                            else
52
                                RAILS_DEFAULT_LOGGER.info "Creating reporitory: #{path}"
53
                                interface.execute(ScmConfig['pre_create'], path, @project) if ScmConfig['pre_create']
54
                                if interface.create_repository(path)
55
                                    interface.execute(ScmConfig['post_create'], path, @project) if ScmConfig['post_create']
56
                                    @repository.created_with_scm = true
57
                                    unless interface.copy_hooks(path)
58
                                        RAILS_DEFAULT_LOGGER.warn "Hooks copy failed"
59
                                    end
60
                                else
61
                                    RAILS_DEFAULT_LOGGER.error "Repository creation failed"
62
                                end
63
                            end
64

  
65
                            @repository.root_url = interface.access_root_url(path)
66
                            @repository.url = interface.access_url(path)
67

  
68
                            if !interface.repository_name_equal?(name, @project.identifier)
69
                                flash[:warning] = l(:text_cannot_be_used_redmine_auth)
70
                            end
71
                        else
72
                            @repository.errors.add(:url, :should_be_of_format_local, :format => interface.repository_format)
73
                        end
74

  
75
                    rescue NameError
76
                        RAILS_DEFAULT_LOGGER.error "Can't find interface for #{params[:repository_scm]}."
77
                         @repository.errors.add_to_base(:scm_not_supported)
78
                    end
79
                end
80
            end
81

  
82
        end
83

  
84
        #def create
85
        #  @repository = Repository.factory(params[:repository_scm], params[:repository])
86
        #  @repository.project = @project
87
        #  if request.post? && @repository.save
88
        #    redirect_to settings_project_path(@project, :tab => 'repositories')
89
        #  else
90
        #    render :action => 'new'
91
        #  end
92
        #end
93

  
94
        def create_with_add
95
          @repository = Repository.factory(params[:repository_scm], params[:repository])
96
          @repository.project = @project
97
          if request.post?
98

  
99
            create_repository(params)
100

  
101
            if @repository.save
102
              redirect_to settings_project_path(@project, :tab => 'repositories')
103
            end
104
          else
105
            render :action => 'new'
106
          end
107
        end
108

  
27 109
        # Original function
28 110
        #def edit
29 111
        #    @repository = @project.repository
......
56 138
        #end
57 139

  
58 140
        def edit_with_add
141
            if @project.respond_to?('repositories')
142
                edit_without_add()
143
                return
144
            end
59 145
            @repository = @project.repository
60
            if !@repository && !params[:repository_scm].blank?
146
            if (!@repository || @project.respond_to?('repositories')) && !params[:repository_scm].blank?
61 147
                @repository = Repository.factory(params[:repository_scm])
62 148
                @repository.project = @project if @repository
63 149
            end
......
74 160
                    end
75 161
                end
76 162
                @repository.attributes = attrs
77

  
78
                if @repository.valid? && params[:operation].present? && params[:operation] == 'add'
79
                    if attrs
80

  
81
                        begin
82
                            interface = Object.const_get("#{params[:repository_scm]}Creator")
83

  
84
                            name = interface.repository_name(attrs['url'])
85
                            if name
86
                                path = interface.path(name)
87
                                if File.directory?(path)
88
                                    @repository.errors.add(:url, :already_exists)
89
                                else
90
                                    RAILS_DEFAULT_LOGGER.info "Creating reporitory: #{path}"
91
                                    interface.execute(ScmConfig['pre_create'], path, @project) if ScmConfig['pre_create']
92
                                    if interface.create_repository(path)
93
                                        interface.execute(ScmConfig['post_create'], path, @project) if ScmConfig['post_create']
94
                                        @repository.created_with_scm = true
95
                                        unless interface.copy_hooks(path)
96
                                            RAILS_DEFAULT_LOGGER.warn "Hooks copy failed"
97
                                        end
98
                                    else
99
                                        RAILS_DEFAULT_LOGGER.error "Repository creation failed"
100
                                    end
101
                                end
102

  
103
                                @repository.root_url = interface.access_root_url(path)
104
                                @repository.url = interface.access_url(path)
105

  
106
                                if !interface.repository_name_equal?(name, @project.identifier)
107
                                    flash[:warning] = l(:text_cannot_be_used_redmine_auth)
108
                                end
109
                            else
110
                                @repository.errors.add(:url, :should_be_of_format_local, :format => interface.repository_format)
111
                            end
112

  
113
                        rescue NameError
114
                            RAILS_DEFAULT_LOGGER.error "Can't find interface for #{params[:repository_scm]}."
115
                            @repository.errors.add_to_base(:scm_not_supported)
116
                        end
117
                    end
118
                end
163
                
164
                create_repository(params)
119 165

  
120 166
                if @repository.errors.empty?
121 167
                    @repository.merge_extra_info(extra) if @repository.respond_to?(:merge_extra_info)
......
125 171

  
126 172
            render(:update) do |page|
127 173
                page.replace_html("tab-content-repository", :partial => 'projects/settings/repository')
128
                if @repository && !@project.repository
174
                if @repository && (!@project.repository||@repository.respond_to?('identifier') && (@repository.identifier == nil))
129 175
                    @project.reload
130 176
                    page.replace_html("main-menu", render_main_menu(@project))
131 177
                end
lib/scm_repositories_helper_patch.rb
22 22
        def subversion_field_tags_with_add(form, repository)
23 23
            svntags = subversion_field_tags_without_add(form, repository)
24 24

  
25
            if !@project.repository && SubversionCreator.enabled?
25
            if (!@project.repository || repository.respond_to?('identifier') && (repository.identifier == nil) ) && SubversionCreator.enabled?
26 26
                add = submit_tag(l(:button_create_new_repository), :onclick => "$('repository_operation').value = 'add';")
27 27
                svntags['<br />'] = ' ' + add + '<br />'
28 28
                svntags << hidden_field_tag(:operation, '', :id => 'repository_operation')
29 29
                unless request.post?
30 30
                    path = SubversionCreator.access_root_url(SubversionCreator.default_path(@project.identifier))
31
                    if @project.repository
32
                        path << '.subrepo' + @project.repositories.size.to_s
33
                    end
31 34
                    svntags << javascript_tag("$('repository_url').value = '#{escape_javascript(path)}';")
32 35
                end
33 36

  
......
45 48
        def mercurial_field_tags_with_add(form, repository)
46 49
            hgtags = mercurial_field_tags_without_add(form, repository)
47 50

  
48
            if !@project.repository && MercurialCreator.enabled?
51
            if (!@project.repository || repository.respond_to?('identifier') && (repository.identifier == nil) ) && MercurialCreator.enabled?
49 52
                add = submit_tag(l(:button_create_new_repository), :onclick => "$('repository_operation').value = 'add';")
50 53
                if hgtags.include?('<br />')
51 54
                    hgtags['<br />'] = ' ' + add + '<br />'
......
55 58
                hgtags << hidden_field_tag(:operation, '', :id => 'repository_operation')
56 59
                unless request.post?
57 60
                    path = MercurialCreator.access_root_url(MercurialCreator.default_path(@project.identifier))
61
                    if @project.repository
62
                        path << '.subrepo' + @project.repositories.size.to_s
63
                    end
58 64
                    hgtags << javascript_tag("$('repository_url').value = '#{escape_javascript(path)}';")
59 65
                end
60 66

  
......
78 84
        def bazaar_field_tags_with_add(form, repository)
79 85
            bzrtags = bazaar_field_tags_without_add(form, repository)
80 86

  
81
            if !@project.repository && BazaarCreator.enabled?
87
            if (!@project.repository || repository.respond_to?('identifier') && (repository.identifier == nil) ) && BazaarCreator.enabled?
82 88
                add = submit_tag(l(:button_create_new_repository), :onclick => "$('repository_operation').value = 'add';")
83 89
                bzrtags['</p>'] = ' ' + add + '</p>'
84 90
                bzrtags << hidden_field_tag(:operation, '', :id => 'repository_operation')
85 91
                unless request.post?
86 92
                    path = BazaarCreator.access_root_url(BazaarCreator.default_path(@project.identifier))
93
                    if @project.repository
94
                        path << '.subrepo' + @project.repositories.size.to_s
95
                    end
87 96
                    bzrtags << javascript_tag("$('repository_url').value = '#{escape_javascript(path)}';")
88 97
                    if BazaarCreator.options['log_encoding']
89 98
                        bzrtags << javascript_tag("$('repository_log_encoding').value = '#{escape_javascript(BazaarCreator.options['log_encoding'])}';")
......
104 113
        def git_field_tags_with_add(form, repository)
105 114
            gittags = git_field_tags_without_add(form, repository)
106 115

  
107
            if !@project.repository && GitCreator.enabled?
116
            if (!@project.repository || repository.respond_to?('identifier') && (repository.identifier == nil) ) && GitCreator.enabled?
108 117
                add = submit_tag(l(:button_create_new_repository), :onclick => "$('repository_operation').value = 'add';")
109 118
                if gittags.include?('<br />')
110 119
                    gittags['<br />'] = ' ' + add + '<br />'
......
114 123
                gittags << hidden_field_tag(:operation, '', :id => 'repository_operation')
115 124
                unless request.post?
116 125
                    path = GitCreator.access_root_url(GitCreator.default_path(@project.identifier))
126
                    if @project.repository
127
                        path << '.subrepo' + @project.repositories.size.to_s
128
                    end
117 129
                    gittags << javascript_tag("$('repository_url').value = '#{escape_javascript(path)}';")
118 130
                end
119 131

  
Terms of use | Privacy policy