captain_hook 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,8 @@
1
+ # captain_hook 0.1.1
2
+
3
+ * Added `post-commit` hook to DSL
4
+
5
+ # captain_hook 0.1.0
6
+
7
+ * Initial release
8
+ * `post-receive` hook added to DSL
@@ -4,15 +4,33 @@ A ruby library for managing git hooks.
4
4
 
5
5
  == Usage
6
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
7
+ === An example post-receive hook sending to campfire (tinder)
8
+ your_repo/.git/hooks/post-receive
9
+ require 'captain_hook'
10
+ require 'tinder'
10
11
 
11
- @hook = CaptainHook.from_path(File.join(File.dirname(__FILE__), "..",".."))
12
+ hook = CaptainHook.from_path("../../")
12
13
 
13
- @hook.handle_post_receive do
14
- @room = Campfire.room(1)
15
- @room.message "New commit: '#{message}' by #{author} to #{ref_name}"
14
+ hook.handle_post_receive do
15
+ room = MyCampfire.my_room
16
+ room.speak "New commit: '#{message}' by #{author} to #{ref_name}"
17
+ end
18
+
19
+ === An example post-commit hook sending an email detailing the commit
20
+ your_repo/.git/hooks/post-commit
21
+ require 'captain_hook'
22
+ require 'mail'
23
+
24
+ hook = CaptainHook.from_path(File.join(File.dirname(__FILE__), '..','..'))
25
+ hook.handle_post_commit do
26
+ mail = Mail.new
27
+ mail.from 'from@example.com'
28
+ mail.to 'to@example.com'
29
+ mail.subject "#{message} committed by #{author} to #{ref_name}"
30
+ mail.body diff
31
+
32
+ mail.delivery_method(:sendmail)
33
+ mail.deliver
16
34
  end
17
35
 
18
36
  == Note on Patches/Pull Requests
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -1,15 +1,15 @@
1
1
  # Generated by jeweler
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in rakefile, and run the gemspec command
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{captain_hook}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Adam Pearson"]
12
- s.date = %q{2010-02-25}
12
+ s.date = %q{2010-03-02}
13
13
  s.description = %q{Makes handling git hooks easy, with built-in notification to campfire}
