[NEW] Initial Revision
[obnox/ohloh/ohloh_scm.git] / lib / scm / adapters / git / push.rb
1 require 'socket'
2
3 module Scm::Adapters
4         class GitAdapter < AbstractAdapter
5
6                 COMMITTER_NAME = 'ohloh_slave' unless defined?(COMMITTER_NAME)
7
8                 def push(to)
9                         logger.info { "Pushing to #{to.url}" }
10
11                         if to.exist?
12                                 ENV['GIT_COMMITTER_NAME'] = COMMITTER_NAME
13                                 run "cd '#{self.url}' && git push '#{to.url}' #{self.branch_name}:#{to.branch_name}"
14                         else
15                                 if to.local?
16                                         # Create a new repo on the same local machine. Just use existing pull code in reverse.
17                                         to.pull(self)
18                                 else
19                                         run "ssh #{to.hostname} 'mkdir -p #{to.path}'"
20                                         run "scp -rpqB #{git_path} #{to.hostname}:#{to.path}"
21                                 end
22                         end
23                 end
24
25                 def local?
26                         return true if hostname == Socket.gethostname
27                         return false if url =~ /:/
28                         true
29                 end
30
31                 def hostname
32                         url =~ /^([^:^\/]+):(.+)/ ? $1 : nil
33                 end
34
35                 def path
36                         url =~ /^([^:^\/]+):(.+)/ ? $2 : nil
37                 end
38         end
39 end