summaryrefslogtreecommitdiffstats
path: root/lib/git/base.rb
blob: 11118876695e76784a2e302c2c34735ff406a81b (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
module Git
  
  class Base

    @working_directory = nil
    @repository = nil
    @index = nil

    # opens a Git Repository - no working directory options
    def self.repo(git_dir)
      self.new :repository => git_dir
    end
    
    # opens a new Git Project from a working directory
    # you can specify non-standard git_dir and index file in the options
    def self.open(working_dir, opts={})    
      default = {:working_directory => working_dir,
                 :repository => File.join(working_dir, '.git'), 
                 :index => File.join(working_dir, '.git', 'index')}
      git_options = default.merge(opts)
      
      self.new(git_options)
    end
    
    def self.init(working_dir, opts = {})
      default = {:working_directory => working_dir,
                 :repository => File.join(working_dir, '.git')}
      git_options = default.merge(opts)
      
      if git_options[:working_directory]
        # if !working_dir, make it
        FileUtils.mkdir_p(git_options[:working_directory]) if !File.directory?(git_options[:working_directory])
      end
      
      # run git_init there
      Git::Lib.new(git_options).init
       
      self.new(git_options)
    end

    def self.clone
      raise NotImplementedError
    end
        
    def initialize(options = {})
      @working_directory = Git::WorkingDirectory.new(options[:working_directory]) if options[:working_directory]
      @repository = Git::Repository.new(options[:repository]) if options[:repository]
      @index = Git::Index.new(options[:index]) if options[:index]
    end
  


    
    def dir
      @working_directory
    end

    def repo
      @repository
    end
    
    def index
      @index
    end
    
    
    #g.config('user.name', 'Scott Chacon') # sets value
    #g.config('user.email', 'email@email.com')  # sets value
    #g.config('user.name')  # returns 'Scott Chacon'
    #g.config # returns whole config hash
    def config(name = nil, value = nil)
      if(name && value)
        # set value
      elsif (name)
        # return value
        lib.config_get(name)
      else
        # return hash
        lib.config_list
      end
    end
    
    # factory methods
    
    def object(objectish)
      Git::Object.new(self, objectish)
    end
    alias_method :tree, :object
    alias_method :commit, :object
    alias_method :blob, :object
    
    
    def log(count = 30)
      Git::Log.new(self, count)
    end
    
    def branches
      Git::Branches.new(self)
    end
    
    def lib
      Git::Lib.new(self)
    end
    
    def grep(string)
      self.object('HEAD').grep(string)
    end
    
    def diff(objectish = 'HEAD', obj2 = nil)
      Git::Diff.new(self, objectish, obj2)
    end
    
    # convenience methods
    
    def revparse(objectish)
      self.lib.revparse(objectish)
    end
    
  end
  
end