grit 1.1.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. data/History.txt +9 -0
  2. data/README.md +37 -11
  3. data/VERSION.yml +3 -3
  4. data/examples/ex_add_commit.rb +13 -0
  5. data/examples/ex_index.rb +14 -0
  6. data/lib/grit.rb +10 -3
  7. data/lib/grit/actor.rb +5 -5
  8. data/lib/grit/blob.rb +12 -12
  9. data/lib/grit/commit.rb +3 -3
  10. data/lib/grit/commit_stats.rb +26 -26
  11. data/lib/grit/config.rb +9 -9
  12. data/lib/grit/diff.rb +16 -16
  13. data/lib/grit/errors.rb +1 -1
  14. data/lib/grit/git-ruby.rb +108 -27
  15. data/lib/grit/git-ruby/commit_db.rb +11 -11
  16. data/lib/grit/git-ruby/file_index.rb +28 -28
  17. data/lib/grit/git-ruby/git_object.rb +14 -14
  18. data/lib/grit/git-ruby/internal/file_window.rb +4 -4
  19. data/lib/grit/git-ruby/internal/loose.rb +10 -10
  20. data/lib/grit/git-ruby/internal/pack.rb +29 -29
  21. data/lib/grit/git-ruby/internal/raw_object.rb +4 -4
  22. data/lib/grit/git-ruby/object.rb +9 -9
  23. data/lib/grit/git-ruby/repository.rb +111 -107
  24. data/lib/grit/git.rb +191 -14
  25. data/lib/grit/index.rb +21 -21
  26. data/lib/grit/lazy.rb +1 -1
  27. data/lib/grit/merge.rb +9 -9
  28. data/lib/grit/ref.rb +6 -31
  29. data/lib/grit/repo.rb +110 -65
  30. data/lib/grit/ruby1.9.rb +1 -1
  31. data/lib/grit/status.rb +24 -24
  32. data/lib/grit/submodule.rb +15 -15
  33. data/lib/grit/tag.rb +7 -57
  34. data/lib/grit/tree.rb +12 -12
  35. data/test/bench/benchmarks.rb +126 -0
  36. data/test/helper.rb +18 -0
  37. data/test/profile.rb +21 -0
  38. data/test/suite.rb +6 -0
  39. data/test/test_actor.rb +35 -0
  40. data/test/test_blame.rb +32 -0
  41. data/test/test_blame_tree.rb +33 -0
  42. data/test/test_blob.rb +83 -0
  43. data/test/test_commit.rb +207 -0
  44. data/test/test_commit_stats.rb +33 -0
  45. data/test/test_commit_write.rb +20 -0
  46. data/test/test_config.rb +58 -0
  47. data/test/test_diff.rb +18 -0
  48. data/test/test_file_index.rb +56 -0
  49. data/test/test_git.rb +105 -0
  50. data/test/test_grit.rb +32 -0
  51. data/test/test_head.rb +47 -0
  52. data/test/test_index_status.rb +40 -0
  53. data/test/test_merge.rb +17 -0
  54. data/test/test_raw.rb +16 -0
  55. data/test/test_real.rb +19 -0
  56. data/test/test_reality.rb +17 -0
  57. data/test/test_remote.rb +14 -0
  58. data/test/test_repo.rb +349 -0
  59. data/test/test_rubygit.rb +192 -0
  60. data/test/test_rubygit_alt.rb +40 -0
  61. data/test/test_rubygit_index.rb +76 -0
  62. data/test/test_rubygit_iv2.rb +28 -0
  63. data/test/test_submodule.rb +69 -0
  64. data/test/test_tag.rb +67 -0
  65. data/test/test_tree.rb +101 -0
  66. metadata +43 -13
@@ -1,7 +1,7 @@
1
1
  #
2
2
  # converted from the gitrb project
3
3
  #
4
- # authors:
4
+ # authors:
5
5
  # Matthias Lederhofer <matled@gmx.net>
