From 7b74894c4a50f8292e189ae963eb3ee1d206c010 Mon Sep 17 00:00:00 2001 From: Michal Papis Date: Mon, 21 Jan 2019 14:28:32 +0100 Subject: [PATCH 1/2] Fix Git module config method The Git module config method when used via `include Git` does not work as the `lambda` requires parameters to be present and the second part of condition in Git::Lib::config_get calls the lambda without any params, switching to `Proc.new` fixes the problem. Signed-off-by: Michal Papis --- lib/git/lib.rb | 4 ++-- tests/units/test_config_module.rb | 40 +++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 tests/units/test_config_module.rb diff --git a/lib/git/lib.rb b/lib/git/lib.rb index a698cf3e..69d9a086 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -441,7 +441,7 @@ def config_remote(name) end def config_get(name) - do_get = lambda do |path| + do_get = Proc.new do |path| command('config', ['--get', name]) end @@ -457,7 +457,7 @@ def global_config_get(name) end def config_list - build_list = lambda do |path| + build_list = Proc.new do |path| parse_config_list command_lines('config', ['--list']) end diff --git a/tests/units/test_config_module.rb b/tests/units/test_config_module.rb new file mode 100644 index 00000000..b19b9625 --- /dev/null +++ b/tests/units/test_config_module.rb @@ -0,0 +1,40 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../test_helper' + +class TestConfigModule < Test::Unit::TestCase + def setup + set_file_paths + git_class = Class.new do + include Git + end + @git = git_class.new + @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 From 117b45f0eebf74185505ad7ff6b25cbcfee3f4c1 Mon Sep 17 00:00:00 2001 From: Michal Papis Date: Mon, 21 Jan 2019 15:15:29 +0100 Subject: [PATCH 2/2] Refactor Git::config The Git module has config and global_config methods that are available only when Git module is included. Refactored the code to remove duplication and to make it more explicit we are managing `git config` not the library setup. Signed-off-by: Michal Papis --- README.md | 14 ++++++++ lib/git.rb | 23 +----------- lib/git/base.rb | 21 ++--------- lib/git/git_config.rb | 36 +++++++++++++++++++ ...st_config_module.rb => test_git_config.rb} | 7 ++-- 5 files changed, 56 insertions(+), 45 deletions(-) create mode 100644 lib/git/git_config.rb rename tests/units/{test_config_module.rb => test_git_config.rb} (85%) diff --git a/README.md b/README.md index bc82b9e0..99fd47fe 100644 --- a/README.md +++ b/README.md @@ -295,6 +295,20 @@ Some examples of more low-level index and tree operations 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 1992dc1d..1719712c 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' @@ -42,24 +43,6 @@ # Author:: Scott Chacon (mailto:schacon@gmail.com) # License:: MIT License 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 @@ -69,10 +52,6 @@ def self.config return Base.config end - def global_config(name = nil, value = nil) - self.class.global_config(name, value) - end - # open a bare repository # # this takes the path to a bare git repo diff --git a/lib/git/base.rb b/lib/git/base.rb index 068d7931..a3e7d809 100644 --- a/lib/git/base.rb +++ b/lib/git/base.rb @@ -1,10 +1,12 @@ require 'git/base/factory' +require 'git/git_config' module Git class Base include Git::Base::Factory + include Git::GitConfig # opens a bare Git Repository - no working directory options def self.bare(git_dir, opts = {}) @@ -106,24 +108,7 @@ def chdir # :yields: the Git::Path yield dir.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_config_module.rb b/tests/units/test_git_config.rb similarity index 85% rename from tests/units/test_config_module.rb rename to tests/units/test_git_config.rb index b19b9625..6ecbce43 100644 --- a/tests/units/test_config_module.rb +++ b/tests/units/test_git_config.rb @@ -2,13 +2,10 @@ require File.dirname(__FILE__) + '/../test_helper' -class TestConfigModule < Test::Unit::TestCase +class TestGitConfig < Test::Unit::TestCase def setup set_file_paths - git_class = Class.new do - include Git - end - @git = git_class.new + @git = Git::GitConfig @old_dir = Dir.pwd Dir.chdir(@wdir) end