From d1fa49ad26122726b876d3d2d7dcb8a494302d32 Mon Sep 17 00:00:00 2001
From: Rajko Albrecht <rajko.albrecht@7p-group.com>
Date: Wed, 19 Feb 2014 10:30:05 +0100
Subject: [PATCH 09/10] Special repository name input if allow_add_local ==
 false

---
 app/views/repositories/_form.html.erb         |  4 +++
 assets/javascripts/scm_create_pathname.coffee | 14 +++++++++
 assets/javascripts/scm_create_pathname.js     | 25 +++++++++++++++
 lib/creator/git_creator.rb                    |  6 ++++
 lib/creator/scm_creator.rb                    |  4 +++
 lib/scm_repositories_helper_patch.rb          | 45 +++++++++++++++++++++++----
 6 files changed, 92 insertions(+), 6 deletions(-)
 create mode 100644 assets/javascripts/scm_create_pathname.coffee
 create mode 100644 assets/javascripts/scm_create_pathname.js

diff --git a/app/views/repositories/_form.html.erb b/app/views/repositories/_form.html.erb
index 13aadb3..aef2b18 100644
--- a/app/views/repositories/_form.html.erb
+++ b/app/views/repositories/_form.html.erb
@@ -1,3 +1,7 @@
+<% content_for :header_tags do %>
+  <%= javascript_include_tag('scm_create_pathname', :plugin => 'redmine_scm') %>
+<% end %>
+
 <%= error_messages_for 'repository' %>
 
 <div class="box tabular">
diff --git a/assets/javascripts/scm_create_pathname.coffee b/assets/javascripts/scm_create_pathname.coffee
new file mode 100644
index 0000000..e805cde
--- /dev/null
+++ b/assets/javascripts/scm_create_pathname.coffee
@@ -0,0 +1,14 @@
+$(document).ready () ->
+  $('#repositoryname').keyup () -> checknamevalue()
+
+@checknamevalue = () ->
+  curval = $('#repositoryname').val()
+  if curval == undefined
+    return undefined
+  base = $('#uri_result_text').attr('data_base')
+  bpath = $('#uri_result_text').attr('data_basepath')
+  ext = $('#uri_result_text').attr('data_ext')
+  res = base+ (if curval.length > 0 then "."+curval else "")+(if ext.length > 0 then "."+ext else "")
+  $('#uri_result_text').html(res)
+  pres = bpath + (if curval.length > 0 then "."+curval else "")+(if ext.length > 0 then "."+ext else "")
+  $('#repository_url').val(pres)
diff --git a/assets/javascripts/scm_create_pathname.js b/assets/javascripts/scm_create_pathname.js
new file mode 100644
index 0000000..4baec03
--- /dev/null
+++ b/assets/javascripts/scm_create_pathname.js
@@ -0,0 +1,25 @@
+// Generated by CoffeeScript 1.4.0
+(function() {
+
+  $(document).ready(function() {
+    return $('#repositoryname').keyup(function() {
+      return checknamevalue();
+    });
+  });
+
+  this.checknamevalue = function() {
+    var base, bpath, curval, ext, pres, res;
+    curval = $('#repositoryname').val();
+    if (curval === void 0) {
+      return void 0;
+    }
+    base = $('#uri_result_text').attr('data_base');
+    bpath = $('#uri_result_text').attr('data_basepath');
+    ext = $('#uri_result_text').attr('data_ext');
+    res = base + (curval.length > 0 ? "." + curval : "") + (ext.length > 0 ? "." + ext : "");
+    $('#uri_result_text').html(res);
+    pres = bpath + (curval.length > 0 ? "." + curval : "") + (ext.length > 0 ? "." + ext : "");
+    return $('#repository_url').val(pres);
+  };
+
+}).call(this);
diff --git a/lib/creator/git_creator.rb b/lib/creator/git_creator.rb
index 291a772..0ab2eb7 100644
--- a/lib/creator/git_creator.rb
+++ b/lib/creator/git_creator.rb
@@ -22,6 +22,8 @@ class GitCreator < SCMCreator
             false
         end
 