6
6
  # Simon 'corecode' Schubert <corecode@fs.ei.tum.de>
7
7
  # Scott Chacon <schacon@gmail.com>
@@ -31,20 +31,20 @@ module Grit
31
31
 
32
32
  class NoSuchPath < StandardError
33
33
  end
34
-
34
+
35
35
  attr_accessor :git_dir, :options
36
-
36
+
37
37
  def initialize(git_dir, options = {})
38
38
  @git_dir = git_dir
39
39
  @options = options
40
40
  @packs = []
41
41
  end
42
-
42
+
43
43
  # returns the loose objects object lazily
44
44
  def loose
45
45
  @loose ||= initloose
46
46
  end
47
-
47
+
48
48
  # returns the array of pack list objects
49
49
  def packs
50
50
  @packs ||= initpacks
@@ -67,7 +67,7 @@ module Grit
67
67
  end
68
68
  end
69
69
 
70
-
70
+
71
71
  # returns a raw object given a SHA1
72
72
  def get_raw_object_by_sha1(sha1o)
73
73
  raise NoSuchShaFound if sha1o.nil? || sha1o.empty? || !sha1o.is_a?(String)
@@ -99,19 +99,19 @@ module Grit
99
99
  def cached(key, object, do_cache = true)
100
100
  object
101
101
  end
102
-
102
+
103
103
  # returns GitRuby object of any type given a SHA1
104
104
  def get_object_by_sha1(sha1)
105
105
  r = get_raw_object_by_sha1(sha1)
106
106
  return nil if !r
107
107
  GitObject.from_raw(r)
108
108
  end
109
-
109
+
110
110
  # writes a raw object into the git repo
111
111
  def put_raw_object(content, type)
112
112
  loose.first.put_raw_object(content, type)
113
113
  end
114
-
114
+
115
115
  # returns true or false if that sha exists in the db
116
116
  def object_exists?(sha1)
117
117
  sha_hex = [sha1].pack("H*")
@@ -121,7 +121,7 @@ module Grit
121
121
  return true if in_packs?(sha_hex) #maybe the object got packed in the meantime
122
122
  false
123
123
  end
124
-
124
+
125
125
  # returns true if the hex-packed sha is in the packfiles
126
126
  def in_packs?(sha_hex)
127
127
  # try packs
@@ -130,7 +130,7 @@ module Grit
130
130
  end
131
131
  false
132
132
  end
133
-
133
+
134
134
  # returns true if the hex-packed sha is in the loose objects
135
135
  def in_loose?(sha_hex)
136
136
  loose.each do |lsobj|
@@ -138,34 +138,34 @@ module Grit
138
138
  end
139
139
  false
140
140
  end
141
-
142
-
141
+
142
+
143
143
  # returns the file type (as a symbol) of this sha
144
144
  def cat_file_type(sha)
145
145
  get_raw_object_by_sha1(sha).type
146
146
  end
147
-
148
- # returns the file size (as an int) of this sha
147
+
148
+ # returns the file size (as an int) of this sha
149
149
  def cat_file_size(sha)
150
150
  get_raw_object_by_sha1(sha).content.size
151
151
  end
152
-
152
+
153
153
  # returns the raw file contents of this sha
154
154
  def cat_file(sha)
155
155
  get_object_by_sha1(sha).raw_content
156
156
  end
157
-
157
+
158
158
  # returns a 2-d hash of the tree
159
159
  # ['blob']['FILENAME'] = {:mode => '100644', :sha => SHA}
160
160
  # ['tree']['DIRNAME'] = {:mode => '040000', :sha => SHA}
161
- def list_tree(sha)
161
+ def list_tree(sha)
162
162
  data = {'blob' => {}, 'tree' => {}, 'link' => {}, 'commit' => {}}
163
163
  get_object_by_sha1(sha).entry.each do |e|
164
164
  data[e.format_type][e.name] = {:mode => e.format_mode, :sha => e.sha1}
165
- end
165
+ end
166
166
  data
167
167
  end
