diff --git a/README.md b/README.md index 0ff9a0a5..3ebfd0e1 100644 --- a/README.md +++ b/README.md @@ -339,6 +339,20 @@ g.with_temp_working(dir) do end ``` +Managing git configuration + +```ruby + # current git project config + Git::GitConfig.config # returns whole config hash + Git::GitConfig.config('user.name') # returns 'Scott Chacon' + Git::GitConfig.config('user.name', 'Scott Chacon') # sets new user.name + + # global config + Git::GitConfig.global_config # returns whole global config hash + Git::GitConfig.global_config('user.name') # returns 'Scott Chacon' + Git::GitConfig.global_config('user.name', 'Scott Chacon') # sets new user.name +``` + ## License licensed under MIT License Copyright (c) 2008 Scott Chacon. See LICENSE for further details. diff --git a/lib/git.rb b/lib/git.rb index eb4c7cce..c7c16996 100644 --- a/lib/git.rb +++ b/lib/git.rb @@ -9,6 +9,7 @@ require 'git/branches' require 'git/config' require 'git/diff' +require 'git/git_config' require 'git/index' require 'git/lib' require 'git/log' @@ -37,24 +38,6 @@ # @author Scott Chacon (mailto:schacon@gmail.com) # module Git - #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) - lib = Git::Lib.new - if(name && value) - # set value - lib.config_set(name, value) - elsif (name) - # return value - lib.config_get(name) - else - # return hash - lib.config_list - end - end - def self.configure yield Base.config end diff --git a/lib/git/base.rb b/lib/git/base.rb index fbca5f1b..8384e60d 100644 --- a/lib/git/base.rb +++ b/lib/git/base.rb @@ -1,4 +1,5 @@ require 'git/base/factory' +require 'git/git_config' module Git # Git::Base is the main public interface for interacting with Git commands. @@ -9,6 +10,7 @@ module Git # class Base include Git::Base::Factory + include Git::GitConfig # (see Git.bare) def self.bare(git_dir, options = {}) @@ -135,23 +137,6 @@ def chdir # :yields: the Git::Path 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) - if(name && value) - # set value - lib.config_set(name, value) - elsif (name) - # return value - lib.config_get(name) - else - # return hash - lib.config_list - end - end - # returns a reference to the working directory # @git.dir.path # @git.dir.writeable? diff --git a/lib/git/git_config.rb b/lib/git/git_config.rb new file mode 100644 index 00000000..5ffb93c7 --- /dev/null +++ b/lib/git/git_config.rb @@ -0,0 +1,36 @@ +module Git + module GitConfig + extend self + + #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) + if(name && value) + # set value + lib.config_set(name, value) + elsif (name) + # return value + lib.config_get(name) + else + # return hash + lib.config_list + end + end + + def global_config(name = nil, value = nil) + Git.global_config(name, value) + end + + class << self + private + + def lib + @lib ||= Git::Lib.new + end + end + end +end + +# Git::GitConfig.config \ No newline at end of file diff --git a/tests/units/test_git_config.rb b/tests/units/test_git_config.rb new file mode 100644 index 00000000..6ecbce43 --- /dev/null +++ b/tests/units/test_git_config.rb @@ -0,0 +1,37 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../test_helper' + +class TestGitConfig < Test::Unit::TestCase + def setup + set_file_paths + @git = Git::GitConfig + @old_dir = Dir.pwd + Dir.chdir(@wdir) + end + + teardown + def test_teardown + Dir.chdir(@old_dir) + end + + def test_config + c = @git.config + assert_equal('Scott Chacon', c['user.name']) + assert_equal('false', c['core.bare']) + end + + def test_read_config + assert_equal('Scott Chacon', @git.config('user.name')) + assert_equal('false', @git.config('core.bare')) + end + + def test_set_config + in_temp_dir do |path| + g = Git.clone(@wbare, 'bare') + assert_not_equal('bully', g.config('user.name')) + g.config('user.name', 'bully') + assert_equal('bully', g.config('user.name')) + end + end +end