Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Feature/workdir #120

Closed
wants to merge 4 commits into from

2 participants

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
1  grit.gemspec
@@ -23,6 +23,7 @@ Gem::Specification.new do |s|
s.add_dependency('posix-spawn', "~> 0.3.6")
s.add_dependency('mime-types', "~> 1.15")
s.add_dependency('diff-lcs', "~> 1.1")
+ s.add_dependency("activesupport", "~> 3.2.0")
s.add_development_dependency('mocha')
View
1  lib/grit.rb
@@ -10,6 +10,7 @@
require 'digest/sha1'
# third party
+require 'active_support/core_ext/array/extract_options' # extract_options
begin
require 'mime/types'
View
12 lib/grit/git.rb
@@ -93,9 +93,11 @@ def self.with_timeout(timeout = 10)
attr_accessor :git_dir, :bytes_read, :work_tree
- def initialize(git_dir)
+ def initialize(git_dir, options = {})
+ options, self.work_tree = {}, options if !options.is_a?(Hash)
+ self.work_tree = nil if self.work_tree && self.work_tree.length <= 0
self.git_dir = git_dir
- self.work_tree = git_dir.gsub(/\/\.git$/,'')
+ self.work_tree = options[:working_dir] || git_dir.gsub(/\/\.git$/,'') if !self.work_tree || (self.work_tree && !self.work_tree.is_a?(String))
self.bytes_read = 0
end
@@ -282,6 +284,8 @@ def apply_patch(options={}, head_sha=nil, patch=nil)
# use that number of seconds; when false or 0, disable timeout.
# :base - Set false to avoid passing the --git-dir argument when
# invoking the git command.
+ # :work - Set true to pass the --work-tree argument when
+ # invoking the git command if work_tree exists.
# :env - Hash of environment variable key/values that are set on the
# child process.
# :raise - When set true, commands that exit with a non-zero status
@@ -328,12 +332,16 @@ def native(cmd, options = {}, *args, &block)
input = options.delete(:input)
timeout = options.delete(:timeout); timeout = true if timeout.nil?
base = options.delete(:base); base = true if base.nil?
+ work = options.delete(:work); work = false if work.nil?
chdir = options.delete(:chdir)
# build up the git process argv
argv = []
argv << Git.git_binary
argv << "--git-dir=#{git_dir}" if base
+ if work && work_tree && work_tree.length > 0
+ argv << "--work-tree=#{work_tree}"
+ end
argv << cmd.to_s.tr('_', '-')
argv.concat(options_to_argv(options))
argv.concat(args)
View
37 lib/grit/repo.rb
@@ -46,6 +46,7 @@ def initialize(path, options = {})
@bare = false
elsif File.exist?(epath) && (epath =~ /\.git$/ || options[:is_bare])
self.path = epath
+ self.working_dir = options[:working_dir] if options[:working_dir]
@bare = true
elsif File.exist?(epath)
raise InvalidGitRepositoryError.new(epath)
@@ -53,7 +54,7 @@ def initialize(path, options = {})
raise NoSuchPathError.new(epath)
end
- self.git = Git.new(self.path)
+ self.git = Git.new(self.path, self.working_dir)
end
# Public: Initialize a git repository (create it on the filesystem). By
@@ -234,20 +235,44 @@ def head
# Commits current index
#
# Returns true/false if commit worked
- def commit_index(message)
- self.git.commit({}, '-m', message)
+ def commit_index(message, options = {})
+ self.git.commit(options, '-m', message)
end
# Commits all tracked and modified files
#
# Returns true/false if commit worked
- def commit_all(message)
- self.git.commit({}, '-a', '-m', message)
+ def commit_all(message, options = {})
+ self.git.commit(options, '-a', '-m', message)
end
# Adds files to the index
+ #
+ # *files - the added multi file name related to working_dir
+ # options - Command line option arguments passed to the git command.
+ # Single char keys are converted to short options (:a => -a).
+ # Multi-char keys are converted to long options (:arg => '--arg').
+ # Underscores in keys are converted to dashes. These special options
+ # are used to control command execution and are not passed in command
+ # invocation:
+ # :timeout - Maximum amount of time the command can run for before
+ # being aborted. When true, use Grit::Git.git_timeout; when numeric,
+ # use that number of seconds; when false or 0, disable timeout.
+ # :base - Set false to avoid passing the --git-dir argument when
+ # invoking the git command.
+ # :work - Set true to pass the --work-tree argument when
+ # invoking the git command if work_tree exists.
+ # :env - Hash of environment variable key/values that are set on the
+ # child process.
+ # :raise - When set true, commands that exit with a non-zero status
+ # raise a CommandFailed exception. This option is available only on
+ # platforms that support fork(2).
+ # :process_info - By default, a single string with output written to
+ # the process's stdout is returned. Setting this option to true
+ # results in a [exitstatus, out, err] tuple being returned instead.
def add(*files)
- self.git.add({}, *files.flatten)
+ options = files.extract_options!
+ self.git.add(options, *files.flatten)
end
# Remove files from the index
View
18 test/test_repo.rb
@@ -10,7 +10,7 @@ def create_temp_repo(clone_path)
tmp_path = File.join("/tmp/", filename)
FileUtils.mkdir_p(tmp_path)
FileUtils.cp_r(clone_path, tmp_path)
- File.join(tmp_path, 'dot_git')
+ File.join(tmp_path, File.basename(clone_path))
end
def test_update_refs_packed
@@ -416,4 +416,20 @@ def test_select_existing_objects
after = ['634396b2f541a9f2d58b00be1a07f0c358b999b3']
assert_equal after, @r.git.select_existing_objects(before)
end
+
+ def test_bare_repo_add_commit
+# Grit.debug = true
+ org_dir=File.join(File.dirname(__FILE__), *%w[dot_git_spaces])
+ working_dir = File.join(File.dirname(__FILE__), 'fixtures')
+ gpath = create_temp_repo(org_dir)
+ git = Grit::Repo.new(gpath, :is_bare => true, :working_dir => working_dir)
+ assert_equal working_dir, git.working_dir
+ Dir.chdir(working_dir)
+ git.add 'ls_files', :work => true
+ result = git.commit_index('commit ls_files should ok.', :work => true)
+ assert_match(/commit ls_files should ok.\n 4 files changed, 17 insertions\(\+\), 3 deletions\(-\)\n/, result)
+# Grit.debug = false
+
+ FileUtils.rm_r(gpath)
+ end
end
View
21 test/test_rubygit_alt.rb
@@ -7,6 +7,7 @@ def setup
@git1 = Grit::Repo.new(File.join(File.dirname(__FILE__), *%w[dot_git]), :is_bare => true)
@git2 = Grit::Repo.new(File.join(File.dirname(__FILE__), *%w[dot_git_clone]), :is_bare => true)
@git3 = Grit::Repo.new(File.join(File.dirname(__FILE__), *%w[dot_git_clone2]), :is_bare => true)
+ @git4 = Grit::Repo.new(File.join(File.dirname(__FILE__), *%w[dot_git_clone]), :is_bare => true, :working_dir => 'my_working_dir')
@commit_sha = 'ca8a30f5a7f0f163bbe3b6f0abf18a6c83b0687a'
@tree_sha = 'cd7422af5a2e0fff3e94d6fb1a8fff03b2841881'
@blob_sha = '4232d073306f01cf0b895864e5a5cfad7dd76fce'
@@ -37,4 +38,22 @@ def test_tree_path
assert_equal file, 'test/test_head.rb'
end
-end
+ def test_bare
+ assert @git1.bare
+ assert @git2.bare
+ assert @git3.bare
+ assert @git4.bare
+ end
+
+ def test_working_dir
+ assert_equal nil, @git1.working_dir
+ assert_equal nil, @git2.working_dir
+ assert_equal nil, @git3.working_dir
+ assert_equal 'my_working_dir', @git4.git.work_tree
+ git = Grit::Git.new(File.join(File.dirname(__FILE__), *%w[dot_git_clone]), :working_dir => 'my_working_dir1')
+ assert_equal 'my_working_dir1', git.work_tree
+ git = Grit::Git.new(File.join(File.dirname(__FILE__), *%w[dot_git_clone]), 'my_working_dir2')
+ assert_equal 'my_working_dir2', git.work_tree
+ end
+
+end
Something went wrong with that request. Please try again.