168
-
168
+
169
169
  # returns the raw (cat-file) output for a tree
170
170
  # if given a commit sha, it will print the tree of that commit
171
171
  # if given a path limiter array, it will limit the output to those
@@ -244,11 +244,11 @@ module Grit
244
244
  tree
245
245
  end
246
246
  end
247
- end
248
-
247
+ end
248
+
249
249
  # returns an array of GitRuby Commit objects
250
250
  # [ [sha, raw_output], [sha, raw_output], [sha, raw_output] ... ]
251
- #
251
+ #
252
252
  # takes the following options:
253
253
  # :since - Time object specifying that you don't want commits BEFORE this
254
254
  # :until - Time object specifying that you don't want commit AFTER this
@@ -270,40 +270,40 @@ module Grit
270
270
  end
271
271
  return new_arr
272
272
  end
273
-
273
+
274
274
  def rev_list(sha, options)
275
275
  if sha.is_a? Array
276
276
  (end_sha, sha) = sha
277
277
  end
278
-
278
+
279
279
  log = log(sha, options)
280
280
  log = log.sort { |a, b| a[2] <=> b[2] }.reverse
281
-
281
+
282
282
  if end_sha
283
283
  log = truncate_arr(log, end_sha)
284
284
  end
285
-
285
+
286
286
  # shorten the list if it's longer than max_count (had to get everything in branches)
287
287
  if options[:max_count]
288
288
  if (opt_len = options[:max_count].to_i) < log.size
289
289
  log = log[0, opt_len]
290
290
  end
291
291
  end
292
-
292
+
293
293
  if options[:pretty] == 'raw'
294
294
  log.map {|k, v| v }.join('')
295
295
  else
296
296
  log.map {|k, v| k }.join("\n")
297
297
  end
298
298
  end
299
-
299
+
300
300
  # called by log() to recursively walk the tree
301
301
  def walk_log(sha, opts, total_size = 0)
302
302
  return [] if @already_searched[sha] # to prevent rechecking branches
303
303
  @already_searched[sha] = true
304
-
305
- array = []
306
- if (sha)
304
+
305
+ array = []
306
+ if (sha)
307
307
  o = get_raw_object_by_sha1(sha)
308
308
  if o.type == :tag
309
309
  commit_sha = get_object_by_sha1(sha).object
@@ -313,61 +313,61 @@ module Grit
313
313
  end
314
314
 
315
315
  return [] if c.type != :commit
316
-
316
+
317
317
  add_sha = true
318
-
318
+
319
319
  if opts[:since] && opts[:since].is_a?(Time) && (opts[:since] > c.committer.date)
320
320
  add_sha = false
321
321
  end
322
322
  if opts[:until] && opts[:until].is_a?(Time) && (opts[:until] < c.committer.date)
323
323
  add_sha = false
324
324
  end
325
-
325
+
326
326
  # follow all parents unless '--first-parent' is specified #
327
327
  subarray = []
328
-
328
+
329
329
  if !c.parent.first && opts[:path_limiter] # check for the last commit
330
330
  add_sha = false
331
331
  end
332
-
332
+
333
333
  if (!opts[:max_count] || ((array.size + total_size) < opts[:max_count]))
334
-
334
+
335
335
  if !opts[:path_limiter]
336
336
  output = c.raw_log(sha)
337
337
  array << [sha, output, c.committer.date]
338
338
  end
339
-
339
+
340
340
  if (opts[:max_count] && (array.size + total_size) >= opts[:max_count])
341
341
  return array
342
342
  end
343
-
343
+
344
344
  c.parent.each do |psha|
345
345
  if psha && !files_changed?(c.tree, get_object_by_sha1(psha).tree,
346
346
  opts[:path_limiter])
347
- add_sha = false
347
+ add_sha = false
348
348
  end
349
- subarray += walk_log(psha, opts, (array.size + total_size))
349
+ subarray += walk_log(psha, opts, (array.size + total_size))
350
350
  next if opts[:first_parent]
