| |   |
| 0 | | module Grit |
| 1 | | HEAD_PREFIX = 'refs/heads' |
| 2 | | |
| 3 | | # A Head is a named reference to a Commit. Every Head instance contains a name |
| 4 | | # and a Commit object. |
| 5 | | # |
| 6 | | # r = Grit::Repo.new("/path/to/repo") |
| 7 | | # h = r.heads.first |
| 8 | | # h.name # => "master" |
| 9 | | # h.commit # => #<Grit::Commit "1c09f116cbc2cb4100fb6935bb162daa4723f455"> |
| 10 | | # h.commit.id # => "1c09f116cbc2cb4100fb6935bb162daa4723f455" |
| 11 | | class Head |
| 12 | | attr_reader :name |
| 13 | | attr_reader :commit |
| 14 | | |
| 15 | | # Instantiate a new Head |
| 16 | | # +name+ is the name of the head |
| 17 | | # +commit+ is the Commit that the head points to |
| 18 | | # |
| 19 | | # Returns Grit::Head (baked) |
| 20 | | def initialize(name, commit) |
| 21 | | @name = name |
| 22 | | @commit = commit |
| 23 | | end |
| 24 | | |
| 25 | | # Find all Heads |
| 26 | | # +repo+ is the Repo |
| 27 | | # +options+ is a Hash of options |
| 28 | | # |
| 29 | | # Returns Grit::Head[] (baked) |
| 30 | | def self.find_all(repo, options = {}) |
| 31 | | default_options = {:sort => "committerdate", |
| 32 | | :format => "%(refname)%00%(objectname)"} |
| 33 | | |
| 34 | | actual_options = default_options.merge(options) |
| 35 | | |
| 36 | | output = repo.git.for_each_ref(actual_options, HEAD_PREFIX) |
| 37 | | |
| 38 | | self.list_from_string(repo, output) |
| 39 | | end |
| 40 | | |
| 41 | | # Get the HEAD revision of the repo. |
| 42 | | # +repo+ is the Repo |
| 43 | | # +options+ is a Hash of options |
| 44 | | # |
| 45 | | # Returns Grit::Head (baked) |
| 46 | | def self.current(repo, options = {}) |
| 47 | | head = File.open(File.join(repo.path, 'HEAD')).read.chomp |
| 48 | | if /ref: refs\/heads\/(.*)/.match(head) |
| 49 | | self.new($1, repo.git.rev_parse(options, 'HEAD')) |
| 50 | | end |
| 51 | | end |
| 52 | | |
| 53 | | # Parse out head information into an array of baked head objects |
| 54 | | # +repo+ is the Repo |
| 55 | | # +text+ is the text output from the git command |
| 56 | | # |
| 57 | | # Returns Grit::Head[] (baked) |
| 58 | | def self.list_from_string(repo, text) |
| 59 | | heads = [] |
| 60 | | |
| 61 | | text.split("\n").each do |line| |
| 62 | | heads << self.from_string(repo, line) |
| 63 | | end |
| 64 | | |
| 65 | | heads |
| 66 | | end |
| 67 | | |
| 68 | | # Create a new Head instance from the given string. |
| 69 | | # +repo+ is the Repo |
| 70 | | # +line+ is the formatted head information |
| 71 | | # |
| 72 | | # Format |
| 73 | | # name: [a-zA-Z_/]+ |
| 74 | | # <null byte> |
| 75 | | # id: [0-9A-Fa-f]{40} |
| 76 | | # |
| 77 | | # Returns Grit::Head (baked) |
| 78 | | def self.from_string(repo, line) |
| 79 | | full_name, id = line.split("\0") |
| 80 | | name = full_name.sub("#{HEAD_PREFIX}/", '') |
| 81 | | commit = Commit.create(repo, :id => id) |
| 82 | | self.new(name, commit) |
| 83 | | end |
| 84 | | |
| 85 | | # Pretty object inspection |
| 86 | | def inspect |
| 87 | | %Q{#<Grit::Head "#{@name}">} |
| 88 | | end |
| 89 | | end # Head |
| 90 | | |
| 91 | | end # Grit |
| toggle raw diff |
--- a/lib/grit/head.rb
+++ /dev/null
@@ -1,92 +0,0 @@
-module Grit
- HEAD_PREFIX = 'refs/heads'
-
- # A Head is a named reference to a Commit. Every Head instance contains a name
- # and a Commit object.
- #
- # r = Grit::Repo.new("/path/to/repo")
- # h = r.heads.first
- # h.name # => "master"
- # h.commit # => #<Grit::Commit "1c09f116cbc2cb4100fb6935bb162daa4723f455">
- # h.commit.id # => "1c09f116cbc2cb4100fb6935bb162daa4723f455"
- class Head
- attr_reader :name
- attr_reader :commit
-
- # Instantiate a new Head
- # +name+ is the name of the head
- # +commit+ is the Commit that the head points to
- #
- # Returns Grit::Head (baked)
- def initialize(name, commit)
- @name = name
- @commit = commit
- end
-
- # Find all Heads
- # +repo+ is the Repo
- # +options+ is a Hash of options
- #
- # Returns Grit::Head[] (baked)
- def self.find_all(repo, options = {})
- default_options = {:sort => "committerdate",
- :format => "%(refname)%00%(objectname)"}
-
- actual_options = default_options.merge(options)
-
- output = repo.git.for_each_ref(actual_options, HEAD_PREFIX)
-
- self.list_from_string(repo, output)
- end
-
- # Get the HEAD revision of the repo.
- # +repo+ is the Repo
- # +options+ is a Hash of options
- #
- # Returns Grit::Head (baked)
- def self.current(repo, options = {})
- head = File.open(File.join(repo.path, 'HEAD')).read.chomp
- if /ref: refs\/heads\/(.*)/.match(head)
- self.new($1, repo.git.rev_parse(options, 'HEAD'))
- end
- end
-
- # Parse out head information into an array of baked head objects
- # +repo+ is the Repo
- # +text+ is the text output from the git command
- #
- # Returns Grit::Head[] (baked)
- def self.list_from_string(repo, text)
- heads = []
-
- text.split("\n").each do |line|
- heads << self.from_string(repo, line)
- end
-
- heads
- end
-
- # Create a new Head instance from the given string.
- # +repo+ is the Repo
- # +line+ is the formatted head information
- #
- # Format
- # name: [a-zA-Z_/]+
- # <null byte>
- # id: [0-9A-Fa-f]{40}
- #
- # Returns Grit::Head (baked)
- def self.from_string(repo, line)
- full_name, id = line.split("\0")
- name = full_name.sub("#{HEAD_PREFIX}/", '')
- commit = Commit.create(repo, :id => id)
- self.new(name, commit)
- end
-
- # Pretty object inspection
- def inspect
- %Q{#<Grit::Head "#{@name}">}
- end
- end # Head
-
-end # Grit |
| |   |
| 1 | module Grit |
| 2 | |
| 3 | class Ref |
| 4 | |
| 5 | class << self |
| 6 | |
| 7 | # Find all Refs |
| 8 | # +repo+ is the Repo |
| 9 | # +options+ is a Hash of options |
| 10 | # |
| 11 | # Returns Grit::Ref[] (baked) |
| 12 | def find_all(repo, options = {}) |
| 13 | default_options = {:sort => "committerdate", |
| 14 | :format => "%(refname)%00%(objectname)"} |
| 15 | |
| 16 | actual_options = default_options.merge(options) |
| 17 | |
| 18 | output = repo.git.for_each_ref(actual_options, prefix) |
| 19 | |
| 20 | self.list_from_string(repo, output) |
| 21 | end |
| 22 | |
| 23 | # Parse out ref information into an array of baked refs objects |
| 24 | # +repo+ is the Repo |
| 25 | # +text+ is the text output from the git command |
| 26 | # |
| 27 | # Returns Grit::Ref[] (baked) |
| 28 | def list_from_string(repo, text) |
| 29 | refs = [] |
| 30 | |
| 31 | text.split("\n").each do |line| |
| 32 | refs << self.from_string(repo, line) |
| 33 | end |
| 34 | |
| 35 | refs.sort { | x, y | x.name <=> y.name } |
| 36 | end |
| 37 | |
| 38 | # Create a new Ref instance from the given string. |
| 39 | # +repo+ is the Repo |
| 40 | # +line+ is the formatted head information |
| 41 | # |
| 42 | # Format |
| 43 | # name: [a-zA-Z_/]+ |
| 44 | # <null byte> |
| 45 | # id: [0-9A-Fa-f]{40} |
| 46 | # |
| 47 | # Returns Grit::Ref (baked) |
| 48 | def from_string(repo, line) |
| 49 | full_name, id = line.split("\0") |
| 50 | name = full_name.sub("#{prefix}/", '') |
| 51 | commit = Commit.create(repo, :id => id) |
| 52 | self.new(name, commit) |
| 53 | end |
| 54 | |
| 55 | protected |
| 56 | |
| 57 | def prefix |
| 58 | "refs/#{name.to_s.gsub(/^.*::/, '').downcase}s" |
| 59 | end |
| 60 | |
| 61 | end |
| 62 | |
| 63 | attr_reader :name |
| 64 | attr_reader :commit |
| 65 | |
| 66 | # Instantiate a new Head |
| 67 | # +name+ is the name of the head |
| 68 | # +commit+ is the Commit that the head points to |
| 69 | # |
| 70 | # Returns Grit::Head (baked) |
| 71 | def initialize(name, commit) |
| 72 | @name = name |
| 73 | @commit = commit |
| 74 | end |
| 75 | |
| 76 | # Pretty object inspection |
| 77 | def inspect |
| 78 | %Q{#<#{self.class.name} "#{@name}">} |
| 79 | end |
| 80 | end # Ref |
| 81 | |
| 82 | # A Head is a named reference to a Commit. Every Head instance contains a name |
| 83 | # and a Commit object. |
| 84 | # |
| 85 | # r = Grit::Repo.new("/path/to/repo") |
| 86 | # h = r.heads.first |
| 87 | # h.name # => "master" |
| 88 | # h.commit # => #<Grit::Commit "1c09f116cbc2cb4100fb6935bb162daa4723f455"> |
| 89 | # h.commit.id # => "1c09f116cbc2cb4100fb6935bb162daa4723f455" |
| 90 | class Head < Ref |
| 91 | |
| 92 | # Get the HEAD revision of the repo. |
| 93 | # +repo+ is the Repo |
| 94 | # +options+ is a Hash of options |
| 95 | # |
| 96 | # Returns Grit::Head (baked) |
| 97 | def self.current(repo, options = {}) |
| 98 | head = File.open(File.join(repo.path, 'HEAD')).read.chomp |
| 99 | if /ref: refs\/heads\/(.*)/.match(head) |
| 100 | self.new($1, repo.git.rev_parse(options, 'HEAD')) |
| 101 | end |
| 102 | end |
| 103 | |
| 104 | end # Head |
| 105 | |
| 106 | class Tag < Ref ; end |
| 107 | |
| 108 | class Remote < Ref ; end |
| 109 | |
| 110 | end # Grit |
| toggle raw diff |
--- /dev/null
+++ b/lib/grit/ref.rb
@@ -0,0 +1,110 @@
+module Grit
+
+ class Ref
+
+ class << self
+
+ # Find all Refs
+ # +repo+ is the Repo
+ # +options+ is a Hash of options
+ #
+ # Returns Grit::Ref[] (baked)
+ def find_all(repo, options = {})
+ default_options = {:sort => "committerdate",
+ :format => "%(refname)%00%(objectname)"}
+
+ actual_options = default_options.merge(options)
+
+ output = repo.git.for_each_ref(actual_options, prefix)
+
+ self.list_from_string(repo, output)
+ end
+
+ # Parse out ref information into an array of baked refs objects
+ # +repo+ is the Repo
+ # +text+ is the text output from the git command
+ #
+ # Returns Grit::Ref[] (baked)
+ def list_from_string(repo, text)
+ refs = []
+
+ text.split("\n").each do |line|
+ refs << self.from_string(repo, line)
+ end
+
+ refs.sort { | x, y | x.name <=> y.name }
+ end
+
+ # Create a new Ref instance from the given string.
+ # +repo+ is the Repo
+ # +line+ is the formatted head information
+ #
+ # Format
+ # name: [a-zA-Z_/]+
+ # <null byte>
+ # id: [0-9A-Fa-f]{40}
+ #
+ # Returns Grit::Ref (baked)
+ def from_string(repo, line)
+ full_name, id = line.split("\0")
+ name = full_name.sub("#{prefix}/", '')
+ commit = Commit.create(repo, :id => id)
+ self.new(name, commit)
+ end
+
+ protected
+
+ def prefix
+ "refs/#{name.to_s.gsub(/^.*::/, '').downcase}s"
+ end
+
+ end
+
+ attr_reader :name
+ attr_reader :commit
+
+ # Instantiate a new Head
+ # +name+ is the name of the head
+ # +commit+ is the Commit that the head points to
+ #
+ # Returns Grit::Head (baked)
+ def initialize(name, commit)
+ @name = name
+ @commit = commit
+ end
+
+ # Pretty object inspection
+ def inspect
+ %Q{#<#{self.class.name} "#{@name}">}
+ end
+ end # Ref
+
+ # A Head is a named reference to a Commit. Every Head instance contains a name
+ # and a Commit object.
+ #
+ # r = Grit::Repo.new("/path/to/repo")
+ # h = r.heads.first
+ # h.name # => "master"
+ # h.commit # => #<Grit::Commit "1c09f116cbc2cb4100fb6935bb162daa4723f455">
+ # h.commit.id # => "1c09f116cbc2cb4100fb6935bb162daa4723f455"
+ class Head < Ref
+
+ # Get the HEAD revision of the repo.
+ # +repo+ is the Repo
+ # +options+ is a Hash of options
+ #
+ # Returns Grit::Head (baked)
+ def self.current(repo, options = {})
+ head = File.open(File.join(repo.path, 'HEAD')).read.chomp
+ if /ref: refs\/heads\/(.*)/.match(head)
+ self.new($1, repo.git.rev_parse(options, 'HEAD'))
+ end
+ end
+
+ end # Head
+
+ class Tag < Ref ; end
+
+ class Remote < Ref ; end
+
+end # Grit |
| |   |
| 0 | | module Grit |
| 1 | | |
| 2 | | class Tag |
| 3 | | attr_reader :name |
| 4 | | attr_reader :commit |
| 5 | | |
| 6 | | # Instantiate a new Tag |
| 7 | | # +name+ is the name of the head |
| 8 | | # +commit+ is the Commit that the head points to |
| 9 | | # |
| 10 | | # Returns Grit::Tag (baked) |
| 11 | | def initialize(name, commit) |
| 12 | | @name = name |
| 13 | | @commit = commit |
| 14 | | end |
| 15 | | |
| 16 | | # Find all Tags |
| 17 | | # +repo+ is the Repo |
| 18 | | # +options+ is a Hash of options |
| 19 | | # |
| 20 | | # Returns Grit::Tag[] (baked) |
| 21 | | def self.find_all(repo, options = {}) |
| 22 | | default_options = {:sort => "committerdate", |
| 23 | | :format => "%(refname)%00%(objectname)"} |
| 24 | | |
| 25 | | actual_options = default_options.merge(options) |
| 26 | | |
| 27 | | output = repo.git.for_each_ref(actual_options, "refs/tags") |
| 28 | | |
| 29 | | self.list_from_string(repo, output) |
| 30 | | end |
| 31 | | |
| 32 | | # Parse out tag information into an array of baked Tag objects |
| 33 | | # +repo+ is the Repo |
| 34 | | # +text+ is the text output from the git command |
| 35 | | # |
| 36 | | # Returns Grit::Tag[] (baked) |
| 37 | | def self.list_from_string(repo, text) |
| 38 | | tags = [] |
| 39 | | |
| 40 | | text.split("\n").each do |line| |
| 41 | | tags << self.from_string(repo, line) |
| 42 | | end |
| 43 | | |
| 44 | | tags |
| 45 | | end |
| 46 | | |
| 47 | | # Create a new Tag instance from the given string. |
| 48 | | # +repo+ is the Repo |
| 49 | | # +line+ is the formatted tag information |
| 50 | | # |
| 51 | | # Format |
| 52 | | # name: [a-zA-Z_/]+ |
| 53 | | # <null byte> |
| 54 | | # id: [0-9A-Fa-f]{40} |
| 55 | | # |
| 56 | | # Returns Grit::Tag (baked) |
| 57 | | def self.from_string(repo, line) |
| 58 | | full_name, id = line.split("\0") |
| 59 | | name = full_name.split("/").last |
| 60 | | commit = Commit.create(repo, :id => id) |
| 61 | | self.new(name, commit) |
| 62 | | end |
| 63 | | |
| 64 | | # Pretty object inspection |
| 65 | | def inspect |
| 66 | | %Q{#<Grit::Tag "#{@name}">} |
| 67 | | end |
| 68 | | end # Tag |
| 69 | | |
| 70 | | end # Grit |
| toggle raw diff |
--- a/lib/grit/tag.rb
+++ /dev/null
@@ -1,71 +0,0 @@
-module Grit
-
- class Tag
- attr_reader :name
- attr_reader :commit
-
- # Instantiate a new Tag
- # +name+ is the name of the head
- # +commit+ is the Commit that the head points to
- #
- # Returns Grit::Tag (baked)
- def initialize(name, commit)
- @name = name
- @commit = commit
- end
-
- # Find all Tags
- # +repo+ is the Repo
- # +options+ is a Hash of options
- #
- # Returns Grit::Tag[] (baked)
- def self.find_all(repo, options = {})
- default_options = {:sort => "committerdate",
- :format => "%(refname)%00%(objectname)"}
-
- actual_options = default_options.merge(options)
-
- output = repo.git.for_each_ref(actual_options, "refs/tags")
-
- self.list_from_string(repo, output)
- end
-
- # Parse out tag information into an array of baked Tag objects
- # +repo+ is the Repo
- # +text+ is the text output from the git command
- #
- # Returns Grit::Tag[] (baked)
- def self.list_from_string(repo, text)
- tags = []
-
- text.split("\n").each do |line|
- tags << self.from_string(repo, line)
- end
-
- tags
- end
-
- # Create a new Tag instance from the given string.
- # +repo+ is the Repo
- # +line+ is the formatted tag information
- #
- # Format
- # name: [a-zA-Z_/]+
- # <null byte>
- # id: [0-9A-Fa-f]{40}
- #
- # Returns Grit::Tag (baked)
- def self.from_string(repo, line)
- full_name, id = line.split("\0")
- name = full_name.split("/").last
- commit = Commit.create(repo, :id => id)
- self.new(name, commit)
- end
-
- # Pretty object inspection
- def inspect
- %Q{#<Grit::Tag "#{@name}">}
- end
- end # Tag
-
-end # Grit
\ No newline at end of file |
| |   |
| 4 | 4 | def setup |
| 5 | 5 | @r = Repo.new(GRIT_REPO) |
| 6 | 6 | end |
| 7 | | |
| 7 | |
| 8 | 8 | # new |
| 9 | | |
| 9 | |
| 10 | 10 | def test_new_should_raise_on_invalid_repo_location |
| 11 | 11 | assert_raise(InvalidGitRepositoryError) do |
| 12 | 12 | Repo.new("/tmp") |
| 13 | 13 | end |
| 14 | 14 | end |
| 15 | | |
| 15 | |
| 16 | 16 | def test_new_should_raise_on_non_existant_path |
| 17 | 17 | assert_raise(NoSuchPathError) do |
| 18 | 18 | Repo.new("/foobar") |
| 19 | 19 | end |
| 20 | 20 | end |
| 21 | | |
| 21 | |
| 22 | 22 | # descriptions |
| 23 | | |
| 23 | |
| 24 | 24 | def test_description |
| 25 | 25 | assert_equal "Unnamed repository; edit this file to name it for gitweb.", @r.description |
| 26 | 26 | end |
| 27 | | |
| 27 | |
| 28 | # refs |
| 29 | |
| 30 | def test_refs_should_return_array_of_ref_objects |
| 31 | @r.refs.each do |ref| |
| 32 | assert ref.is_a? Grit::Ref |
| 33 | end |
| 34 | end |
| 35 | |
| 28 | 36 | # heads |
| 29 | | |
| 37 | |
| 30 | 38 | def test_heads_should_return_array_of_head_objects |
| 31 | 39 | @r.heads.each do |head| |
| 32 | 40 | assert_equal Grit::Head, head.class |
| 33 | 41 | end |
| 34 | 42 | end |
| 35 | | |
| 43 | |
| 36 | 44 | def test_heads_should_populate_head_data |
| 37 | 45 | Git.any_instance.expects(:for_each_ref).returns(fixture('for_each_ref')) |
| 38 | | |
| 46 | |
| 39 | 47 | head = @r.heads.first |
| 40 | | |
| 48 | |
| 41 | 49 | assert_equal 'master', head.name |
| 42 | 50 | assert_equal '634396b2f541a9f2d58b00be1a07f0c358b999b3', head.commit.id |
| 43 | 51 | end |
| 44 | | |
| 52 | |
| 45 | 53 | # branches |
| 46 | | |
| 54 | |
| 47 | 55 | def test_branches |
| 48 | 56 | # same as heads |
| 49 | 57 | end |
| 50 | | |
| 58 | |
| 51 | 59 | # commits |
| 52 | | |
| 60 | |
| 53 | 61 | def test_commits |
| 54 | 62 | Git.any_instance.expects(:rev_list).returns(fixture('rev_list')) |
| 55 | | |
| 63 | |
| 56 | 64 | commits = @r.commits('master', 10) |
| 57 | | |
| 65 | |
| 58 | 66 | c = commits[0] |
| 59 | 67 | assert_equal '4c8124ffcf4039d292442eeccabdeca5af5c5017', c.id |
| 60 | 68 | assert_equal ["634396b2f541a9f2d58b00be1a07f0c358b999b3"], c.parents.map { |p| p.id } |
| … | … | |
| 74 | 74 | assert_equal "tom@mojombo.com", c.committer.email |
| 75 | 75 | assert_equal Time.at(1191999972), c.committed_date |
| 76 | 76 | assert_equal "implement Grit#heads", c.message |
| 77 | | |
| 77 | |
| 78 | 78 | c = commits[1] |
| 79 | 79 | assert_equal [], c.parents |
| 80 | | |
| 80 | |
| 81 | 81 | c = commits[2] |
| 82 | 82 | assert_equal ["6e64c55896aabb9a7d8e9f8f296f426d21a78c2c", "7f874954efb9ba35210445be456c74e037ba6af2"], c.parents.map { |p| p.id } |
| 83 | 83 | assert_equal "Merge branch 'site'\n\n * Some other stuff\n * just one more", c.message |
| 84 | 84 | assert_equal "Merge branch 'site'", c.short_message |
| 85 | 85 | end |
| 86 | | |
| 86 | |
| 87 | 87 | # commit_count |
| 88 | | |
| 88 | |
| 89 | 89 | def test_commit_count |
| 90 | 90 | Git.any_instance.expects(:rev_list).with({}, 'master').returns(fixture('rev_list_count')) |
| 91 | | |
| 91 | |
| 92 | 92 | assert_equal 655, @r.commit_count('master') |
| 93 | 93 | end |
| 94 | | |
| 94 | |
| 95 | 95 | # commit |
| 96 | | |
| 96 | |
| 97 | 97 | def test_commit |
| 98 | 98 | commit = @r.commit('634396b2f541a9f2d58b00be1a07f0c358b999b3') |
| 99 | | |
| 99 | |
| 100 | 100 | assert_equal "634396b2f541a9f2d58b00be1a07f0c358b999b3", commit.id |
| 101 | 101 | end |
| 102 | | |
| 102 | |
| 103 | 103 | # tree |
| 104 | | |
| 104 | |
| 105 | 105 | def test_tree |
| 106 | 106 | Git.any_instance.expects(:ls_tree).returns(fixture('ls_tree_a')) |
| 107 | 107 | tree = @r.tree('master') |
| 108 | | |
| 108 | |
| 109 | 109 | assert_equal 4, tree.contents.select { |c| c.instance_of?(Blob) }.size |
| 110 | 110 | assert_equal 3, tree.contents.select { |c| c.instance_of?(Tree) }.size |
| 111 | 111 | end |
| 112 | | |
| 112 | |
| 113 | 113 | # blob |
| 114 | | |
| 114 | |
| 115 | 115 | def test_blob |
| 116 | 116 | Git.any_instance.expects(:cat_file).returns(fixture('cat_file_blob')) |
| 117 | 117 | blob = @r.blob("abc") |
| 118 | 118 | assert_equal "Hello world", blob.data |
| 119 | 119 | end |
| 120 | | |
| 120 | |
| 121 | 121 | # init_bare |
| 122 | | |
| 122 | |
| 123 | 123 | def test_init_bare |
| 124 | 124 | Git.any_instance.expects(:init).returns(true) |
| 125 | 125 | Repo.expects(:new).with("/foo/bar.git") |
| 126 | 126 | Repo.init_bare("/foo/bar.git") |
| 127 | 127 | end |
| 128 | | |
| 128 | |
| 129 | 129 | def test_init_bare_with_options |
| 130 | 130 | Git.any_instance.expects(:init).with( |
| 131 | 131 | :template => "/baz/sweet").returns(true) |
| 132 | 132 | Repo.expects(:new).with("/foo/bar.git") |
| 133 | 133 | Repo.init_bare("/foo/bar.git", :template => "/baz/sweet") |
| 134 | 134 | end |
| 135 | | |
| 135 | |
| 136 | 136 | # fork_bare |
| 137 | | |
| 137 | |
| 138 | 138 | def test_fork_bare |
| 139 | 139 | Git.any_instance.expects(:clone).with( |
| 140 | | {:bare => true, :shared => true}, |
| 140 | {:bare => true, :shared => true}, |
| 141 | 141 | "#{absolute_project_path}/.git", |
| 142 | 142 | "/foo/bar.git").returns(nil) |
| 143 | 143 | Repo.expects(:new) |
| 144 | | |
| 144 | |
| 145 | 145 | @r.fork_bare("/foo/bar.git") |
| 146 | 146 | end |
| 147 | | |
| 147 | |
| 148 | 148 | def test_fork_bare_with_options |
| 149 | 149 | Git.any_instance.expects(:clone).with( |
| 150 | | {:bare => true, :shared => true, :template => '/awesome'}, |
| 150 | {:bare => true, :shared => true, :template => '/awesome'}, |
| 151 | 151 | "#{absolute_project_path}/.git", |
| 152 | 152 | "/foo/bar.git").returns(nil) |
| 153 | 153 | Repo.expects(:new) |
| 154 | | |
| 154 | |
| 155 | 155 | @r.fork_bare("/foo/bar.git", :template => '/awesome') |
| 156 | 156 | end |
| 157 | | |
| 157 | |
| 158 | 158 | # diff |
| 159 | | |
| 159 | |
| 160 | 160 | def test_diff |
| 161 | 161 | Git.any_instance.expects(:diff).with({}, 'master^', 'master', '--') |
| 162 | 162 | @r.diff('master^', 'master') |
| 163 | | |
| 163 | |
| 164 | 164 | Git.any_instance.expects(:diff).with({}, 'master^', 'master', '--', 'foo/bar') |
| 165 | 165 | @r.diff('master^', 'master', 'foo/bar') |
| 166 | | |
| 166 | |
| 167 | 167 | Git.any_instance.expects(:diff).with({}, 'master^', 'master', '--', 'foo/bar', 'foo/baz') |
| 168 | 168 | @r.diff('master^', 'master', 'foo/bar', 'foo/baz') |
| 169 | 169 | end |
| 170 | | |
| 170 | |
| 171 | 171 | # commit_diff |
| 172 | | |
| 172 | |
| 173 | 173 | def test_diff |
| 174 | 174 | Git.any_instance.expects(:diff).returns(fixture('diff_p')) |
| 175 | 175 | diffs = @r.commit_diff('master') |
| 176 | | |
| 176 | |
| 177 | 177 | assert_equal 15, diffs.size |
| 178 | 178 | end |
| 179 | | |
| 179 | |
| 180 | 180 | # init bare |
| 181 | | |
| 181 | |
| 182 | 182 | # archive |
| 183 | | |
| 183 | |
| 184 | 184 | def test_archive_tar |
| 185 | 185 | @r.archive_tar |
| 186 | 186 | end |
| 187 | | |
| 187 | |
| 188 | 188 | # archive_tar_gz |
| 189 | | |
| 189 | |
| 190 | 190 | def test_archive_tar_gz |
| 191 | 191 | @r.archive_tar_gz |
| 192 | 192 | end |
| 193 | | |
| 193 | |
| 194 | 194 | # enable_daemon_serve |
| 195 | | |
| 195 | |
| 196 | 196 | def test_enable_daemon_serve |
| 197 | 197 | FileUtils.expects(:touch).with(File.join(@r.path, 'git-daemon-export-ok')) |
| 198 | 198 | @r.enable_daemon_serve |
| 199 | 199 | end |
| 200 | | |
| 200 | |
| 201 | 201 | # disable_daemon_serve |
| 202 | | |
| 202 | |
| 203 | 203 | def test_disable_daemon_serve |
| 204 | 204 | FileUtils.expects(:rm_f).with(File.join(@r.path, 'git-daemon-export-ok')) |
| 205 | 205 | @r.disable_daemon_serve |
| 206 | 206 | end |
| 207 | | |
| 207 | |
| 208 | 208 | # alternates |
| 209 | | |
| 209 | |
| 210 | 210 | def test_alternates_with_two_alternates |
| 211 | 211 | File.expects(:exist?).with("#{absolute_project_path}/.git/objects/info/alternates").returns(true) |
| 212 | 212 | File.expects(:read).returns("/path/to/repo1/.git/objects\n/path/to/repo2.git/objects\n") |
| 213 | | |
| 213 | |
| 214 | 214 | assert_equal ["/path/to/repo1/.git/objects", "/path/to/repo2.git/objects"], @r.alternates |
| 215 | 215 | end |
| 216 | | |
| 216 | |
| 217 | 217 | def test_alternates_no_file |
| 218 | 218 | File.expects(:exist?).returns(false) |
| 219 | | |
| 219 | |
| 220 | 220 | assert_equal [], @r.alternates |
| 221 | 221 | end |
| 222 | | |
| 222 | |
| 223 | 223 | # alternates= |
| 224 | | |
| 224 | |
| 225 | 225 | def test_alternates_setter_ok |
| 226 | 226 | alts = %w{/path/to/repo.git/objects /path/to/repo2.git/objects} |
| 227 | | |
| 227 | |
| 228 | 228 | alts.each do |alt| |
| 229 | 229 | File.expects(:exist?).with(alt).returns(true) |
| 230 | 230 | end |
| 231 | | |
| 231 | |
| 232 | 232 | File.any_instance.expects(:write).with(alts.join("\n")) |
| 233 | | |
| 233 | |
| 234 | 234 | assert_nothing_raised do |
| 235 | 235 | @r.alternates = alts |
| 236 | 236 | end |
| 237 | 237 | end |
| 238 | | |
| 238 | |
| 239 | 239 | def test_alternates_setter_bad |
| 240 | 240 | alts = %w{/path/to/repo.git/objects} |
| 241 | | |
| 241 | |
| 242 | 242 | alts.each do |alt| |
| 243 | 243 | File.expects(:exist?).with(alt).returns(false) |
| 244 | 244 | end |
| 245 | | |
| 245 | |
| 246 | 246 | File.any_instance.expects(:write).never |
| 247 | | |
| 247 | |
| 248 | 248 | assert_raise RuntimeError do |
| 249 | 249 | @r.alternates = alts |
| 250 | 250 | end |
| 251 | 251 | end |
| 252 | | |
| 252 | |
| 253 | 253 | def test_alternates_setter_empty |
| 254 | 254 | File.expects(:delete) |
| 255 | | |
| 255 | |
| 256 | 256 | @r.alternates = [] |
| 257 | 257 | end |
| 258 | | |
| 258 | |
| 259 | 259 | # inspect |
| 260 | | |
| 260 | |
| 261 | 261 | def test_inspect |
| 262 | 262 | assert_equal %Q{#<Grit::Repo "#{File.expand_path(GRIT_REPO)}/.git">}, @r.inspect |
| 263 | 263 | end |
| toggle raw diff |
--- a/test/test_repo.rb
+++ b/test/test_repo.rb
@@ -4,57 +4,65 @@ class TestRepo < Test::Unit::TestCase
def setup
@r = Repo.new(GRIT_REPO)
end
-
+
# new
-
+
def test_new_should_raise_on_invalid_repo_location
assert_raise(InvalidGitRepositoryError) do
Repo.new("/tmp")
end
end
-
+
def test_new_should_raise_on_non_existant_path
assert_raise(NoSuchPathError) do
Repo.new("/foobar")
end
end
-
+
# descriptions
-
+
def test_description
assert_equal "Unnamed repository; edit this file to name it for gitweb.", @r.description
end
-
+
+ # refs
+
+ def test_refs_should_return_array_of_ref_objects
+ @r.refs.each do |ref|
+ assert ref.is_a? Grit::Ref
+ end
+ end
+
# heads
-
+
def test_heads_should_return_array_of_head_objects
@r.heads.each do |head|
assert_equal Grit::Head, head.class
end
end
-
+
def test_heads_should_populate_head_data
Git.any_instance.expects(:for_each_ref).returns(fixture('for_each_ref'))
-
+
head = @r.heads.first
-
+
assert_equal 'master', head.name
assert_equal '634396b2f541a9f2d58b00be1a07f0c358b999b3', head.commit.id
end
-
+
# branches
-
+
def test_branches
# same as heads
end
-
+
# commits
-
+
def test_commits
Git.any_instance.expects(:rev_list).returns(fixture('rev_list'))
-
+
commits = @r.commits('master', 10)
-
+
c = commits[0]
assert_equal '4c8124ffcf4039d292442eeccabdeca5af5c5017', c.id
assert_equal ["634396b2f541a9f2d58b00be1a07f0c358b999b3"], c.parents.map { |p| p.id }
@@ -66,190 +74,190 @@ class TestRepo < Test::Unit::TestCase
assert_equal "tom@mojombo.com", c.committer.email
assert_equal Time.at(1191999972), c.committed_date
assert_equal "implement Grit#heads", c.message
-
+
c = commits[1]
assert_equal [], c.parents
-
+
c = commits[2]
assert_equal ["6e64c55896aabb9a7d8e9f8f296f426d21a78c2c", "7f874954efb9ba35210445be456c74e037ba6af2"], c.parents.map { |p| p.id }
assert_equal "Merge branch 'site'\n\n * Some other stuff\n * just one more", c.message
assert_equal "Merge branch 'site'", c.short_message
end
-
+
# commit_count
-
+
def test_commit_count
Git.any_instance.expects(:rev_list).with({}, 'master').returns(fixture('rev_list_count'))
-
+
assert_equal 655, @r.commit_count('master')
end
-
+
# commit
-
+
def test_commit
commit = @r.commit('634396b2f541a9f2d58b00be1a07f0c358b999b3')
-
+
assert_equal "634396b2f541a9f2d58b00be1a07f0c358b999b3", commit.id
end
-
+
# tree
-
+
def test_tree
Git.any_instance.expects(:ls_tree).returns(fixture('ls_tree_a'))
tree = @r.tree('master')
-
+
assert_equal 4, tree.contents.select { |c| c.instance_of?(Blob) }.size
assert_equal 3, tree.contents.select { |c| c.instance_of?(Tree) }.size
end
-
+
# blob
-
+
def test_blob
Git.any_instance.expects(:cat_file).returns(fixture('cat_file_blob'))
blob = @r.blob("abc")
assert_equal "Hello world", blob.data
end
-
+
# init_bare
-
+
def test_init_bare
Git.any_instance.expects(:init).returns(true)
Repo.expects(:new).with("/foo/bar.git")
Repo.init_bare("/foo/bar.git")
end
-
+
def test_init_bare_with_options
Git.any_instance.expects(:init).with(
:template => "/baz/sweet").returns(true)
Repo.expects(:new).with("/foo/bar.git")
Repo.init_bare("/foo/bar.git", :template => "/baz/sweet")
end
-
+
# fork_bare
-
+
def test_fork_bare
Git.any_instance.expects(:clone).with(
- {:bare => true, :shared => true},
+ {:bare => true, :shared => true},
"#{absolute_project_path}/.git",
"/foo/bar.git").returns(nil)
Repo.expects(:new)
-
+
@r.fork_bare("/foo/bar.git")
end
-
+
def test_fork_bare_with_options
Git.any_instance.expects(:clone).with(
- {:bare => true, :shared => true, :template => '/awesome'},
+ {:bare => true, :shared => true, :template => '/awesome'},
"#{absolute_project_path}/.git",
"/foo/bar.git").returns(nil)
Repo.expects(:new)
-
+
@r.fork_bare("/foo/bar.git", :template => '/awesome')
end
-
+
# diff
-
+
def test_diff
Git.any_instance.expects(:diff).with({}, 'master^', 'master', '--')
@r.diff('master^', 'master')
-
+
Git.any_instance.expects(:diff).with({}, 'master^', 'master', '--', 'foo/bar')
@r.diff('master^', 'master', 'foo/bar')
-
+
Git.any_instance.expects(:diff).with({}, 'master^', 'master', '--', 'foo/bar', 'foo/baz')
@r.diff('master^', 'master', 'foo/bar', 'foo/baz')
end
-
+
# commit_diff
-
+
def test_diff
Git.any_instance.expects(:diff).returns(fixture('diff_p'))
diffs = @r.commit_diff('master')
-
+
assert_equal 15, diffs.size
end
-
+
# init bare
-
+
# archive
-
+
def test_archive_tar
@r.archive_tar
end
-
+
# archive_tar_gz
-
+
def test_archive_tar_gz
@r.archive_tar_gz
end
-
+
# enable_daemon_serve
-
+
def test_enable_daemon_serve
FileUtils.expects(:touch).with(File.join(@r.path, 'git-daemon-export-ok'))
@r.enable_daemon_serve
end
-
+
# disable_daemon_serve
-
+
def test_disable_daemon_serve
FileUtils.expects(:rm_f).with(File.join(@r.path, 'git-daemon-export-ok'))
@r.disable_daemon_serve
end
-
+
# alternates
-
+
def test_alternates_with_two_alternates
File.expects(:exist?).with("#{absolute_project_path}/.git/objects/info/alternates").returns(true)
File.expects(:read).returns("/path/to/repo1/.git/objects\n/path/to/repo2.git/objects\n")
-
+
assert_equal ["/path/to/repo1/.git/objects", "/path/to/repo2.git/objects"], @r.alternates
end
-
+
def test_alternates_no_file
File.expects(:exist?).returns(false)
-
+
assert_equal [], @r.alternates
end
-
+
# alternates=
-
+
def test_alternates_setter_ok
alts = %w{/path/to/repo.git/objects /path/to/repo2.git/objects}
-
+
alts.each do |alt|
File.expects(:exist?).with(alt).returns(true)
end
-
+
File.any_instance.expects(:write).with(alts.join("\n"))
-
+
assert_nothing_raised do
@r.alternates = alts
end
end
-
+
def test_alternates_setter_bad
alts = %w{/path/to/repo.git/objects}
-
+
alts.each do |alt|
File.expects(:exist?).with(alt).returns(false)
end
-
+
File.any_instance.expects(:write).never
-
+
assert_raise RuntimeError do
@r.alternates = alts
end
end
-
+
def test_alternates_setter_empty
File.expects(:delete)
-
+
@r.alternates = []
end
-
+
# inspect
-
+
def test_inspect
assert_equal %Q{#<Grit::Repo "#{File.expand_path(GRIT_REPO)}/.git">}, @r.inspect
end |