davetron5000-grit 1.1.2

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 (70) hide show
  1. data/API.txt +101 -0
  2. data/History.txt +49 -0
  3. data/LICENSE +22 -0
  4. data/README.md +216 -0
  5. data/VERSION.yml +4 -0
  6. data/examples/ex_add_commit.rb +13 -0
  7. data/examples/ex_index.rb +14 -0
  8. data/lib/grit/actor.rb +41 -0
  9. data/lib/grit/blame.rb +61 -0
  10. data/lib/grit/blob.rb +126 -0
  11. data/lib/grit/commit.rb +242 -0
  12. data/lib/grit/commit_stats.rb +128 -0
  13. data/lib/grit/config.rb +44 -0
  14. data/lib/grit/diff.rb +70 -0
  15. data/lib/grit/errors.rb +7 -0
  16. data/lib/grit/git-ruby/commit_db.rb +52 -0
  17. data/lib/grit/git-ruby/file_index.rb +193 -0
  18. data/lib/grit/git-ruby/git_object.rb +350 -0
  19. data/lib/grit/git-ruby/internal/file_window.rb +58 -0
  20. data/lib/grit/git-ruby/internal/loose.rb +137 -0
  21. data/lib/grit/git-ruby/internal/pack.rb +382 -0
  22. data/lib/grit/git-ruby/internal/raw_object.rb +37 -0
  23. data/lib/grit/git-ruby/object.rb +325 -0
  24. data/lib/grit/git-ruby/repository.rb +736 -0
  25. data/lib/grit/git-ruby.rb +186 -0
  26. data/lib/grit/git.rb +140 -0
  27. data/lib/grit/index.rb +122 -0
  28. data/lib/grit/lazy.rb +33 -0
  29. data/lib/grit/merge.rb +45 -0
  30. data/lib/grit/ref.rb +99 -0
  31. data/lib/grit/repo.rb +447 -0
  32. data/lib/grit/ruby1.9.rb +7 -0
  33. data/lib/grit/status.rb +151 -0
  34. data/lib/grit/submodule.rb +88 -0
  35. data/lib/grit/tag.rb +66 -0
  36. data/lib/grit/tree.rb +123 -0
  37. data/lib/grit.rb +68 -0
  38. data/lib/open3_detach.rb +46 -0
  39. data/test/bench/benchmarks.rb +126 -0
  40. data/test/helper.rb +18 -0
  41. data/test/profile.rb +21 -0
  42. data/test/suite.rb +6 -0
  43. data/test/test_actor.rb +51 -0
  44. data/test/test_blame.rb +32 -0
  45. data/test/test_blame_tree.rb +33 -0
  46. data/test/test_blob.rb +83 -0
  47. data/test/test_commit.rb +206 -0
  48. data/test/test_commit_stats.rb +33 -0
  49. data/test/test_commit_write.rb +54 -0
  50. data/test/test_config.rb +58 -0
  51. data/test/test_diff.rb +18 -0
  52. data/test/test_file_index.rb +56 -0
  53. data/test/test_git.rb +84 -0
  54. data/test/test_grit.rb +32 -0
  55. data/test/test_head.rb +47 -0
  56. data/test/test_index_status.rb +40 -0
  57. data/test/test_merge.rb +17 -0
  58. data/test/test_raw.rb +16 -0
  59. data/test/test_real.rb +19 -0
  60. data/test/test_reality.rb +17 -0
  61. data/test/test_remote.rb +14 -0
  62. data/test/test_repo.rb +347 -0
  63. data/test/test_rubygit.rb +188 -0
  64. data/test/test_rubygit_alt.rb +40 -0
  65. data/test/test_rubygit_index.rb +76 -0
  66. data/test/test_rubygit_iv2.rb +28 -0
  67. data/test/test_submodule.rb +69 -0
  68. data/test/test_tag.rb +67 -0
  69. data/test/test_tree.rb +101 -0
  70. metadata +141 -0
