Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 87 lines (71 sloc) 2.117 kB
9268881 @Tarrasch Began on an git post-receive hook [#143]
Tarrasch authored
1 # A bit of ruby comments
2
3 oldrev, newrev, refname = gets.split
4
d2dfd16 @Tarrasch Clean up in post-receive.rb
Tarrasch authored
5 # I made this banner using the unix `banner` command
7d72296 @Tarrasch Print nice ascii "water" art on git push
Tarrasch authored
6 puts %Q{
7
8
9 # # ## ##### ###### #####
10 # # # # # # # #
11 # # # # # ##### # #
12 # ## # ###### # # #####
13 ## ## # # # # # #
14 # # # # # ###### # #
15
16
17 }
18
2518d70 @Tarrasch Print water before first in git CLI
Tarrasch authored
19 if refname.split("/").last != "master"
20 puts "You did not push to master, so water doesn't process your commits"
21 exit 0
22 end
23
24 commits = `git rev-list #{oldrev}..#{newrev}`.split("\n")
6d5a51a @Tarrasch Eventmachine in procfile + hooks + processor
Tarrasch authored
25 $command = nil
26 $submit_hash = nil
2518d70 @Tarrasch Print water before first in git CLI
Tarrasch authored
27 commits.each { |commit_hash|
4e26a61 @Tarrasch Also parse commit body for #submit tags in hooks
Tarrasch authored
28 msg = `git show -s --format=format:'%s %b' #{commit_hash}`
6d5a51a @Tarrasch Eventmachine in procfile + hooks + processor
Tarrasch authored
29 matches = msg.match(/#(submit|update)/) || []
30 $command = matches[0]
31 $submit_hash = commit_hash if $command
32 break if $command
2518d70 @Tarrasch Print water before first in git CLI
Tarrasch authored
33 }
34
6d5a51a @Tarrasch Eventmachine in procfile + hooks + processor
Tarrasch authored
35 unless $submit_hash
d2dfd16 @Tarrasch Clean up in post-receive.rb
Tarrasch authored
36 puts %Q{
37 We see that you didn't want to submit your code, so we don't. To submit include
6d5a51a @Tarrasch Eventmachine in procfile + hooks + processor
Tarrasch authored
38 the token #submit in your commit message.\n
d2dfd16 @Tarrasch Clean up in post-receive.rb
Tarrasch authored
39 }
40 exit 0
41 end
7d72296 @Tarrasch Print nice ascii "water" art on git push
Tarrasch authored
42
d2dfd16 @Tarrasch Clean up in post-receive.rb
Tarrasch authored
43 # TODO: Implement actual submission request
44 puts %Q{
6d5a51a @Tarrasch Eventmachine in procfile + hooks + processor
Tarrasch authored
45 Ok, water tries to submit #{$submit_hash} for you...\n
d2dfd16 @Tarrasch Clean up in post-receive.rb
Tarrasch authored
46 }
9268881 @Tarrasch Began on an git post-receive hook [#143]
Tarrasch authored
47
30d08d1 @Tarrasch Add explenation of unobvious code
Tarrasch authored
48 # In the code below we extract the repo-hash from $PWD like this:
49 #
50 # "/tmp/git-repos/6cf/4a4/bd6392293577efe9875b6f13842bba2b9d.git\n"[-47..-6] =>
51 # "6cf/4a4/bd6392293577efe9875b6f13842bba2b9d"
e18e750 @Tarrasch First #submit on hooks
Tarrasch authored
52 s = `echo $PWD`
6d5a51a @Tarrasch Eventmachine in procfile + hooks + processor
Tarrasch authored
53 $hashed_path = s[-47..-6]
e18e750 @Tarrasch First #submit on hooks
Tarrasch authored
54
6d5a51a @Tarrasch Eventmachine in procfile + hooks + processor
Tarrasch authored
55 require 'eventmachine'
e18e750 @Tarrasch First #submit on hooks
Tarrasch authored
56
6d5a51a @Tarrasch Eventmachine in procfile + hooks + processor
Tarrasch authored
57 class Submitter < EventMachine::Connection
58 def post_init
59 send_data "#{$command} #{$submit_hash} #{$hashed_path}"
60 end
4203bd3 @Tarrasch Don't hardcode database credentials in hook
Tarrasch authored
61
6d5a51a @Tarrasch Eventmachine in procfile + hooks + processor
Tarrasch authored
62 def receive_data(reply)
63 puts ""
64 puts reply
65 EventMachine.stop
66 end
e18e750 @Tarrasch First #submit on hooks
Tarrasch authored
67 end
68
6d5a51a @Tarrasch Eventmachine in procfile + hooks + processor
Tarrasch authored
69 EventMachine.run {
70 port = ENV['HOOK_PORT']
71 puts "Trying to connect on port #{port}"
72 EventMachine.connect '127.0.0.1', port, Submitter
73 MAX = 3
74 0.upto(MAX - 1) do |i|
75 EventMachine.add_timer(i) do
76 print ("\r"*100 + "#{MAX - i} seconds untill timeout")
77 $stdout.flush
78 end
e18e750 @Tarrasch First #submit on hooks
Tarrasch authored
79 end
6d5a51a @Tarrasch Eventmachine in procfile + hooks + processor
Tarrasch authored
80 EventMachine.add_timer(MAX) do
81 puts "\n\n"
82 puts "FAILED, please #{$command} through the web interface instead. Sorry!"
83 $stdout.flush
84 EventMachine.stop
85 end
86 } if $command
Something went wrong with that request. Please try again.