From c3b9455dfdec26b658fa57944d541035c7c2cf85 Mon Sep 17 00:00:00 2001 From: Petr Korolev Date: Mon, 25 May 2015 13:02:10 +0300 Subject: [PATCH] fix regex mess --- .rubocop_todo.yml | 22 +++---- lib/github_changelog_generator/parser.rb | 75 ++++++++++++++---------- 2 files changed, 52 insertions(+), 45 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 784052c..7ad00f1 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,5 +1,5 @@ # This configuration was generated by `rubocop --auto-gen-config` -# on 2015-05-22 17:34:14 +0300 using RuboCop version 0.31.0. +# on 2015-05-25 12:59:32 +0300 using RuboCop version 0.31.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -7,29 +7,25 @@ # Offense count: 16 Metrics/AbcSize: - Max: 73 - -# Offense count: 1 -Metrics/BlockNesting: - Max: 4 + Max: 68 # Offense count: 4 # Configuration parameters: CountComments. Metrics/ClassLength: - Max: 162 + Max: 166 -# Offense count: 4 +# Offense count: 3 Metrics/CyclomaticComplexity: - Max: 15 + Max: 9 -# Offense count: 21 +# Offense count: 22 # Configuration parameters: CountComments. Metrics/MethodLength: - Max: 121 + Max: 117 -# Offense count: 5 +# Offense count: 4 Metrics/PerceivedComplexity: - Max: 18 + Max: 12 # Offense count: 2 Style/AccessorMethodName: diff --git a/lib/github_changelog_generator/parser.rb b/lib/github_changelog_generator/parser.rb index 0070c74..eebb044 100644 --- a/lib/github_changelog_generator/parser.rb +++ b/lib/github_changelog_generator/parser.rb @@ -30,7 +30,7 @@ module GitHubChangelogGenerator issue_prefix: "**Closed issues:**", bug_prefix: "**Fixed bugs:**", enhancement_prefix: "**Implemented enhancements:**", - branch: "origin" + git_remote: "origin" } parser = OptionParser.new do |opts| @@ -123,11 +123,6 @@ module GitHubChangelogGenerator exit end - if ARGV[1] - options[:tag1] = ARGV[0] - options[:tag2] = ARGV[1] - end - if options[:verbose] puts "Performing task with options:" pp options @@ -138,6 +133,18 @@ module GitHubChangelogGenerator end def self.detect_user_and_project(options) + user_project_from_option(options) + if !options[:user] || !options[:project] + if ENV["RUBYLIB"] =~ /ruby-debug-ide/ + options[:user] = "skywinder" + options[:project] = "changelog_test" + else + options[:user], options[:project] = user_project_from_remote(options[:git_remote]) + end + end + end + + def self.user_project_from_option(options) if ARGV[0] && !ARGV[1] github_site = options[:github_site] ? options[:github_site] : "github.com" # this match should parse strings such "https://github.com/skywinder/Github-Changelog-Generator" or "skywinder/Github-Changelog-Generator" to user and name @@ -155,37 +162,41 @@ module GitHubChangelogGenerator options[:user] = match[1] options[:project] = match[2] end - end + end - if !options[:user] && !options[:project] - if ENV["RUBYLIB"] =~ /ruby-debug-ide/ - options[:user] = "skywinder" - options[:project] = "changelog_test" - else - remote = `git config --get remote.#{options[:branch]}.url` - # try to find repo in format: - # origin git@github.com:skywinder/Github-Changelog-Generator.git (fetch) - # git@github.com:skywinder/Github-Changelog-Generator.git - match = /.*(?:[:\/])((?:-|\w|\.)*)\/((?:-|\w|\.)*)(?:\.git).*/.match(remote) + # Try to find user and project name from git remote + # + # @return [Tuple] user and project + def self.user_project_from_remote(git_remote) + # try to find repo in format: + # origin git@github.com:skywinder/Github-Changelog-Generator.git (fetch) + # git@github.com:skywinder/Github-Changelog-Generator.git + regex1 = /.*(?:[:\/])((?:-|\w|\.)*)\/((?:-|\w|\.)*)(?:\.git).*/ - if match && match[1] && match[2] - puts "Detected user:#{match[1]}, project:#{match[2]}" - options[:user] = match[1] - options[:project] = match[2] - else - # try to find repo in format: - # origin https://github.com/skywinder/ChangelogMerger (fetch) - # https://github.com/skywinder/ChangelogMerger - match = /.*\/((?:-|\w|\.)*)\/((?:-|\w|\.)*).*/.match(remote) - if match && match[1] && match[2] - puts "Detected user:#{match[1]}, project:#{match[2]}" - options[:user] = match[1] - options[:project] = match[2] - end - end + # try to find repo in format: + # origin https://github.com/skywinder/ChangelogMerger (fetch) + # https://github.com/skywinder/ChangelogMerger + regex2 = /.*\/((?:-|\w|\.)*)\/((?:-|\w|\.)*).*/ + + remote_structures = [regex1, regex2] + + remote = `git config --get remote.#{git_remote}.url` + user = nil + project = nil + remote_structures.each do |regex| + matches = Regexp.new(regex).match(remote) + + if matches && matches[1] && matches[2] + puts "Detected user:#{matches[1]}, project:#{matches[2]}" + user = matches[1] + project = matches[2] end + + break unless matches.nil? end + + [user, project] end end end