public
Description: Don't mind this, go to http://github.com/integrity/integrity
Homepage: http://integrityapp.com
Clone URL: git://github.com/foca/integrity.git
integrity / lib / integrity / scm / git.rb
100644 84 lines (66 sloc) 2.186 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
module Integrity
  module SCM
    class Git
      require File.dirname(__FILE__) / "git/uri"
 
      attr_reader :uri, :branch, :working_directory
 
      def self.working_tree_path(uri)
        Git::URI.new(uri).working_tree_path
      end
 
      def initialize(uri, branch, working_directory)
        @uri = uri.to_s
        @branch = branch.to_s
        @working_directory = working_directory
      end
 
      def with_revision(revision)
        fetch_code
        checkout(revision)
        yield
      end
 
      def commit_identifier(sha1)
        `cd #{working_directory} && git show -s --pretty=format:%H #{sha1}`.chomp
      end
 
      def commit_metadata(sha1)
        format = %Q(---%n:author: %an <%ae>%n:message: >-%n %s%n:date: %ci%n)
        YAML.load(`cd #{working_directory} && git show -s --pretty=format:"#{format}" #{sha1}`)
      end
      
      def name
        self.class.name.split("::").last
      end
 
      private
 
        def fetch_code
          clone unless cloned?
          checkout unless on_branch?
          pull
        end
    
        def clone
          log "Cloning #{uri} to #{working_directory}"
          `git clone #{uri} #{working_directory}`
        end
 
        def checkout(treeish=nil)
          strategy = case
            when treeish then treeish
            when local_branches.include?(branch) then branch
            else "-b #{branch} origin/#{branch}"
          end
 
          log "Checking-out #{strategy}"
          `cd #{working_directory} && git checkout #{strategy}`
        end
 
        def pull
          log "Pull-ing in #{working_directory}"
          `cd #{working_directory} && git pull`
        end
 
        def local_branches
          `cd #{working_directory} && git branch`.split("\n").map {|b| b.delete("*").strip }
        end
 
        def cloned?
          File.directory?(working_directory / ".git")
        end
 
        def on_branch?
          File.basename(`cd #{working_directory} && git symbolic-ref HEAD`).chomp == branch
        end
 
        def log(message)
          Integrity.logger.info("Git") { message }
        end
    end
  end
end