added remove and reset
[rubygit.git] / lib / git / base.rb
blob46c75ebfa182fdefe06bc7f115f9c3730cac9997
1 module Git
2   
3   class Base
5     @working_directory = nil
6     @repository = nil
7     @index = nil
9     # opens a bare Git Repository - no working directory options
10     def self.bare(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       git_options = default.merge(opts)
19       
20       self.new(git_options)
21     end
23     # initializes a git repository
24     #
25     # options:
26     #  :repository
27     #  :index_file
28     #
29     def self.init(working_dir, opts = {})
30       default = {:working_directory => working_dir,
31                  :repository => File.join(working_dir, '.git')}
32       git_options = default.merge(opts)
33       
34       if git_options[:working_directory]
35         # if !working_dir, make it
36         FileUtils.mkdir_p(git_options[:working_directory]) if !File.directory?(git_options[:working_directory])
37       end
38       
39       # run git_init there
40       Git::Lib.new(git_options).init
41        
42       self.new(git_options)
43     end
45     # clones a git repository locally
46     #
47     #  repository - http://repo.or.cz/w/sinatra.git
48     #  name - sinatra
49     #
50     # options:
51     #   :repository
52     #
53     #    :bare
54     #   or 
55     #    :working_directory
56     #    :index_file
57     #
58     def self.clone(repository, name, opts = {})
59       # run git-clone 
60       self.new(Git::Lib.new.clone(repository, name, opts))
61     end
62         
63     def initialize(options = {})
64       if working_dir = options[:working_directory]
65         options[:repository] = File.join(working_dir, '.git') if !options[:repository]
66         options[:index] = File.join(working_dir, '.git', 'index') if !options[:index]
67       end
68       
69       @working_directory = Git::WorkingDirectory.new(options[:working_directory]) if options[:working_directory]
70       @repository = Git::Repository.new(options[:repository]) if options[:repository]
71       @index = Git::Index.new(options[:index], false) if options[:index]
72     end
73   
74   
75     def dir
76       @working_directory
77     end
79     def repo
80       @repository
81     end
82     
83     def index
84       @index
85     end
86     
87     
88     #g.config('user.name', 'Scott Chacon') # sets value
89     #g.config('user.email', 'email@email.com')  # sets value
90     #g.config('user.name')  # returns 'Scott Chacon'
91     #g.config # returns whole config hash
92     def config(name = nil, value = nil)
93       if(name && value)
94         # set value
95         lib.config_set(name, value)
96       elsif (name)
97         # return value
98         lib.config_get(name)
99       else
100         # return hash
101         lib.config_list
102       end
103     end
104     
105     # factory methods
106     
107     def object(objectish)
108       Git::Object.new(self, objectish)
109     end
110     alias_method :gtree, :object
111     alias_method :gcommit, :object
112     alias_method :gblob, :object
113     
114     
115     def log(count = 30)
116       Git::Log.new(self, count)
117     end
119     def status
120       Git::Status.new(self)
121     end
122         
123     def branches
124       Git::Branches.new(self)
125     end
126     
127     def lib
128       Git::Lib.new(self)
129     end
130     
131     def grep(string)
132       self.object('HEAD').grep(string)
133     end
134     
135     def diff(objectish = 'HEAD', obj2 = nil)
136       Git::Diff.new(self, objectish, obj2)
137     end
138     
139     # adds files from the working directory to the git repository
140     def add(path = '.')
141       self.lib.add(path)
142     end
144     def remove(path = '.', opts = {})
145       self.lib.remove(path, opts)
146     end
148     def reset(commitish = nil, opts = {})
149       self.lib.reset(commitish, opts)
150     end
152     def reset_hard(commitish = nil, opts = {})
153       opts = {:hard => true}.merge(opts)
154       self.lib.reset(path, opts)
155     end
157     def commit(message, opts = {})
158       self.lib.commit(message, opts)
159     end
160         
161     def commit_all(message, opts = {})
162       opts = {:add_all => true}.merge(opts)
163       self.lib.commit(message, opts)
164     end
165     
166     # convenience methods
167     
168     def revparse(objectish)
169       self.lib.revparse(objectish)
170     end
171     
172   end
173