lib/git/base.rb in git-1.2.8 vs lib/git/base.rb in git-1.2.9
- old
+ new
@@ -1,62 +1,82 @@
+require 'git/base/factory'
+
module Git
class Base
+ include Git::Base::Factory
+
# opens a bare Git Repository - no working directory options
def self.bare(git_dir, opts = {})
self.new({:repository => git_dir}.merge(opts))
end
- # opens a new Git Project from a working directory
- # you can specify non-standard git_dir and index file in the options
- def self.open(working_dir, opts={})
- self.new({:working_directory => working_dir}.merge(opts))
+ # clones a git repository locally
+ #
+ # repository - http://repo.or.cz/w/sinatra.git
+ # name - sinatra
+ #
+ # options:
+ # :repository
+ #
+ # :bare
+ # or
+ # :working_directory
+ # :index_file
+ #
+ def self.clone(repository, name, opts = {})
+ # run git-clone
+ self.new(Git::Lib.new.clone(repository, name, opts))
end
+
+ # Returns (and initialize if needed) a Git::Config instance
+ #
+ # @return [Git::Config] the current config instance.
+ def self.config
+ return @@config ||= Config.new
+ end
# initializes a git repository
#
# options:
# :bare
# :index
# :repository
#
def self.init(working_dir, opts = {})
- opts[:working_directory] = working_dir if !opts[:working_directory]
- opts[:repository] = File.join(opts[:working_directory], '.git') if !opts[:repository]
+ opts[:working_directory] ||= working_dir
+ opts[:repository] ||= File.join(opts[:working_directory], '.git')
FileUtils.mkdir_p(opts[:working_directory]) if opts[:working_directory] && !File.directory?(opts[:working_directory])
init_opts = {
:bare => opts[:bare]
}
opts.delete(:working_directory) if opts[:bare]
+
+ # Submodules have a .git *file* not a .git folder.
+ # This file's contents point to the location of
+ # where the git refs are held (In the parent repo)
+ if File.file?('.git')
+ git_file = File.open('.git').read[8..-1].strip
+ opts[:repository] = git_file
+ opts[:index] = git_file + '/index'
+ end
Git::Lib.new(opts).init(init_opts)
self.new(opts)
end
-
- # clones a git repository locally
- #
- # repository - http://repo.or.cz/w/sinatra.git
- # name - sinatra
- #
- # options:
- # :repository
- #
- # :bare
- # or
- # :working_directory
- # :index_file
- #
- def self.clone(repository, name, opts = {})
- # run git-clone
- self.new(Git::Lib.new.clone(repository, name, opts))
+
+ # opens a new Git Project from a working directory
+ # you can specify non-standard git_dir and index file in the options
+ def self.open(working_dir, opts={})
+ self.new({:working_directory => working_dir}.merge(opts))
end
-
+
def initialize(options = {})
if working_dir = options[:working_directory]
options[:repository] ||= File.join(working_dir, '.git')
options[:index] ||= File.join(working_dir, '.git', 'index')
end
@@ -69,41 +89,11 @@
@working_directory = options[:working_directory] ? Git::WorkingDirectory.new(options[:working_directory]) : nil
@repository = options[:repository] ? Git::Repository.new(options[:repository]) : nil
@index = options[:index] ? Git::Index.new(options[:index], false) : nil
end
-
-
- # returns a reference to the working directory
- # @git.dir.path
- # @git.dir.writeable?
- def dir
- @working_directory
- end
-
- # returns reference to the git repository directory
- # @git.dir.path
- def repo
- @repository
- end
- # returns reference to the git index file
- def index
- @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
#
# example
# @git.chdir do
@@ -115,17 +105,10 @@
Dir.chdir(dir.path) do
yield dir.path
end
end
- # returns the repository size in bytes
- def repo_size
- Dir.chdir(repo.path) do
- return `du -s`.chomp.split.first.to_i
- end
- end
-
#g.config('user.name', 'Scott Chacon') # sets value
#g.config('user.email', 'email@email.com') # sets value
#g.config('user.name') # returns 'Scott Chacon'
#g.config # returns whole config hash
def config(name = nil, value = nil)
@@ -138,80 +121,64 @@
else
# return hash
lib.config_list
end
end
-
- # factory methods
-
- # returns a Git::Object of the appropriate type
- # you can also call @git.gtree('tree'), but that's
- # just for readability. If you call @git.gtree('HEAD') it will
- # still return a Git::Object::Commit object.
- #
- # @git.object calls a factory method that will run a rev-parse
- # on the objectish and determine the type of the object and return
- # an appropriate object for that type
- def object(objectish)
- Git::Object.new(self, objectish)
+
+ # returns a reference to the working directory
+ # @git.dir.path
+ # @git.dir.writeable?
+ def dir
+ @working_directory
end
- def gtree(objectish)
- Git::Object.new(self, objectish, 'tree')
+ # returns reference to the git index file
+ def index
+ @index
end
-
- def gcommit(objectish)
- Git::Object.new(self, objectish, 'commit')
+
+ # returns reference to the git repository directory
+ # @git.dir.path
+ def repo
+ @repository
end
- def gblob(objectish)
- Git::Object.new(self, objectish, 'blob')
+ # returns the repository size in bytes
+ def repo_size
+ Dir.chdir(repo.path) do
+ return `du -s`.chomp.split.first.to_i
+ end
end
- # returns a Git::Log object with count commits
- def log(count = 30)
- Git::Log.new(self, count)
+ def set_index(index_file, check = true)
+ @lib = nil
+ @index = Git::Index.new(index_file.to_s, check)
end
-
- # returns a Git::Status object
- def status
- Git::Status.new(self)
- end
-
- # returns a Git::Branches object of all the Git::Branch objects for this repo
- def branches
- Git::Branches.new(self)
- end
- # returns a Git::Branch object for branch_name
- def branch(branch_name = 'master')
- Git::Branch.new(self, branch_name)
+ def set_working(work_dir, check = true)
+ @lib = nil
+ @working_directory = Git::WorkingDirectory.new(work_dir.to_s, check)
end
# returns +true+ if the branch exists locally
def is_local_branch?(branch)
branch_names = self.branches.local.map {|b| b.name}
branch_names.include?(branch)
end
# returns +true+ if the branch exists remotely
def is_remote_branch?(branch)
- branch_names = self.branches.local.map {|b| b.name}
+ branch_names = self.branches.remote.map {|b| b.name}
branch_names.include?(branch)
end
# returns +true+ if the branch exists
def is_branch?(branch)
branch_names = self.branches.map {|b| b.name}
branch_names.include?(branch)
end
- # returns a Git::Remote object
- def remote(remote_name = 'origin')
- Git::Remote.new(self, remote_name)
- end
-
# this is a convenience method for accessing the class that wraps all the
# 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
@lib ||= Git::Lib.new(self, @logger)
@@ -238,16 +205,11 @@
# end
def grep(string, path_limiter = nil, opts = {})
self.object('HEAD').grep(string, path_limiter, opts)
end
- # returns a Git::Diff object
- def diff(objectish = 'HEAD', obj2 = nil)
- Git::Diff.new(self, objectish, obj2)
- end
-
- # updates the repository index using the workig dorectory content
+ # updates the repository index using the working directory content
#
# @git.add('path/to/file')
# @git.add(['path/to/file1','path/to/file2'])
# @git.add(:all => true)
#
@@ -288,10 +250,29 @@
#
def clean(opts = {})
self.lib.clean(opts)
end
+ # returns the most recent tag that is reachable from a commit
+ #
+ # options:
+ # :all
+ # :tags
+ # :contains
+ # :debug
+ # :exact_match
+ # :dirty
+ # :abbrev
+ # :candidates
+ # :long
+ # :always
+ # :match
+ #
+ def describe(committish=nil, opts={})
+ self.lib.describe(committish, opts)
+ end
+
# reverts the working directory to the provided commitish.
# Accepts a range, such as comittish..HEAD
#
# options:
# :no_edit
@@ -400,15 +381,10 @@
# returns an array of all Git::Tag objects for this repository
def tags
self.lib.tags.map { |r| tag(r) }
end
-
- # returns a Git::Tag object
- def tag(tag_name)
- Git::Object.new(self, tag_name, 'tag', true)
- end
# Creates a new git tag (Git::Tag)
# Usage:
# repo.add_tag('tag_name', object_reference)
# repo.add_tag('tag_name', object_reference, {:options => 'here'})
@@ -421,11 +397,11 @@
# :m | :message -> String
# :s -> true
#
def add_tag(name, *opts)
self.lib.tag(name, *opts)
- tag(name)
+ self.tag(name)
end
# deletes a tag
def delete_tag(name)
self.lib.tag(name, {:d => true})
@@ -452,10 +428,19 @@
end
def apply_mail(file)
self.lib.apply_mail(file) if File.exist?(file)
end
+
+ # Shows objects
+ #
+ # @param [String|NilClass] objectish the target object reference (nil == HEAD)
+ # @param [String|NilClass] path the path of the file to be shown
+ # @return [String] the object information
+ def show(objectish=nil, path=nil)
+ self.lib.show(objectish, path)
+ end
## LOWER LEVEL INDEX OPERATIONS ##
def with_index(new_index) # :yields: new_index
old_index = @index
@@ -490,14 +475,10 @@
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
@@ -551,10 +532,9 @@
# returns the name of the branch the working directory is currently on
def current_branch
self.lib.branch_current
end
-
end
end