14
14
  s.email = %q{ampearson@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
19
19
  s.files = [
20
20
  ".document",
21
21
  ".gitignore",
22
+ "CHANGELOG.markdown",
22
23
  "LICENSE",
23
24
  "README.rdoc",
24
25
  "Rakefile",
@@ -27,11 +28,17 @@ Gem::Specification.new do |s|
27
28
  "lib/captain_hook.rb",
28
29
  "lib/captain_hook/base.rb",
29
30
  "lib/captain_hook/dsl.rb",
31
+ "lib/captain_hook/dsl/base.rb",
32
+ "lib/captain_hook/dsl/handle_post_commit.rb",
30
33
  "lib/captain_hook/dsl/handle_post_receive.rb",
31
- "lib/captain_hook/post_receive_event.rb",
34
+ "lib/captain_hook/events.rb",
35
+ "lib/captain_hook/events/post_commit.rb",
36
+ "lib/captain_hook/events/post_receive.rb",
32
37
  "spec/captain_hook/base_spec.rb",
38
+ "spec/captain_hook/dsl/handle_post_commit_spec.rb",
33
39
  "spec/captain_hook/dsl/handle_post_receive_spec.rb",
34
- "spec/captain_hook/post_receive_event_spec.rb",
40
+ "spec/captain_hook/events/post_commit_spec.rb",
41
+ "spec/captain_hook/events/post_receive_spec.rb",
35
42
  "spec/captain_hook_spec.rb",
36
43
  "spec/spec.opts",
37
44
  "spec/spec_helper.rb"
@@ -39,12 +46,14 @@ Gem::Specification.new do |s|
39
46
  s.homepage = %q{http://github.com/radamant/captain_hook}
40
47
  s.rdoc_options = ["--charset=UTF-8"]
41
48
  s.require_paths = ["lib"]
42
- s.rubygems_version = %q{1.3.5}
49
+ s.rubygems_version = %q{1.3.6}
43
50
  s.summary = %q{A ruby manager for git hooks}
44
51
  s.test_files = [
45
52
  "spec/captain_hook/base_spec.rb",
53
+ "spec/captain_hook/dsl/handle_post_commit_spec.rb",
46
54
  "spec/captain_hook/dsl/handle_post_receive_spec.rb",
47
- "spec/captain_hook/post_receive_event_spec.rb",
55
+ "spec/captain_hook/events/post_commit_spec.rb",
56
+ "spec/captain_hook/events/post_receive_spec.rb",
48
57
  "spec/captain_hook_spec.rb",
49
58
  "spec/spec_helper.rb"
50
59
  ]
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'grit'
3
3
  module CaptainHook
4
4
  require 'captain_hook/base'
5
- require 'captain_hook/post_receive_event'
5
+ require 'captain_hook/events'
6
6
  require 'captain_hook/dsl'
7
7
 
8
8
  def self.from_path(path)
@@ -14,11 +14,18 @@ module CaptainHook
14
14
  dsl.run(&block) if block_given?
15
15
  end
16
16
 
17
+ def handle_post_commit(&block)
18
+ post_commit = CaptainHook::Events::PostCommit.new(:repo => self.repo)
19
+ if block_given?
20
+ CaptainHook::DSL::HandlePostCommit.new(post_commit).run(&block)
21
+ end
22
+ end
23
+
17
24
  private
18
25
 
19
26
  def post_receive_from_data(data)
20
27
  args = data.read.split(" ")
21
- post_receive = CaptainHook::PostReceiveEvent.new(
28
+ post_receive = CaptainHook::Events::PostReceive.new(
22
29
  :old_sha => args[0],
23
30
  :new_sha => args[1],
24
31
  :ref_name => args[2],
@@ -1,5 +1,7 @@
1
1
  module CaptainHook
2
2
  module DSL
3
+ require 'captain_hook/dsl/base'
3
4
  require 'captain_hook/dsl/handle_post_receive'
5
+ require 'captain_hook/dsl/handle_post_commit'
4
6
  end
5
7
  end
@@ -0,0 +1,9 @@
1
+ module CaptainHook
2
+ module DSL
3
+ module Base
4
+ def run &block
5
+ instance_eval &block
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,29 @@
1
+ module CaptainHook
2
+ module DSL
3
+ class HandlePostCommit
4
+ include Base
5
+
6
+ def initialize(event)
7
+ @event = event
8
+ end
9
+
10
+ def message
11
+ @event.message
12
+ end
13
+
14
+ def author
15
+ @event.author
16
+ end
17
+
18
+ def diff
19
+ @event.diff
20
+ end
21
+
22
+ def ref_name
23
+ @event.ref_name
24
+ end
25
+
26
+
27
+ end
28
+ end
29
+ end
@@ -1,12 +1,13 @@
1
1
  module CaptainHook
2
2
  module DSL
3
3
  class HandlePostReceive
4
+ include Base
5
+
4
6
  def initialize(post_receive_event)
5
7
  @post_receive_event = post_receive_event
6
8
  end
7
9
 
8
10
  def message
9
- puts "post-receive: #{@post_receive_event.message}"
10
11
  @post_receive_event.message
11
12
  end
12
13
 
@@ -16,11 +17,7 @@ module CaptainHook
16
17
 
17
18
  def author
18
19
  @post_receive_event.author
19
- end
20
-
21
- def run &block
22
- instance_eval &block
23
- end
20
+ end
24
21
  end
25
22
  end
26
23
  end
@@ -0,0 +1,6 @@
1
+ module CaptainHook
2
+ module Events
3
+ require 'captain_hook/events/post_receive'
4
+ require 'captain_hook/events/post_commit'
5
+ end
6
+ end
@@ -0,0 +1,36 @@
1
+ module CaptainHook
2
+ module Events
3
+ class PostCommit
4
+ def initialize(options={})
5
+ @repo = options[:repo]
6
+ end
7
+
8
+ # The represents the git commit object in question
9
+ def commit
10
+ @commit ||= @repo.commit(@repo.head.commit)
11
+ end
12
+
13
+ # The commit message
14
+ def message
15
+ commit.message
16
+ end
17
+
18
+ def author
19
+ commit.author
20
+ end
21
+
22
+ # The diff string from the commit
23
+ # Concatenates all of the sub-diffs into one string
24
+ def diff
25
+ @diff ||= begin
26
+ commit.diffs.collect{|diff| diff.diff}.join("\n")
27
+ end
28
+ end
29
+
30
+ def ref_name
31
+ @ref_name ||= @repo.head.name
32
+ end
33
+
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,45 @@
1
+ module CaptainHook
2
+ module Events
3
+ class PostReceive
4
+ attr_accessor :repo, :old_commit, :new_commit
5
+
6
+ def initialize(options = {})
7
+ @repo = options[:repo]
8
+ @old_commit = @repo.commit(options[:old_sha])
9
+ @new_commit = @repo.commit(options[:new_sha])
10
+ @full_ref_name = options[:ref_name]
11
+ end
12
+
13
+ def commits
14
+ @commits ||= @repo.commits_between(new_commit, old_commit)
15
+ end
16
+
17
+ def ref_name
18
+ @ref_name ||= begin
19
+ @full_ref_name =~ %r{^refs/(?:tags|heads|remotes)/(.+)} ?
20
+ $1 : "unknown"
21
+ end
22
+ end
23
+
24
+ # Returns the author of the most-recent revision
25
+ def author
26
+ new_commit.author
27
+ end
28
+
29
+ # Returns the first line of the most recent revision's commit message
30
+ def message
31
+ @message ||= new_commit.message.split("\n")[0]
32
+ end
33
+
34
+ # Returns lines 1..n of the most recent revision's commit message
35
+ def message_detail
36
+ @message_detail ||= begin
37
+ new_commit.message.
38
+ split("\n")[1..-1].
39
+ reject{|s| s == ""}.
40
+ join("\n")
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -9,7 +9,6 @@ describe CaptainHook::Base do
9
9
 
10
10
  describe :handle_post_receive do
11
11
  before(:each) do
12
- @yielded = nil
13
12
  @args = "sha1 sha2 refree"
14
13
  $stdin.stub!(:read).and_return(@args)
15
14
  end
@@ -22,7 +21,7 @@ describe CaptainHook::Base do
22
21
  end
23
22
 
24
23
  it "should use $stdin to parse args" do
25
- CaptainHook::PostReceiveEvent.
24
+ CaptainHook::Events::PostReceive.
26
25
  should_receive(:new).
27
26
  with({
28
27
  :old_sha => "sha1",
@@ -35,6 +34,22 @@ describe CaptainHook::Base do
35
34
  end
36
35
  end
37
36
 
37
+ describe :handle_post_commit do
38
+ it "should call run on the right DSL handler" do
39
+ dsl = mock()
40
+ CaptainHook::DSL::HandlePostCommit.stub!(:new).and_return(dsl)
41
+ dsl.should_receive(:run)
42
+ @hook.handle_post_commit {}
43
+ end
44
+
45
+ it "should create a post-commit event with the repo" do
46
+ CaptainHook::Events::PostCommit.should_receive(:new).with({:repo => @repo})
47
+ @hook.handle_post_commit
48
+ end
49
+
50
+ end
51
+
52
+
38
53
 
39
54
  end
40
55
 
@@ -0,0 +1,37 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe CaptainHook::DSL::HandlePostCommit do
4
+ before(:each) do
5
+ @event = mock('post_commit_event',
6
+ :message => "wave a white flag",
7
+ :author => "costello",
8
+ :ref_name => "put away the pistol",
9
+ :diff => "none"
10
+ )
11
+ end
12
+
13
+ subject{ CaptainHook::DSL::HandlePostCommit.new(@event)}
14
+
15
+ it "should return the event's commit message for #message" do
16
+ subject.message.should == "wave a white flag"
17
+ end
18
+
19
+ it "should return the event's author for #author" do
20
+ subject.author.should == "costello"
21
+ end
22
+
23
+ it "should return the event's ref_name for #ref_name" do
24
+ subject.ref_name.should == "put away the pistol"
25
+ end
26
+
27
+ it "should return the event's diff for #diff" do
28
+ subject.diff.should == "none"
29
+ end
30
+
31
+ it "should let a block run against itself like a good DSL" do
32
+ subject.run do
33
+ "#{author} - #{ref_name}"
34
+ end.should == "costello - put away the pistol"
35
+
36
+ end
37
+ end
@@ -3,8 +3,8 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
3
3
  describe CaptainHook::DSL::HandlePostReceive do
4
4
  before(:each) do
5
5
  @event = mock('post_receive_event',
6
- :message => "hey hey, mama",
7
- :author => "zeppelin",
6
+ :message => "hey hey, mama",
7
+ :author => "zeppelin",
8
8
  :ref_name => "misty mountain hop"
9
9
  )
10
10
  end
@@ -0,0 +1,39 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe CaptainHook::Events::PostCommit do
4
+ before(:each) do
5
+ @diff = mock('diff', :diff => "foo")
6
+ @repo = mock('repo',
7
+ :head => mock('head', :commit => 'sha1', :name => "talking"),
8
+ :commit => mock('commit',
9
+ :message => 'been a long time',
10
+ :author => 'a visionary',
11
+ :diffs => [@diff, @diff, @diff])
12
+ )
13
+ end
14
+
15
+ subject { CaptainHook::Events::PostCommit.new(:repo => @repo) }
16
+
17
+ it "should expose the latest commit" do
18
+ subject.commit.message.should be
19
+ end
20
+
21
+ it "should expose the message of the latest commit" do
22
+ subject.message.should == "been a long time"
23
+ end
24
+
25
+ it "should expose the author of the latest commit" do
26
+ subject.author.should == 'a visionary'
27
+ end
28
+
29
+ it "should expose the ref-name of the latest commit" do
30
+ subject.ref_name.should == "talking"
31
+ end
32
+
33
+ describe "#diff" do
34
+ it "should concatenate the diffs from the latest commit" do
35
+ expected_diff = "#{@diff.diff}\n#{@diff.diff}\n#{@diff.diff}"
36
+ subject.diff.should == expected_diff
37
+ end
38
+ end
39
+ end
@@ -1,6 +1,6 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
 
3
- describe CaptainHook::PostReceiveEvent do
3
+ describe CaptainHook::Events::PostReceive do
4
4
  before(:each) do
5
5
  @repo = mock('repo')
6
6
  @ref_name = 'refs/heads/master'
@@ -11,7 +11,7 @@ describe CaptainHook::PostReceiveEvent do
11
11
  end
12
12
 
13
13
  subject {
14
- CaptainHook::PostReceiveEvent.new(
14
+ CaptainHook::Events::PostReceive.new(
15
15
  :old_sha => "oldsha",
16
16
  :new_sha => "newsha",
17
17
  :ref_name => @ref_name,
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: captain_hook
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 1
9
+ version: 0.1.1
5
10
  platform: ruby
6
11
  authors:
7
12
  - Adam Pearson
@@ -9,29 +14,37 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-02-25 00:00:00 -08:00
17
+ date: 2010-03-02 00:00:00 -08:00
13
18
  default_executable:
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: rspec
17
- type: :development
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
20
24
  requirements:
21
25
  - - ">="
22
26
  - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 2
30
+ - 9
23
31
  version: 1.2.9
24
- version:
32
+ type: :development
33
+ version_requirements: *id001
25
34
  - !ruby/object:Gem::Dependency
26
35
  name: grit
27
- type: :runtime
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
30
38
  requirements:
31
39
  - - "="
32
40
  - !ruby/object:Gem::Version
41
+ segments:
42
+ - 2
43
+ - 0
44
+ - 0
33
45
  version: 2.0.0
34
- version:
46
+ type: :runtime
47
+ version_requirements: *id002
35
48
  description: Makes handling git hooks easy, with built-in notification to campfire
36
49
  email: ampearson@gmail.com
37
50
  executables: []
@@ -44,6 +57,7 @@ extra_rdoc_files:
44
57
  files:
45
58
  - .document
46
59
  - .gitignore
60
+ - CHANGELOG.markdown
47
61
  - LICENSE
48
62
  - README.rdoc
49
63
  - Rakefile
@@ -52,11 +66,17 @@ files:
52
66
  - lib/captain_hook.rb
53
67
  - lib/captain_hook/base.rb
54
68
  - lib/captain_hook/dsl.rb
69
+ - lib/captain_hook/dsl/base.rb
70
+ - lib/captain_hook/dsl/handle_post_commit.rb
55
71
  - lib/captain_hook/dsl/handle_post_receive.rb
56
- - lib/captain_hook/post_receive_event.rb
72
+ - lib/captain_hook/events.rb
73
+ - lib/captain_hook/events/post_commit.rb
74
+ - lib/captain_hook/events/post_receive.rb
57
75
  - spec/captain_hook/base_spec.rb
76
+ - spec/captain_hook/dsl/handle_post_commit_spec.rb
58
77
  - spec/captain_hook/dsl/handle_post_receive_spec.rb
59
- - spec/captain_hook/post_receive_event_spec.rb
78
+ - spec/captain_hook/events/post_commit_spec.rb
79
+ - spec/captain_hook/events/post_receive_spec.rb
60
80
  - spec/captain_hook_spec.rb
61
81
  - spec/spec.opts
62
82
  - spec/spec_helper.rb
@@ -73,24 +93,28 @@ required_ruby_version: !ruby/object:Gem::Requirement
73
93
  requirements:
74
94
  - - ">="
75
95
  - !ruby/object:Gem::Version
96
+ segments:
97
+ - 0
76
98
  version: "0"
77
- version:
78
99
  required_rubygems_version: !ruby/object:Gem::Requirement
79
100
  requirements:
80
101
  - - ">="
81
102
  - !ruby/object:Gem::Version
103
+ segments:
104
+ - 0
82
105
  version: "0"
83
- version:
84
106
  requirements: []
85
107
 
86
108
  rubyforge_project:
87
- rubygems_version: 1.3.5
109
+ rubygems_version: 1.3.6
88
110
  signing_key:
89
111
  specification_version: 3
90
112
  summary: A ruby manager for git hooks
91
113
  test_files:
92
114
  - spec/captain_hook/base_spec.rb
115
+ - spec/captain_hook/dsl/handle_post_commit_spec.rb
93
116
  - spec/captain_hook/dsl/handle_post_receive_spec.rb
94
- - spec/captain_hook/post_receive_event_spec.rb
117
+ - spec/captain_hook/events/post_commit_spec.rb
118
+ - spec/captain_hook/events/post_receive_spec.rb
95
119
  - spec/captain_hook_spec.rb
96
120
  - spec/spec_helper.rb
@@ -1,44 +0,0 @@
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