summaryrefslogtreecommitdiffstats
path: root/lib/git/base.rb
blob: 6fb8adb39105765b88fd59f00bc6712c13f7b235 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
module Git
  
  class Base

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

    # opens a bare Git Repository - no working directory options
    def self.bare(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}
      git_options = default.merge(opts)
      
      self.new(git_options)
    end

    # initializes a git repository
    #
    # options:
    #  :repository
    #  :index_file
    #
    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

    # clones a git repository locally
    #
    #  repository - http://repo.or.cz/w/sinatra.git
    #  name - sinatra
    #
    # options:
    #   :repository
    #
    #    :bare
    #   or 
    #    :working_directory
    #    :index_file
    #
    def self.clone(repository, name, opts = {})
      # run git-clone 
      self.new(Git::Lib.new.clone(repository, name, opts))
    end
        
    def initialize(options = {})
      if working_dir = options[:working_directory]
        options[:repository] = File.join(working_dir, '.git') if !options[:repository]
        options[:index] = File.join(working_dir, '.git', 'index') if !options[:index]
      end
      
      @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], false) if options[:index]
    end
  
  
    # returns a reference to the working directory
    #  @git.dir.path
    #  @git.dir.writeable?
    def dir
      @working_directory
    end

    # returns reference to the git repository directory
    #  @git.dir.path
    def repo
      @repository
    end
    
    # returns reference to the git index file
    def index
      @index
    end
    
    # changes current working directory for a block
    # to the git working directory
    #
    # example
    #  @git.chdir do 
    #    # write files
    #    @git.add
    #    @git.commit('message')
    #  end
    def chdir
      Dir.chdir(dir.path) do
        yield dir.path
      end
    end
    
    # returns the repository size in bytes
    def repo_size
      size = 0
      Dir.chdir(repo.path) do
        (size, dot) = `du -d0`.chomp.split
      end
      size.to_i
    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
        lib.config_set(name, value)
      elsif (name)
        # return value
        lib.config_get(name)
      else
        # return hash
        lib.config_list
      end
    end
    
    # factory methods
    
    # returns a Git::Object of the appropriate type
    # you can also call @git.gtree('tree'), but that's 
    # just for readability.  If you call @git.gtree('HEAD') it will
    # still return a Git::Object::Commit object.  
    #
    # @git.object calls a factory method that will run a rev-parse 
    # on the objectish and determine the type of the object and return 
    # an appropriate object for that type 
    def object(objectish)
      Git::Object.new(self, objectish)
    end
    alias_method :gtree, :object
    alias_method :gcommit, :object
    alias_method :gblob, :object
    
    # returns a Git::Log object with count commits
    def log(count = 30)
      Git::Log.new(self, count)
    end

    # returns a Git::Status object
    def status
      Git::Status.new(self)
    end
        
    # returns a Git::Branches object of all the Git::Branch objects for this repo
    def branches
      Git::Branches.new(self)
    end
    
    # returns a Git::Branch object for branch_name
    def branch(branch_name = 'master')
      Git::Branch.new(self, branch_name)
    end

    # returns a Git::Remote object
    def remote(remote_name = 'origin')
      Git::Remote.new(self, remote_name)
    end

    # this is a convenience method for accessing the class that wraps all the 
    # actual 'git' forked system calls.  At some point I hope to replace the Git::Lib
    # class with one that uses native methods or libgit C bindings
    def lib
      Git::Lib.new(self)
    end
    
    # will run a grep for 'string' on the HEAD of the git repository
    # 
    # to be more surgical in your grep, you can call grep() off a specific
    # git object.  for example:
    #
    #  @git.object("v2.3").grep('TODO')
    #
    # in any case, it returns a hash of arrays of the type:
    #  hsh[tree-ish] = [[line_no, match], [line_no, match2]]
    #  hsh[tree-ish] = [[line_no, match], [line_no, match2]]
    #
    # so you might use it like this:
    #
    #   @git.grep("TODO").each do |sha, arr|
    #     puts "in blob #{sha}:"
    #     arr.each do |match|
    #       puts "\t line #{match[0]}: '#{match[1]}'"
    #     end
    #   end
    def grep(string)
      self.object('HEAD').grep(string)
    end
    
    # returns a Git::Diff object
    def diff(objectish = 'HEAD', obj2 = nil)
      Git::Diff.new(self, objectish, obj2)
    end
    
    # adds files from the working directory to the git repository
    def add(path = '.')
      self.lib.add(path)
    end

    # removes file(s) from the git repository
    def remove(path = '.', opts = {})
      self.lib.remove(path, opts)
    end

    # resets the working directory to the provided commitish
    def reset(commitish = nil, opts = {})
      self.lib.reset(commitish, opts)
    end

    # resets the working directory to the commitish with '--hard'
    def reset_hard(commitish = nil, opts = {})
      opts = {:hard => true}.merge(opts)
      self.lib.reset(commitish, opts)
    end

    # commits all pending changes in the index file to the git repository
    def commit(message, opts = {})
      self.lib.commit(message, opts)
    end
        
    # commits all pending changes in the index file to the git repository,
    # but automatically adds all modified files without having to explicitly
    # calling @git.add() on them.  
    def commit_all(message, opts = {})
      opts = {:add_all => true}.merge(opts)
      self.lib.commit(message, opts)
    end

    # checks out a branch as the new git working directory
    def checkout(branch = 'master', opts = {})
      self.lib.checkout(branch, opts)
    end
    
    # fetches changes from a remote branch - this does not modify the working directory,
    # it just gets the changes from the remote if there are any
    def fetch(remote = 'origin')
      self.lib.fetch(remote)
    end

    # pushes changes to a remote repository - easiest if this is a cloned repository,
    # otherwise you may have to run something like this first to setup the push parameters:
    #
    #  @git.config('remote.remote-name.push', 'refs/heads/master:refs/heads/master')
    #
    def push(remote = 'origin', branch = 'master')
      self.lib.push(remote, branch)
    end
    
    # merges one or more branches into the current working branch
    #
    # you can specify more than one branch to merge by passing an array of branches
    def merge(branch, message = 'merge')
      self.lib.merge(branch, message)
    end

    # fetches a branch from a remote and merges it into the current working branch
    def pull(remote = 'origin', branch = 'master', message = 'origin pull')
      fetch(remote)
      merge(branch, message)
    end
    
    # returns an array of Git:Remote objects
    def remotes
      self.lib.remotes.map { |r| Git::Remote.new(self, r) }
    end

    # adds a new remote to this repository
    # url can be a git url or a Git::Base object if it's a local reference
    # 
    #  @git.add_remote('scotts_git', 'git://repo.or.cz/rubygit.git')
    #  @git.fetch('scotts_git')
    #  @git.merge('scotts_git/master')
    #
    def add_remote(name, url, opts = {})
      if url.is_a?(Git::Base)
        url = url.repo.path
      end
      self.lib.remote_add(name, url, opts)
      Git::Remote.new(self, name)
    end

    # returns an array of all Git::Tag objects for this repository
    def tags
      self.lib.tags.map { |r| tag(r) }
    end
    
    # returns a Git::Tag object
    def tag(tag_name)
      Git::Object.new(self, tag_name, true)
    end

    # creates a new git tag (Git::Tag)
    def add_tag(tag_name)
      self.lib.tag(tag_name)
      tag(tag_name)
    end
    
    # creates an archive file of the given tree-ish
    def archive(treeish, file = nil, opts = {})
      self.object(treeish).archive(file, opts)
    end
    
    # repacks the repository
    def repack
      self.lib.repack
    end
    
    # runs git rev-parse to convert the objectish to a full sha
    #
    #   @git.revparse("HEAD^^")
    #   @git.revparse('v2.4^{tree}')
    #   @git.revparse('v2.4:/doc/index.html')
    #
    def revparse(objectish)
      self.lib.revparse(objectish)
    end

    # returns the name of the branch the working directory is currently on
    def current_branch
      self.lib.branch_current
    end

    
  end
  
end