Skip to content

Commit 852a0e6

Browse files
author
scott Chacon
committed
added a bunch of good stuff to the commit object
1 parent ec59c5c commit 852a0e6

File tree

9 files changed

+142
-30
lines changed

9 files changed

+142
-30
lines changed

README

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@ Here are the operations that need read permission only.
5555
g.gblob(treeish)
5656
g.gcommit(treeish)
5757

58+
59+
commit = g.gcommit('1cc8667014381')
60+
commit.gtree
61+
commit.parent.sha
62+
commit.parents.size
63+
commit.author.name
64+
commit.author.email
65+
commit.author.date.strftime("%m-%d-%y")
66+
commit.committer.name
67+
commit.date.strftime("%m-%d-%y")
68+
commit.message
69+
70+
5871
g.revparse('v2.5:Makefile')
5972

6073
g.branches # returns Git::Branch objects

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'rake/gempackagetask'
55
spec = Gem::Specification.new do |s|
66
s.platform = Gem::Platform::RUBY
77
s.name = "git"
8-
s.version = "1.0.1"
8+
s.version = "1.0.2"
99
s.author = "Scott Chacon"
1010
s.email = "schacon@gmail.com"
1111
s.summary = "A package for using Git in Ruby code."

TODO

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,13 @@
22
* Git::Object methods
33
- tree recursion
44
- commit information
5-
6-
Git::Object
7-
- sha
8-
- type
9-
- cat_file
10-
- raw
11-
12-
Git::Commit
13-
- tree
14-
- parent
15-
- author # git author
16-
- author_date
17-
- committer # git author
18-
- committer_date / date
19-
- message
205

216
Git::Tree
227
- children
238
- blobs/files
249
- subtrees/subdirs
25-
26-
Git::Blob << File
27-
- size
28-
- permissions
29-
30-
* pushing
3110

11+
3212
* More Error Examples
3313

3414
* More Git::Status methods

lib/git.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,11 @@
2121

2222
require 'git/diff'
2323
require 'git/status'
24-
=begin
2524
require 'git/author'
26-
require 'git/file'
27-
28-
require 'git/sha'
29-
require 'git/ref'
30-
=end
3125

3226
module Git
3327

34-
VERSION = '1.0.1'
28+
VERSION = '1.0.2'
3529

3630
def self.bare(git_dir)
3731
Base.bare(git_dir)

lib/git/author.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module Git
2+
class Author
3+
attr_accessor :name, :email, :date
4+
5+
def initialize(author_string)
6+
if m = /(.*?) <(.*?)> (\d+) (.*)/.match(author_string)
7+
@name = m[1]
8+
@email = m[2]
9+
@date = Time.at(m[3].to_i)
10+
end
11+
end
12+
13+
end
14+
end

lib/git/lib.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,32 @@ def object_size(sha)
7979
command('cat-file', ['-s', sha]).to_i
8080
end
8181

82+
# returns useful array of raw commit object data
83+
def commit_data(sha)
84+
in_message = false
85+
86+
hsh = {'message' => '', 'parent' => []}
87+
command_lines('cat-file', ['commit', sha.to_s]).each do |line|
88+
if in_message
89+
hsh['message'] += line + "\n"
90+
end
91+
92+
if (line != '') && !in_message
93+
data = line.split
94+
key = data.shift
95+
value = data.join(' ')
96+
if key == 'parent'
97+
hsh[key] << value
98+
else
99+
hsh[key] = value
100+
end
101+
else
102+
in_message = true
103+
end
104+
end
105+
hsh
106+
end
107+
82108
def object_contents(sha)
83109
command('cat-file', ['-p', sha])
84110
end

lib/git/object.rb

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class AbstractObject
1212

1313
def initialize(base, sha)
1414
@base = base
15-
@sha = sha
15+
@sha = sha.to_s
1616
@size = @base.lib.object_size(@sha)
1717
setup
1818
end
@@ -63,9 +63,74 @@ def setup
6363
end
6464

6565
class Commit < AbstractObject
66+
67+
@tree = nil
68+
@parents = nil
69+
@author = nil
70+
@committer = nil
71+
@message = nil
72+
6673
def setup
6774
@type = 'commit'
6875
end
76+
77+
def message
78+
check_commit
79+
@message
80+
end
81+
82+
def gtree
83+
check_commit
84+
Tree.new(@base, @tree)
85+
end
86+
87+
def parent
88+
parents.first
89+
end
90+
91+
# array of all parent commits
92+
def parents
93+
check_commit
94+
@parents
95+
end
96+
97+
# git author
98+
def author
99+
check_commit
100+
@author
101+
end
102+
103+
def author_date
104+
author.date
105+
end
106+
107+
# git author
108+
def committer
109+
check_commit
110+
@committer
111+
end
112+
113+
def committer_date
114+
committer.date
115+
end
116+
alias_method :date, :committer_date
117+
118+
def diff_parent
119+
diff(parent)
120+
end
121+
122+
private
123+
124+
# see if this object has been initialized and do so if not
125+
def check_commit
126+
data = @base.lib.commit_data(@sha)
127+
@committer = Git::Author.new(data['committer'])
128+
@author = Git::Author.new(data['author'])
129+
@tree = Tree.new(@base, data['tree'])
130+
@parents = data['parent'].map{ |sha| Commit.new(@base, sha) }
131+
@message = data['message'].chomp
132+
end
133+
69134
end
70135

71136
class Tag < AbstractObject

tests/units/test_lib.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ def setup
1313
set_file_paths
1414
@lib = Git.open(@wdir).lib
1515
end
16+
17+
def test_commit_data
18+
data = @lib.commit_data('1cc8667014381')
19+
assert_equal('scott Chacon <schacon@agadorsparticus.corp.reactrix.com> 1194561188 -0800', data['author'])
20+
assert_equal('94c827875e2cadb8bc8d4cdd900f19aa9e8634c7', data['tree'])
21+
assert_equal("test\n", data['message'])
22+
assert_equal(["546bec6f8872efa41d5d97a369f669165ecda0de"], data['parent'])
23+
end
1624

1725
# takes parameters, returns array of appropriate commit objects
1826
# :count

tests/units/test_object.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ def test_commit
1616
o = @git.object('1cc8667014381')
1717
assert(o.is_a?(Git::Object::Commit))
1818

19+
assert_equal('94c827875e2cadb8bc8d4cdd900f19aa9e8634c7', o.gtree.to_s)
20+
assert_equal('546bec6f8872efa41d5d97a369f669165ecda0de', o.parent.sha)
21+
assert_equal(1, o.parents.size)
22+
assert_equal('scott Chacon', o.author.name)
23+
assert_equal('schacon@agadorsparticus.corp.reactrix.com', o.author.email)
24+
assert_equal('11-08-07', o.author.date.strftime("%m-%d-%y"))
25+
assert_equal('11-08-07', o.author_date.strftime("%m-%d-%y"))
26+
assert_equal('scott Chacon', o.committer.name)
27+
assert_equal('11-08-07', o.committer_date.strftime("%m-%d-%y"))
28+
assert_equal('11-08-07', o.date.strftime("%m-%d-%y"))
29+
assert_equal('test', o.message)
30+
1931
o = @git.object('HEAD')
2032
assert(o.is_a?(Git::Object::Commit))
2133
assert_equal('commit', o.type)

0 commit comments

Comments
 (0)