@@ -31,15 +31,27 @@ def system(command)
3131 Kernel . system ( command )
3232 end
3333
34- # Executes shell command. Returns true if the shell command exits with a success status code
35- def exec ( command )
34+ # Executes shell command. Returns true if the shell command exits with a
35+ # success status code.
36+ #
37+ # If directory is given the current directory will be changed to that
38+ # directory before executing command.
39+ def exec ( command , directory )
3640 logger . debug { "GithubHook: Executing command: '#{ command } '" }
3741
3842 # Get a path to a temp file
3943 logfile = Tempfile . new ( 'github_hook_exec' )
4044 logfile . close
4145
42- success = system ( "#{ command } > #{ logfile . path } 2>&1" )
46+ full_command = "#{ command } > #{ logfile . path } 2>&1"
47+ success = if directory . present?
48+ Dir . chdir ( directory ) do
49+ system ( full_command )
50+ end
51+ else
52+ system ( full_command )
53+ end
54+
4355 output_from_command = File . readlines ( logfile . path )
4456 if success
4557 logger . debug { "GithubHook: Command output: #{ output_from_command . inspect } " }
@@ -58,12 +70,10 @@ def git_command(command)
5870
5971 # Fetches updates from the remote repository
6072 def update_repository ( repository )
61- Dir . chdir ( repository . url ) do
62- command = git_command ( 'fetch origin' )
63- if exec ( command )
64- command = git_command ( "fetch origin \" +refs/heads/*:refs/heads/*\" " )
65- exec ( command )
66- end
73+ command = git_command ( 'fetch origin' )
74+ if exec ( command , repository . url )
75+ command = git_command ( "fetch origin \" +refs/heads/*:refs/heads/*\" " )
76+ exec ( command , repository . url )
6777 end
6878 end
6979
0 commit comments