mojombo-grit 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +6 -0
- data/Manifest.txt +53 -0
- data/README.txt +213 -0
- data/Rakefile +29 -0
- data/grit.gemspec +16 -0
- data/lib/grit/actor.rb +36 -0
- data/lib/grit/blob.rb +117 -0
- data/lib/grit/commit.rb +208 -0
- data/lib/grit/config.rb +44 -0
- data/lib/grit/diff.rb +70 -0
- data/lib/grit/errors.rb +7 -0
- data/lib/grit/git.rb +112 -0
- data/lib/grit/head.rb +92 -0
- data/lib/grit/lazy.rb +31 -0
- data/lib/grit/repo.rb +298 -0
- data/lib/grit/tag.rb +71 -0
- data/lib/grit/tree.rb +99 -0
- data/lib/mojombo-grit.rb +37 -0
- data/test/fixtures/blame +131 -0
- data/test/fixtures/cat_file_blob +1 -0
- data/test/fixtures/cat_file_blob_size +1 -0
- data/test/fixtures/diff_2 +54 -0
- data/test/fixtures/diff_2f +19 -0
- data/test/fixtures/diff_f +15 -0
- data/test/fixtures/diff_i +201 -0
- data/test/fixtures/diff_mode_only +1152 -0
- data/test/fixtures/diff_new_mode +17 -0
- data/test/fixtures/diff_p +610 -0
- data/test/fixtures/for_each_ref +0 -0
- data/test/fixtures/for_each_ref_tags +0 -0
- data/test/fixtures/ls_tree_a +7 -0
- data/test/fixtures/ls_tree_b +2 -0
- data/test/fixtures/ls_tree_commit +3 -0
- data/test/fixtures/rev_list +26 -0
- data/test/fixtures/rev_list_count +655 -0
- data/test/fixtures/rev_list_single +7 -0
- data/test/fixtures/rev_parse +1 -0
- data/test/fixtures/show_empty_commit +6 -0
- data/test/fixtures/simple_config +2 -0
- data/test/helper.rb +17 -0
- data/test/profile.rb +21 -0
- data/test/suite.rb +6 -0
- data/test/test_actor.rb +35 -0
- data/test/test_blob.rb +74 -0
- data/test/test_commit.rb +182 -0
- data/test/test_config.rb +58 -0
- data/test/test_diff.rb +18 -0
- data/test/test_git.rb +52 -0
- data/test/test_head.rb +22 -0
- data/test/test_real.rb +17 -0
- data/test/test_reality.rb +17 -0
- data/test/test_repo.rb +270 -0
- data/test/test_tag.rb +29 -0
- data/test/test_tree.rb +91 -0
- metadata +128 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
History.txt
|
2
|
+
Manifest.txt
|
3
|
+
README.txt
|
4
|
+
Rakefile
|
5
|
+
grit.gemspec
|
6
|
+
lib/grit.rb
|
7
|
+
lib/grit/actor.rb
|
8
|
+
lib/grit/blob.rb
|
9
|
+
lib/grit/commit.rb
|
10
|
+
lib/grit/config.rb
|
11
|
+
lib/grit/diff.rb
|
12
|
+
lib/grit/errors.rb
|
13
|
+
lib/grit/git.rb
|
14
|
+
lib/grit/head.rb
|
15
|
+
lib/grit/lazy.rb
|
16
|
+
lib/grit/repo.rb
|
17
|
+
lib/grit/tag.rb
|
18
|
+
lib/grit/tree.rb
|
19
|
+
test/fixtures/blame
|
20
|
+
test/fixtures/cat_file_blob
|
21
|
+
test/fixtures/cat_file_blob_size
|
22
|
+
test/fixtures/diff_2
|
23
|
+
test/fixtures/diff_2f
|
24
|
+
test/fixtures/diff_f
|
25
|
+
test/fixtures/diff_i
|
26
|
+
test/fixtures/diff_mode_only
|
27
|
+
test/fixtures/diff_new_mode
|
28
|
+
test/fixtures/diff_p
|
29
|
+
test/fixtures/for_each_ref
|
30
|
+
test/fixtures/for_each_ref_tags
|
31
|
+
test/fixtures/ls_tree_a
|
32
|
+
test/fixtures/ls_tree_b
|
33
|
+
test/fixtures/ls_tree_commit
|
34
|
+
test/fixtures/rev_list
|
35
|
+
test/fixtures/rev_list_count
|
36
|
+
test/fixtures/rev_list_single
|
37
|
+
test/fixtures/rev_parse
|
38
|
+
test/fixtures/show_empty_commit
|
39
|
+
test/fixtures/simple_config
|
40
|
+
test/helper.rb
|
41
|
+
test/profile.rb
|
42
|
+
test/suite.rb
|
43
|
+
test/test_actor.rb
|
44
|
+
test/test_blob.rb
|
45
|
+
test/test_commit.rb
|
46
|
+
test/test_config.rb
|
47
|
+
test/test_diff.rb
|
48
|
+
test/test_git.rb
|
49
|
+
test/test_head.rb
|
50
|
+
test/test_reality.rb
|
51
|
+
test/test_repo.rb
|
52
|
+
test/test_tag.rb
|
53
|
+
test/test_tree.rb
|
data/README.txt
ADDED
@@ -0,0 +1,213 @@
|
|
1
|
+
grit
|
2
|
+
by Tom Preston-Werner, Chris Wanstrath
|
3
|
+
http://github.com/mojombo/grit
|
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
|
+
$ gem sources -a http://gems.github.com/ (you only need to do this once)
|
17
|
+
$ gem install mojombo-grit
|
18
|
+
|
19
|
+
== SOURCE:
|
20
|
+
|
21
|
+
Grit's git repo is available on GitHub, which can be browsed at:
|
22
|
+
|
23
|
+
http://github.com/mojombo/grit
|
24
|
+
|
25
|
+
and cloned from:
|
26
|
+
|
27
|
+
git://github.com/mojombo/grit.git
|
28
|
+
|
29
|
+
== USAGE:
|
30
|
+
|
31
|
+
Grit gives you object model access to your git repository. Once you have
|
32
|
+
created a repository object, you can traverse it to find parent commit(s),
|
33
|
+
trees, blobs, etc.
|
34
|
+
|
35
|
+
= Initialize a Repo object
|
36
|
+
|
37
|
+
The first step is to create a Grit::Repo object to represent your repo. I
|
38
|
+
include the Grit module so reduce typing.
|
39
|
+
|
40
|
+
require 'mojombo-grit'
|
41
|
+
include Grit
|
42
|
+
repo = Repo.new("/Users/tom/dev/grit")
|
43
|
+
|
44
|
+
In the above example, the directory /Users/tom/dev/grit is my working
|
45
|
+
repo and contains the .git directory. You can also initialize Grit with a
|
46
|
+
bare repo.
|
47
|
+
|
48
|
+
repo = Repo.new("/var/git/grit.git")
|
49
|
+
|
50
|
+
= Getting a list of commits
|
51
|
+
|
52
|
+
From the Repo object, you can get a list of commits as an array of Commit
|
53
|
+
objects.
|
54
|
+
|
55
|
+
repo.commits
|
56
|
+
# => [#<Grit::Commit "e80bbd2ce67651aa18e57fb0b43618ad4baf7750">,
|
57
|
+
#<Grit::Commit "91169e1f5fa4de2eaea3f176461f5dc784796769">,
|
58
|
+
#<Grit::Commit "038af8c329ef7c1bae4568b98bd5c58510465493">,
|
59
|
+
#<Grit::Commit "40d3057d09a7a4d61059bca9dca5ae698de58cbe">,
|
60
|
+
#<Grit::Commit "4ea50f4754937bf19461af58ce3b3d24c77311d9">]
|
61
|
+
|
62
|
+
Called without arguments, Repo#commits returns a list of up to ten commits
|
63
|
+
reachable by the master branch (starting at the latest commit). You can ask
|
64
|
+
for commits beginning at a different branch, commit, tag, etc.
|
65
|
+
|
66
|
+
repo.commits('mybranch')
|
67
|
+
repo.commits('40d3057d09a7a4d61059bca9dca5ae698de58cbe')
|
68
|
+
repo.commits('v0.1')
|
69
|
+
|
70
|
+
You can specify the maximum number of commits to return.
|
71
|
+
|
72
|
+
repo.commits('master', 100)
|
73
|
+
|
74
|
+
If you need paging, you can specify a number of commits to skip.
|
75
|
+
|
76
|
+
repo.commits('master', 10, 20)
|
77
|
+
|
78
|
+
The above will return commits 21-30 from the commit list.
|
79
|
+
|
80
|
+
= The Commit object
|
81
|
+
|
82
|
+
Commit objects contain information about that commit.
|
83
|
+
|
84
|
+
head = repo.commits.first
|
85
|
+
|
86
|
+
head.id
|
87
|
+
# => "e80bbd2ce67651aa18e57fb0b43618ad4baf7750"
|
88
|
+
|
89
|
+
head.parents
|
90
|
+
# => [#<Grit::Commit "91169e1f5fa4de2eaea3f176461f5dc784796769">]
|
91
|
+
|
92
|
+
head.tree
|
93
|
+
# => #<Grit::Tree "3536eb9abac69c3e4db583ad38f3d30f8db4771f">
|
94
|
+
|
95
|
+
head.author
|
96
|
+
# => #<Grit::Actor "Tom Preston-Werner <tom@mojombo.com>">
|
97
|
+
|
98
|
+
head.authored_date
|
99
|
+
# => Wed Oct 24 22:02:31 -0700 2007
|
100
|
+
|
101
|
+
head.committer
|
102
|
+
# => #<Grit::Actor "Tom Preston-Werner <tom@mojombo.com>">
|
103
|
+
|
104
|
+
head.committed_date
|
105
|
+
# => Wed Oct 24 22:02:31 -0700 2007
|
106
|
+
|
107
|
+
head.message
|
108
|
+
# => "add Actor inspect"
|
109
|
+
|
110
|
+
You can traverse a commit's ancestry by chaining calls to #parents.
|
111
|
+
|
112
|
+
repo.commits.first.parents[0].parents[0].parents[0]
|
113
|
+
|
114
|
+
The above corresponds to master^^^ or master~3 in git parlance.
|
115
|
+
|
116
|
+
= The Tree object
|
117
|
+
|
118
|
+
A tree records pointers to the contents of a directory. Let's say you want
|
119
|
+
the root tree of the latest commit on the master branch.
|
120
|
+
|
121
|
+
tree = repo.commits.first.tree
|
122
|
+
# => #<Grit::Tree "3536eb9abac69c3e4db583ad38f3d30f8db4771f">
|
123
|
+
|
124
|
+
tree.id
|
125
|
+
# => "3536eb9abac69c3e4db583ad38f3d30f8db4771f"
|
126
|
+
|
127
|
+
Once you have a tree, you can get the contents.
|
128
|
+
|
129
|
+
contents = tree.contents
|
130
|
+
# => [#<Grit::Blob "4ebc8aea50e0a67e000ba29a30809d0a7b9b2666">,
|
131
|
+
#<Grit::Blob "81d2c27608b352814cbe979a6acd678d30219678">,
|
132
|
+
#<Grit::Tree "c3d07b0083f01a6e1ac969a0f32b8d06f20c62e5">,
|
133
|
+
#<Grit::Tree "4d00fe177a8407dbbc64a24dbfc564762c0922d8">]
|
134
|
+
|
135
|
+
This tree contains two Blob objects and two Tree objects. The trees are
|
136
|
+
subdirectories and the blobs are files. Trees below the root have additional
|
137
|
+
attributes.
|
138
|
+
|
139
|
+
contents.last.name
|
140
|
+
# => "lib"
|
141
|
+
|
142
|
+
contents.last.mode
|
143
|
+
# => "040000"
|
144
|
+
|
145
|
+
There is a convenience method that allows you to get a named sub-object
|
146
|
+
from a tree.
|
147
|
+
|
148
|
+
tree/"lib"
|
149
|
+
# => #<Grit::Tree "e74893a3d8a25cbb1367cf241cc741bfd503c4b2">
|
150
|
+
|
151
|
+
You can also get a tree directly from the repo if you know its name.
|
152
|
+
|
153
|
+
repo.tree
|
154
|
+
# => #<Grit::Tree "master">
|
155
|
+
|
156
|
+
repo.tree("91169e1f5fa4de2eaea3f176461f5dc784796769")
|
157
|
+
# => #<Grit::Tree "91169e1f5fa4de2eaea3f176461f5dc784796769">
|
158
|
+
|
159
|
+
= The Blob object
|
160
|
+
|
161
|
+
A blob represents a file. Trees often contain blobs.
|
162
|
+
|
163
|
+
blob = tree.contents.first
|
164
|
+
# => #<Grit::Blob "4ebc8aea50e0a67e000ba29a30809d0a7b9b2666">
|
165
|
+
|
166
|
+
A blob has certain attributes.
|
167
|
+
|
168
|
+
blob.id
|
169
|
+
# => "4ebc8aea50e0a67e000ba29a30809d0a7b9b2666"
|
170
|
+
|
171
|
+
blob.name
|
172
|
+
# => "README.txt"
|
173
|
+
|
174
|
+
blob.mode
|
175
|
+
# => "100644"
|
176
|
+
|
177
|
+
blob.size
|
178
|
+
# => 7726
|
179
|
+
|
180
|
+
You can get the data of a blob as a string.
|
181
|
+
|
182
|
+
blob.data
|
183
|
+
# => "Grit is a library to ..."
|
184
|
+
|
185
|
+
You can also get a blob directly from the repo if you know its name.
|
186
|
+
|
187
|
+
repo.blob("4ebc8aea50e0a67e000ba29a30809d0a7b9b2666")
|
188
|
+
# => #<Grit::Blob "4ebc8aea50e0a67e000ba29a30809d0a7b9b2666">
|
189
|
+
|
190
|
+
== LICENSE:
|
191
|
+
|
192
|
+
(The MIT License)
|
193
|
+
|
194
|
+
Copyright (c) 2007 Tom Preston-Werner
|
195
|
+
|
196
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
197
|
+
a copy of this software and associated documentation files (the
|
198
|
+
'Software'), to deal in the Software without restriction, including
|
199
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
200
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
201
|
+
permit persons to whom the Software is furnished to do so, subject to
|
202
|
+
the following conditions:
|
203
|
+
|
204
|
+
The above copyright notice and this permission notice shall be
|
205
|
+
included in all copies or substantial portions of the Software.
|
206
|
+
|
207
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
208
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
209
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
210
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
211
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
212
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
213
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'hoe'
|
3
|
+
require './lib/grit.rb'
|
4
|
+
|
5
|
+
Hoe.new('grit', Grit::VERSION) do |p|
|
6
|
+
p.author = 'Tom Preston-Werner'
|
7
|
+
p.email = 'tom@rubyisawesome.com'
|
8
|
+
p.summary = 'Object model interface to a git repo'
|
9
|
+
p.description = p.paragraphs_of('README.txt', 2..2).join("\n\n")
|
10
|
+
p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[2..-1].map { |u| u.strip }
|
11
|
+
p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
|
12
|
+
p.extra_deps << ['mime-types']
|
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
|
25
|
+
|
26
|
+
desc "Upload site to Rubyforge"
|
27
|
+
task :site do
|
28
|
+
sh "scp -r doc/* mojombo@grit.rubyforge.org:/var/www/gforge-projects/grit"
|
29
|
+
end
|
data/grit.gemspec
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "grit"
|
3
|
+
s.version = "0.8.0"
|
4
|
+
s.date = "2008-04-24"
|
5
|
+
s.summary = "Object model interface to a git repo"
|
6
|
+
s.email = "tom@rubyisawesome.com"
|
7
|
+
s.homepage = "http://github.com/mojombo/grit"
|
8
|
+
s.description = "Grit is a Ruby library for extracting information from a git repository in and object oriented manner."
|
9
|
+
s.has_rdoc = true
|
10
|
+
s.authors = ["Tom Preston-Werner"]
|
11
|
+
s.files = ["History.txt", "Manifest.txt", "README.txt", "Rakefile", "grit.gemspec", "lib/grit.rb", "lib/grit/actor.rb", "lib/grit/blob.rb", "lib/grit/commit.rb", "lib/grit/config.rb", "lib/grit/diff.rb", "lib/grit/errors.rb", "lib/grit/git.rb", "lib/grit/head.rb", "lib/grit/lazy.rb", "lib/grit/repo.rb", "lib/grit/tag.rb", "lib/grit/tree.rb", "test/fixtures/blame", "test/fixtures/cat_file_blob", "test/fixtures/cat_file_blob_size", "test/fixtures/diff_2", "test/fixtures/diff_2f", "test/fixtures/diff_f", "test/fixtures/diff_i", "test/fixtures/diff_mode_only", "test/fixtures/diff_new_mode", "test/fixtures/diff_p", "test/fixtures/for_each_ref", "test/fixtures/for_each_ref_tags", "test/fixtures/ls_tree_a", "test/fixtures/ls_tree_b", "test/fixtures/ls_tree_commit", "test/fixtures/rev_list", "test/fixtures/rev_list_count", "test/fixtures/rev_list_single", "test/fixtures/rev_parse", "test/fixtures/show_empty_commit", "test/fixtures/simple_config", "test/helper.rb", "test/profile.rb", "test/suite.rb", "test/test_actor.rb", "test/test_blob.rb", "test/test_commit.rb", "test/test_config.rb", "test/test_diff.rb", "test/test_git.rb", "test/test_head.rb", "test/test_reality.rb", "test/test_repo.rb", "test/test_tag.rb", "test/test_tree.rb", "test/test_real.rb"]
|
12
|
+
s.test_files = ["test/test_actor.rb", "test/test_blob.rb", "test/test_commit.rb", "test/test_config.rb", "test/test_diff.rb", "test/test_git.rb", "test/test_head.rb", "test/test_real.rb", "test/test_reality.rb", "test/test_repo.rb", "test/test_tag.rb", "test/test_tree.rb"]
|
13
|
+
s.rdoc_options = ["--main", "README.txt"]
|
14
|
+
s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.txt"]
|
15
|
+
s.add_dependency("mime-types", ["> 0.0.0"])
|
16
|
+
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
|
data/lib/grit/commit.rb
ADDED
@@ -0,0 +1,208 @@
|
|
1
|
+
module Grit
|
2
|
+
|
3
|
+
class Commit
|
4
|
+
attr_reader :id
|
5
|
+
lazy_reader :parents
|
6
|
+
lazy_reader :tree
|
7
|
+
lazy_reader :author
|
8
|
+
lazy_reader :authored_date
|
9
|
+
lazy_reader :committer
|
10
|
+
lazy_reader :committed_date
|
11
|
+
lazy_reader :message
|
12
|
+
lazy_reader :short_message
|
13
|
+
|
14
|
+
# Instantiate a new Commit
|
15
|
+
# +id+ is the id of the commit
|
16
|
+
# +parents+ is an array of commit ids (will be converted into Commit instances)
|
17
|
+
# +tree+ is the correspdonding tree id (will be converted into a Tree object)
|
18
|
+
# +author+ is the author string
|
19
|
+
# +authored_date+ is the authored Time
|
20
|
+
# +committer+ is the committer string
|
21
|
+
# +committed_date+ is the committed Time
|
22
|
+
# +message+ is an array of commit message lines
|
23
|
+
#
|
24
|
+
# Returns Grit::Commit (baked)
|
25
|
+
def initialize(repo, id, parents, tree, author, authored_date, committer, committed_date, message)
|
26
|
+
@repo = repo
|
27
|
+
@id = id
|
28
|
+
@parents = parents.map { |p| Commit.create(repo, :id => p) }
|
29
|
+
@tree = Tree.create(repo, :id => tree)
|
30
|
+
@author = author
|
31
|
+
@authored_date = authored_date
|
32
|
+
@committer = committer
|
33
|
+
@committed_date = committed_date
|
34
|
+
@message = message.join("\n")
|
35
|
+
@short_message = message[0] || ''
|
36
|
+
end
|
37
|
+
|
38
|
+
def id_abbrev
|
39
|
+
@id_abbrev ||= @repo.git.rev_parse({:short => true}, self.id).chomp
|
40
|
+
end
|
41
|
+
|
42
|
+
# Create an unbaked Commit containing just the specified attributes
|
43
|
+
# +repo+ is the Repo
|
44
|
+
# +atts+ is a Hash of instance variable data
|
45
|
+
#
|
46
|
+
# Returns Grit::Commit (unbaked)
|
47
|
+
def self.create(repo, atts)
|
48
|
+
self.allocate.create_initialize(repo, atts)
|
49
|
+
end
|
50
|
+
|
51
|
+
# Initializer for Commit.create
|
52
|
+
# +repo+ is the Repo
|
53
|
+
# +atts+ is a Hash of instance variable data
|
54
|
+
#
|
55
|
+
# Returns Grit::Commit (unbaked)
|
56
|
+
def create_initialize(repo, atts)
|
57
|
+
@repo = repo
|
58
|
+
atts.each do |k, v|
|
59
|
+
instance_variable_set("@#{k}", v)
|
60
|
+
end
|
61
|
+
self
|
62
|
+
end
|
63
|
+
|
64
|
+
def lazy_source
|
65
|
+
self.class.find_all(@repo, @id, {:max_count => 1}).first
|
66
|
+
end
|
67
|
+
|
68
|
+
# Count the number of commits reachable from this ref
|
69
|
+
# +repo+ is the Repo
|
70
|
+
# +ref+ is the ref from which to begin (SHA1 or name)
|
71
|
+
#
|
72
|
+
# Returns Integer
|
73
|
+
def self.count(repo, ref)
|
74
|
+
repo.git.rev_list({}, ref).strip.split("\n").size
|
75
|
+
end
|
76
|
+
|
77
|
+
# Find all commits matching the given criteria.
|
78
|
+
# +repo+ is the Repo
|
79
|
+
# +ref+ is the ref from which to begin (SHA1 or name) or nil for --all
|
80
|
+
# +options+ is a Hash of optional arguments to git
|
81
|
+
# :max_count is the maximum number of commits to fetch
|
82
|
+
# :skip is the number of commits to skip
|
83
|
+
#
|
84
|
+
# Returns Grit::Commit[] (baked)
|
85
|
+
def self.find_all(repo, ref, options = {})
|
86
|
+
allowed_options = [:max_count, :skip, :since]
|
87
|
+
|
88
|
+
default_options = {:pretty => "raw"}
|
89
|
+
actual_options = default_options.merge(options)
|
90
|
+
|
91
|
+
if ref
|
92
|
+
output = repo.git.rev_list(actual_options, ref)
|
93
|
+
else
|
94
|
+
output = repo.git.rev_list(actual_options.merge(:all => true))
|
95
|
+
end
|
96
|
+
|
97
|
+
self.list_from_string(repo, output)
|
98
|
+
end
|
99
|
+
|
100
|
+
# Parse out commit information into an array of baked Commit objects
|
101
|
+
# +repo+ is the Repo
|
102
|
+
# +text+ is the text output from the git command (raw format)
|
103
|
+
#
|
104
|
+
# Returns Grit::Commit[] (baked)
|
105
|
+
def self.list_from_string(repo, text)
|
106
|
+
lines = text.split("\n")
|
107
|
+
|
108
|
+
commits = []
|
109
|
+
|
110
|
+
while !lines.empty?
|
111
|
+
id = lines.shift.split.last
|
112
|
+
tree = lines.shift.split.last
|
113
|
+
|
114
|
+
parents = []
|
115
|
+
parents << lines.shift.split.last while lines.first =~ /^parent/
|
116
|
+
|
117
|
+
author, authored_date = self.actor(lines.shift)
|
118
|
+
committer, committed_date = self.actor(lines.shift)
|
119
|
+
|
120
|
+
lines.shift
|
121
|
+
|
122
|
+
message_lines = []
|
123
|
+
message_lines << lines.shift[4..-1] while lines.first =~ /^ {4}/
|
124
|
+
|
125
|
+
lines.shift while lines.first && lines.first.empty?
|
126
|
+
|
127
|
+
commits << Commit.new(repo, id, parents, tree, author, authored_date, committer, committed_date, message_lines)
|
128
|
+
end
|
129
|
+
|
130
|
+
commits
|
131
|
+
end
|
132
|
+
|
133
|
+
# Show diffs between two trees:
|
134
|
+
# +repo+ is the Repo
|
135
|
+
# +a+ is a named commit
|
136
|
+
# +b+ is an optional named commit. Passing an array assumes you
|
137
|
+
# wish to omit the second named commit and limit the diff to the
|
138
|
+
# given paths.
|
139
|
+
# +paths* is an array of paths to limit the diff.
|
140
|
+
#
|
141
|
+
# Returns Grit::Diff[] (baked)
|
142
|
+
def self.diff(repo, a, b = nil, paths = [])
|
143
|
+
if b.is_a?(Array)
|
144
|
+
paths = b
|
145
|
+
b = nil
|
146
|
+
end
|
147
|
+
paths.unshift("--") unless paths.empty?
|
148
|
+
paths.unshift(b) unless b.nil?
|
149
|
+
paths.unshift(a)
|
150
|
+
text = repo.git.diff({:full_index => true}, *paths)
|
151
|
+
Diff.list_from_string(repo, text)
|
152
|
+
end
|
153
|
+
|
154
|
+
def diffs
|
155
|
+
if parents.empty?
|
156
|
+
diff = @repo.git.show({:full_index => true, :pretty => 'raw'}, @id)
|
157
|
+
if diff =~ /diff --git a/
|
158
|
+
diff = diff.sub(/.+?(diff --git a)/m, '\1')
|
159
|
+
else
|
160
|
+
diff = ''
|
161
|
+
end
|
162
|
+
Diff.list_from_string(@repo, diff)
|
163
|
+
else
|
164
|
+
self.class.diff(@repo, parents.first.id, @id)
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
# Convert this Commit to a String which is just the SHA1 id
|
169
|
+
def to_s
|
170
|
+
@id
|
171
|
+
end
|
172
|
+
|
173
|
+
# Pretty object inspection
|
174
|
+
def inspect
|
175
|
+
%Q{#<Grit::Commit "#{@id}">}
|
176
|
+
end
|
177
|
+
|
178
|
+
# private
|
179
|
+
|
180
|
+
# Parse out the actor (author or committer) info
|
181
|
+
#
|
182
|
+
# Returns [String (actor name and email), Time (acted at time)]
|
183
|
+
def self.actor(line)
|
184
|
+
m, actor, epoch = *line.match(/^.+? (.*) (\d+) .*$/)
|
185
|
+
[Actor.from_string(actor), Time.at(epoch.to_i)]
|
186
|
+
end
|
187
|
+
|
188
|
+
def to_hash
|
189
|
+
{
|
190
|
+
'id' => id,
|
191
|
+
'parents' => parents.map { |p| { 'id' => p.id } },
|
192
|
+
'tree' => tree.id,
|
193
|
+
'message' => message,
|
194
|
+
'author' => {
|
195
|
+
'name' => author.name,
|
196
|
+
'email' => author.email
|
197
|
+
},
|
198
|
+
'committer' => {
|
199
|
+
'name' => committer.name,
|
200
|
+
'email' => committer.email
|
201
|
+
},
|
202
|
+
'authored_date' => authored_date.xmlschema,
|
203
|
+
'committed_date' => committed_date.xmlschema,
|
204
|
+
}
|
205
|
+
end
|
206
|
+
end # Commit
|
207
|
+
|
208
|
+
end # Grit
|