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 all commits
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
19 changes: 1 addition & 18 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 @@ -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
Expand Down
19 changes: 2 additions & 17 deletions lib/git/base.rb
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -9,6 +10,7 @@ module Git
#
class Base
include Git::Base::Factory
include Git::GitConfig

# (see Git.bare)
def self.bare(git_dir, options = {})
Expand Down Expand Up @@ -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?
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
37 changes: 37 additions & 0 deletions tests/units/test_git_config.rb
Original file line number Diff line number Diff line change
@@ -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