Skip to content

Commit

Permalink
abstract all file system calls into git class
Browse files Browse the repository at this point in the history
  • Loading branch information
mojombo committed Apr 21, 2009
1 parent 0826e1a commit afe9c14
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 18 deletions.
29 changes: 29 additions & 0 deletions lib/grit/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,35 @@ def shell_escape(str)
end
alias_method :e, :shell_escape

# Read a normal file from the filesystem.
# +file+ is the relative path from the Git dir
#
# Returns the String contents of the file
def fs_read(file)
File.open(File.join(self.git_dir, file)).read
end

# Write a normal file to the filesystem.
# +file+ is the relative path from the Git dir
# +contents+ is the String content to be written
#
# Returns nothing
def fs_write(file, contents)
path = File.join(self.git_dir, file)
FileUtils.mkdir_p(File.dirname(path))
File.open(path, 'w') do |f|
f.write(contents)
end
end

# Delete a normal file from the filesystem
# +file+ is the relative path from the Git dir
#
# Returns nothing
def fs_delete(file)
FileUtils.rm_f(File.join(self.git_dir,file))
end

# Run the given git command with the specified arguments and return
# the result as a String
# +cmd+ is the command
Expand Down
2 changes: 1 addition & 1 deletion lib/grit/ref.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class Head < Ref
#
# Returns Grit::Head (baked)
def self.current(repo, options = {})
head = File.open(File.join(repo.path, 'HEAD')).read.chomp
head = repo.git.fs_read('HEAD').chomp
if /ref: refs\/heads\/(.*)/.match(head)
self.new($1, repo.git.rev_parse(options, 'HEAD'))
end
Expand Down
22 changes: 6 additions & 16 deletions lib/grit/repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def self.init(path)
#
# Returns String
def description
File.open(File.join(self.path, 'description')).read.chomp
self.git.fs_read('description').chomp
end

def blame(file, commit = nil)
Expand Down Expand Up @@ -358,15 +358,15 @@ def archive_to_file(treeish = 'master', prefix = nil, filename = 'archive.tar.gz
#
# Returns nothing
def enable_daemon_serve
FileUtils.touch(File.join(self.path, DAEMON_EXPORT_FILE))
self.git.fs_write(DAEMON_EXPORT_FILE, '')
end

# Disable git-daemon serving of this repository by ensuring there is no
# git-daemon-export-ok file in its git directory
#
# Returns nothing
def disable_daemon_serve
FileUtils.rm_f(File.join(self.path, DAEMON_EXPORT_FILE))
self.git.fs_delete(DAEMON_EXPORT_FILE)
end

def gc_auto
Expand Down Expand Up @@ -398,13 +398,9 @@ def alternates=(alts)
end

if alts.empty?
File.open(File.join(self.path, *%w{objects info alternates}), 'w') do |f|
f.write ''
end
self.git.fs_write('objects/info/alternates', '')
else
File.open(File.join(self.path, *%w{objects info alternates}), 'w') do |f|
f.write alts.join("\n")
end
self.git.fs_write('objects/info/alternates', alts.join("\n"))
end
end

Expand All @@ -418,14 +414,8 @@ def index

def update_ref(head, commit_sha)
return nil if !commit_sha || (commit_sha.size != 40)

ref_heads = File.join(self.path, 'refs', 'heads')
FileUtils.mkdir_p(ref_heads)
File.open(File.join(ref_heads, head), 'w') do |f|
f.write(commit_sha)
end
self.git.fs_write("refs/heads/#{head}", commit_sha)
commit_sha

end

# Pretty object inspection
Expand Down
20 changes: 20 additions & 0 deletions test/test_git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,24 @@ def test_piping_should_work_on_1_9
@git.expects(:sh).with("#{Git.git_binary} --git-dir='#{@git.git_dir}' archive 'master' | gzip")
@git.archive({}, "master", "| gzip")
end

def test_fs_read
f = stub
f.expects(:read).returns('bar')
File.expects(:open).with(File.join(@git.git_dir, 'foo')).returns(f)
assert_equal 'bar', @git.fs_read('foo')
end

def test_fs_write
f = stub
f.expects(:write).with('baz')
FileUtils.expects(:mkdir_p).with(File.join(@git.git_dir, 'foo'))
File.expects(:open).with(File.join(@git.git_dir, 'foo/bar'), 'w').yields(f)
@git.fs_write('foo/bar', 'baz')
end

def test_fs_delete
FileUtils.expects(:rm_f).with(File.join(@git.git_dir, 'foo'))
@git.fs_delete('foo')
end
end
4 changes: 3 additions & 1 deletion test/test_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,9 @@ def test_archive_tar_gz
# enable_daemon_serve

def test_enable_daemon_serve
FileUtils.expects(:touch).with(File.join(@r.path, 'git-daemon-export-ok'))
f = stub
f.expects("write").with('')
File.expects(:open).with(File.join(@r.path, 'git-daemon-export-ok'), 'w').yields(f)
@r.enable_daemon_serve
end

Expand Down

0 comments on commit afe9c14

Please sign in to comment.