schacon-git 1.0.7 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -1,6 +1,6 @@
1
1
  == Git Library for Ruby
2
2
 
3
- Library for using Git in Ruby.
3
+ Library for using Git in Ruby. Test.
4
4
 
5
5
  = Homepage
6
6
 
@@ -12,6 +12,12 @@ Git public hosting of the project source code is at:
12
12
 
13
13
  http://github/schacon/ruby-git
14
14
 
15
+ = Install
16
+
17
+ You can install Ruby/Git like this:
18
+
19
+ $ sudo gem install schacon-git --source=http://gems.github.com
20
+
15
21
  = Major Objects
16
22
 
17
23
  Git::Base - this is the object returned from a Git.open or Git.clone.
data/lib/git/base.rb CHANGED
@@ -2,28 +2,15 @@ module Git
2
2
 
3
3
  class Base
4
4
 
5
- @working_directory = nil
6
- @repository = nil
7
- @index = nil
8
-
9
- @lib = nil
10
- @logger = nil
11
-
12
5
  # opens a bare Git Repository - no working directory options
13
6
  def self.bare(git_dir, opts = {})
14
- default = {:repository => git_dir}
15
- git_options = default.merge(opts)
16
-
17
- self.new(git_options)
7
+ self.new({:repository => git_dir}.merge(opts))
18
8
  end
19
9
 
20
10
  # opens a new Git Project from a working directory
21
11
  # you can specify non-standard git_dir and index file in the options
22
- def self.open(working_dir, opts={})
23
- default = {:working_directory => working_dir}
24
- git_options = default.merge(opts)
25
-
26
- self.new(git_options)
12
+ def self.open(working_dir, opts={})
13
+ self.new({:working_directory => working_dir}.merge(opts))
27
14
  end
28
15
 
29
16
  # initializes a git repository
@@ -33,19 +20,17 @@ module Git
33
20
  # :index_file
34
21
  #
35
22
  def self.init(working_dir, opts = {})
36
- default = {:working_directory => working_dir,
37
- :repository => File.join(working_dir, '.git')}
38
- git_options = default.merge(opts)
23
+ opts = {
24
+ :working_directory => working_dir,
25
+ :repository => File.join(working_dir, '.git')
26
+ }.merge(opts)
39
27
 
40
- if git_options[:working_directory]
41
- # if !working_dir, make it
42
- FileUtils.mkdir_p(git_options[:working_directory]) if !File.directory?(git_options[:working_directory])
43
- end
28
+ FileUtils.mkdir_p(opts[:working_directory]) if opts[:working_directory] && !File.directory?(opts[:working_directory])
44
29
 
45
30
  # run git_init there
46
- Git::Lib.new(git_options).init
31
+ Git::Lib.new(opts).init
47
32
 
48
- self.new(git_options)
33
+ self.new(opts)
49
34
  end
50
35
 
51
36
  # clones a git repository locally
@@ -68,17 +53,19 @@ module Git
68
53
 
69
54
  def initialize(options = {})
70
55
  if working_dir = options[:working_directory]
71
- options[:repository] = File.join(working_dir, '.git') if !options[:repository]
72
- options[:index] = File.join(working_dir, '.git', 'index') if !options[:index]
56
+ options[:repository] ||= File.join(working_dir, '.git')
57
+ options[:index] ||= File.join(working_dir, '.git', 'index')
73
58
  end
74
59
  if options[:log]
75
60
  @logger = options[:log]
76
61
  @logger.info("Starting Git")
62
+ else
63
+ @logger = nil
77
64
  end
78
-
79
- @working_directory = Git::WorkingDirectory.new(options[:working_directory]) if options[:working_directory]
80
- @repository = Git::Repository.new(options[:repository]) if options[:repository]
81
- @index = Git::Index.new(options[:index], false) if options[:index]
65
+
66
+ @working_directory = options[:working_directory] ? Git::WorkingDirectory.new(options[:working_directory]) : nil
67
+ @repository = options[:repository] ? Git::Repository.new(options[:repository]) : nil
68
+ @index = options[:index] ? Git::Index.new(options[:index], false) : nil
82
69
  end
83
70
 
84
71
 
@@ -120,7 +107,7 @@ module Git
120
107
  # @git.add
121
108
  # @git.commit('message')
122
109
  # end
123
- def chdir
110
+ def chdir # :yields: the Git::Path
124
111
  Dir.chdir(dir.path) do
