few hours work - diff is done
[rubygit.git] / lib / git / base.rb
blob11118876695e76784a2e302c2c34735ff406a81b
1 module Git
2   
3   class Base
5     @working_directory = nil
6     @repository = nil
7     @index = nil
9     # opens a Git Repository - no working directory options
10     def self.repo(git_dir)
11       self.new :repository => git_dir
12     end
13     
14     # opens a new Git Project from a working directory
15     # you can specify non-standard git_dir and index file in the options
16     def self.open(working_dir, opts={})    
17       default = {:working_directory => working_dir,
18                  :repository => File.join(working_dir, '.git'), 
19                  :index => File.join(working_dir, '.git', 'index')}
20       git_options = default.merge(opts)
21       
22       self.new(git_options)
23     end
24     
25     def self.init(working_dir, opts = {})
26       default = {:working_directory => working_dir,
27                  :repository => File.join(working_dir, '.git')}
28       git_options = default.merge(opts)
29       
30       if git_options[:working_directory]
31         # if !working_dir, make it
32         FileUtils.mkdir_p(git_options[:working_directory]) if !File.directory?(git_options[:working_directory])
33       end
34       
35       # run git_init there
36       Git::Lib.new(git_options).init
37        
38       self.new(git_options)
39     end
41     def self.clone
42       raise NotImplementedError
43     end
44         
45     def initialize(options = {})
46       @working_directory = Git::WorkingDirectory.new(options[:working_directory]) if options[:working_directory]
47       @repository = Git::Repository.new(options[:repository]) if options[:repository]
48       @index = Git::Index.new(options[:index]) if options[:index]
49     end
50   
53     
54     def dir
55       @working_directory
56     end
58     def repo
59       @repository
60     end
61     
62     def index
63       @index
64     end
65     
66     
67     #g.config('user.name', 'Scott Chacon') # sets value
68     #g.config('user.email', 'email@email.com')  # sets value
69     #g.config('user.name')  # returns 'Scott Chacon'
70     #g.config # returns whole config hash
71     def config(name = nil, value = nil)
72       if(name && value)
73         # set value
74       elsif (name)
75         # return value
76         lib.config_get(name)
77       else
78         # return hash
79         lib.config_list
80       end
81     end
82     
83     # factory methods
84     
85     def object(objectish)
86       Git::Object.new(self, objectish)
87     end
88     alias_method :tree, :object
89     alias_method :commit, :object
90     alias_method :blob, :object
91     
92     
93     def log(count = 30)
94       Git::Log.new(self, count)
95     end
96     
97     def branches
98       Git::Branches.new(self)
99     end
100     
101     def lib
102       Git::Lib.new(self)
103     end
104     
105     def grep(string)
106       self.object('HEAD').grep(string)
107     end
108     
109     def diff(objectish = 'HEAD', obj2 = nil)
110       Git::Diff.new(self, objectish, obj2)
111     end
112     
113     # convenience methods
114     
115     def revparse(objectish)
116       self.lib.revparse(objectish)
117     end
118     
119   end
120