added tagging
[rubygit.git] / lib / git / base.rb
blobffd8a2b974d2379e8b314fda76667ef7f57e2da9
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     def chdir
88       Dir.chdir(dir.path) do
89         yield dir.path
90       end
91     end
92     
93     #g.config('user.name', 'Scott Chacon') # sets value
94     #g.config('user.email', 'email@email.com')  # sets value
95     #g.config('user.name')  # returns 'Scott Chacon'
96     #g.config # returns whole config hash
97     def config(name = nil, value = nil)
98       if(name && value)
99         # set value
100         lib.config_set(name, value)
101       elsif (name)
102         # return value
103         lib.config_get(name)
104       else
105         # return hash
106         lib.config_list
107       end
108     end
109     
110     # factory methods
111     
112     def object(objectish)
113       Git::Object.new(self, objectish)
114     end
115     alias_method :gtree, :object
116     alias_method :gcommit, :object
117     alias_method :gblob, :object
118     
119     
120     def log(count = 30)
121       Git::Log.new(self, count)
122     end
124     def status
125       Git::Status.new(self)
126     end
127         
128     def branches
129       Git::Branches.new(self)
130     end
131     
132     def branch(branch_name = 'master')
133       Git::Branch.new(self, branch_name)
134     end
136     def remote(remote_name = 'origin')
137       Git::Remote.new(self, remote_name)
138     end
140     
141     def lib
142       Git::Lib.new(self)
143     end
144     
145     def grep(string)
146       self.object('HEAD').grep(string)
147     end
148     
149     def diff(objectish = 'HEAD', obj2 = nil)
150       Git::Diff.new(self, objectish, obj2)
151     end
152     
153     # adds files from the working directory to the git repository
154     def add(path = '.')
155       self.lib.add(path)
156     end
158     def remove(path = '.', opts = {})
159       self.lib.remove(path, opts)
160     end
162     def reset(commitish = nil, opts = {})
163       self.lib.reset(commitish, opts)
164     end
166     def reset_hard(commitish = nil, opts = {})
167       opts = {:hard => true}.merge(opts)
168       self.lib.reset(commitish, opts)
169     end
171     def commit(message, opts = {})
172       self.lib.commit(message, opts)
173     end
174         
175     def commit_all(message, opts = {})
176       opts = {:add_all => true}.merge(opts)
177       self.lib.commit(message, opts)
178     end
180     def checkout(branch, opts = {})
181       self.lib.checkout(branch, opts)
182     end
183     
184     def fetch(remote = 'origin')
185       self.lib.fetch(remote)
186     end
188     def merge(branch, message = 'merge')
189       self.lib.merge(branch, message)
190     end
192     def pull(remote = 'origin', branch = 'master', message = 'origin pull')
193       fetch(remote)
194       merge(branch, message)
195     end
196     
197     def remotes
198       self.lib.remotes.map { |r| Git::Remote.new(self, r) }
199     end
200     
201     def add_remote(name, url, opts = {})
202       if url.is_a?(Git::Base)
203         url = url.repo.path
204       end
205       self.lib.remote_add(name, url, opts)
206       Git::Remote.new(self, name)
207     end
209     def tags
210       self.lib.tags.map { |r| tag(r) }
211     end
212     
213     def tag(tag_name)
214       Git::Object.new(self, tag_name, true)
215     end
217     def add_tag(tag_name)
218       self.lib.tag(tag_name)
219       tag(tag_name)
220     end
221     
222     # convenience methods
224     
225     def revparse(objectish)
226       self.lib.revparse(objectish)
227     end
229     def current_branch
230       self.lib.branch_current
231     end
233     
234   end
235