Permalink
Browse files

Add Repo#init.

  • Loading branch information...
1 parent 0d9d9d7 commit 886960895d751d209bbece4f36d8943f36507c2a @mojombo committed Aug 31, 2010
Showing with 140 additions and 68 deletions.
  1. +4 −0 History.txt
  2. +2 −2 lib/grit/git-ruby.rb
  3. +7 −2 lib/grit/git.rb
  4. +117 −64 lib/grit/repo.rb
  5. +10 −0 test/test_repo.rb
View
@@ -1,3 +1,7 @@
+== HEAD
+ * Minor Enhancements
+ * Add Grit::Repo.init.
+
== 2.2.1 / 2010-08-23
* Bug Fixes
* Fix minor regression due to the changed default values in Grit::Index#commit.
View
@@ -10,11 +10,11 @@ module GitRuby
attr_accessor :ruby_git_repo, :git_file_index
- def init(options)
+ def init(options, *args)
if options.size == 0
Grit::GitRuby::Repository.init(@git_dir)
else
- method_missing('init', options)
+ method_missing('init', options, *args)
end
end
View
@@ -232,14 +232,19 @@ def run(prefix, cmd, postfix, options, args)
timeout = options.delete(:timeout) rescue nil
timeout = true if timeout.nil?
+ base = options.delete(:base) rescue nil
+ base = true if base.nil?
+
opt_args = transform_options(options)
if RUBY_PLATFORM.downcase =~ /mswin(?!ce)|mingw|bccwin/
ext_args = args.reject { |a| a.empty? }.map { |a| (a == '--' || a[0].chr == '|' || Grit.no_quote) ? a : "\"#{e(a)}\"" }
- call = "#{prefix}#{Git.git_binary} --git-dir=\"#{self.git_dir}\" #{cmd.to_s.gsub(/_/, '-')} #{(opt_args + ext_args).join(' ')}#{e(postfix)}"
+ gitdir = base ? "--git-dir=\"#{self.git_dir}\"" : ""
+ call = "#{prefix}#{Git.git_binary} #{gitdir} #{cmd.to_s.gsub(/_/, '-')} #{(opt_args + ext_args).join(' ')}#{e(postfix)}"
else
ext_args = args.reject { |a| a.empty? }.map { |a| (a == '--' || a[0].chr == '|' || Grit.no_quote) ? a : "'#{e(a)}'" }
- call = "#{prefix}#{Git.git_binary} --git-dir='#{self.git_dir}' #{cmd.to_s.gsub(/_/, '-')} #{(opt_args + ext_args).join(' ')}#{e(postfix)}"
+ gitdir = base ? "--git-dir='#{self.git_dir}'" : ""
+ call = "#{prefix}#{Git.git_binary} #{gitdir} #{cmd.to_s.gsub(/_/, '-')} #{(opt_args + ext_args).join(' ')}#{e(postfix)}"
end
Grit.log(call) if Grit.debug
response, err = timeout ? sh(call) : wild_sh(call)
View
@@ -3,23 +3,34 @@ module Grit
class Repo
DAEMON_EXPORT_FILE = 'git-daemon-export-ok'
- # The path of the git repo as a String
+ # Public: The String path of the Git repo.
attr_accessor :path
+
+ # Public: The String path to the working directory of the repo, or nil if
+ # there is no working directory.
attr_accessor :working_dir
+
+ # Public: The Boolean of whether or not the repo is bare.
attr_reader :bare
- # The git command line interface object
+ # Public: The Grit::Git command line interface object.
attr_accessor :git
- # Create a new Repo instance
- # +path+ is the path to either the root git directory or the bare git repo
- # +options+ :is_bare force to load a bare repo
+ # Public: Create a new Repo instance.
+ #
+ # path - The String path to either the root git directory or the bare
+ # git repo.
+ # options - :is_bare force to load a bare repo
#
# Examples
+ #
# g = Repo.new("/Users/tom/dev/grit")
# g = Repo.new("/Users/tom/public/grit.git")
#
- # Returns Grit::Repo
+ # Returns a newly initialized Grit::Repo.
+ # Raises Grit::InvalidGitRepositoryError if the path exists but is not
+ # a Git repository.
+ # Raises Grit::NoSuchPathError if the path does not exist.
def initialize(path, options = {})
epath = File.expand_path(path)
@@ -39,12 +50,106 @@ def initialize(path, options = {})
self.git = Git.new(self.path)
end
- # Does nothing yet...
- def self.init(path)
- # !! TODO !!
- # create directory
- # generate initial git directory
- # create new Grit::Repo on that dir, return it
+ # Public: Initialize a git repository (create it on the filesystem). By
+ # default, the newly created repository will contain a working directory.
+ # If you would like to create a bare repo, use Gollum::Repo.init_bare.
+ #
+ # path - The String full path to the repo. Traditionally ends with
+ # "/<name>.git".
+ # git_options - A Hash of additional options to the git init command
+ # (default: {}).
+ # repo_options - A Hash of additional options to the Grit::Repo.new call
+ # (default: {}).
+ #
+ # Examples
+ #
+ # Grit::Repo.init('/var/git/myrepo.git')
+ #
+ # Returns the newly created Grit::Repo.
+ def self.init(path, git_options = {}, repo_options = {})
+ git_options = {:base => false}.merge(git_options)
+ git = Git.new(path)
+ git.fs_mkdir('..')
+ git.init(git_options, path)
+ self.new(path, repo_options)
+ end
+
+ # Public: Initialize a bare git repository (create it on the filesystem).
+ #
+ # path - The String full path to the repo. Traditionally ends with
+ # "/<name>.git".
+ # git_options - A Hash of additional options to the git init command
+ # (default: {}).
+ # repo_options - A Hash of additional options to the Grit::Repo.new call
+ # (default: {}).
+ #
+ # Examples
+ #
+ # Grit::Repo.init_bare('/var/git/myrepo.git')
+ #
+ # Returns the newly created Grit::Repo.
+ def self.init_bare(path, git_options = {}, repo_options = {})
+ git_options = {:bare => true}.merge(git_options)
+ git = Git.new(path)
+ git.fs_mkdir('..')
+ git.init(git_options)
+ self.new(path, repo_options)
+ end
+
+ # Public: Initialize a bare Git repository (create it on the filesystem)
+ # or, if the repo already exists, simply return it.
+ #
+ # path - The String full path to the repo. Traditionally ends with
+ # "/<name>.git".
+ # git_options - A Hash of additional options to the git init command
+ # (default: {}).
+ # repo_options - A Hash of additional options to the Grit::Repo.new call
+ # (default: {}).
+ #
+ # Returns the new or existing Grit::Repo.
+ def self.init_bare_or_open(path, git_options = {}, repo_options = {})
+ git = Git.new(path)
+
+ unless git.exist?
+ git.fs_mkdir(path)
+ git.init(git_options)
+ end
+
+ self.new(path, repo_options)
+ end
+
+ # Public: Create a bare fork of this repository.
+ #
+ # path - The String full path of where to create the new fork.
+ # Traditionally ends with "/<name>.git".
+ # options - The Hash of additional options to the git clone command.
+ # These options will be merged on top of the default Hash:
+ # {:bare => true, :shared => true}.
+ #
+ # Returns the newly forked Grit::Repo.
+ def fork_bare(path, options = {})
+ default_options = {:bare => true, :shared => true}
+ real_options = default_options.merge(options)
+ Git.new(path).fs_mkdir('..')
+ self.git.clone(real_options, self.path, path)
+ Repo.new(path)
+ end
+
+ # Public: Fork a bare git repository from another repo.
+ #
+ # path - The String full path of the repo from which to fork..
+ # Traditionally ends with "/<name>.git".
+ # options - The Hash of additional options to the git clone command.
+ # These options will be merged on top of the default Hash:
+ # {:bare => true, :shared => true}.
+ #
+ # Returns the newly forked Grit::Repo.
+ def fork_bare_from(path, options = {})
+ default_options = {:bare => true, :shared => true}
+ real_options = default_options.merge(options)
+ Git.new(self.path).fs_mkdir('..')
+ self.git.clone(real_options, path, self.path)
+ Repo.new(self.path)
end
# The project's description. Taken verbatim from GIT_REPO/description
@@ -58,7 +163,6 @@ def blame(file, commit = nil)
Blame.new(self, file, commit)
end
-
# An array of Head objects representing the branch heads in
# this repo
#
@@ -369,57 +473,6 @@ def commit_diff(commit)
Commit.diff(self, commit)
end
- # Initialize a bare git repository at the given path
- # +path+ is the full path to the repo (traditionally ends with /<name>.git)
- # +options+ is any additional options to the git init command
- #
- # Examples
- # Grit::Repo.init_bare('/var/git/myrepo.git')
- #
- # Returns Grit::Repo (the newly created repo)
- def self.init_bare(path, git_options = {}, repo_options = {})
- git_options = {:bare => true}.merge(git_options)
- git = Git.new(path)
- git.fs_mkdir('..')
- git.init(git_options)
- self.new(path, repo_options)
- end
-
- def self.init_bare_or_open(path, git_options = {}, repo_options = {})
- git = Git.new(path)
- if !git.exist?
- git.fs_mkdir(path)
- git.init(git_options)
- end
- self.new(path, repo_options)
- end
-
- # Fork a bare git repository from this repo
- # +path+ is the full path of the new repo (traditionally ends with /<name>.git)
- # +options+ is any additional options to the git clone command (:bare and :shared are true by default)
- #
- # Returns Grit::Repo (the newly forked repo)
- def fork_bare(path, options = {})
- default_options = {:bare => true, :shared => true}
- real_options = default_options.merge(options)
- Git.new(path).fs_mkdir('..')
- self.git.clone(real_options, self.path, path)
- Repo.new(path)
- end
-
- # Fork a bare git repository from another repo
- # +path+ is the full path of the new repo (traditionally ends with /<name>.git)
- # +options+ is any additional options to the git clone command (:bare and :shared are true by default)
- #
- # Returns Grit::Repo (the newly forked repo)
- def fork_bare_from(path, options = {})
- default_options = {:bare => true, :shared => true}
- real_options = default_options.merge(options)
- Git.new(self.path).fs_mkdir('..')
- self.git.clone(real_options, path, self.path)
- Repo.new(self.path)
- end
-
# Archive the given treeish
# +treeish+ is the treeish name/id (default 'master')
# +prefix+ is the optional prefix
View
@@ -157,6 +157,16 @@ def test_blob
assert_equal "Hello world", blob.data
end
+ # init
+
+ def test_init
+ FileUtils.stubs(:mkdir_p)
+
+ Git.any_instance.expects(:init).with({:base => false}, "/foo/bar").returns(true)
+ Repo.expects(:new).with("/foo/bar", {})
+ Repo.init("/foo/bar")
+ end
+
# init_bare
def test_init_bare

0 comments on commit 8869608

Please sign in to comment.