commit-msg-url-shortener 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ lib/commit-msg-url-shortener/services/leetspeak.rb
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in commit-msg-url-shortener.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Daniele Molteni
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,60 @@
1
+ # commit-msg-url-shortener
2
+
3
+ Shortens the urls that may appear in your commit message (and more fun stuff)
4
+
5
+ ## Install it
6
+
7
+ $ gem install commit-msg-url-shortener
8
+
9
+ ## Use it
10
+
11
+ Move to a git-controlled folder and type the command commit-msg-url-shortener to show the help page.
12
+ Available commands are:
13
+
14
+ $ commit-msg-url-shortener use [service]
15
+ $ commit-msg-url-shortener remove
16
+ $ commit-msg-url-shortener status
17
+
18
+ Example:
19
+
20
+ $ commit-msg-url-shortener use tinyurl.com
21
+ > Using "tinyurl.com".
22
+ $ git commit -m "fixes a security flow, see http://railspikes.com/2008/9/22/is-your-rails-application-safe-from-mass-assignment"
23
+ > [master d0adf01] fixes a security flow, see http://tinyurl.com/2g9mqh
24
+ 1 files changed, 10 insertions(+), 9 deletions(-)
25
+
26
+ ## Hack it
27
+
28
+ Fork it, and add support for new url-shortener sites. You can also create your own service that pre-process commit messages!
29
+ For example you can create a must-have service that automatically translate your commit message into leetspeak:
30
+
31
+ 1. Create a new file called leetspeak.rb in the services folder
32
+ 2. Fill the leetspeak.rb file with the following code:
33
+
34
+ CommitMsgUrlShortener::Service.define do |commit_message|
35
+ translation = {:a=>4,:b=>8,:e=>3,:i=>'!',
36
+ :k=>:X,:l=>1,:o=>0,:s=>7,:t=>7,:z=>2}
37
+ new_message = ''
38
+ commit_message.each_char do |c|
39
+ t = translation[c.downcase.to_sym]
40
+ new_message += t ? t.to_s : c
41
+ end
42
+ new_message
43
+ end
44
+
45
+ 3. Rebuild/reinstall the gem:
46
+
47
+ $ bundle exec rake install
48
+
49
+ 4. Now you can use your brand new service! Move to your super-secret-git-versioned-project and type:
50
+
51
+ $ commit-msg-url-shortener use leetspeak
52
+ > Using "leetspeak".
53
+ $ git commit -am "initial commit!"
54
+ > [master d0adf00] !n!7!41 c0mm!7!
55
+ 1 files changed, 4 insertions(+), 4 deletions(-)
56
+
57
+
58
+
59
+
60
+
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,78 @@
1
+ #!/usr/bin/env ruby
2
+ if ENV and ENV['ENV'] == 'development'
3
+ fullpath = File.expand_path 'lib/commit-msg-url-shortener.rb'
4
+ $LOAD_PATH.push File.dirname fullpath
5
+ require fullpath
6
+ elsif
7
+ require 'commit-msg-url-shortener'
8
+ end
9
+
10
+ def usage
11
+ bin_name = File.basename __FILE__
12
+ services_list = CommitMsgUrlShortener::SERVICES.map{|s| s.name}.join ', '
13
+ puts <<-TEXT
14
+ Usage:
15
+ #{bin_name} use [service] # Apply the service to your commits' messages.
16
+ #{bin_name} remove # Stop using any service.
17
+ #{bin_name} status # Outputs the status.
18
+ Services:
19
+ #{services_list}
20
+ TEXT
21
+ exit
22
+ end
23
+
24
+ def parse_argv
25
+ modes = [:use, :remove, :status, :apply]
26
+ usage if ARGV.empty?
27
+ mode = ARGV.first.to_sym
28
+ usage unless modes.include? mode
29
+ [mode, ARGV[1], ARGV[2]]
30
+ end
31
+
32
+ def quit msg=''
33
+ puts msg if msg and msg.size > 0
34
+ exit
35
+ end
36
+
37
+ def get_hook_script git_path
38
+ hook_script = CommitMsgUrlShortener::HookScript.new git_path
39
+ if hook_script.exist?
40
+ quit "The language used in your #{hook_script.name} hook is not supported yet." if hook_script.interpreter.nil?
41
+ else
42
+ hook_script.create
43
+ end
44
+ hook_script
45
+ end
46
+
47
+ def get_hook_code interpreter
48
+ hook_code = CommitMsgUrlShortener::HOOK_CODES.select{|hook_code| hook_code.interpreter == interpreter}.first
49
+ if hook_code.nil? or hook_code.to_s.size == 0
50
+ quit "Cannot find a suitable hook for the #{interpreter} interpreter."
51
+ end
52
+ hook_code
53
+ end
54
+
55
+ mode, service_name, commit_msg_filepath = parse_argv
56
+ git_path = CommitMsgUrlShortener::Git.toplevel
57
+ quit "Not in a git repository." unless git_path
58
+ hook_script = get_hook_script git_path
59
+ hook_code = get_hook_code hook_script.interpreter
60
+ service_name ||= CommitMsgUrlShortener::DEFAULT_SERVICE
61
+ service = CommitMsgUrlShortener::SERVICES.select{|s| s.name == service_name}.first
62
+
63
+ if mode == :use
64
+ quit "Missing service #{service_name.inspect}." unless service
65
+ hook_script.append_code hook_code.code.gsub(CommitMsgUrlShortener::HOOK_CODE_SRV_TOKEN, service.name)
66
+ quit "Using #{service_name.inspect}."
67
+ elsif mode == :status
68
+ used_service = hook_script.get_used_service
69
+ quit "No service in use." unless used_service
70
+ quit "#{used_service.name.inspect} is in use."
71
+ elsif mode == :remove
72
+ removed_lines = hook_script.remove_code
73
+ quit "No service in use." if removed_lines.empty?
74
+ quit "Removed."
75
+ elsif mode == :apply
76
+ commit_msg_filepath = File.expand_path commit_msg_filepath
77
+ service.apply commit_msg_filepath
78
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/commit-msg-url-shortener/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Daniele Molteni"]
6
+ gem.email = ["dani.m.mobile@gmail.com"]
7
+ gem.description = %q{Shortens the url that may appear in your commit message.}
8
+ gem.summary = %q{Shortens the url that may appear in your commit message.}
9
+ gem.homepage = ""
10
+ gem.platform = Gem::Platform::RUBY
11
+
12
+ gem.files = `git ls-files`.split($\)
13
+ gem.executables << CommitMsgUrlShortener::BIN_NAME
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+ gem.name = CommitMsgUrlShortener::GEM_NAME
16
+ gem.require_paths = ["lib"]
17
+ gem.version = CommitMsgUrlShortener::VERSION
18
+
19
+ gem.add_development_dependency "bundler"
20
+ gem.add_development_dependency "rake"
21
+ gem.add_development_dependency "rspec"
22
+ end
@@ -0,0 +1,22 @@
1
+ gem_name = File.basename(__FILE__).gsub File.extname(__FILE__), ''
2
+ require "#{gem_name}/version"
3
+ require "#{gem_name}/hook_script"
4
+ require "#{gem_name}/hook_code"
5
+ require "#{gem_name}/git"
6
+ require "#{gem_name}/service"
7
+ require "#{gem_name}/service_helper"
8
+
9
+ module CommitMsgUrlShortener
10
+ DEFAULT_SERVICE = 'goo.gl'
11
+ HOOK_CODE_SRV_TOKEN = 'SERVICE'
12
+ ROOT_PATH = File.dirname File.expand_path(__FILE__)
13
+ HOOK_CODES = Dir[File.join ROOT_PATH, "#{GEM_NAME}/hook_codes/*.*"].map do |path|
14
+ hook_code = HookCode.fabricate path
15
+ path = hook_code
16
+ end
17
+ SERVICES = Dir[File.join ROOT_PATH, "#{GEM_NAME}/services/*.rb"].map do |path|
18
+ service = Service.fabricate path
19
+ path = service if service
20
+ end.compact
21
+ INTERPRETERS = HOOK_CODES.map {|hook_code| hook_code = hook_code.interpreter}
22
+ end
@@ -0,0 +1,10 @@
1
+ module CommitMsgUrlShortener
2
+ module Git
3
+ extend self
4
+ def toplevel
5
+ git_path = %x[git rev-parse --show-toplevel] rescue ''
6
+ git_path.strip!
7
+ git_path if File.directory? git_path
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,15 @@
1
+ module CommitMsgUrlShortener
2
+ class HookCode
3
+ attr_reader :interpreter, :code
4
+ def initialize interpreter, code
5
+ @interpreter = interpreter
6
+ @code = code
7
+ end
8
+
9
+ def self.fabricate path
10
+ interpreter = File.basename(path).gsub(File.extname(path), '').downcase
11
+ code = File.read(path).strip
12
+ HookCode.new interpreter, code
13
+ end
14
+ end
15
+ end
@@ -0,0 +1 @@
1
+ system "commit-msg-url-shortener apply \"SERVICE\" \"#{ARGV[0]}\""
@@ -0,0 +1 @@
1
+ commit-msg-url-shortener apply "SERVICE" "$1"
@@ -0,0 +1,86 @@
1
+ module CommitMsgUrlShortener
2
+ class HookScript
3
+ attr_reader :name
4
+
5
+ def initialize git_path
6
+ @name = 'commit-msg'
7
+ @hook_path = File.expand_path File.join(git_path, ".git/hooks/#{@name}")
8
+ end
9
+
10
+ def append_code code
11
+ remove_code
12
+ open('a+b') {|file| file.puts "#{code}"}
13
+ end
14
+
15
+ def interpreter
16
+ @interpreter ||= begin
17
+ return unless exist?
18
+ interpreter = nil
19
+ open('rb') do |file|
20
+ shebang_line = ''
21
+ file.each_line{|line| shebang_line = line if line.index '#!'}
22
+ INTERPRETERS.each{|name| interpreter = name if shebang_line.downcase.index name}
23
+ end
24
+ interpreter
25
+ end
26
+ end
27
+
28
+ def remove_code
29
+ new_lines = []
30
+ removed_lines = []
31
+ return removed_lines unless exist?
32
+ open('rb') do |file|
33
+ file.each_line do |line|
34
+ unless line.downcase.index BIN_NAME.downcase
35
+ new_lines << line
36
+ else
37
+ removed_lines << line
38
+ end
39
+ end
40
+ end
41
+ open('wb') do |file|
42
+ new_lines.each{|line| file.puts line}
43
+ end
44
+ removed_lines
45
+ end
46
+
47
+ def get_used_service
48
+ lines_call_the_gem = []
49
+ service = nil
50
+ return service unless exist?
51
+ open('rb') do |file|
52
+ file.each_line {|line| lines_call_the_gem << line if line.downcase.index(BIN_NAME.downcase)}
53
+ if lines_call_the_gem.size > 1
54
+ raise "It appears that there are multiple calls to #{BIN_NAME} in your #{name} hook."
55
+ end
56
+ unless lines_call_the_gem.empty?
57
+ service = SERVICES.select{|s| lines_call_the_gem.first.include? s.name}.first
58
+ raise "It appears that your are using an unknow service." unless service
59
+ end
60
+ end
61
+ service
62
+ end
63
+
64
+ def exist?
65
+ File.exist? @hook_path
66
+ end
67
+
68
+ def create
69
+ open('wb') {|file| file.puts "#!/bin/sh"}
70
+ @interpreter = 'sh'
71
+ make_executable
72
+ end
73
+
74
+ private
75
+
76
+ def make_executable
77
+ command = 'chmod ug+x "'+@hook_path+'" >/dev/null 2>&1'
78
+ system_result = system(command) rescue false
79
+ raise "I was unable to make the file #{@hook_path} executable." unless system_result
80
+ end
81
+
82
+ def open mode, &block
83
+ File.send :open, @hook_path, mode, &block
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,43 @@
1
+ module CommitMsgUrlShortener
2
+ class Service
3
+ attr_reader :name
4
+ def initialize name, &block
5
+ @name = name
6
+ @block = block
7
+ end
8
+
9
+ def apply commit_msg_filepath
10
+ raise "Cannot find temporary commit message file #{commit_msg_filepath.inspect}" unless File.exist? commit_msg_filepath
11
+ commit_message = File.read(commit_msg_filepath).strip
12
+ transformed_message = ServiceHelper.module_exec(commit_message, &@block) || ''
13
+ transformed_message.strip!
14
+ if transformed_message != commit_message and transformed_message.size > 0
15
+ File.open(commit_msg_filepath, 'wb') {|file| file.write transformed_message}
16
+ transformed_message
17
+ else
18
+ nil
19
+ end
20
+ end
21
+
22
+ def self.define &block
23
+ if @@expected.nil? or @@expected.kind_of?(Service)
24
+ raise "You must load the service script using the Service#fabricate method."
25
+ end
26
+ @@expected = Service.new @@expected, &block
27
+ end
28
+
29
+ def self.fabricate path
30
+ @@expected = name_of path
31
+ require path
32
+ unless @@expected.kind_of? Service
33
+ puts "Warning! Service #{@@expected.inspect} should call the Service#define method."
34
+ else
35
+ @@expected
36
+ end
37
+ end
38
+
39
+ def self.name_of path
40
+ File.basename(path).gsub(File.extname(path), '').downcase.strip
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,38 @@
1
+ module CommitMsgUrlShortener
2
+ module ServiceHelper
3
+ extend self
4
+ require 'net/http'
5
+ require 'net/https'
6
+ require 'json'
7
+
8
+ def extract_urls text
9
+ regex = /(http[s]?\:\/\/[a-zA-Z0-9\-\.]+\.[\S+]+)/
10
+ text.scan(regex).flatten
11
+ end
12
+
13
+ def error_safe_request &block
14
+ connection_error_message = "#{CommitMsgUrlShortener::GEM_NAME} connection error! Maybe you're offline?\n"+
15
+ "In this case you should disable #{CommitMsgUrlShortener::GEM_NAME}."
16
+ yield
17
+ rescue Timeout::Error => e
18
+ puts connection_error_message
19
+ rescue SocketError => e
20
+ puts connection_error_message
21
+ end
22
+
23
+ def each_long_url text, &block
24
+ urls = extract_urls text
25
+ return text if urls.empty?
26
+ new_text = text
27
+ urls.each do |url|
28
+ short_url = yield(url)
29
+ unless short_url
30
+ new_text = nil
31
+ break
32
+ end
33
+ new_text = new_text.gsub(url, short_url)
34
+ end
35
+ new_text
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,16 @@
1
+ CommitMsgUrlShortener::Service.define do |commit_message|
2
+ def call_googl_api long_url
3
+ error_safe_request do
4
+ uri = URI 'https://www.googleapis.com/urlshortener/v1/url'
5
+ http = Net::HTTP.new uri.host, 443
6
+ http.use_ssl = true
7
+ http.read_timeout = 3
8
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
9
+ resp = http.post uri.path, "{'longUrl': '#{long_url}'}", {"content-type" => "application/json"}
10
+ json = JSON.parse resp.body
11
+ json['id']
12
+ end
13
+ end
14
+
15
+ each_long_url(commit_message) {|url| call_googl_api url}
16
+ end
@@ -0,0 +1,10 @@
1
+ CommitMsgUrlShortener::Service.define do |commit_message|
2
+ def call_isgd_api long_url
3
+ error_safe_request do
4
+ uri = URI "http://is.gd/create.php?format=simple&url=#{long_url}"
5
+ Net::HTTP.get uri
6
+ end
7
+ end
8
+
9
+ each_long_url(commit_message) {|url| call_isgd_api url}
10
+ end
@@ -0,0 +1,10 @@
1
+ CommitMsgUrlShortener::Service.define do |commit_message|
2
+ def call_tinyurl_api long_url
3
+ error_safe_request do
4
+ uri = URI "http://tinyurl.com/api-create.php?url=#{long_url}"
5
+ Net::HTTP.get uri
6
+ end
7
+ end
8
+
9
+ each_long_url(commit_message) {|url| call_tinyurl_api url}
10
+ end
@@ -0,0 +1,5 @@
1
+ module CommitMsgUrlShortener
2
+ VERSION = "0.0.1"
3
+ BIN_NAME = 'commit-msg-url-shortener'
4
+ GEM_NAME = 'commit-msg-url-shortener'
5
+ end
@@ -0,0 +1 @@
1
+ do it
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: commit-msg-url-shortener
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Daniele Molteni
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Shortens the url that may appear in your commit message.
63
+ email:
64
+ - dani.m.mobile@gmail.com
65
+ executables:
66
+ - commit-msg-url-shortener
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - .gitignore
71
+ - Gemfile
72
+ - LICENSE
73
+ - README.md
74
+ - Rakefile
75
+ - bin/commit-msg-url-shortener
76
+ - commit-msg-url-shortener.gemspec
77
+ - lib/commit-msg-url-shortener.rb
78
+ - lib/commit-msg-url-shortener/git.rb
79
+ - lib/commit-msg-url-shortener/hook_code.rb
80
+ - lib/commit-msg-url-shortener/hook_codes/ruby.rb
81
+ - lib/commit-msg-url-shortener/hook_codes/sh.sh
82
+ - lib/commit-msg-url-shortener/hook_script.rb
83
+ - lib/commit-msg-url-shortener/service.rb
84
+ - lib/commit-msg-url-shortener/service_helper.rb
85
+ - lib/commit-msg-url-shortener/services/goo.gl.rb
86
+ - lib/commit-msg-url-shortener/services/is.gd.rb
87
+ - lib/commit-msg-url-shortener/services/tinyurl.com.rb
88
+ - lib/commit-msg-url-shortener/version.rb
89
+ - spec/fill_me
90
+ homepage: ''
91
+ licenses: []
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ segments:
103
+ - 0
104
+ hash: -1375090855405394249
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ segments:
112
+ - 0
113
+ hash: -1375090855405394249
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 1.8.24
117
+ signing_key:
118
+ specification_version: 3
119
+ summary: Shortens the url that may appear in your commit message.
120
+ test_files:
121
+ - spec/fill_me