Skip to content

Commit dad56fc

Browse files
author
Michal Papis
committed
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>
1 parent 79eceaa commit dad56fc

File tree

5 files changed

+56
-45
lines changed

5 files changed

+56
-45
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,20 @@ Some examples of more low-level index and tree operations
295295
end
296296
```
297297

298+
Managing git configuration
299+
300+
```ruby
301+
# current git project config
302+
Git::GitConfig.config # returns whole config hash
303+
Git::GitConfig.config('user.name') # returns 'Scott Chacon'
304+
Git::GitConfig.config('user.name', 'Scott Chacon') # sets new user.name
305+
306+
# global config
307+
Git::GitConfig.global_config # returns whole global config hash
308+
Git::GitConfig.global_config('user.name') # returns 'Scott Chacon'
309+
Git::GitConfig.global_config('user.name', 'Scott Chacon') # sets new user.name
310+
```
311+
298312
## License
299313

300314
licensed under MIT License Copyright (c) 2008 Scott Chacon. See LICENSE for further details.

lib/git.rb

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
require 'git/branches'
1010
require 'git/config'
1111
require 'git/diff'
12+
require 'git/git_config'
1213
require 'git/index'
1314
require 'git/lib'
1415
require 'git/log'
@@ -42,24 +43,6 @@
4243
# Author:: Scott Chacon (mailto:schacon@gmail.com)
4344
# License:: MIT License
4445
module Git
45-
46-
#g.config('user.name', 'Scott Chacon') # sets value
47-
#g.config('user.email', 'email@email.com') # sets value
48-
#g.config('user.name') # returns 'Scott Chacon'
49-
#g.config # returns whole config hash
50-
def config(name = nil, value = nil)
51-
lib = Git::Lib.new
52-
if(name && value)
53-
# set value
54-
lib.config_set(name, value)
55-
elsif (name)
56-
# return value
57-
lib.config_get(name)
58-
else
59-
# return hash
60-
lib.config_list
61-
end
62-
end
6346

6447
def self.configure
6548
yield Base.config
@@ -69,10 +52,6 @@ def self.config
6952
return Base.config
7053
end
7154

72-
def global_config(name = nil, value = nil)
73-
self.class.global_config(name, value)
74-
end
75-
7655
# open a bare repository
7756
#
7857
# this takes the path to a bare git repo

lib/git/base.rb

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
require 'git/base/factory'
2+
require 'git/git_config'
23

34
module Git
45

56
class Base
67

78
include Git::Base::Factory
9+
include Git::GitConfig
810

911
# opens a bare Git Repository - no working directory options
1012
def self.bare(git_dir, opts = {})
@@ -106,24 +108,7 @@ def chdir # :yields: the Git::Path
106108
yield dir.path
107109
end
108110
end
109-
110-
#g.config('user.name', 'Scott Chacon') # sets value
111-
#g.config('user.email', 'email@email.com') # sets value
112-
#g.config('user.name') # returns 'Scott Chacon'
113-
#g.config # returns whole config hash
114-
def config(name = nil, value = nil)
115-
if(name && value)
116-
# set value
117-
lib.config_set(name, value)
118-
elsif (name)
119-
# return value
120-
lib.config_get(name)
121-
else
122-
# return hash
123-
lib.config_list
124-
end
125-
end
126-
111+
127112
# returns a reference to the working directory
128113
# @git.dir.path
129114
# @git.dir.writeable?

lib/git/git_config.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module Git
2+
module GitConfig
3+
extend self
4+
5+
#g.config('user.name', 'Scott Chacon') # sets value
6+
#g.config('user.email', 'email@email.com') # sets value
7+
#g.config('user.name') # returns 'Scott Chacon'
8+
#g.config # returns whole config hash
9+
def config(name = nil, value = nil)
10+
if(name && value)
11+
# set value
12+
lib.config_set(name, value)
13+
elsif (name)
14+
# return value
15+
lib.config_get(name)
16+
else
17+
# return hash
18+
lib.config_list
19+
end
20+
end
21+
22+
def global_config(name = nil, value = nil)
23+
Git.global_config(name, value)
24+
end
25+
26+
class << self
27+
private
28+
29+
def lib
30+
@lib ||= Git::Lib.new
31+
end
32+
end
33+
end
34+
end
35+
36+
# Git::GitConfig.config

tests/units/test_config_module.rb renamed to tests/units/test_git_config.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22

33
require File.dirname(__FILE__) + '/../test_helper'
44

5-
class TestConfigModule < Test::Unit::TestCase
5+
class TestGitConfig < Test::Unit::TestCase
66
def setup
77
set_file_paths
8-
git_class = Class.new do
9-
include Git
10-
end
11-
@git = git_class.new
8+
@git = Git::GitConfig
129
@old_dir = Dir.pwd
1310
Dir.chdir(@wdir)
1411
end

0 commit comments

Comments
 (0)