125
112
  yield dir.path
126
113
  end
@@ -197,6 +184,24 @@ module Git
197
184
  def branch(branch_name = 'master')
198
185
  Git::Branch.new(self, branch_name)
199
186
  end
187
+
188
+ # returns +true+ if the branch exists locally
189
+ def is_local_branch?(branch)
190
+ branch_names = self.branches.local.map {|b| b.name}
191
+ branch_names.include?(branch)
192
+ end
193
+
194
+ # returns +true+ if the branch exists remotely
195
+ def is_remote_branch?(branch)
196
+ branch_names = self.branches.local.map {|b| b.name}
197
+ branch_names.include?(branch)
198
+ end
199
+
200
+ # returns +true+ if the branch exists
201
+ def is_branch?(branch)
202
+ branch_names = self.branches.map {|b| b.name}
203
+ branch_names.include?(branch)
204
+ end
200
205
 
201
206
  # returns a Git::Remote object
202
207
  def remote(remote_name = 'origin')
@@ -229,8 +234,8 @@ module Git
229
234
  # puts "\t line #{match[0]}: '#{match[1]}'"
230
235
  # end
231
236
  # end
232
- def grep(string)
233
- self.object('HEAD').grep(string)
237
+ def grep(string, path_limiter = nil, opts = {})
238
+ self.object('HEAD').grep(string, path_limiter, opts)
234
239
  end
235
240
 
236
241
  # returns a Git::Diff object
@@ -260,6 +265,11 @@ module Git
260
265
  end
261
266
 
262
267
  # commits all pending changes in the index file to the git repository
268
+ #
269
+ # options:
270
+ # :add_all
271
+ # :allow_empty
272
+ # :author
263
273
  def commit(message, opts = {})
264
274
  self.lib.commit(message, opts)
265
275
  end
@@ -293,8 +303,8 @@ module Git
293
303
  #
294
304
  # @git.config('remote.remote-name.push', 'refs/heads/master:refs/heads/master')
295
305
  #
296
- def push(remote = 'origin', branch = 'master')
297
- self.lib.push(remote, branch)
306
+ def push(remote = 'origin', branch = 'master', tags = false)
307
+ self.lib.push(remote, branch, tags)
298
308
  end
299
309
 
300
310
  # merges one or more branches into the current working branch
@@ -305,9 +315,7 @@ module Git
305
315
  end
306
316
 
307
317
  # iterates over the files which are unmerged
308
- #
309
- # yields file, your_version, their_version
310
- def each_conflict(&block)
318
+ def each_conflict(&block) # :yields: file, your_version, their_version
311
319
  self.lib.conflicts(&block)
312
320
  end
313
321
 
@@ -330,9 +338,7 @@ module Git
330
338
  # @git.merge('scotts_git/master')
331
339
  #
332
340
  def add_remote(name, url, opts = {})
333
- if url.is_a?(Git::Base)
334
- url = url.repo.path
335
- end
341
+ url = url.repo.path if url.is_a?(Git::Base)
336
342
  self.lib.remote_add(name, url, opts)
337
343
  Git::Remote.new(self, name)
338
344
  end
@@ -374,14 +380,12 @@ module Git
374
380
  end
375
381
 
376
382
  def apply_mail(file)
377
- if File.exists?(file)
378
- self.lib.apply_mail(file)
379
- end
383
+ self.lib.apply_mail(file) if File.exists?(file)
380
384
  end
381
385
 
382
386
  ## LOWER LEVEL INDEX OPERATIONS ##
383
387
 
384
- def with_index(new_index)
388
+ def with_index(new_index) # :yields: new_index
385
389
  old_index = @index
386
390
  set_index(new_index, false)
387
391
  return_value = yield @index
@@ -426,7 +430,7 @@ module Git
426
430
  self.lib.ls_files
427
431
  end
428
432
 
429
- def with_working(work_dir)
433
+ def with_working(work_dir) # :yields: the Git::WorkingDirectory
430
434
  return_value = false
431
435
  old_working = @working_directory
432
436
  set_working(work_dir)
data/lib/git/branch.rb CHANGED
@@ -3,14 +3,12 @@ module Git
3
3
 
4
4
  attr_accessor :full, :remote, :name
5
5
 
6
- @base = nil
7
- @gcommit = nil
8
- @stashes = nil
9
-
10
6
  def initialize(base, name)
11
7
  @remote = nil
12
8
  @full = name
13
9
  @base = base
