Commits

Alessio Caiazza committed e79d9f9

git class and dsl

Comments (0)

Files changed (2)

lib/dependency_wiring/dsl.rb

 require 'dependency_wiring/mount_point'
 require 'dependency_wiring/mercurial'
+require 'dependency_wiring/git'
 
 
 module DependencyWiring
     def hg(src,dest, opts={})
       @deps << Mercurial.new(src, dest, opts)
     end
+
+    def git(src,dest, opts={})
+      @deps << Git.new(src, dest, opts)
+    end
   end
 end

lib/dependency_wiring/git.rb

+require 'dependency_wiring/scm'
+
+module DependencyWiring
+  class Git < SCM
+    
+    def clone
+      ret = sh_with_code("git clone #{git_opts} #{@src} .")
+      raise("Cannot clone #{@src} into #{@dest}! Output: #{ret[0]}") if ret[1] != 0
+    end
+
+    def pull
+      ret = sh_with_code("git fetch --all #{@src}")
+      raise("Cannot fetch #{@dest}! Output: #{ret[0]}") if ret[1] != 0
+    end
+
+    def update
+      ret = sh_with_code("git checkout #{git_revision}")
+      if ret[1] != 0
+        raise("Cannot update #{@dest} to #{git_revision}! Output: #{ret[0]}") 
+      end
+    end    
+
+    def wire
+      pull
+      update
+    end
+
+    def to_s
+      "git(#{@src}, #{@opts}) -> #{@dest}"
+    end
+
+    def default_opts
+      { rev: 'HEAD' }
+    end
+
+    def valid_destination?
+      ret = sh_with_code('git su')
+      ret[1] == 0
+    end  
+
+    protected
+    def git_opts
+      branch = @opts[:branch]
+      return "-b #{branch} " unless branch.nil?
+      ""
+    end
+
+    def git_revision
+      branch = @opts[:branch]
+      rev = @opts[:rev]
+      tag = @opts[:tag]
+      if rev == default_opts[:rev]
+        if(!branch.nil? && !tag.nil?)
+          raise("Cannot specify tag and branch")
+        elsif branch.nil?
+          tag
+        elsif tag.nil?
+          branch
+        else
+          rev
+        end
+      elsif(branch.nil? && tag.nil?)
+        rev
+      else
+        raise("Cannot specify tag, rev and branch")  
+      end
+    end
+  end
+end