+        alias_method :parent_external_url, :external_url
+        
         def external_url(name, regexp = %r{^(?:https?|git|ssh)://})
             if options['git_ext'] && File.directory?("#{path(name)}.git")
               super+".git"
@@ -30,6 +32,10 @@ class GitCreator < SCMCreator
             end
         end
 
+        def base_url(name,regex= %r{^(?:https?|git|ssh)://})
+          parent_external_url(name,regex)
+        end
+        
         def default_path(identifier)
             if options['git_ext']
                 path(identifier) + '.git'
diff --git a/lib/creator/scm_creator.rb b/lib/creator/scm_creator.rb
index 7c4f19e..ee0b0d4 100644
--- a/lib/creator/scm_creator.rb
+++ b/lib/creator/scm_creator.rb
@@ -59,6 +59,10 @@ class SCMCreator
             end
         end
 
+        def base_url(name,regex= %r{^(?:https?|git|ssh)://})
+          external_url(name,regex)
+        end
+
         # constructs default path using project identifier
         def default_path(identifier)
             path(identifier)
diff --git a/lib/scm_repositories_helper_patch.rb b/lib/scm_repositories_helper_patch.rb
index 7296e4d..2dd92ef 100644
--- a/lib/scm_repositories_helper_patch.rb
+++ b/lib/scm_repositories_helper_patch.rb
@@ -49,6 +49,9 @@ module ScmRepositoriesHelperPatch
                     reptags << javascript_tag("$(document).ready(function() { $('#repository_save')." + (button_disabled ? "attr('disabled','disabled')" : "removeAttr('enable')") + "; });")
                 end
             end
+            
+            reptags << javascript_tag("$(document).ready(function() { checknamevalue(); });")
+            reptags << javascript_tag("$('#repositoryname').keyup(function() {checknamevalue();});")
 
             return reptags.html_safe
         end
@@ -64,6 +67,20 @@ module ScmRepositoriesHelperPatch
           end
         end
 
+        def build_repository_name (extraname,creator,ext="")
+          if ScmConfig['allow_add_local']
+            return ""
+          end
+          base_uri = creator.base_url(@project.identifier)
+          p = creator.default_path(@project.identifier)
+          unless ext.blank?
+            p = p.gsub!('.'+ext,'')
+          end
+          content_tag(:p, label_tag("repositoryname","Repository name")+@project.identifier+"." + text_field_tag("repositoryname",extraname)+(ext.blank? ? "" : "."+ext ))+
+          content_tag(:p, content_tag(:span, base_uri,:data_base => base_uri, :data_ext => ext, :data_basepath => p, :id => "uri_result_text" ) ) +
+          javascript_tag("$('#repository_url').attr('readonly',true);")
+        end
+        
         def subversion_field_tags_with_add(form, repository)
             svntags = subversion_field_tags_reduced(form,repository)
             svntags.gsub!('&lt;br /&gt;', '<br />')
@@ -80,13 +97,18 @@ module ScmRepositoriesHelperPatch
                 unless request.post?
                     path = SubversionCreator.access_root_url(SubversionCreator.default_path(@project.identifier))
                     if SubversionCreator.repository_exists?(@project.identifier) && @project.respond_to?(:repositories)
-                        path << '.' + @project.repositories.select{ |r| r.created_with_scm }.size.to_s
+                        pext = @project.repositories.select{ |r| r.created_with_scm }.size.to_s
+                        path << '.' + pext
+                    else
+                        pext = ""
                     end
                     if defined? observe_field # Rails 3.0 and below
                         svntags << javascript_tag("$('repository_url').value = '#{escape_javascript(path)}';")
                     else # Rails 3.1 and above
                         svntags << javascript_tag("$('#repository_url').val('#{escape_javascript(path)}');")
                     end
+                    svntags << "\n" << build_repository_name(pext,SubversionCreator,"")
+                    svntags << javascript_tag("checknamevalue();")
                 end
 
             elsif !repository.new_record? && repository.created_with_scm &&
@@ -119,13 +141,18 @@ module ScmRepositoriesHelperPatch
                 unless request.post?
                     path = MercurialCreator.access_root_url(MercurialCreator.default_path(@project.identifier))
                     if MercurialCreator.repository_exists?(@project.identifier) && @project.respond_to?(:repositories)
-                        path << '.' + @project.repositories.select{ |r| r.created_with_scm }.size.to_s
+                        pext = @project.repositories.select{ |r| r.created_with_scm }.size.to_s
+                        path << '.' + pext
+                    else
+                        pext = ""
                     end
                     if defined? observe_field # Rails 3.0 and below
                         hgtags << javascript_tag("$('repository_url').value = '#{escape_javascript(path)}';")
                     else # Rails 3.1 and above
                         hgtags << javascript_tag("$('#repository_url').val('#{escape_javascript(path)}');")
                     end
+                    hgtags << "\n" << build_repository_name(pext,MercurialCreator,"" )
+                    hgtags << javascript_tag("checknamevalue();")
                 end
 
             elsif !repository.new_record? && repository.created_with_scm &&
@@ -160,7 +187,10 @@ module ScmRepositoriesHelperPatch
                 unless request.post?
                     path = BazaarCreator.access_root_url(BazaarCreator.default_path(@project.identifier))
                     if BazaarCreator.repository_exists?(@project.identifier) && @project.respond_to?(:repositories)
-                        path << '.' + @project.repositories.select{ |r| r.created_with_scm }.size.to_s
+                        pext = @project.repositories.select{ |r| r.created_with_scm }.size.to_s
+                        path << '.' + pext
+                    else
+                        pext = ""
                     end
                     if defined? observe_field # Rails 3.0 and below
                         bzrtags << javascript_tag("$('repository_url').value = '#{escape_javascript(path)}';")
@@ -174,6 +204,7 @@ module ScmRepositoriesHelperPatch
                             bzrtags << javascript_tag("$('#repository_log_encoding').val('#{escape_javascript(BazaarCreator.options['log_encoding'])}');")
                         end
                     end
+                    bzrtags << "\n" << build_repository_name(pext,BazaarCreator,"" )
                 end
 
             elsif !repository.new_record? && repository.created_with_scm &&
@@ -202,11 +233,11 @@ module ScmRepositoriesHelperPatch
                 unless request.post?
                     path = GitCreator.access_root_url(GitCreator.default_path(@project.identifier))
                     if GitCreator.repository_exists?(@project.identifier) && @project.respond_to?(:repositories)
-                        offset = '.'+@project.repositories.select{ |r| r.created_with_scm }.size.to_s
+                        offset = @project.repositories.select{ |r| r.created_with_scm }.size.to_s
                         if GitCreator.options['git_ext']
-                          path.gsub!('.git',offset+'.git')
+                          path.gsub!('.git','.'+offset+'.git')
                         else
-                          path << offset
+                          path << '.' << offset
                         end
                     end
                     if defined? observe_field # Rails 3.0 and below
@@ -214,6 +245,8 @@ module ScmRepositoriesHelperPatch
                     else # Rails 3.1 and above
                         gittags << javascript_tag("$('#repository_url').val('#{escape_javascript(path)}');")
                     end
+                    gittags << "\n" << build_repository_name(offset,GitCreator,GitCreator.options['git_ext'] ? "git" : "" )
+                    gittags << javascript_tag("checknamevalue();")
                 end
 
             elsif !repository.new_record? && repository.created_with_scm &&
-- 
1.9.2

