Skip to content
This repository was archived by the owner on Dec 13, 2023. It is now read-only.

Commit a234d71

Browse files
committed
[NEW] Adding Bazaar support: add misc, pull and push adapter modules with tests
Michael
1 parent e13f9d8 commit a234d71

File tree

7 files changed

+210
-0
lines changed

7 files changed

+210
-0
lines changed

lib/scm/adapters/bzr/misc.rb

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module Scm::Adapters
2+
class BzrAdapter < AbstractAdapter
3+
def exist?
4+
begin
5+
!!(head_token)
6+
rescue
7+
logger.debug { $! }
8+
false
9+
end
10+
end
11+
12+
def ls_tree(token)
13+
run("cd #{path} && bzr ls -V -r #{token}").split("\n")
14+
end
15+
end
16+
end

lib/scm/adapters/bzr/pull.rb

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module Scm::Adapters
2+
class BzrAdapter < AbstractAdapter
3+
4+
def pull(from, &block)
5+
raise ArgumentError.new("Cannot pull from #{from.inspect}") unless from.is_a?(BzrAdapter)
6+
logger.info { "Pulling #{from.url}" }
7+
8+
yield(0,1) if block_given? # Progress bar callback
9+
10+
unless self.exist?
11+
run "mkdir -p '#{self.url}'"
12+
run "rm -rf '#{self.url}'"
13+
run "bzr branch '#{from.url}' '#{self.url}'"
14+
else
15+
run "cd '#{self.url}' && bzr pull --overwrite '#{from.url}'"
16+
end
17+
18+
yield(1,1) if block_given? # Progress bar callback
19+
end
20+
21+
end
22+
end

lib/scm/adapters/bzr/push.rb

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module Scm::Adapters
2+
class BzrAdapter < AbstractAdapter
3+
4+
def push(to, &block)
5+
raise ArgumentError.new("Cannot push to #{to.inspect}") unless to.is_a?(BzrAdapter)
6+
logger.info { "Pushing to #{to.url}" }
7+
8+
yield(0,1) if block_given? # Progress bar callback
9+
10+
unless to.exist?
11+
if to.local?
12+
# Create a new repo on the same local machine. Just use existing pull code in reverse.
13+
to.pull(self)
14+
else
15+
run "ssh #{to.hostname} 'mkdir -p #{to.path}'"
16+
run "scp -rpqB #{bzr_path} #{to.hostname}:#{to.path}"
17+
end
18+
else
19+
run "cd '#{self.url}' && bzr push '#{to.url}'"
20+
end
21+
22+
yield(1,1) if block_given? # Progress bar callback
23+
end
24+
25+
def local?
26+
return true if hostname == Socket.gethostname
27+
return true if url =~ /^file:\/\//
28+
return true if url !~ /:/
29+
false
30+
end
31+
32+
def hostname
33+
$1 if url =~ /^ssh:\/\/([^\/]+)/
34+
end
35+
36+
def path
37+
case url
38+
when /^file:\/\/(.+)$/
39+
$1
40+
when /^ssh:\/\/[^\/]+(\/.+)$/
41+
$1
42+
when /^[^:]*$/
43+
url
44+
end
45+
end
46+
47+
def bzr_path
48+
path && File.join(path, '.bzr')
49+
end
50+
end
51+
end

lib/scm/adapters/bzr_adapter.rb

+3
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ def english_name
1010
require 'lib/scm/adapters/bzr/commits'
1111
require 'lib/scm/adapters/bzr/head'
1212
require 'lib/scm/adapters/bzr/cat_file'
13+
require 'lib/scm/adapters/bzr/misc'
14+
require 'lib/scm/adapters/bzr/pull'
15+
require 'lib/scm/adapters/bzr/push'

test/unit/bzr_misc_test.rb

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require File.dirname(__FILE__) + '/../test_helper'
2+
3+
module Scm::Adapters
4+
class BzrMiscTest < Scm::Test
5+
6+
def test_exist
7+
save_bzr = nil
8+
with_bzr_repository('bzr') do |bzr|
9+
save_bzr = bzr
10+
assert save_bzr.exist?
11+
end
12+
assert !save_bzr.exist?
13+
end
14+
15+
def test_ls_tree
16+
with_bzr_repository('bzr') do |bzr|
17+
assert_equal ['file1.txt',
18+
'file3.txt',
19+
'file4.txt',
20+
'file5.txt'],
21+
bzr.ls_tree(bzr.head_token).sort
22+
end
23+
end
24+
25+
end
26+
end

