summaryrefslogtreecommitdiffstats
path: root/lib/git/lib.rb
blob: 2937d52038eb6ce18213d33e3dfb1d5cd443c608 (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
module Git
  
  class GitExecuteError < StandardError 
  end
  
  class GitNoOutput < StandardError 
  end
  
  class Lib
      
    @base = nil
    
    def initialize(base)
      @base = base
    end
    
    def log_commits(opts = {})
      arr_opts = ['--pretty=oneline']
      arr_opts << "-#{opts[:count]}" if opts[:count]
      arr_opts << "--since=\"#{opts[:since]}\"" if opts[:since].is_a? String
      arr_opts << "#{opts[:between][0]}..#{opts[:between][1].to_s}" if (opts[:between] && opts[:between].size == 2)
      arr_opts << opts[:object] if opts[:object].is_a? String
      arr_opts << '-- ' + opts[:path_limiter] if opts[:path_limiter].is_a? String
      
      command_lines('log', arr_opts).map { |l| l.split.first }
    end
    
    def revparse(string)
      command('rev-parse', string)
    end
    
    def object_type(sha)
      command('cat-file', ['-t', sha])
    end
    
    def object_size(sha)
      command('cat-file', ['-s', sha]).to_i
    end
    
    def object_contents(sha)
      command('cat-file', ['-p', sha])
    end

    def branches_all
      command_lines('branch', '-a').map do |b| 
        current = false
        current = true if b[0, 2] == '* '
        Git::Branch.new(@base, b.gsub('* ', '').strip, current)
      end
    end
    
    def config_remote(name)
      hsh = {}
      command_lines('config', ['--get-regexp', "remote.#{name}"]).each do |line|
        (key, value) = line.split
        hsh[key.gsub("remote.#{name}.", '')] = value
      end
      hsh
    end
    
    # returns hash
    # [tree-ish] = [[line_no, match], [line_no, match2]]
    # [tree-ish] = [[line_no, match], [line_no, match2]]
    def grep(string, opts = {})
      opts[:object] = 'HEAD' if !opts[:object]

      grep_opts = ['-n']
      grep_opts << '-i' if opts[:ignore_case]
      grep_opts << '-v' if opts[:invert_match]
      grep_opts << "-e '#{string}'"
      grep_opts << opts[:object] if opts[:object].is_a? String
      grep_opts << ('-- ' + opts[:path_limiter]) if opts[:path_limiter].is_a? String
      hsh = {}
      command_lines('grep', grep_opts).each do |line|
        if m = /(.*)\:(\d+)\:(.*)/.match(line)        
          hsh[m[1]] ||= []
          hsh[m[1]] << [m[2].to_i, m[3]] 
        end
      end
      hsh
    end
    
    private
    
    def command_lines(cmd, opts)
      command(cmd, opts).split("\n")
    end
    
    def command(cmd, opts)
      ENV['GIT_DIR'] = @base.repo.path
      ENV['GIT_INDEX_FILE'] = @base.index.path   
      ENV['GIT_WORK_DIR'] = @base.dir.path   
      Dir.chdir(@base.dir.path) do  
        opts = opts.to_a.join(' ')
        #puts "git #{cmd} #{opts}"
        out = `git #{cmd} #{opts} 2>&1`.chomp
        #puts out
        if $?.exitstatus > 1
          raise Git::GitExecuteError.new(out)
        end
        out
      end
    end
    
  end
end