351
351
  end
352
-
352
+
353
353
  if opts[:path_limiter] && add_sha
354
354
  output = c.raw_log(sha)
355
355
  array << [sha, output, c.committer.date]
356
- end
357
-
356
+ end
357
+
358
358
  if add_sha
359
359
  array += subarray
360
360
  end
361
361
  end
362
-
362
+
363
363
  end
364
-
364
+
365
365
  array
366
366
  end
367
367
 
368
368
  def diff(commit1, commit2, options = {})
369
369
  patch = ''
370
-
370
+
371
371
  commit_obj1 = get_object_by_sha1(commit1)
372
372
  tree1 = commit_obj1.tree
373
373
  if commit2
@@ -375,37 +375,41 @@ module Grit
375
375
  else
376
376
  tree2 = get_object_by_sha1(commit_obj1.parent.first).tree
377
377
  end
378
-
378
+
379
379
  qdiff = quick_diff(tree1, tree2)
380
-
380
+
381
381
  qdiff.sort.each do |diff_arr|
382
+ path, status, treeSHA1, treeSHA2 = *diff_arr
382
383
  format, lines, output = :unified, 3, ''
383
384
  file_length_difference = 0
384
-
385
- fileA = (diff_arr[2]) ? cat_file(diff_arr[2]) : ''
386
- fileB = (diff_arr[3]) ? cat_file(diff_arr[3]) : ''
387
-
388
- sha1 = (diff_arr[2]) ? diff_arr[2] : '0000000000000000000000000000000000000000'
389
- sha2 = (diff_arr[3]) ? diff_arr[3] : '0000000000000000000000000000000000000000'
385
+
386
+ fileA = treeSHA1 ? cat_file(treeSHA1) : ''
387
+ fileB = treeSHA2 ? cat_file(treeSHA2) : ''
388
+
389
+ sha1 = treeSHA1 || '0000000000000000000000000000000000000000'
390
+ sha2 = treeSHA2 || '0000000000000000000000000000000000000000'
390
391
 
391
392
  data_old = fileA.split(/\n/).map! { |e| e.chomp }
392
393
  data_new = fileB.split(/\n/).map! { |e| e.chomp }
393
-
394
- diffs = Difference::LCS.diff(data_old, data_new)
394
+
395
+ diffs = Difference::LCS.diff(data_old, data_new)
395
396
  next if diffs.empty?
396
397
 
397
- header = 'diff --git a/' + diff_arr[0].gsub('./', '') + ' b/' + diff_arr[0].gsub('./', '')
398
+ a_path = "a/#{path.gsub('./', '')}"
399
+ b_path = "b/#{path.gsub('./', '')}"
400
+
401
+ header = "diff --git #{a_path} #{b_path}"
398
402
  if options[:full_index]
399
403
  header << "\n" + 'index ' + sha1 + '..' + sha2
400
- header << ' 100644' if diff_arr[3] # hard coding this because i don't think we use it
404
+ header << ' 100644' if treeSHA2 # hard coding this because i don't think we use it
401
405
  else
402
406
  header << "\n" + 'index ' + sha1[0,7] + '..' + sha2[0,7]
403
- header << ' 100644' if diff_arr[3] # hard coding this because i don't think we use it
407
+ header << ' 100644' if treeSHA2 # hard coding this because i don't think we use it
404
408
  end
405
- header << "\n--- " + 'a/' + diff_arr[0].gsub('./', '')
406
- header << "\n+++ " + 'b/' + diff_arr[0].gsub('./', '')
409
+ header << "\n--- " + (treeSHA1 ? a_path : '/dev/null')
410
+ header << "\n+++ " + (treeSHA2 ? b_path : '/dev/null')
407
411
  header += "\n"
408
-
412
+
409
413
  oldhunk = hunk = nil
410
414
 
411
415
  diffs.each do |piece|
@@ -425,21 +429,21 @@ module Grit
425
429
  output << "\n"
426
430
  end
