File tree Expand file tree Collapse file tree 9 files changed +142
-30
lines changed Expand file tree Collapse file tree 9 files changed +142
-30
lines changed Original file line number Diff line number Diff line change @@ -55,6 +55,19 @@ Here are the operations that need read permission only.
55
55
g.gblob(treeish)
56
56
g.gcommit(treeish)
57
57
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
+
58
71
g.revparse('v2.5:Makefile')
59
72
60
73
g.branches # returns Git::Branch objects
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ require 'rake/gempackagetask'
5
5
spec = Gem ::Specification . new do |s |
6
6
s . platform = Gem ::Platform ::RUBY
7
7
s . name = "git"
8
- s . version = "1.0.1 "
8
+ s . version = "1.0.2 "
9
9
s . author = "Scott Chacon"
10
10
s . email = "schacon@gmail.com"
11
11
s . summary = "A package for using Git in Ruby code."
Original file line number Diff line number Diff line change 2
2
* Git::Object methods
3
3
- tree recursion
4
4
- 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
20
5
21
6
Git::Tree
22
7
- children
23
8
- blobs/files
24
9
- subtrees/subdirs
25
-
26
- Git::Blob << File
27
- - size
28
- - permissions
29
-
30
- * pushing
31
10
11
+
32
12
* More Error Examples
33
13
34
14
* More Git::Status methods
Original file line number Diff line number Diff line change 21
21
22
22
require 'git/diff'
23
23
require 'git/status'
24
- =begin
25
24
require 'git/author'
26
- require 'git/file'
27
-
28
- require 'git/sha'
29
- require 'git/ref'
30
- =end
31
25
32
26
module Git
33
27
34
- VERSION = '1.0.1 '
28
+ VERSION = '1.0.2 '
35
29
36
30
def self . bare ( git_dir )
37
31
Base . bare ( git_dir )
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change @@ -79,6 +79,32 @@ def object_size(sha)
79
79
command ( 'cat-file' , [ '-s' , sha ] ) . to_i
80
80
end
81
81
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
+
82
108
def object_contents ( sha )
83
109
command ( 'cat-file' , [ '-p' , sha ] )
84
110
end
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ class AbstractObject
12
12
13
13
def initialize ( base , sha )
14
14
@base = base
15
- @sha = sha
15
+ @sha = sha . to_s
16
16
@size = @base . lib . object_size ( @sha )
17
17
setup
18
18
end
@@ -63,9 +63,74 @@ def setup
63
63
end
64
64
65
65
class Commit < AbstractObject
66
+
67
+ @tree = nil
68
+ @parents = nil
69
+ @author = nil
70
+ @committer = nil
71
+ @message = nil
72
+
66
73
def setup
67
74
@type = 'commit'
68
75
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
+
69
134
end
70
135
71
136
class Tag < AbstractObject
Original file line number Diff line number Diff line change @@ -13,6 +13,14 @@ def setup
13
13
set_file_paths
14
14
@lib = Git . open ( @wdir ) . lib
15
15
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
16
24
17
25
# takes parameters, returns array of appropriate commit objects
18
26
# :count
Original file line number Diff line number Diff line change @@ -16,6 +16,18 @@ def test_commit
16
16
o = @git . object ( '1cc8667014381' )
17
17
assert ( o . is_a? ( Git ::Object ::Commit ) )
18
18
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
+
19
31
o = @git . object ( 'HEAD' )
20
32
assert ( o . is_a? ( Git ::Object ::Commit ) )
21
33
assert_equal ( 'commit' , o . type )
You can’t perform that action at this time.
0 commit comments