captain_hook 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Adam Pearson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,30 @@
1
+ = captain_hook
2
+
3
+ A ruby library for managing git hooks.
4
+
5
+ == Usage
6
+
7
+ === A post-receive hook sending to campfire (using your favorite campfire lib)
8
+ your_repo/.git/hooks/post-receive:
9
+ #! /usr/bin/env ruby
10
+
11
+ @hook = CaptainHook.from_path(File.join(File.dirname(__FILE__), "..",".."))
12
+
13
+ @hook.handle_post_receive do
14
+ @room = Campfire.room(1)
15
+ @room.message "New commit: '#{message}' by #{author} to #{ref_name}"
16
+ end
17
+
18
+ == Note on Patches/Pull Requests
19
+
20
+ * Fork the project.
21
+ * Make your feature addition or bug fix.
22
+ * Add tests for it. This is important so I don't break it in a
23
+ future version unintentionally.
24
+ * Commit, do not mess with rakefile, version, or history.
25
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
26
+ * Send me a pull request. Bonus points for topic branches.
27
+
28
+ == Copyright
29
+
30
+ Copyright (c) 2010 Adam Pearson. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "captain_hook"
8
+ gem.summary = %Q{A ruby manager for git hooks}
9
+ gem.description = %Q{Makes handling git hooks easy, with built-in notification to campfire}
10
+ gem.email = "ampearson@gmail.com"
11
+ gem.homepage = "http://github.com/radamant/captain_hook"
12
+ gem.authors = ["Adam Pearson"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ gem.add_dependency "grit", "2.0.0"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'spec/rake/spectask'
23
+ Spec::Rake::SpecTask.new(:spec) do |spec|
24
+ spec.libs << 'lib' << 'spec'
25
+ spec.spec_files = FileList['spec/**/*_spec.rb']
26
+ end
27
+
28
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
29
+ spec.libs << 'lib' << 'spec'
30
+ spec.pattern = 'spec/**/*_spec.rb'
31
+ spec.rcov = true
32
+ end
33
+
34
+ task :spec => :check_dependencies
35
+
36
+ task :default => :spec
37
+
38
+ require 'rake/rdoctask'
39
+ Rake::RDocTask.new do |rdoc|
40
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "captain_hook #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,68 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{captain_hook}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Adam Pearson"]
12
+ s.date = %q{2010-02-25}
13
+ s.description = %q{Makes handling git hooks easy, with built-in notification to campfire}
14
+ s.email = %q{ampearson@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "captain_hook.gemspec",
27
+ "lib/captain_hook.rb",
28
+ "lib/captain_hook/base.rb",
29
+ "lib/captain_hook/dsl.rb",
30
+ "lib/captain_hook/dsl/handle_post_receive.rb",
31
+ "lib/captain_hook/post_receive_event.rb",
32
+ "spec/captain_hook/base_spec.rb",
33
+ "spec/captain_hook/dsl/handle_post_receive_spec.rb",
34
+ "spec/captain_hook/post_receive_event_spec.rb",
35
+ "spec/captain_hook_spec.rb",
36
+ "spec/spec.opts",
37
+ "spec/spec_helper.rb"
38
+ ]
39
+ s.homepage = %q{http://github.com/radamant/captain_hook}
40
+ s.rdoc_options = ["--charset=UTF-8"]
41
+ s.require_paths = ["lib"]
42
+ s.rubygems_version = %q{1.3.5}
43
+ s.summary = %q{A ruby manager for git hooks}
44
+ s.test_files = [
45
+ "spec/captain_hook/base_spec.rb",
46
+ "spec/captain_hook/dsl/handle_post_receive_spec.rb",
47
+ "spec/captain_hook/post_receive_event_spec.rb",
48
+ "spec/captain_hook_spec.rb",
49
+ "spec/spec_helper.rb"
50
+ ]
51
+
52
+ if s.respond_to? :specification_version then
53
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
54
+ s.specification_version = 3
55
+
56
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
57
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
58
+ s.add_runtime_dependency(%q<grit>, ["= 2.0.0"])
59
+ else
60
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
61
+ s.add_dependency(%q<grit>, ["= 2.0.0"])
62
+ end
63
+ else
64
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
65
+ s.add_dependency(%q<grit>, ["= 2.0.0"])
66
+ end
67
+ end
68
+
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'grit'
3
+ module CaptainHook
4
+ require 'captain_hook/base'
5
+ require 'captain_hook/post_receive_event'
6
+ require 'captain_hook/dsl'
7
+
8
+ def self.from_path(path)
9
+ repo = Grit::Repo.new(path)
10
+ Base.new(repo)
11
+ end
12
+ end
@@ -0,0 +1,29 @@
1
+ module CaptainHook
2
+ class Base
3
+ attr_accessor :repo
4
+
5
+ def initialize(repo)
6
+ @repo = repo
7
+ end
8
+
9
+ def handle_post_receive(data = nil, &block)
10
+ data = data || $stdin
11
+ post_receive = post_receive_from_data(data)
12
+
13
+ dsl = CaptainHook::DSL::HandlePostReceive.new(post_receive)
14
+ dsl.run(&block) if block_given?
15
+ end
16
+
17
+ private
18
+
19
+ def post_receive_from_data(data)
20
+ args = data.read.split(" ")
21
+ post_receive = CaptainHook::PostReceiveEvent.new(
22
+ :old_sha => args[0],
23
+ :new_sha => args[1],
24
+ :ref_name => args[2],
25
+ :repo => @repo
26
+ )
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,5 @@
1
+ module CaptainHook
2
+ module DSL
3
+ require 'captain_hook/dsl/handle_post_receive'
4
+ end
5
+ end
@@ -0,0 +1,26 @@
1
+ module CaptainHook
2
+ module DSL
3
+ class HandlePostReceive
4
+ def initialize(post_receive_event)
5
+ @post_receive_event = post_receive_event
6
+ end
7
+
8
+ def message
9
+ puts "post-receive: #{@post_receive_event.message}"
10
+ @post_receive_event.message
11
+ end
12
+
13
+ def ref_name
14
+ @post_receive_event.ref_name
15
+ end
16
+
17
+ def author
18
+ @post_receive_event.author
19
+ end
20
+
21
+ def run &block
22
+ instance_eval &block
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,44 @@
1
+ module CaptainHook
2
+ class PostReceiveEvent
3
+ attr_accessor :repo, :old_commit, :new_commit
4
+
5
+ def initialize(options = {})
6
+ @repo = options[:repo]
7
+ @old_commit = @repo.commit(options[:old_sha])
8
+ @new_commit = @repo.commit(options[:new_sha])
9
+ @full_ref_name = options[:ref_name]
10
+ end
11
+
12
+ def commits
13
+ @commits ||= @repo.commits_between(new_commit, old_commit)
14
+ end
15
+
16
+ def ref_name
17
+ @ref_name ||= begin
18
+ @full_ref_name =~ %r{^refs/(?:tags|heads|remotes)/(.+)} ?
19
+ $1 : "unknown"
20
+ end
21
+ end
22
+
23
+ # Returns the author of the most-recent revision
24
+ def author
25
+ new_commit.author
26
+ end
27
+
28
+ # Returns the first line of the most recent revision's commit message
29
+ def message
30
+ @message ||= new_commit.message.split("\n")[0]
31
+ end
32
+
33
+ # Returns lines 1..n of the most recent revision's commit message
34
+ def message_detail
35
+ @message_detail ||= begin
36
+ new_commit.message.
37
+ split("\n")[1..-1].
38
+ reject{|s| s == ""}.
39
+ join("\n")
40
+ end
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,41 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe CaptainHook::Base do
4
+ context "with a valid repo" do
5
+ before(:each) do
6
+ @repo = mock("Git repo").as_null_object
7
+ @hook = CaptainHook::Base.new(@repo)
8
+ end
9
+
10
+ describe :handle_post_receive do
11
+ before(:each) do
12
+ @yielded = nil
13
+ @args = "sha1 sha2 refree"
14
+ $stdin.stub!(:read).and_return(@args)
15
+ end
16
+
17
+ it "should call run on a CaptainHook::DSL::HandlePostReceive" do
18
+ dsl = mock()
19
+ CaptainHook::DSL::HandlePostReceive.stub!(:new).and_return(dsl)
20
+ dsl.should_receive(:run)
21
+ @hook.handle_post_receive {}
22
+ end
23
+
24
+ it "should use $stdin to parse args" do
25
+ CaptainHook::PostReceiveEvent.
26
+ should_receive(:new).
27
+ with({
28
+ :old_sha => "sha1",
29
+ :new_sha => "sha2",
30
+ :ref_name => "refree",
31
+ :repo => @repo
32
+ })
33
+
34
+ @hook.handle_post_receive
35
+ end
36
+ end
37
+
38
+
39
+ end
40
+
41
+ end
@@ -0,0 +1,31 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe CaptainHook::DSL::HandlePostReceive do
4
+ before(:each) do
5
+ @event = mock('post_receive_event',
6
+ :message => "hey hey, mama",
7
+ :author => "zeppelin",
8
+ :ref_name => "misty mountain hop"
9
+ )
10
+ end
11
+
12
+ subject{ CaptainHook::DSL::HandlePostReceive.new(@event)}
13
+
14
+ it "should return the event's commit message for #message" do
15
+ subject.message.should == "hey hey, mama"
16
+ end
17
+
18
+ it "should return the event's author for #author" do
19
+ subject.author.should == "zeppelin"
20
+ end
21
+
22
+ it "should return the event's ref_name for #ref_name" do
23
+ subject.ref_name.should == "misty mountain hop"
24
+ end
25
+
26
+ it "should let a block run against itself like a good DSL" do
27
+ subject.run do
28
+ "#{author} - #{ref_name}"
29
+ end.should == "zeppelin - misty mountain hop"
30
+ end
31
+ end
@@ -0,0 +1,91 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe CaptainHook::PostReceiveEvent do
4
+ before(:each) do
5
+ @repo = mock('repo')
6
+ @ref_name = 'refs/heads/master'
7
+ @old_commit = mock('commit').as_null_object
8
+ @new_commit = mock('commit').as_null_object
9
+ @repo.stub!(:commit).with('oldsha').and_return(@old_commit)
10
+ @repo.stub!(:commit).with('newsha').and_return(@new_commit)
11
+ end
12
+
13
+ subject {
14
+ CaptainHook::PostReceiveEvent.new(
15
+ :old_sha => "oldsha",
16
+ :new_sha => "newsha",
17
+ :ref_name => @ref_name,
18
+ :repo => @repo
19
+ )
20
+ }
21
+
22
+ it "should expose new_commit" do
23
+ subject.new_commit.should == @new_commit
24
+ end
25
+
26
+ it "should expose old_commit" do
27
+ subject.old_commit.should == @old_commit
28
+ end
29
+
30
+ describe "#ref_name" do
31
+ it "should parse from refs/heads/etc" do
32
+ subject.ref_name.should == "master"
33
+ end
34
+
35
+ it "should return 'unknown' if it can't be parsed" do
36
+ @ref_name = "bogus"
37
+ subject.ref_name.should == "unknown"
38
+ end
39
+
40
+ end
41
+
42
+ describe "#commits" do
43
+ it "should expose commits between old and new" do
44
+ @repo.stub!(:commits_between).and_return(%w{id ego})
45
+ subject.commits.should == %w{id ego}
46
+ end
47
+
48
+ it "should cache the list of commits" do
49
+ @repo.should_receive(:commits_between).
50
+ once().
51
+ and_return(true)
52
+ 2.times { subject.commits }
53
+ end
54
+
55
+ it "should get the commits from old -> new" do
56
+ @repo.should_receive(:commits_between).with(@new_commit, @old_commit)
57
+ subject.commits
58
+ end
59
+ end
60
+
61
+ context "convenience api" do
62
+ before(:each) do
63
+ message = <<EOM
64
+ this message is going to be long
65
+
66
+ i mean really long
67
+ and longer
68
+ EOM
69
+ subject.stub!(:new_commit).and_return(mock(:message => message))
70
+ end
71
+
72
+ describe "#author" do
73
+ it "should return new_commit.author" do
74
+ subject.stub!(:new_commit).and_return(mock(:author => "Kurt Vonnegut"))
75
+ subject.author.should == "Kurt Vonnegut"
76
+ end
77
+ end
78
+
79
+ describe "#message" do
80
+ it "should return the first line of new_commit#message" do
81
+ subject.message.should == "this message is going to be long"
82
+ end
83
+ end
84
+
85
+ describe "#message_detail" do
86
+ it "should return lines 1.. of the new_commit#message" do
87
+ subject.message_detail.should == "i mean really long\nand longer"
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,18 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "CaptainHook" do
4
+ describe "CaptainHook.from_path" do
5
+ before(:each) do
6
+ Grit::Repo.stub!(:new)
7
+ end
8
+ it "should create a grit repo based on the path" do
9
+ Grit::Repo.should_receive(:new).with('foo')
10
+ c = CaptainHook.from_path('foo')
11
+ end
12
+
13
+ it "should return an instance of CaptainHook" do
14
+ CaptainHook.from_path('foo').should be_an_instance_of(CaptainHook::Base)
15
+ end
16
+ end
17
+
18
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,11 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'captain_hook'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ require 'rubygems'
8
+ require 'grit'
9
+ Spec::Runner.configure do |config|
10
+
11
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: captain_hook
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Pearson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-25 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.9
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: grit
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.0.0
34
+ version:
35
+ description: Makes handling git hooks easy, with built-in notification to campfire
36
+ email: ampearson@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.rdoc
44
+ files:
45
+ - .document
46
+ - .gitignore
47
+ - LICENSE
48
+ - README.rdoc
49
+ - Rakefile
50
+ - VERSION
51
+ - captain_hook.gemspec
52
+ - lib/captain_hook.rb
53
+ - lib/captain_hook/base.rb
54
+ - lib/captain_hook/dsl.rb
55
+ - lib/captain_hook/dsl/handle_post_receive.rb
56
+ - lib/captain_hook/post_receive_event.rb
57
+ - spec/captain_hook/base_spec.rb
58
+ - spec/captain_hook/dsl/handle_post_receive_spec.rb
59
+ - spec/captain_hook/post_receive_event_spec.rb
60
+ - spec/captain_hook_spec.rb
61
+ - spec/spec.opts
62
+ - spec/spec_helper.rb
63
+ has_rdoc: true
64
+ homepage: http://github.com/radamant/captain_hook
65
+ licenses: []
66
+
67
+ post_install_message:
68
+ rdoc_options:
69
+ - --charset=UTF-8
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: "0"
77
+ version:
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: "0"
83
+ version:
84
+ requirements: []
85
+
86
+ rubyforge_project:
87
+ rubygems_version: 1.3.5
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: A ruby manager for git hooks
91
+ test_files:
92
+ - spec/captain_hook/base_spec.rb
93
+ - spec/captain_hook/dsl/handle_post_receive_spec.rb
94
+ - spec/captain_hook/post_receive_event_spec.rb
95
+ - spec/captain_hook_spec.rb
96
+ - spec/spec_helper.rb