summaryrefslogtreecommitdiff
path: root/lib/gitlab/git
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2015-08-14 16:00:36 +0200
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2015-08-14 16:04:17 +0200
commit4e4866f2559262b3c858de15890eb864f18eeca8 (patch)
tree6a55206eeb83fcd815ffe995f5646f260c2c01fd /lib/gitlab/git
parentbacad39ef984b99520f8d2a7921acd5c8bdab1ef (diff)
downloadgitlab-ce-4e4866f2559262b3c858de15890eb864f18eeca8.tar.gz
Refactor pre/post receive commit services into one class
Diffstat (limited to 'lib/gitlab/git')
-rw-r--r--lib/gitlab/git/hook.rb59
1 files changed, 59 insertions, 0 deletions
diff --git a/lib/gitlab/git/hook.rb b/lib/gitlab/git/hook.rb
new file mode 100644
index 00000000000..dd393fe09d2
--- /dev/null
+++ b/lib/gitlab/git/hook.rb
@@ -0,0 +1,59 @@
+module Gitlab
+ module Git
+ class Hook
+ attr_reader :name, :repo_path, :path
+
+ def initialize(name, repo_path)
+ @name = name
+ @repo_path = repo_path
+ @path = File.join(repo_path.strip, 'hooks', name)
+ end
+
+ def exists?
+ File.exist?(path)
+ end
+
+ def trigger(gl_id, oldrev, newrev, ref)
+ return true unless exists?
+
+ changes = [oldrev, newrev, ref].join(" ")
+
+ # function will return true if succesful
+ exit_status = false
+
+ vars = {
+ 'GL_ID' => gl_id,
+ 'PWD' => repo_path
+ }
+
+ options = {
+ chdir: repo_path
+ }
+
+ Open3.popen2(vars, path, options) do |stdin, _, wait_thr|
+ exit_status = true
+ stdin.sync = true
+
+ # in git, pre- and post- receive hooks may just exit without
+ # reading stdin. We catch the exception to avoid a broken pipe
+ # warning
+ begin
+ # inject all the changes as stdin to the hook
+ changes.lines do |line|
+ stdin.puts line
+ end
+ rescue Errno::EPIPE
+ end
+
+ stdin.close
+
+ unless wait_thr.value == 0
+ exit_status = false
+ end
+ end
+
+ exit_status
+ end
+ end
+ end
+end