427
431
  end
428
-
432
+
429
433
  output << oldhunk.diff(format)
430
434
  output << "\n"
431
-
432
- patch << header + output.lstrip
435
+
436
+ patch << header + output.lstrip
433
437
  end
434
438
  patch
435
439
  rescue
436
- '' # one of the trees was bad or lcs isn't there - no diff
440
+ '' # one of the trees was bad or lcs isn't there - no diff
437
441
  end
438
-
442
+
439
443
  # takes 2 tree shas and recursively walks them to find out what
440
- # files or directories have been modified in them and returns an
444
+ # files or directories have been modified in them and returns an
441
445
  # array of changes
442
- # [ [full_path, 'added', tree1_hash, nil],
446
+ # [ [full_path, 'added', tree1_hash, nil],
443
447
  # [full_path, 'removed', nil, tree2_hash],
444
448
  # [full_path, 'modified', tree1_hash, tree2_hash]
445
449
  # ]
@@ -447,10 +451,10 @@ module Grit
447
451
  # handle empty trees
448
452
  changed = []
449
453
  return changed if tree1 == tree2
450
-
454
+
451
455
  t1 = list_tree(tree1) if tree1
452
456
  t2 = list_tree(tree2) if tree2
453
-
457
+
454
458
  # finding files that are different
455
459
  t1['blob'].each do |file, hsh|
456
460
  t2_file = t2['blob'][file] rescue nil
@@ -472,7 +476,7 @@ module Grit
472
476
  full = File.join(path, dir)
473
477
  if !t2_tree
474
478
  if recurse
475
- changed += quick_diff(hsh[:sha], nil, full, true)
479
+ changed += quick_diff(hsh[:sha], nil, full, true)
476
480
  else
477
481
  changed << [full, 'added', hsh[:sha], nil] # not in parent
478
482
  end
@@ -489,7 +493,7 @@ module Grit
489
493
  full = File.join(path, dir)
490
494
  if !t1_tree
491
495
  if recurse
492
- changed += quick_diff(nil, hsh[:sha], full, true)
496
+ changed += quick_diff(nil, hsh[:sha], full, true)
493
497
  else
494
498
  changed << [full, 'removed', nil, hsh[:sha]]
495
499
  end
@@ -514,10 +518,10 @@ module Grit
514
518
  end
515
519
  true
516
520
  end
517
-
521
+
518
522
  def get_subtree(commit_sha, path)
519
523
  tree_sha = get_object_by_sha1(commit_sha).tree
520
-
524
+
521
525
  if path && !(path == '' || path == '.' || path == './')
522
526
  paths = path.split('/')
523
527
  paths.each do |path|
@@ -529,10 +533,10 @@ module Grit
529
533
  end
530
534
  end
531
535
  end
532
-
536
+
533
537
  tree_sha
534
538
  end
535
-
539
+
536
540
  def blame_tree(commit_sha, path)
537
541
  # find subtree
538
542
  tree_sha = get_subtree(commit_sha, path)
@@ -542,10 +546,10 @@ module Grit
542
546
  get_object_by_sha1(tree_sha).entry.each do |e|
543
547
  looking_for << File.join('.', e.name)
544
548
  end
545
-
549
+
546
550
  @already_searched = {}
547
551
  commits = look_for_commits(commit_sha, path, looking_for)
548
-
552
+
549
553
  # cleaning up array
550
554
  arr = {}
551
555
  commits.each do |commit_array|
@@ -554,33 +558,33 @@ module Grit
554
558
  end
555
559
  arr
556
560
  end
557
-
558
- def look_for_commits(commit_sha, path, looking_for, options = {})
561
+
562
+ def look_for_commits(commit_sha, path, looking_for, options = {})
559
563
  return [] if @already_searched[commit_sha] # to prevent rechecking branches
560
-
564
+
561
565
  @already_searched[commit_sha] = true
562
-
566
+
563
567
  commit = get_object_by_sha1(commit_sha)
