Skip to content

Commit

Permalink
Add a "Local" source
Browse files Browse the repository at this point in the history
This allows you to specify a local source for fast lookups. Currently,
it doesn't support an url (as it's local), but it would be nice to be
able to point to a gitweb site. To do this, we probably have to extend
the repo list format, though.
  • Loading branch information
pieter committed Jul 22, 2008
1 parent b8428a0 commit 1d544b4
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 4 deletions.
54 changes: 54 additions & 0 deletions lib/git/sources/local.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion lib/git/sources/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ def find_public(url)
end

require 'git/sources/gitweb'
require 'git/sources/repo'
require 'git/sources/repo'
require 'git/sources/local'
27 changes: 26 additions & 1 deletion test/gitweb/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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: <file://" + GIT_DIR + " HEAD>")
assert_nil(h)
end

def test_local_access
h = parse("This is a head: <tmptestgitdir HEAD>")
assert(h)
end
end

FileUtils.remove_dir(GIT_DIR)
27 changes: 25 additions & 2 deletions test/gitweb/plugin.rb
Original file line number Diff line number Diff line change
@@ -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 = {}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -82,4 +94,15 @@ def test_external_path
@web.hook_privmsg_chan(@irc, "Look at <repo:etorrent.git HEAD>")
assert_match(/^\[etorrent HEAD\]: http.* -- .*/, @irc.message)
end
end

def test_unacessible_repo_nil
@web.hook_privmsg_chan(@irc, "This is local: <file://" + GIT_DIR + " HEAD>")
assert_nil(@irc.message)
end

def test_accessible_local_repo
@web.hook_privmsg_chan(@irc, "This is local: <tmptestgitdir HEAD>")
assert(@irc.message)
assert_equal("[tmptestgitdir HEAD]: -- First commit", @irc.message)
end
end
65 changes: 65 additions & 0 deletions test/gitweb/sources/local.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/gitweb/test_repos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ carnique:
- "repo:git.git"
- "repo:egit.git"
- "http://git.frim.nl/?p=gitbot.git"
- "file:///tmp/tmptestgitdir"

0 comments on commit 1d544b4

Please sign in to comment.