diff --git a/lib/git/sources/local.rb b/lib/git/sources/local.rb new file mode 100644 index 0000000..c5bed37 --- /dev/null +++ b/lib/git/sources/local.rb @@ -0,0 +1,54 @@ +class Git::Source::Local + + Git::Source::Source.add_handler(/file:\/\//, self) + def self.public?; false; end + + class RepositoryNotFoundError < RuntimeError; end + def initialize(url) + url =~ /file:\/\/(.*)/ + @location = $1 + command("git rev-parse") + ret = $? + raise RepositoryNotFoundError.new unless ret == 0 + end + + def name + return File.basename(@location).gsub(/\.git$/, "") + end + + def matches?(match) + return name =~ /#{match}/ + end + + def command(cmd) + `(cd #{@location} && #{cmd}) 2>/dev/null`.strip + end + + def lookup(ref, file) + if file + file_add = ":#{file}" + else + file_add = "" + end + + type = command("git cat-file -t #{ref}#{file_add} 2> /dev/null") + return nil unless $? == 0 + subject = nil + if type == "commit" + details = command("git cat-file commit #{ref}") + if details =~ /\n\n(.+)$/ + subject = $1 + end + end + # Point to commitdiff and not commitpage when referring a commit + return { + :file => file, + :ref => ref, + :type => type, + :url => nil, + :subject => subject, + :reponame => name + } + end + +end \ No newline at end of file diff --git a/lib/git/sources/source.rb b/lib/git/sources/source.rb index cda9a35..aed68c1 100644 --- a/lib/git/sources/source.rb +++ b/lib/git/sources/source.rb @@ -32,4 +32,5 @@ def find_public(url) end require 'git/sources/gitweb' -require 'git/sources/repo' \ No newline at end of file +require 'git/sources/repo' +require 'git/sources/local' \ No newline at end of file diff --git a/test/gitweb/loader.rb b/test/gitweb/loader.rb index f4ca4b3..33a44b3 100644 --- a/test/gitweb/loader.rb +++ b/test/gitweb/loader.rb @@ -5,15 +5,28 @@ require 'git' require 'test/unit' require 'ostruct' +require 'fileutils' +# Create a simple repository +GIT_DIR = "/tmp/tmptestgitdir" +FileUtils.remove_dir(GIT_DIR) if File.exist?(GIT_DIR) +`mkdir #{GIT_DIR}; cd #{GIT_DIR}; git init; touch a; git add a; git commit -m "First commit"` +`mkdir #{GIT_DIR}; cd #{GIT_DIR}; git init; touch a; git add a; git commit -m "First commit"` TEST_CONFIG = File.join(File.dirname(__FILE__), "test_repos.yml") class GitwebTest < Test::Unit::TestCase def setup + # Create a simple repository + FileUtils.remove_dir(GIT_DIR) if File.exist?(GIT_DIR) + `mkdir #{GIT_DIR}; cd #{GIT_DIR}; git init; touch a; git add a; git commit -m "First commit"` + @runner = Git.new(TEST_CONFIG) @channel = OpenStruct.new({:nname => "#pieter", :server => OpenStruct.new({:name => "carnique"})}) - + end + + def teardown + FileUtils.remove_dir(GIT_DIR) end def parse(message) @@ -148,4 +161,16 @@ def test_parse_external_gitweb assert_equal("gitbot", h[:reponame]) assert_equal("HEAD", h[:ref]) end + + def test_inacessible_local + h = parse("This is local: ") + assert_nil(h) + end + + def test_local_access + h = parse("This is a head: ") + assert(h) + end end + +FileUtils.remove_dir(GIT_DIR) diff --git a/test/gitweb/plugin.rb b/test/gitweb/plugin.rb index 79fffc9..1e4fe87 100644 --- a/test/gitweb/plugin.rb +++ b/test/gitweb/plugin.rb @@ -1,11 +1,15 @@ $: << File.join(File.dirname(__FILE__), "..", "..", "lib") $: << File.join(File.dirname(__FILE__), "..", "..") + +require 'fileutils' require 'lib/pluginbase' require 'plugins/irc' require 'plugins/gitweb' require 'test/unit' require 'ostruct' +GIT_DIR = "/tmp/tmptestgitdir" + TEST_CONFIG = File.join(File.dirname(__FILE__), "test_repos.yml") $config = { "plugins/gitweb/configfile" => TEST_CONFIG} $hooks = {} @@ -38,11 +42,19 @@ def reply(message) class GitwebPluginTest < Test::Unit::TestCase - def setup + def setup + # Create a simple repository + FileUtils.remove_dir(GIT_DIR) if File.exist?(GIT_DIR) + `mkdir #{GIT_DIR}; cd #{GIT_DIR}; git init; touch a; git add a; git commit -m "First commit"` + @irc = MockIrc.new @web = Gitweb.new end + def teardown + FileUtils.remove_dir(GIT_DIR) + end + def test_nil_irc_reply @web.hook_privmsg_chan(@irc, "A failed ref should output nothing: 324aabbb3434") assert_equal(@irc.message, nil) @@ -82,4 +94,15 @@ def test_external_path @web.hook_privmsg_chan(@irc, "Look at ") assert_match(/^\[etorrent HEAD\]: http.* -- .*/, @irc.message) end -end + + def test_unacessible_repo_nil + @web.hook_privmsg_chan(@irc, "This is local: ") + assert_nil(@irc.message) + end + + def test_accessible_local_repo + @web.hook_privmsg_chan(@irc, "This is local: ") + assert(@irc.message) + assert_equal("[tmptestgitdir HEAD]: -- First commit", @irc.message) + end +end \ No newline at end of file diff --git a/test/gitweb/sources/local.rb b/test/gitweb/sources/local.rb new file mode 100644 index 0000000..1b47c63 --- /dev/null +++ b/test/gitweb/sources/local.rb @@ -0,0 +1,65 @@ +$: << File.join(File.dirname(__FILE__), "..", "..", "..", "lib") +require 'git' +require 'fileutils' +require 'test/unit' +require 'ostruct' + +GIT_DIR = "/tmp/tmptestgitdir" +FileUtils.remove_dir(GIT_DIR) if File.exist?(GIT_DIR) + +`mkdir #{GIT_DIR}; cd #{GIT_DIR}; git init; touch a; mkdir b; touch b/c; git add a; git commit -m "First commit"; git add b/c; git commit -m "a commit";` + +class GitwebTest < Test::Unit::TestCase + + def setup + @source = Git::Source::Local.new("file://" + GIT_DIR) + end + + def test_false_repo + assert_raise(Git::Source::Local::RepositoryNotFoundError) do + Git::Source::Local.new("file:///tmp/dosutaosu") + end + end + + def test_matches + assert(@source.matches?(/tmptest/)) + assert(@source.matches?(/tmptestgitdir/)) + assert(!@source.matches?(/temp-test/)) + end + + def test_name + assert_equal("tmptestgitdir", @source.name) + end + + def test_lookup_HEAD + l = @source.lookup("HEAD", nil) + assert(l) + assert_equal(nil, l[:url]) + assert_equal("commit", l[:type]) + assert_equal("a commit", l[:subject]) + end + + def test_lookup_false_branch + l = @source.lookup("Nonexisting branch", nil) + assert_nil(l) + end + + def test_lookup_tree + l = @source.lookup("HEAD", "b") + assert(l) + assert_equal("tree", l[:type]) + assert_equal("b", l[:file]) + end + + def test_lookup_blob + l = @source.lookup("HEAD", "a") + assert(l) + assert_equal("blob", l[:type]) + assert_equal("a", l[:file]) + end + + def test_create + a = Git::Source::Source.create("file://" + GIT_DIR) + assert(a.is_a?(Git::Source::Local)) + end +end diff --git a/test/gitweb/test_repos.yml b/test/gitweb/test_repos.yml index 9d462ca..6207e92 100644 --- a/test/gitweb/test_repos.yml +++ b/test/gitweb/test_repos.yml @@ -4,3 +4,4 @@ carnique: - "repo:git.git" - "repo:egit.git" - "http://git.frim.nl/?p=gitbot.git" + - "file:///tmp/tmptestgitdir" \ No newline at end of file