grit 0.7.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of grit might be problematic. Click here for more details.

data/History.txt ADDED
@@ -0,0 +1,5 @@
1
+ == 0.7.0 / 2008-01-07
2
+
3
+ * 1 major enhancement
4
+ * First public release!
5
+
data/Manifest.txt ADDED
@@ -0,0 +1,36 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ lib/grit.rb
6
+ lib/grit/actor.rb
7
+ lib/grit/blob.rb
8
+ lib/grit/commit.rb
9
+ lib/grit/diff.rb
10
+ lib/grit/errors.rb
11
+ lib/grit/git.rb
12
+ lib/grit/head.rb
13
+ lib/grit/lazy.rb
14
+ lib/grit/repo.rb
15
+ lib/grit/tree.rb
16
+ test/fixtures/blame
17
+ test/fixtures/cat_file_blob
18
+ test/fixtures/cat_file_blob_size
19
+ test/fixtures/diff_p
20
+ test/fixtures/for_each_ref
21
+ test/fixtures/ls_tree_a
22
+ test/fixtures/ls_tree_b
23
+ test/fixtures/rev_list
24
+ test/fixtures/rev_list_single
25
+ test/fixtures/rev_parse
26
+ test/helper.rb
27
+ test/profile.rb
28
+ test/suite.rb
29
+ test/test_actor.rb
30
+ test/test_blob.rb
31
+ test/test_commit.rb
32
+ test/test_git.rb
33
+ test/test_head.rb
34
+ test/test_reality.rb
35
+ test/test_repo.rb
36
+ test/test_tree.rb
data/README.txt ADDED
@@ -0,0 +1,201 @@
1
+ grit
2
+ by Tom Preston-Werner, Chris Wanstrath
3
+ http://grit.rubyforge.org
4
+
5
+ == DESCRIPTION:
6
+
7
+ Grit is a Ruby library for extracting information from a git repository in and
8
+ object oriented manner.
9
+
10
+ == REQUIREMENTS:
11
+
12
+ * git (http://git.or.cz) tested with 1.5.3.4
13
+
14
+ == INSTALL:
15
+
16
+ sudo gem install grit
17
+
18
+ == USAGE:
19
+
20
+ Grit gives you object model access to your git repository. Once you have
21
+ created a repository object, you can traverse it to find parent commit(s),
22
+ trees, blobs, etc.
23
+
24
+ = Initialize a Repo object
25
+
26
+ The first step is to create a Grit::Repo object to represent your repo. I
27
+ include the Grit module so reduce typing.
28
+
29
+ include Grit
30
+ repo = Repo.new("/Users/tom/dev/grit")
31
+
32
+ In the above example, the directory /Users/tom/dev/grit is my working
33
+ repo and contains the .git directory. You can also initialize Grit with a
34
+ bare repo.
35
+
36
+ repo = Repo.new("/var/git/grit.git")
37
+
38
+ = Getting a list of commits
39
+
40
+ From the Repo object, you can get a list of commits as an array of Commit
41
+ objects.
42
+
43
+ repo.commits
44
+ # => [#<Grit::Commit "e80bbd2ce67651aa18e57fb0b43618ad4baf7750">,
45
+ #<Grit::Commit "91169e1f5fa4de2eaea3f176461f5dc784796769">,
46
+ #<Grit::Commit "038af8c329ef7c1bae4568b98bd5c58510465493">,
47
+ #<Grit::Commit "40d3057d09a7a4d61059bca9dca5ae698de58cbe">,
48
+ #<Grit::Commit "4ea50f4754937bf19461af58ce3b3d24c77311d9">]
49
+
50
+ Called without arguments, Repo#commits returns a list of up to ten commits
51
+ reachable by the master branch (starting at the latest commit). You can ask
52
+ for commits beginning at a different branch, commit, tag, etc.
53
+
54
+ repo.commits('mybranch')
55
+ repo.commits('40d3057d09a7a4d61059bca9dca5ae698de58cbe')
56
+ repo.commits('v0.1')
57
+
58
+ You can specify the maximum number of commits to return.
59
+
60
+ repo.commits('master', 100)
61
+
62
+ If you need paging, you can specify a number of commits to skip.
63
+
64
+ repo.commits('master', 10, 20)
65
+
66
+ The above will return commits 21-30 from the commit list.
67
+
68
+ = The Commit object
69
+
70
+ Commit objects contain information about that commit.
71
+
72
+ head = repo.commits.first
73
+
74
+ head.id
75
+ # => "e80bbd2ce67651aa18e57fb0b43618ad4baf7750"
76
+
77
+ head.parents
78
+ # => [#<Grit::Commit "91169e1f5fa4de2eaea3f176461f5dc784796769">]
79
+
80
+ head.tree
81
+ # => #<Grit::Tree "3536eb9abac69c3e4db583ad38f3d30f8db4771f">
82
+
83
+ head.author
84
+ # => #<Grit::Actor "Tom Preston-Werner <tom@mojombo.com>">
85
+
86
+ head.authored_date
87
+ # => Wed Oct 24 22:02:31 -0700 2007
88
+
89
+ head.committer
90
+ # => #<Grit::Actor "Tom Preston-Werner <tom@mojombo.com>">
91
+
92
+ head.committed_date
93
+ # => Wed Oct 24 22:02:31 -0700 2007
94
+
95
+ head.message
96
+ # => "add Actor inspect"
97
+
98
+ You can traverse a commit's ancestry by chaining calls to #parents.
99
+
100
+ repo.commits.first.parents[0].parents[0].parents[0]
101
+
102
+ The above corresponds to master^^^ or master~3 in git parlance.
103
+
104
+ = The Tree object
105
+
106
+ A tree records pointers to the contents of a directory. Let's say you want
107
+ the root tree of the latest commit on the master branch.
108
+
109
+ tree = repo.commits.first.tree
110
+ # => #<Grit::Tree "3536eb9abac69c3e4db583ad38f3d30f8db4771f">
111
+
112
+ tree.id
113
+ # => "3536eb9abac69c3e4db583ad38f3d30f8db4771f"
114
+
115
+ Once you have a tree, you can get the contents.
116
+
117
+ contents = tree.contents
118
+ # => [#<Grit::Blob "4ebc8aea50e0a67e000ba29a30809d0a7b9b2666">,
119
+ #<Grit::Blob "81d2c27608b352814cbe979a6acd678d30219678">,
120
+ #<Grit::Tree "c3d07b0083f01a6e1ac969a0f32b8d06f20c62e5">,
121
+ #<Grit::Tree "4d00fe177a8407dbbc64a24dbfc564762c0922d8">]
122
+
123
+ This tree contains two Blob objects and two Tree objects. The trees are
124
+ subdirectories and the blobs are files. Trees below the root have additional
125
+ attributes.
126
+
127
+ contents.last.name
128
+ # => "lib"
129
+
130
+ contents.last.mode
131
+ # => "040000"
132
+
133
+ There is a convenience method that allows you to get a named sub-object
134
+ from a tree.
135
+
136
+ tree/"lib"
137
+ # => #<Grit::Tree "e74893a3d8a25cbb1367cf241cc741bfd503c4b2">
138
+
139
+ You can also get a tree directly from the repo if you know its name.
140
+
141
+ repo.tree
142
+ # => #<Grit::Tree "master">
143
+
144
+ repo.tree("91169e1f5fa4de2eaea3f176461f5dc784796769")
145
+ # => #<Grit::Tree "91169e1f5fa4de2eaea3f176461f5dc784796769">
146
+
147
+ = The Blob object
148
+
149
+ A blob represents a file. Trees often contain blobs.
150
+
151
+ blob = tree.contents.first
152
+ # => #<Grit::Blob "4ebc8aea50e0a67e000ba29a30809d0a7b9b2666">
153
+
154
+ A blob has certain attributes.
155
+
156
+ blob.id
157
+ # => "4ebc8aea50e0a67e000ba29a30809d0a7b9b2666"
158
+
159
+ blob.name
160
+ # => "README.txt"
161
+
162
+ blob.mode
163
+ # => "100644"
164
+
165
+ blob.size
166
+ # => 7726
167
+
168
+ You can get the data of a blob as a string.
169
+
170
+ blob.data
171
+ # => "Grit is a library to ..."
172
+
173
+ You can also get a blob directly from the repo if you know its name.
174
+
175
+ repo.blob("4ebc8aea50e0a67e000ba29a30809d0a7b9b2666")
176
+ # => #<Grit::Blob "4ebc8aea50e0a67e000ba29a30809d0a7b9b2666">
177
+
178
+ == LICENSE:
179
+
180
+ (The MIT License)
181
+
182
+ Copyright (c) 2007 Tom Preston-Werner
183
+
184
+ Permission is hereby granted, free of charge, to any person obtaining
185
+ a copy of this software and associated documentation files (the
186
+ 'Software'), to deal in the Software without restriction, including
187
+ without limitation the rights to use, copy, modify, merge, publish,
188
+ distribute, sublicense, and/or sell copies of the Software, and to
189
+ permit persons to whom the Software is furnished to do so, subject to
190
+ the following conditions:
191
+
192
+ The above copyright notice and this permission notice shall be
193
+ included in all copies or substantial portions of the Software.
194
+
195
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
196
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
197
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
198
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
199
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
200
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
201
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ require 'rubygems'
2
+ require 'hoe'
3
+ require './lib/grit.rb'
4
+
5
+ Hoe.new('grit', Grit::VERSION) do |p|
6
+ p.rubyforge_name = 'grit'
7
+ p.author = 'Tom Preston-Werner'
8
+ p.email = 'tom@rubyisawesome.com'
9
+ p.summary = 'Object model interface to a git repo'
10
+ p.description = p.paragraphs_of('README.txt', 2..2).join("\n\n")
11
+ p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[2..-1].map { |u| u.strip }
12
+ p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
13
+ end
14
+
15
+ desc "Open an irb session preloaded with this library"
16
+ task :console do
17
+ sh "irb -rubygems -r ./lib/grit.rb"
18
+ end
19
+
20
+ task :coverage do
21
+ system("rm -fr coverage")
22
+ system("rcov test/test_*.rb")
23
+ system("open coverage/index.html")
24
+ end
data/lib/grit.rb ADDED
@@ -0,0 +1,31 @@
1
+ $:.unshift File.dirname(__FILE__) # For use/testing when no gem is installed
2
+
3
+ # core
4
+
5
+ # stdlib
6
+
7
+ # third party
8
+ require 'rubygems'
9
+ require 'mime/types'
10
+
11
+ # internal requires
12
+ require 'grit/lazy'
13
+ require 'grit/errors'
14
+ require 'grit/git'
15
+ require 'grit/head'
16
+ require 'grit/commit'
17
+ require 'grit/tree'
18
+ require 'grit/blob'
19
+ require 'grit/actor'
20
+ require 'grit/diff'
21
+ require 'grit/repo'
22
+
23
+ module Grit
24
+ class << self
25
+ attr_accessor :debug
26
+ end
27
+
28
+ self.debug = false
29
+
30
+ VERSION = '0.7.0'
31
+ end
data/lib/grit/actor.rb ADDED
@@ -0,0 +1,36 @@
1
+ module Grit
2
+
3
+ class Actor
4
+ attr_reader :name
5
+ attr_reader :email
6
+
7
+ def initialize(name, email)
8
+ @name = name
9
+ @email = email
10
+ end
11
+ alias_method :to_s, :name
12
+
13
+ # Create an Actor from a string.
14
+ # +str+ is the string, which is expected to be in regular git format
15
+ #
16
+ # Format
17
+ # John Doe <jdoe@example.com>
18
+ #
19
+ # Returns Actor
20
+ def self.from_string(str)
21
+ case str
22
+ when /<.+>/
23
+ m, name, email = *str.match(/(.*) <(.+?)>/)
24
+ return self.new(name, email)
25
+ else
26
+ return self.new(str, nil)
27
+ end
28
+ end
29
+
30
+ # Pretty object inspection
31
+ def inspect
32
+ %Q{#<Grit::Actor "#{@name} <#{@email}>">}
33
+ end
34
+ end # Actor
35
+
36
+ end # Grit
data/lib/grit/blob.rb ADDED
@@ -0,0 +1,117 @@
1
+ module Grit
2
+
3
+ class Blob
4
+ DEFAULT_MIME_TYPE = "text/plain"
5
+
6
+ attr_reader :id
7
+ attr_reader :mode
8
+ attr_reader :name
9
+
10
+ # Create an unbaked Blob containing just the specified attributes
11
+ # +repo+ is the Repo
12
+ # +atts+ is a Hash of instance variable data
13
+ #
14
+ # Returns Grit::Blob (unbaked)
15
+ def self.create(repo, atts)
16
+ self.allocate.create_initialize(repo, atts)
17
+ end
18
+
19
+ # Initializer for Blob.create
20
+ # +repo+ is the Repo
21
+ # +atts+ is a Hash of instance variable data
22
+ #
23
+ # Returns Grit::Blob (unbaked)
24
+ def create_initialize(repo, atts)
25
+ @repo = repo
26
+ atts.each do |k, v|
27
+ instance_variable_set("@#{k}".to_sym, v)
28
+ end
29
+ self
30
+ end
31
+
32
+ # The size of this blob in bytes
33
+ #
34
+ # Returns Integer
35
+ def size
36
+ @size ||= @repo.git.cat_file({:s => true}, id).chomp.to_i
37
+ end
38
+
39
+ # The binary contents of this blob.
40
+ #
41
+ # Returns String
42
+ def data
43
+ @data ||= @repo.git.cat_file({:p => true}, id)
44
+ end
45
+
46
+ # The mime type of this file (based on the filename)
47
+ #
48
+ # Returns String
49
+ def mime_type
50
+ guesses = MIME::Types.type_for(self.name) rescue []
51
+ guesses.first ? guesses.first.simplified : DEFAULT_MIME_TYPE
52
+ end
53
+
54
+ # The blame information for the given file at the given commit
55
+ #
56
+ # Returns Array: [Grit::Commit, Array: [<line>]]
57
+ def self.blame(repo, commit, file)
58
+ data = repo.git.blame({:p => true}, commit, '--', file)
59
+
60
+ commits = {}
61
+ blames = []
62
+ info = nil
63
+
64
+ data.split("\n").each do |line|
65
+ parts = line.split(/\s+/, 2)
66
+ case parts.first
67
+ when /^[0-9A-Fa-f]{40}$/
68
+ case line
69
+ when /^([0-9A-Fa-f]{40}) (\d+) (\d+) (\d+)$/
70
+ _, id, origin_line, final_line, group_lines = *line.match(/^([0-9A-Fa-f]{40}) (\d+) (\d+) (\d+)$/)
71
+ info = {:id => id}
72
+ blames << [nil, []]
73
+ when /^([0-9A-Fa-f]{40}) (\d+) (\d+)$/
74
+ _, id, origin_line, final_line = *line.match(/^([0-9A-Fa-f]{40}) (\d+) (\d+)$/)
75
+ info = {:id => id}
76
+ end
77
+ when /^(author|committer)/
78
+ case parts.first
79
+ when /^(.+)-mail$/
80
+ info["#{$1}_email".intern] = parts.last
81
+ when /^(.+)-time$/
82
+ info["#{$1}_date".intern] = Time.at(parts.last.to_i)
83
+ when /^(author|committer)$/
84
+ info[$1.intern] = parts.last
85
+ end
86
+ when /^filename/
87
+ info[:filename] = parts.last
88
+ when /^summary/
89
+ info[:summary] = parts.last
90
+ when ''
91
+ c = commits[info[:id]]
92
+ unless c
93
+ c = Commit.create(repo, :id => info[:id],
94
+ :author => Actor.from_string(info[:author] + ' ' + info[:author_email]),
95
+ :authored_date => info[:author_date],
96
+ :committer => Actor.from_string(info[:committer] + ' ' + info[:committer_email]),
97
+ :committed_date => info[:committer_date],
98
+ :message => info[:summary])
99
+ commits[info[:id]] = c
100
+ end
101
+ _, text = *line.match(/^\t(.*)$/)
102
+ blames.last[0] = c
103
+ blames.last[1] << text
104
+ info = nil
105
+ end
106
+ end
107
+
108
+ blames
109
+ end
110
+
111
+ # Pretty object inspection
112
+ def inspect
113
+ %Q{#<Grit::Blob "#{@id}">}
114
+ end
115
+ end # Blob
116
+
117
+ end # Grit