summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/git.rb8
-rw-r--r--lib/git/author.rb14
-rw-r--r--lib/git/lib.rb26
-rw-r--r--lib/git/object.rb67
4 files changed, 107 insertions, 8 deletions
diff --git a/lib/git.rb b/lib/git.rb
index 1452427..7dfbd40 100644
--- a/lib/git.rb
+++ b/lib/git.rb
@@ -21,17 +21,11 @@ require 'git/remote'
require 'git/diff'
require 'git/status'
-=begin
require 'git/author'
-require 'git/file'
-
-require 'git/sha'
-require 'git/ref'
-=end
module Git
- VERSION = '1.0.1'
+ VERSION = '1.0.2'
def self.bare(git_dir)
Base.bare(git_dir)
diff --git a/lib/git/author.rb b/lib/git/author.rb
new file mode 100644
index 0000000..545abb9
--- /dev/null
+++ b/lib/git/author.rb
@@ -0,0 +1,14 @@
+module Git
+ class Author
+ attr_accessor :name, :email, :date
+
+ def initialize(author_string)
+ if m = /(.*?) <(.*?)> (\d+) (.*)/.match(author_string)
+ @name = m[1]
+ @email = m[2]
+ @date = Time.at(m[3].to_i)
+ end
+ end
+
+ end
+end \ No newline at end of file
diff --git a/lib/git/lib.rb b/lib/git/lib.rb
index 575f0d7..13a3b4c 100644
--- a/lib/git/lib.rb
+++ b/lib/git/lib.rb
@@ -79,6 +79,32 @@ module Git
command('cat-file', ['-s', sha]).to_i
end
+ # returns useful array of raw commit object data
+ def commit_data(sha)
+ in_message = false
+
+ hsh = {'message' => '', 'parent' => []}
+ command_lines('cat-file', ['commit', sha.to_s]).each do |line|
+ if in_message
+ hsh['message'] += line + "\n"
+ end
+
+ if (line != '') && !in_message
+ data = line.split
+ key = data.shift
+ value = data.join(' ')
+ if key == 'parent'
+ hsh[key] << value
+ else
+ hsh[key] = value
+ end
+ else
+ in_message = true
+ end
+ end
+ hsh
+ end
+
def object_contents(sha)
command('cat-file', ['-p', sha])
end
diff --git a/lib/git/object.rb b/lib/git/object.rb
index a7d7c17..58ea7ab 100644
--- a/lib/git/object.rb
+++ b/lib/git/object.rb
@@ -12,7 +12,7 @@ module Git
def initialize(base, sha)
@base = base
- @sha = sha
+ @sha = sha.to_s
@size = @base.lib.object_size(@sha)
setup
end
@@ -63,9 +63,74 @@ module Git
end
class Commit < AbstractObject
+
+ @tree = nil
+ @parents = nil
+ @author = nil
+ @committer = nil
+ @message = nil
+
def setup
@type = 'commit'
end
+
+ def message
+ check_commit
+ @message
+ end
+
+ def gtree
+ check_commit
+ Tree.new(@base, @tree)
+ end
+
+ def parent
+ parents.first
+ end
+
+ # array of all parent commits
+ def parents
+ check_commit
+ @parents
+ end
+
+ # git author
+ def author
+ check_commit
+ @author
+ end
+
+ def author_date
+ author.date
+ end
+
+ # git author
+ def committer
+ check_commit
+ @committer
+ end
+
+ def committer_date
+ committer.date
+ end
+ alias_method :date, :committer_date
+
+ def diff_parent
+ diff(parent)
+ end
+
+ private
+
+ # see if this object has been initialized and do so if not
+ def check_commit
+ data = @base.lib.commit_data(@sha)
+ @committer = Git::Author.new(data['committer'])
+ @author = Git::Author.new(data['author'])
+ @tree = Tree.new(@base, data['tree'])
+ @parents = data['parent'].map{ |sha| Commit.new(@base, sha) }
+ @message = data['message'].chomp
+ end
+
end
class Tag < AbstractObject