Commit 3ea514188523ae9e8ecd603f760442f147042d6c
- Date: Mon Feb 18 07:19:01 +0000 2008
- Committer: rick (technoweenie@gmail.com)
- Author: Tom Preston-Werner (tom@mojombo.com)
- Commit SHA1: 3ea514188523ae9e8ecd603f760442f147042d6c
- Tree SHA1: d03ebb928ede2410e267dcd6e4464a1843d458ff
add alternates getter and setter
Commit diff
| |   |
| 245 | 245 | end |
| 246 | 246 | end |
| 247 | 247 | |
| 248 | # The list of alternates for this repo |
| 249 | # |
| 250 | # Returns Array[String] (pathnames of alternates) |
| 251 | def alternates |
| 252 | alternates_path = File.join(self.path, *%w{objects info alternates}) |
| 253 | |
| 254 | if File.exist?(alternates_path) |
| 255 | File.read(alternates_path).strip.split("\n") |
| 256 | else |
| 257 | [] |
| 258 | end |
| 259 | end |
| 260 | |
| 261 | # Sets the alternates |
| 262 | # +alts+ is the Array of String paths representing the alternates |
| 263 | # |
| 264 | # Returns nothing |
| 265 | def alternates=(alts) |
| 266 | alts.each do |alt| |
| 267 | unless File.exist?(alt) |
| 268 | raise "Could not set alternates. Alternate path #{alt} must exist" |
| 269 | end |
| 270 | end |
| 271 | |
| 272 | File.open(File.join(self.path, *%w{objects info alternates}), 'w') do |f| |
| 273 | f.write alts.join("\n") |
| 274 | end |
| 275 | end |
| 276 | |
| 248 | 277 | # Pretty object inspection |
| 249 | 278 | def inspect |
| 250 | 279 | %Q{#<Grit::Repo "#{@path}">} |
| toggle raw diff |
--- a/lib/grit/repo.rb
+++ b/lib/grit/repo.rb
@@ -245,6 +245,35 @@ module Grit
end
end
+ # The list of alternates for this repo
+ #
+ # Returns Array[String] (pathnames of alternates)
+ def alternates
+ alternates_path = File.join(self.path, *%w{objects info alternates})
+
+ if File.exist?(alternates_path)
+ File.read(alternates_path).strip.split("\n")
+ else
+ []
+ end
+ end
+
+ # Sets the alternates
+ # +alts+ is the Array of String paths representing the alternates
+ #
+ # Returns nothing
+ def alternates=(alts)
+ alts.each do |alt|
+ unless File.exist?(alt)
+ raise "Could not set alternates. Alternate path #{alt} must exist"
+ end
+ end
+
+ File.open(File.join(self.path, *%w{objects info alternates}), 'w') do |f|
+ f.write alts.join("\n")
+ end
+ end
+
# Pretty object inspection
def inspect
%Q{#<Grit::Repo "#{@path}">} |
| |   |
| 196 | 196 | @r.disable_daemon_serve |
| 197 | 197 | end |
| 198 | 198 | |
| 199 | # alternates |
| 200 | |
| 201 | def test_alternates_with_two_alternates |
| 202 | File.expects(:exist?).with('/Users/tom/dev/mojombo/grit/.git/objects/info/alternates').returns(true) |
| 203 | File.expects(:read).returns("/path/to/repo1/.git/objects\n/path/to/repo2.git/objects\n") |
| 204 | |
| 205 | assert_equal ["/path/to/repo1/.git/objects", "/path/to/repo2.git/objects"], @r.alternates |
| 206 | end |
| 207 | |
| 208 | def test_alternates_no_file |
| 209 | File.expects(:exist?).returns(false) |
| 210 | |
| 211 | assert_equal [], @r.alternates |
| 212 | end |
| 213 | |
| 214 | # alternates= |
| 215 | |
| 216 | def test_alternates_setter_ok |
| 217 | alts = %w{/path/to/repo.git/objects /path/to/repo2.git/objects} |
| 218 | |
| 219 | alts.each do |alt| |
| 220 | File.expects(:exist?).with(alt).returns(true) |
| 221 | end |
| 222 | |
| 223 | File.any_instance.expects(:write).with(alts.join("\n")) |
| 224 | |
| 225 | assert_nothing_raised do |
| 226 | @r.alternates = alts |
| 227 | end |
| 228 | end |
| 229 | |
| 230 | def test_alternates_setter_bad |
| 231 | alts = %w{/path/to/repo.git/objects} |
| 232 | |
| 233 | alts.each do |alt| |
| 234 | File.expects(:exist?).with(alt).returns(false) |
| 235 | end |
| 236 | |
| 237 | File.any_instance.expects(:write).never |
| 238 | |
| 239 | assert_raise RuntimeError do |
| 240 | @r.alternates = alts |
| 241 | end |
| 242 | end |
| 243 | |
| 199 | 244 | # inspect |
| 200 | 245 | |
| 201 | 246 | def test_inspect |
| toggle raw diff |
--- a/test/test_repo.rb
+++ b/test/test_repo.rb
@@ -196,6 +196,51 @@ class TestRepo < Test::Unit::TestCase
@r.disable_daemon_serve
end
+ # alternates
+
+ def test_alternates_with_two_alternates
+ File.expects(:exist?).with('/Users/tom/dev/mojombo/grit/.git/objects/info/alternates').returns(true)
+ File.expects(:read).returns("/path/to/repo1/.git/objects\n/path/to/repo2.git/objects\n")
+
+ assert_equal ["/path/to/repo1/.git/objects", "/path/to/repo2.git/objects"], @r.alternates
+ end
+
+ def test_alternates_no_file
+ File.expects(:exist?).returns(false)
+
+ assert_equal [], @r.alternates
+ end
+
+ # alternates=
+
+ def test_alternates_setter_ok
+ alts = %w{/path/to/repo.git/objects /path/to/repo2.git/objects}
+
+ alts.each do |alt|
+ File.expects(:exist?).with(alt).returns(true)
+ end
+
+ File.any_instance.expects(:write).with(alts.join("\n"))
+
+ assert_nothing_raised do
+ @r.alternates = alts
+ end
+ end
+
+ def test_alternates_setter_bad
+ alts = %w{/path/to/repo.git/objects}
+
+ alts.each do |alt|
+ File.expects(:exist?).with(alt).returns(false)
+ end
+
+ File.any_instance.expects(:write).never
+
+ assert_raise RuntimeError do
+ @r.alternates = alts
+ end
+ end
+
# inspect
def test_inspect |