data/API.txt ADDED
@@ -0,0 +1,101 @@
1
+ == TODO ==
2
+
3
+ * Add remote branch references (Grit::Remote)
4
+ * Add status - what is modified, staged
5
+
6
+ g.checkout('new_branch')
7
+ g.checkout(g.branch('new_branch'))
8
+
9
+ g.branch(name).merge(branch2)
10
+ g.branch(branch2).merge # merges HEAD with branch2
11
+
12
+ g.branch(name).in_branch(message) { # add files } # auto-commits
13
+ g.merge('new_branch')
14
+ g.merge('origin/remote_branch')
15
+ g.merge(b.branch('master'))
16
+ g.merge([branch1, branch2])
17
+
18
+ r = g.add_remote(name, uri) # Git::Remote
19
+ r = g.add_remote(name, Git::Base) # Git::Remote
20
+
21
+ g.remotes # array of Git::Remotes
22
+ g.remote(name).fetch
23
+ g.remote(name).remove
24
+ g.remote(name).merge
25
+ g.remote(name).merge(branch)
26
+
27
+ g.fetch
28
+ g.fetch(g.remotes.first)
29
+
30
+ g.pull
31
+ g.pull(Git::Repo, Git::Branch) # fetch and a merge
32
+
33
+ g.add_tag('tag_name') # returns Git::Tag
34
+
35
+ g.repack
36
+
37
+ g.push
38
+ g.push(g.remote('name'))
39
+
40
+ g.reset # defaults to HEAD
41
+ g.reset_hard(Git::Commit)
42
+
43
+ g.branch('new_branch') # creates new or fetches existing
44
+ g.branch('new_branch').checkout
45
+ g.branch('new_branch').delete
46
+ g.branch('existing_branch').checkout
47
+
48
+
49
+
50
+
51
+
52
+ require 'mojombo-grit'
53
+
54
+ include Grit
55
+ Grit.debug
56
+ Grit.use_pure_ruby
57
+
58
+ repo = Repo.new("/Users/tom/dev/grit")
59
+
60
+ = Commit Log
61
+
62
+ repo.commits('mybranch')
63
+ repo.commits('40d3057d09a7a4d61059bca9dca5ae698de58cbe')
64
+ repo.commits('v0.1')
65
+
66
+ repo.log('mybranch', 100, 20)
67
+
68
+ head = repo.commits.first
69
+ head.id
70
+ # => "e80bbd2ce67651aa18e57fb0b43618ad4baf7750"
71
+ head.parents
72
+ # => [#<Grit::Commit "91169e1f5fa4de2eaea3f176461f5dc784796769">]
73
+ head.tree
74
+ # => #<Grit::Tree "3536eb9abac69c3e4db583ad38f3d30f8db4771f">
75
+ head.author
76
+ # => #<Grit::Actor "Tom Preston-Werner <tom@mojombo.com>">
77
+ head.authored_date
78
+ # => Wed Oct 24 22:02:31 -0700 2007
79
+ head.committer
80
+ # => #<Grit::Actor "Tom Preston-Werner <tom@mojombo.com>">
81
+ head.committed_date
82
+ # => Wed Oct 24 22:02:31 -0700 2007
83
+ head.message
84
+ # => "add Actor inspect"
85
+ contents = tree.contents
86
+ # => [#<Grit::Blob "4ebc8aea50e0a67e000ba29a30809d0a7b9b2666">,
87
+ #<Grit::Blob "81d2c27608b352814cbe979a6acd678d30219678">,
88
+ #<Grit::Tree "c3d07b0083f01a6e1ac969a0f32b8d06f20c62e5">,
89
+ #<Grit::Tree "4d00fe177a8407dbbc64a24dbfc564762c0922d8">]
90
+ blob.id
91
+ # => "4ebc8aea50e0a67e000ba29a30809d0a7b9b2666"
92
+ blob.name
93
+ # => "README.txt"
94
+ blob.mode
95
+ # => "100644"
96
+ blob.size
97
+ # => 7726
98
+ blob.data
99
+
100
+ repo.blob("4ebc8aea50e0a67e000ba29a30809d0a7b9b2666")
101
+ # => #<Grit::Blob "4ebc8aea50e0a67e000ba29a30809d0a7b9b2666">
data/History.txt ADDED
@@ -0,0 +1,49 @@
1
+ == 1.1.0 / 2009-03-29
2
+ * Backwards breaking changes
3
+ * Diff#a_commit -> Diff#a_blob, Diff#b_commit -> Diff#b_blob
4
+ * Major Enhancments
5
+ * Ruby 1.9 compatibility [github.com/chapados, github.com/js]
6
+ * Minor Enhancements
7
+ * Convert readme to markdown
8
+ * Added a shortcut for commit_stats as Commit#stats [github.com/js]
9
+ * Add a #basename method to Submodule, Blob and Tree for retrieving the name [github.com/js]
10
+ * Make Grit::Submodule grasp the concept of non-unix lineendings [github.com/js]
11
+ * Added Repo#commit_deltas_from [github.com/js]
12
+ * do some mild shell escaping when running commands [github.com/js]
13
+ * Added two shortcut methods to Tree, for picking trees/blobs only [github.com/Voker57]
14
+ * Added <=> method to Blob, needed for sorting tree [github.com/Voker57]
15
+ * Make the number of bytes to be read from git's stdout configurable [github.com/josb]
16
+ * Repo.archive_to_file accepts extra parameters making plain zipping possible [github.com/darwin]
17
+ * Handle commit stats that summarize commits with binary changes [github.com/therealadam]
18
+ * Add a DiffStat class for easy access to diff stats [github.com/therealadam]
19
+ * Don't split git logs that contain blank lines into two CommitStats [github.com/therealadam]
20
+ * Add DiffStat#net for total change count [github.com/therealadam]
21
+
22
+ == 1.0.3 / 2009-02-13
23
+ * Minor Enhancements
24
+ * Added Grit::Commit#to_patch for plaintext formatted patches.
25
+ * Fixed Grit::Tag to work with annotated tags.
26
+
27
+ == 1.0.2 / 2009-02-10
28
+ * Minor Enhancements
29
+ * Implement Grit.version to use VERSION.yml file
30
+
31
+ == 1.0.1 / 2009-02-10
32
+ * Bug Fixes
33
+ * Add diff-lcs as a dependency
34
+
35
+ == 1.0.0 / 2009-01-27
36
+ * Tons of awesome in here. Also, we suck at updating the history.
37
+ * Let's do better at that from now on.
38
+
39
+ == 0.8.3 / 2008-07-07
40
+ * Capture stderr and log if debug is true (rsanheim)
41
+
42
+ == 0.8.2 / 2008-06-27
43
+ * Allow user provided logger (rsanheim)
44
+
45
+ == 0.8.0 / 2008-04-24
46
+ * Lots of fixes and additions
47
+
48
+ == 0.7.0 / 2008-01-07
49
+ * First public release!
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2007-2009 Tom Preston-Werner
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ 'Software'), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,216 @@
1
+ Grit
2
+ ====
3
+
4
+ Grit gives you object oriented read/write access to Git repositories via Ruby.
5
+ The main goals are stability and performance. To this end, some of the
6
+ interactions with Git repositories are done by shelling out to the system's
7
+ `git` command, and other interactions are done with pure Ruby
8
+ reimplementations of core Git functionality. This choice, however, is
9
+ transparent to end users, and you need not know which method is being used.
10
+
11
+ This software was developed to power GitHub, and should be considered
12
+ production ready. An extensive test suite is provided to verify its correctness.
13
+
14
+ Grit is maintained by Tom Preston-Werner, Scott Chacon, Chris Wanstrath, and
15
+ PJ Hyett.
16
+
17
+ This documentation is accurate as of Grit 1.0.2.
18
+
19
+
20
+ ## Requirements #############################################################
21
+
22
+ * git (http://git-scm.com) tested with 1.6.0.2
23
+
24
+
25
+ ## Install ##################################################################
26
+
27
+ Easiest install is via RubyGems:
28
+
29
+ $ gem install grit
30
+
31
+ or
32
+
33
+ $ gem sources -a http://gems.github.com/ (you only need to do this once)
34
+ $ gem install mojombo-grit
35
+
36
+ The gem from GitHub will generally be available sooner than the gem from
37
+ Rubyforge. Both sources will eventually contain the same releases.
38
+
39
+
40
+ ## Source ###################################################################
41
+
42
+ Grit's Git repo is available on GitHub, which can be browsed at:
43
+
44
+ http://github.com/mojombo/grit
45
+
46
+ and cloned with:
47
+
48
+ git clone git://github.com/mojombo/grit.git
49
+
50
+ ### Development
51
+
52
+ You will need these gems to get tests to pass:
53
+
54
+ * technicalpickles-jeweler
55
+ * mocha
56
+
57
+ ## Usage ####################################################################
58
+
59
+ Grit gives you object model access to your Git repositories. Once you have
60
+ created a `Repo` object, you can traverse it to find parent commits,
61
+ trees, blobs, etc.
62
+
63
+ ### Initialize a Repo object
64
+
65
+ The first step is to create a `Grit::Repo` object to represent your repo. In
66
+ this documentation I include the `Grit` module to reduce typing.
67
+
68
+ require 'grit'
69
+ include Grit
70
+ repo = Repo.new("/Users/tom/dev/grit")
71
+
72
+ In the above example, the directory `/Users/tom/dev/grit` is my working
73
+ directory and contains the `.git` directory. You can also initialize Grit with
74
+ a bare repo.
75
+
76
+ repo = Repo.new("/var/git/grit.git")
77
+
78
+ ### Getting a list of commits
79
+
80
+ From the `Repo` object, you can get a list of commits as an array of `Commit`
81
+ objects.
82
+
83
+ repo.commits
84
+ # => [#<Grit::Commit "e80bbd2ce67651aa18e57fb0b43618ad4baf7750">,
85
+ #<Grit::Commit "91169e1f5fa4de2eaea3f176461f5dc784796769">,
86
+ #<Grit::Commit "038af8c329ef7c1bae4568b98bd5c58510465493">,
87
+ #<Grit::Commit "40d3057d09a7a4d61059bca9dca5ae698de58cbe">,
88
+ #<Grit::Commit "4ea50f4754937bf19461af58ce3b3d24c77311d9">]
89
+
90
+ Called without arguments, `Repo#commits` returns a list of up to ten commits
91
+ reachable by the **master** branch (starting at the latest commit). You can
92
+ ask for commits beginning at a different branch, commit, tag, etc.
93
+
94
+ repo.commits('mybranch')
95
+ repo.commits('40d3057d09a7a4d61059bca9dca5ae698de58cbe')
96
+ repo.commits('v0.1')
97
+
98
+ You can specify the maximum number of commits to return.
99
+
100
+ repo.commits('master', 100)
101
+
102
+ If you need paging, you can specify a number of commits to skip.
103
+
104
+ repo.commits('master', 10, 20)
105
+
106
+ The above will return commits 21-30 from the commit list.
107
+
108
+ ### The Commit object
109
+
110
+ `Commit` objects contain information about that commit.
111
+
112
+ head = repo.commits.first
113
+
114
+ head.id
115
+ # => "e80bbd2ce67651aa18e57fb0b43618ad4baf7750"
116
+
117
+ head.parents
118
+ # => [#<Grit::Commit "91169e1f5fa4de2eaea3f176461f5dc784796769">]
119
+
120
+ head.tree
121
+ # => #<Grit::Tree "3536eb9abac69c3e4db583ad38f3d30f8db4771f">
122
+
123
+ head.author
124
+ # => #<Grit::Actor "Tom Preston-Werner <tom@mojombo.com>">
125
+
126
+ head.authored_date
127
+ # => Wed Oct 24 22:02:31 -0700 2007
128
+
129
+ head.committer
130
+ # => #<Grit::Actor "Tom Preston-Werner <tom@mojombo.com>">
131
+
132
+ head.committed_date
133
+ # => Wed Oct 24 22:02:31 -0700 2007
134
+
135
+ head.message
136
+ # => "add Actor inspect"
137
+
138
+ You can traverse a commit's ancestry by chaining calls to `#parents`.
139
+
140
+ repo.commits.first.parents[0].parents[0].parents[0]
141
+
142
+ The above corresponds to **master^^^** or **master~3** in Git parlance.
143
+
144
+ ### The Tree object
145
+
146
+ A tree records pointers to the contents of a directory. Let's say you want
147
+ the root tree of the latest commit on the **master** branch.
148
+
149
+ tree = repo.commits.first.tree
150
+ # => #<Grit::Tree "3536eb9abac69c3e4db583ad38f3d30f8db4771f">
151
+
152
+ tree.id
153
+ # => "3536eb9abac69c3e4db583ad38f3d30f8db4771f"
154
+
155
+ Once you have a tree, you can get the contents.
156
+
157
+ contents = tree.contents
158
+ # => [#<Grit::Blob "4ebc8aea50e0a67e000ba29a30809d0a7b9b2666">,
159
+ #<Grit::Blob "81d2c27608b352814cbe979a6acd678d30219678">,
160
+ #<Grit::Tree "c3d07b0083f01a6e1ac969a0f32b8d06f20c62e5">,
161
+ #<Grit::Tree "4d00fe177a8407dbbc64a24dbfc564762c0922d8">]
162
+
163
+ This tree contains two `Blob` objects and two `Tree` objects. The trees are
164
+ subdirectories and the blobs are files. Trees below the root have additional
165
+ attributes.
166
+
167
+ contents.last.name
168
+ # => "lib"
169
+
170
+ contents.last.mode
171
+ # => "040000"
172
+
173
+ There is a convenience method that allows you to get a named sub-object
174
+ from a tree.
175
+
176
+ tree / "lib"
177
+ # => #<Grit::Tree "e74893a3d8a25cbb1367cf241cc741bfd503c4b2">
178
+
179
+ You can also get a tree directly from the repo if you know its name.
180
+
181
+ repo.tree
182
+ # => #<Grit::Tree "master">
183
+
184
+ repo.tree("91169e1f5fa4de2eaea3f176461f5dc784796769")
185
+ # => #<Grit::Tree "91169e1f5fa4de2eaea3f176461f5dc784796769">
186
+
187
+ ### The Blob object
188
+
189
+ A blob represents a file. Trees often contain blobs.
190
+
191
+ blob = tree.contents.first
192
+ # => #<Grit::Blob "4ebc8aea50e0a67e000ba29a30809d0a7b9b2666">
193
+
194
+ A blob has certain attributes.
195
+
196
+ blob.id
197
+ # => "4ebc8aea50e0a67e000ba29a30809d0a7b9b2666"
198
+
199
+ blob.name
200
+ # => "README.txt"
201
+
202
+ blob.mode
203
+ # => "100644"
204
+
205
+ blob.size
206
+ # => 7726
207
+
208
+ You can get the data of a blob as a string.
209
+
210
+ blob.data
211
+ # => "Grit is a library to ..."
212
+
213
+ You can also get a blob directly from the repo if you know its name.
214
+
215
+ repo.blob("4ebc8aea50e0a67e000ba29a30809d0a7b9b2666")
216
+ # => #<Grit::Blob "4ebc8aea50e0a67e000ba29a30809d0a7b9b2666">
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :patch: 2
3
+ :major: 1
4
+ :minor: 1
@@ -0,0 +1,13 @@
1
+ require '../lib/grit'
2
+
3
+ count = 1
4
+ Dir.chdir("/Users/schacon/projects/atest") do
5
+ r = Grit::Repo.new('.')
6
+ while(count < 10) do
7
+ fname = Time.now.to_i.to_s + count.to_s
8
+ File.open(fname, 'w') { |f| f.write('hellor ' + fname) }
9
+ r.add(fname)
10
+ count += 1
11
+ end
12
+ r.commit_index('my commit')
13
+ end
@@ -0,0 +1,14 @@
1
+ require '../lib/grit'
2
+
3
+ count = 1
4
+ Dir.chdir("/Users/schacon/projects/atest") do
5
+ r = Grit::Repo.new('.')
6
+ i = r.index
7
+ while(count < 10) do
8
+ fname = Time.now.to_i.to_s + count.to_s
9
+ i.add(fname, 'hello ' + fname)
10
+ count += 1
11
+ end
12
+ puts i.commit('my commit')
13
+ puts i.inspect
14
+ end
data/lib/grit/actor.rb ADDED
@@ -0,0 +1,41 @@
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
+ # Returns this actor in the git format, as taken by from_string
14
+ def to_git_format
15
+ @email ? "#{@name} <#{@email}>" : @name
16
+ end
17
+
18
+ # Create an Actor from a string.
19
+ # +str+ is the string, which is expected to be in regular git format
20
+ #
21
+ # Format
22
+ # John Doe <jdoe@example.com>
23
+ #
24
+ # Returns Actor
25
+ def self.from_string(str)
26
+ case str
27
+ when /<.+>/
28
+ m, name, email = *str.match(/(.*) <(.+?)>/)
29
+ return self.new(name, email)
30
+ else
31
+ return self.new(str, nil)
32
+ end
33
+ end
34
+
35
+ # Pretty object inspection
36
+ def inspect
37
+ %Q{#<Grit::Actor "#{@name} <#{@email}>">}
38
+ end
39
+ end # Actor
40
+
41
+ end # Grit
data/lib/grit/blame.rb ADDED
@@ -0,0 +1,61 @@
1
+ module Grit
2
+
3
+ class Blame
4
+
5
+ attr_reader :lines
6
+
7
+ def initialize(repo, file, commit)
8
+ @repo = repo
9
+ @file = file
10
+ @commit = commit
11
+ @lines = []
12
+ load_blame
13
+ end
14
+
15
+ def load_blame
16
+ output = @repo.git.blame({'p' => true}, @commit, '--', @file)
17
+ process_raw_blame(output)
18
+ end
19
+
20
+ def process_raw_blame(output)
21
+ lines, final = [], []
22
+ info, commits = {}, {}
23
+
24
+ # process the output
25
+ output.split("\n").each do |line|
26
+ if line[0, 1] == "\t"
27
+ lines << line[1, line.size]
28
+ elsif m = /^(\w{40}) (\d+) (\d+)/.match(line)
29
+ if !commits[m[1]]
30
+ commits[m[1]] = @repo.commit(m[1])
31
+ end
32
+ info[m[3].to_i] = [commits[m[1]], m[2].to_i]
33
+ end
34
+ end
35
+
36
+ # get it together
37
+ info.sort.each do |lineno, commit|
38
+ final << BlameLine.new(lineno, commit[1], commit[0], lines[lineno - 1])
39
+ end
40
+
41
+ @lines = final
42
+ end
43
+
44
+ # Pretty object inspection
45
+ def inspect
46
+ %Q{#<Grit::Blame "#{@file} <#{@commit}>">}
47
+ end
48
+
49
+ class BlameLine
50
+ attr_accessor :lineno, :oldlineno, :commit, :line
51
+ def initialize(lineno, oldlineno, commit, line)
52
+ @lineno = lineno
53
+ @oldlineno = oldlineno
54
+ @commit = commit
55
+ @line = line
56
+ end
57
+ end
58
+
59
+ end # Blame
60
+
61
+ end # Grit
data/lib/grit/blob.rb ADDED
@@ -0,0 +1,126 @@
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
+ def basename
112
+ File.basename(name)
113
+ end
114
+
115
+ # Pretty object inspection
116
+ def inspect
117
+ %Q{#<Grit::Blob "#{@id}">}
118
+ end
119
+
120
+ # Compares blobs by name
121
+ def <=>(other)
122
+ name <=> other.name
123
+ end
124
+ end # Blob
125
+
126
+ end # Grit