From b18bca3b853dee6a7bc86f09921aa3b1ee3f3d7b Mon Sep 17 00:00:00 2001 From: scott Chacon Date: Thu, 8 Nov 2007 11:22:46 -0800 Subject: added log functions --- lib/git.rb | 10 ++++--- lib/git/base.rb | 9 ++++++ lib/git/commit.rb | 4 +++ lib/git/lib.rb | 22 ++++++++++++++ lib/git/log.rb | 35 ++++++++++++++++++++++ lib/git/object.rb | 16 ++++++++++ tests/files/working | 1 - tests/files/working/ex_dir/ex.txt | 0 tests/files/working/example.txt | 63 +++++++++++++++++++++++++++++++++++++++ tests/units/test_log.rb | 26 ++++++++++++++++ 10 files changed, 181 insertions(+), 5 deletions(-) create mode 100644 lib/git/commit.rb create mode 100644 lib/git/lib.rb create mode 100644 lib/git/log.rb create mode 100644 lib/git/object.rb delete mode 160000 tests/files/working create mode 100644 tests/files/working/ex_dir/ex.txt create mode 100644 tests/files/working/example.txt create mode 100644 tests/units/test_log.rb diff --git a/lib/git.rb b/lib/git.rb index 2e372d0..c8e14ad 100644 --- a/lib/git.rb +++ b/lib/git.rb @@ -6,16 +6,19 @@ $:.unshift(File.dirname(__FILE__)) unless require 'git/base' require 'git/path' -#require 'git/lib' +require 'git/lib' require 'git/repository' require 'git/index' require 'git/working_directory' -=begin +require 'git/log' require 'git/object' -require 'git/object/commit' +require 'git/commit' + +=begin + require 'git/object/blob' require 'git/object/tree' require 'git/object/tag' @@ -24,7 +27,6 @@ require 'git/author' require 'git/ref' require 'git/file' -require 'git/log' require 'git/sha' require 'git/diff' diff --git a/lib/git/base.rb b/lib/git/base.rb index 6eb3455..c943b86 100644 --- a/lib/git/base.rb +++ b/lib/git/base.rb @@ -49,6 +49,15 @@ module Git @index end + + def log(count = 30) + Git::Log.new(self, count) + end + + def lib + Git::Lib.new(self) + end + private def is_git_dir(dir) diff --git a/lib/git/commit.rb b/lib/git/commit.rb new file mode 100644 index 0000000..1cd5d1d --- /dev/null +++ b/lib/git/commit.rb @@ -0,0 +1,4 @@ +module Git + class Commit < Git::Object + end +end \ No newline at end of file diff --git a/lib/git/lib.rb b/lib/git/lib.rb new file mode 100644 index 0000000..3df049c --- /dev/null +++ b/lib/git/lib.rb @@ -0,0 +1,22 @@ +module Git + class Lib + + @base = nil + + def initialize(base) + @base = base + end + + def log_shas(count) + command('log', "-#{count} --pretty=oneline").split("\n").map { |l| Git::Commit.new(l.split.first) } + end + + private + + def command(cmd, opts) + ENV['GIT_DIR'] = @base.repo.path + `git #{cmd} #{opts}` + end + + end +end \ No newline at end of file diff --git a/lib/git/log.rb b/lib/git/log.rb new file mode 100644 index 0000000..fab605e --- /dev/null +++ b/lib/git/log.rb @@ -0,0 +1,35 @@ +module Git + + # object that holds the last X commits on given branch + class Log + include Enumerable + + @base = nil + @commits = nil + + def initialize(base, count = 30) + @base = base + @commits = @base.lib.log_shas(count) + end + + def size + @commits.size + end + + def each + @commits.each do |c| + yield c + end + end + + def first + @commits.first + end + + def to_s + self.map { |c| c.sha }.join("\n") + end + + end + +end \ No newline at end of file diff --git a/lib/git/object.rb b/lib/git/object.rb new file mode 100644 index 0000000..0252fdb --- /dev/null +++ b/lib/git/object.rb @@ -0,0 +1,16 @@ +module Git + class Object + attr_accessor :sha, :type + + def initialize(sha) + @sha = sha + end + + def cat_file + end + + def raw + end + + end +end \ No newline at end of file diff --git a/tests/files/working b/tests/files/working deleted file mode 160000 index 545ffc7..0000000 --- a/tests/files/working +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 545ffc79786f268524c35e1e05b1770c7c74faf1 diff --git a/tests/files/working/ex_dir/ex.txt b/tests/files/working/ex_dir/ex.txt new file mode 100644 index 0000000..e69de29 diff --git a/tests/files/working/example.txt b/tests/files/working/example.txt new file mode 100644 index 0000000..8a3fb74 --- /dev/null +++ b/tests/files/working/example.txt @@ -0,0 +1,63 @@ +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 diff --git a/tests/units/test_log.rb b/tests/units/test_log.rb new file mode 100644 index 0000000..4dd6a2f --- /dev/null +++ b/tests/units/test_log.rb @@ -0,0 +1,26 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../test_helper' + +class TestInit < Test::Unit::TestCase + def setup + set_file_paths + @git = Git.open(@wdir) + end + + def test_get_log_entries + log = @git.log + assert(log.first.is_a? Git::Commit) + end + + def test_get_log_entries + assert_equal(30, @git.log.size) + assert_equal(50, @git.log(50).size) + assert_equal(10, @git.log(10).size) + end + + def test_get_log_to_s + assert_equal(@git.log.to_s.split("\n").first, @git.log.first.sha) + end + +end -- cgit