From de071dcd8dee3d853291a5077b9dcdec07dd5362 Mon Sep 17 00:00:00 2001 From: scott Chacon Date: Fri, 16 Nov 2007 11:23:24 -0800 Subject: added some low level tree operations and tests --- lib/git.rb | 2 +- lib/git/base.rb | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- lib/git/index.rb | 1 + lib/git/lib.rb | 26 ++++++++++++++++++++ lib/git/path.rb | 4 ++++ 5 files changed, 103 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/git.rb b/lib/git.rb index b1d55af..f18ad11 100644 --- a/lib/git.rb +++ b/lib/git.rb @@ -40,7 +40,7 @@ require 'git/author' # License:: MIT License module Git - VERSION = '1.0.2' + VERSION = '1.0.3' # open a bare repository # diff --git a/lib/git/base.rb b/lib/git/base.rb index 4e1e125..6372beb 100644 --- a/lib/git/base.rb +++ b/lib/git/base.rb @@ -90,6 +90,17 @@ module Git @index end + + def set_working(work_dir, check = true) + @lib = nil + @working_directory = Git::WorkingDirectory.new(work_dir.to_s, check) + end + + def set_index(index_file, check = true) + @lib = nil + @index = Git::Index.new(index_file.to_s, check) + end + # changes current working directory for a block # to the git working directory # @@ -186,7 +197,7 @@ module Git # actual 'git' forked system calls. At some point I hope to replace the Git::Lib # class with one that uses native methods or libgit C bindings def lib - Git::Lib.new(self) + @lib ||= Git::Lib.new(self) end # will run a grep for 'string' on the HEAD of the git repository @@ -330,6 +341,65 @@ module Git self.lib.repack end + + ## LOWER LEVEL INDEX OPERATIONS ## + + def with_index(new_index) + old_index = @index + set_index(new_index, false) + return_value = yield @index + set_index(old_index) + return_value + end + + def with_temp_index &blk + tempfile = Tempfile.new('temp-index') + temp_path = tempfile.path + tempfile.unlink + with_index(temp_path, &blk) + end + + def read_tree(treeish, opts = {}) + self.lib.read_tree(treeish, opts) + end + + def write_tree + self.lib.write_tree + end + + def commit_tree(tree = nil, opts = {}) + Git::Object::Commit.new(self, self.lib.commit_tree(tree, opts)) + end + + def write_and_commit_tree(opts = {}) + tree = write_tree + commit_tree(tree, opts) + end + + def ls_files + self.lib.ls_files + end + + def with_working(work_dir) + return_value = false + old_working = @working_directory + set_working(work_dir) + Dir.chdir work_dir do + return_value = yield @working_directory + end + set_working(old_working) + return_value + end + + def with_temp_working &blk + tempfile = Tempfile.new("temp-workdir") + temp_dir = tempfile.path + tempfile.unlink + Dir.mkdir(temp_dir, 0700) + with_working(temp_dir, &blk) + end + + # runs git rev-parse to convert the objectish to a full sha # # @git.revparse("HEAD^^") diff --git a/lib/git/index.rb b/lib/git/index.rb index b96eedb..c27820d 100644 --- a/lib/git/index.rb +++ b/lib/git/index.rb @@ -1,4 +1,5 @@ module Git class Index < Git::Path + end end diff --git a/lib/git/lib.rb b/lib/git/lib.rb index c196312..690043d 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -350,6 +350,32 @@ module Git command('repack', ['-a', '-d']) end + # reads a tree into the current index file + def read_tree(treeish, opts = {}) + arr_opts = [] + arr_opts << "--prefix=#{opts[:prefix]}" if opts[:prefix] + arr_opts << treeish.to_a.join(' ') + command('read-tree', arr_opts) + end + + def write_tree + command('write-tree') + end + + def commit_tree(tree, opts = {}) + opts[:message] = "commit tree #{tree}" if !opts[:message] + t = Tempfile.new('commit-message') do |t| + t.write(opts[:message]) + end + + arr_opts = [] + arr_opts << tree + arr_opts << "-p #{opts[:parent]}" if opts[:parent] + opts[:parents].each { |p| arr_opts << "-p #{p.to_s}" } if opts[:parents] + arr_opts << "< #{t.path}" + command('commit-tree', arr_opts) + end + # creates an archive file # # options diff --git a/lib/git/path.rb b/lib/git/path.rb index e429d6f..87f5c84 100644 --- a/lib/git/path.rb +++ b/lib/git/path.rb @@ -19,5 +19,9 @@ module Git File.writable?(@path) end + def to_s + @path + end + end end \ No newline at end of file -- cgit