test/unit/bzr_pull_test.rb

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require File.dirname(__FILE__) + '/../test_helper'
2+
3+
module Scm::Adapters
4+
class BzrPullTest < Scm::Test
5+
6+
def test_pull
7+
with_bzr_repository('bzr') do |src|
8+
Scm::ScratchDir.new do |dest_dir|
9+
10+
dest = BzrAdapter.new(:url => dest_dir).normalize
11+
assert !dest.exist?
12+
13+
dest.pull(src)
14+
assert dest.exist?
15+
16+
assert_equal src.log, dest.log
17+
18+
# Commit some new code on the original and pull again
19+
src.run "cd '#{src.url}' && touch foo && bzr add foo && bzr whoami 'test <[email protected]>' && bzr commit -m test"
20+
assert_equal "test\n", src.commits.last.message
21+
assert_equal "test", src.commits.last.committer_name
22+
assert_equal "[email protected]", src.commits.last.committer_email
23+
24+
dest.pull(src)
25+
assert_equal src.log, dest.log
26+
end
27+
end
28+
end
29+
30+
end
31+
end

test/unit/bzr_push_test.rb

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
require File.dirname(__FILE__) + '/../test_helper'
2+
3+
module Scm::Adapters
4+
class BzrPushTest < Scm::Test
5+
6+
def test_hostname
7+
assert !BzrAdapter.new.hostname
8+
assert !BzrAdapter.new(:url => "http://www.ohloh.net/test").hostname
9+
assert !BzrAdapter.new(:url => "/Users/test/foo").hostname
10+
assert_equal "foo", BzrAdapter.new(:url => 'ssh://foo/bar').hostname
11+
end
12+
13+
def test_local
14+
assert !BzrAdapter.new(:url => "foo:/bar").local? # Assuming your machine is not named "foo" :-)
15+
assert !BzrAdapter.new(:url => "http://www.ohloh.net/foo").local?
16+
assert !BzrAdapter.new(:url => "ssh://host/Users/test/src").local?
17+
assert BzrAdapter.new(:url => "src").local?
18+
assert BzrAdapter.new(:url => "/Users/test/src").local?
19+
assert BzrAdapter.new(:url => "file:///Users/test/src").local?
20+
assert BzrAdapter.new(:url => "ssh://#{Socket.gethostname}/Users/test/src").local?
21+
end
22+
23+
def test_path
24+
assert_equal nil, BzrAdapter.new().path
25+
assert_equal nil, BzrAdapter.new(:url => "http://ohloh.net/foo").path
26+
assert_equal nil, BzrAdapter.new(:url => "https://ohloh.net/foo").path
27+
assert_equal "/Users/test/foo", BzrAdapter.new(:url => "file:///Users/test/foo").path
28+
assert_equal "/Users/test/foo", BzrAdapter.new(:url => "ssh://localhost/Users/test/foo").path
29+
assert_equal "/Users/test/foo", BzrAdapter.new(:url => "/Users/test/foo").path
30+
end
31+
32+
def test_bzr_path
33+
assert_equal nil, BzrAdapter.new().bzr_path
34+
assert_equal "/Users/test/src/.bzr", BzrAdapter.new(:url => "/Users/test/src").bzr_path
35+
end
36+
37+
def test_push
38+
with_bzr_repository('bzr') do |src|
39+
Scm::ScratchDir.new do |dest_dir|
40+
41+
dest = BzrAdapter.new(:url => dest_dir).normalize
42+
assert !dest.exist?
43+
44+
src.push(dest)
45+
assert dest.exist?
46+
assert_equal src.log, dest.log
47+
48+
# Commit some new code on the original and pull again
49+
src.run "cd '#{src.url}' && touch foo && bzr add foo && bzr whoami 'test <[email protected]>' && bzr commit -m test"
50+
assert_equal "test\n", src.commits.last.message
51+
assert_equal "test", src.commits.last.committer_name
52+
assert_equal "[email protected]", src.commits.last.committer_email
53+
54+
src.push(dest)
55+
assert_equal src.log, dest.log
56+
end
57+
end
58+
end
59+
60+
end
61+
end

0 commit comments

Comments
 (0)