cf0d80caf293 — Peter Sanchez 11 years ago
Added support for git repos.
1 files changed, 20 insertions(+), 3 deletions(-)

M app/controllers/bitbucket_hook_controller.rb
M app/controllers/bitbucket_hook_controller.rb +20 -3
@@ 16,11 16,15 @@ class BitbucketHookController < Applicat
     
     repository = project.repository
     raise TypeError, "Project '#{identifier}' has no repository" if repository.nil?
-    raise TypeError, "Repository for project '#{identifier}' is not a Mercurial repository" unless repository.is_a?(Repository::Mercurial)
+    raise TypeError, "Repository for project '#{identifier}' is not a BitBucket repository" unless repository.is_a?(Repository::Mercurial) || repository.is_a?(Repository::Git)
 
     # Get updates from the bitbucket repository
-    command = "cd \"#{repository.url}\" && hg pull"
-    exec(command)
+    if repository.is_a?(Repository::Git)
+      update_git_repository(repository)
+    else
+      command = "cd \"#{repository.url}\" && hg pull"
+      exec(command)
+    end
 
     # Fetch the new changesets into Redmine
     repository.fetch_changesets

          
@@ 36,4 40,17 @@ class BitbucketHookController < Applicat
     logger.info { "BitbucketHook: Shell returned '#{output}'" }
   end
 
+  # Taken from: https://github.com/koppen/redmine_github_hook
+  def git_command(command, repository)
+    "git --git-dir='#{repository.url}' #{command}"
+  end
+
+  def update_git_repository(repository)
+    command = git_command('fetch origin', repository)
+    if exec(command)
+      command = git_command("fetch origin '+refs/heads/*:refs/heads/*'", repository)
+      exec(command)
+    end
+  end
+
 end