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

Commit e13f9d8

Browse files
committed
[NEW] Adding Bazaar support: add cat_file.rb adapter module with unit tests
Michael
1 parent 96e67d1 commit e13f9d8

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

lib/scm/adapters/bzr/cat_file.rb

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module Scm::Adapters
2+
class BzrAdapter < AbstractAdapter
3+
def cat_file(commit, diff)
4+
cat(commit.token, diff.path)
5+
end
6+
7+
def cat_file_parent(commit, diff)
8+
p = parents(commit)
9+
cat(p.first.token, diff.path) if p.first
10+
end
11+
12+
def cat(revision, path)
13+
out, err = run_with_err("cd '#{url}' && bzr cat -r #{revision} #{escape(path)}")
14+
return nil if err =~ / is not present in revision /
15+
raise RuntimeError.new(err) unless err.to_s == ''
16+
out
17+
end
18+
19+
# Escape bash-significant characters in the filename
20+
# Example:
21+
# "Foo Bar & Baz" => "Foo\ Bar\ \&\ Baz"
22+
def escape(path)
23+
path.gsub(/[ '"&]/) { |c| '\\' + c }
24+
end
25+
end
26+
end

lib/scm/adapters/bzr_adapter.rb

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ def english_name
99
require 'lib/scm/adapters/bzr/validation'
1010
require 'lib/scm/adapters/bzr/commits'
1111
require 'lib/scm/adapters/bzr/head'
12+
require 'lib/scm/adapters/bzr/cat_file'

test/unit/bzr_cat_file_test.rb

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require File.dirname(__FILE__) + '/../test_helper'
2+
3+
module Scm::Adapters
4+
class BzrCatFileTest < Scm::Test
5+
6+
def test_cat_file
7+
with_bzr_repository('bzr') do |bzr|
8+
expected = <<-EXPECTED
9+
first file
10+
second line
11+
EXPECTED
12+
assert_equal expected,
13+
bzr.cat_file(Scm::Commit::new(:token => 6),
14+
Scm::Diff.new(:path => "file1.txt"))
15+
16+
# file2.txt has been removed in commit #5
17+
assert_equal nil, bzr.cat_file(bzr.head,
18+
Scm::Diff.new(:path => "file2.txt"))
19+
end
20+
end
21+
22+
def test_cat_file_parent
23+
with_bzr_repository('bzr') do |bzr|
24+
expected = <<-EXPECTED
25+
first file
26+
second line
27+
EXPECTED
28+
assert_equal expected,
29+
bzr.cat_file_parent(Scm::Commit::new(:token => 6),
30+
Scm::Diff.new(:path => "file1.txt"))
31+
32+
# file2.txt has been removed in commit #5
33+
expected = <<-EXPECTED
34+
another file
35+
EXPECTED
36+
assert_equal expected,
37+
bzr.cat_file_parent(Scm::Commit.new(:token => 5),
38+
Scm::Diff.new(:path => "file2.txt"))
39+
end
40+
end
41+
42+
end
43+
end

0 commit comments

Comments
 (0)