Blob of data/hooks/post-receive (raw blob data)

1 #!/usr/bin/env ruby
2
3 incpath = File.dirname(__FILE__)
4 $: << incpath
5
6 require 'rails_env'
7 require 'date'
8
9 gitdir = File.expand_path(File.join(incpath, ".."))
10 repository_name = File.basename(gitdir)
11 slug = ""
12
13 if repository_name =~ /\.git$/
14 repository_name.sub!(/\.git$/, "")
15 slug = File.basename(File.expand_path(File.join(gitdir, "..")))
16 end
17
18 git = Grit::Git.new(gitdir)
19
20 while line = gets
21 #puts "@@> #{line.inspect}"
22 # 3b480c5c30962c9f5b82a9c61a75992dc605de21 ee27ee3a022802c0569f9cdef6e9ad29aea096a8 refs/heads/master\n
23 oldrev, newrev, revname = line.split(" ")
24 current_rev = newrev
25 newtype = oldtype = current_rev_type = "commit"
26
27 action = :create
28 if oldrev =~ /^0+$/
29 action = :create
30 else
31 if newrev =~ /^0+$/
32 action = :delete
33 else
34 action = :update
35 end
36 end
37
38 if action != :delete
39 newtype = git.cat_file({:t => true}, newrev)
40 end
41
42 if action == :update
43 oldtype = git.cat_file({:t => true}, oldrev)
44 end
45
46 if action == :delete
47 current_rev = oldrev
48 current_rev_type = oldtype
49 end
50
51 # type => heads, tags, remotes
52 # name => branch name
53 path, type, name = revname.split("/")
54
55 info = git.show({ :pretty => "format:author: %cn%nemail: %ce%ndate: %cd%nmessage: %s", :s => true}, current_rev )
56
57 hash = {}
58 info.each { |line|
59 if line =~ /(\w+):\s(.*)$/
60 key = $1.to_sym
61 value = $2
62
63 value = Date.parse(value) if key == :date
64
65 hash[key] = value
66 end
67 }
68
69 # action => create, delete, update
70 # rev_type => commit, tag
71
72 user = User.find_by_email(hash[:email])
73 if user.nil?
74 # TODO: no user should be ok, no need to skip
75 # $stdout.puts "** The email '#{hash[:email]}' is not registered."
76 next
77 end
78
79 project = Project.find_by_slug(slug)
80 repository = nil
81
82 project.repositories.each { |repo|
83 if repo.name == repository_name
84 repository = repo
85 break
86 end
87 }
88
89 action_id = nil
90 ref = nil
91 if current_rev_type == "commit"
92 if type == "heads"
93 case action
94 when :create
95 action_id = Action::CREATE_BRANCH
96 ref = name
97 when :update
98 action_id = Action::COMMIT
99 ref = current_rev
100 when :delete
101 action_id = Action::DELETE_BRANCH
102 ref = name
103 end
104 elsif type == "tags"
105 if action == :create
106 action_id = Action::CREATE_TAG
107 ref = name
108 elsif action == :delete
109 action_id = Action::DELETE_TAG
110 ref = name
111 end
112 end
113 elsif current_rev_type == "tag"
114 if type == "tags"
115 if action == :create
116 action_id = Action::CREATE_TAG
117 ref = name
118 elsif action == :delete
119 action_id = Action::DELETE_TAG
120 ref = name
121 end
122 end
123 end
124
125 next unless action_id
126
127 # puts "#{hash[:author]}: #{Action.name(action_id)} #{ref} on #{slug} [#{hash[:date]}]"
128 # puts " #{hash[:message]}"
129
130 project.create_event(action_id, repository, user, ref, hash[:message])
131 end
132
133 puts "[OK]"