564
568
  tree_sha = get_subtree(commit_sha, path)
565
569
 
566
570
  found_data = []
567
-
571
+
568
572
  # at the beginning of the branch
569
- if commit.parent.size == 0
573
+ if commit.parent.size == 0
570
574
  looking_for.each do |search|
571
- # prevents the rare case of multiple branch starting points with
575
+ # prevents the rare case of multiple branch starting points with
572
576
  # files that have never changed
573
- if found_data.assoc(search)
577
+ if found_data.assoc(search)
574
578
  found_data << [search, commit_sha]
575
579
  end
576
580
  end
577
581
  return found_data
578
582
  end
579
-
583
+
580
584
  # go through the parents recursively, looking for somewhere this has been changed
581
585
  commit.parent.each do |pc|
582
586
  diff = quick_diff(tree_sha, get_subtree(pc, path), '.', false)
583
-
587
+
584
588
  # remove anything found
585
589
  looking_for.each do |search|
586
590
  if match = diff.assoc(search)
@@ -588,7 +592,7 @@ module Grit
588
592
  looking_for.delete(search)
589
593
  end
590
594
  end
591
-
595
+
592
596
  if looking_for.size <= 0 # we're done
593
597
  return found_data
594
598
  end
@@ -596,13 +600,13 @@ module Grit
596
600
  found_data += look_for_commits(pc, path, looking_for) # recurse into parent
597
601
  return found_data if options[:first_parent]
598
602
  end
599
-
603
+
600
604
  ## TODO : find most recent commit with change in any parent
601
605
  found_data
602
606
  end
603
-
607
+
604
608
  # initialize a git repository
605
- def self.init(dir, bare = false)
609
+ def self.init(dir, bare = true)
606
610
 
607
611
  FileUtils.mkdir_p(dir) if !File.exists?(dir)
608
612
 
@@ -646,22 +650,22 @@ module Grit
646
650
  File.open(name, 'w') do |f|
647
651
  f.write contents
648
652
  end
649
- end
650
-
653
+ end
654
+
651
655
  def close
652
656
  @packs.each do |pack|
653
657
  pack.close
654
658
  end if @packs
655
659
  end
656
-
660
+
657
661
  protected
658
662
 
659
663
  def git_path(path)
660
664
  return "#@git_dir/#{path}"
661
665
  end
662
666
 
663
- private
664
-
667
+ private
668
+
665
669
  def initloose
666
670
  @loaded = []
667
671
  @loose = []
@@ -669,7 +673,7 @@ module Grit
669
673
  load_alternate_loose(git_path('objects'))
670
674
  @loose
671
675
  end
672
-
676
+
673
677
  def load_alternate_loose(path)
674
678
  # load alternate loose, too
675
679
  alt = File.join(path, 'info/alternates')
@@ -684,13 +688,13 @@ module Grit
684
688
  end
685
689
  end
686
690
  end
687
-
691
+
688
692
  def load_loose(path)
689
693
  @loaded << path
690
694
  return if !File.exists?(path)
691
695
  @loose << Grit::GitRuby::Internal::LooseStorage.new(path)
692
696
  end
693
-
697
+
694
698
  def initpacks
695
699
  close
696
700
  @loaded_packs = []
@@ -699,7 +703,7 @@ module Grit
699
703
  load_alternate_packs(git_path('objects'))
700
704
  @packs
701
705
  end
702
-
706
+
703
707
  def load_alternate_packs(path)
704
708
  alt = File.join(path, 'info/alternates')
705
709
  if File.exists?(alt)
@@ -714,7 +718,7 @@ module Grit
714
718
  end
715
719
  end
716
720
  end
717
-
721
+
718
722
  def load_packs(path)
719
723
  @loaded_packs << path
720
724
  return if !File.exists?(path)
@@ -729,8 +733,8 @@ module Grit
729
733
  end
730
734
  end
731
735
  end
732
-
736
+
733
737
  end
734
-
738
+
735
739
  end
736
740
  end