10
+ @gcommit = nil
11
+ @stashes = nil
14
12
 
15
13
  parts = name.split('/')
16
14
  if parts[1]
@@ -22,7 +20,7 @@ module Git
22
20
  end
23
21
 
24
22
  def gcommit
25
- @gcommit = @base.gcommit(@full) if !@gcommit
23
+ @gcommit ||= @base.gcommit(@full)
26
24
  @gcommit
27
25
  end
28
26
 
data/lib/git/branches.rb CHANGED
@@ -4,9 +4,6 @@ module Git
4
4
  class Branches
5
5
  include Enumerable
6
6
 
7
- @base = nil
8
- @branches = nil
9
-
10
7
  def initialize(base)
11
8
  @branches = {}
12
9
 
@@ -31,10 +28,8 @@ module Git
31
28
  @branches.size
32
29
  end
33
30
 
34
- def each
35
- @branches.each do |k, b|
36
- yield b
37
- end
31
+ def each(&block)
32
+ @branches.values.each(&block)
38
33
  end
39
34
 
40
35
  def [](symbol)
@@ -44,14 +39,10 @@ module Git
44
39
  def to_s
45
40
  out = ''
46
41
  @branches.each do |k, b|
47
- if b.current
48
- out += "* " + b.to_s + "\n"
49
- else
50
- out += " " + b.to_s + "\n"
51
- end
42
+ out << (b.current ? '* ' : ' ') << b.to_s << "\n"
52
43
  end
53
44
  out
54
45
  end
55
46
 
56
47
  end
57
- end
48
+ end
data/lib/git/diff.rb CHANGED
@@ -4,20 +4,17 @@ module Git
4
4
  class Diff
5
5
  include Enumerable
6
6
 
7
- @base = nil
8
- @from = nil
9
- @to = nil
10
- @path = nil
11
-
12
- @full_diff = nil
13
- @full_diff_files = nil
14
- @stats = nil
15
-
16
7
  def initialize(base, from = nil, to = nil)
17
8
  @base = base
18
9
  @from = from.to_s
19
10
  @to = to.to_s
11
+
12
+ @path = nil
13
+ @full_diff = nil
14
+ @full_diff_files = nil
15
+ @stats = nil
20
16
  end
17
+ attr_reader :from, :to
21
18
 
22
19
  def path(path)
23
20
  @path = path
@@ -63,11 +60,9 @@ module Git
63
60
  @full_diff_files.assoc(key)[1]
64
61
  end
65
62
 
66
- def each
63
+ def each(&block) # :yields: each Git::DiffFile in turn
67
64
  process_full
68
- @full_diff_files.each do |file|
69
- yield file[1]
70
- end
65
+ @full_diff_files.map { |file| file[1] }.each(&block)
71
66
  end
72
67
 
73
68
  class DiffFile
@@ -82,6 +77,11 @@ module Git
82
77
  @src = hash[:src]
83
78
  @dst = hash[:dst]
84
79
  @type = hash[:type]
80
+ @binary = hash[:binary]
81
+ end
82
+
83
+ def binary?
84
+ !!@binary
85
85
  end
86
86
 
87
87
  def blob(type = :dst)
@@ -96,20 +96,20 @@ module Git
96
96
  private
97
97
 
98
98
  def cache_full
99
- if !@full_diff
99
+ unless @full_diff
100
100
  @full_diff = @base.lib.diff_full(@from, @to, {:path_limiter => @path})
101
101
  end
102
102
  end
103
103
 
104
104
  def process_full
105
- if !@full_diff_files
105
+ unless @full_diff_files
106
106
  cache_full
107
107
  @full_diff_files = process_full_diff
108
108
  end
109
109
  end
110
110
 
111
111
  def cache_stats
112
- if !@stats
112
+ unless @stats
113
113
  @stats = @base.lib.diff_stats(@from, @to, {:path_limiter => @path})
114
114
  end
115
115
  end
@@ -133,6 +133,9 @@ module Git
133
133
  final[current_file][:type] = m[1]
134
134
  final[current_file][:mode] = m[2]
135
135
  end
136
+ if m = /^Binary files /.match(line)
137
+ final[current_file][:binary] = true
138
+ end
136
139
  final[current_file][:patch] << "\n" + line
137
140
  end
138
141
  end
@@ -140,4 +143,4 @@ module Git
140
143
  end
141
144
 
142
145
  end
143
- end
146
+ end