Skip to content

Refactor git config #400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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 <michal.papis@toptal.com>
  • Loading branch information
Michal Papis committed Mar 25, 2019
commit 117b45f0eebf74185505ad7ff6b25cbcfee3f4c1
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
23 changes: 1 addition & 22 deletions lib/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
21 changes: 3 additions & 18 deletions lib/git/base.rb
Original file line number Diff line number Diff line change
@@ -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 = {})
Expand Down Expand Up @@ -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?
Expand Down
36 changes: 36 additions & 0 deletions lib/git/git_config.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down