Class: GitSu::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/gitsu/git.rb

Direct Known Subclasses

CachingGit

Defined Under Namespace

Classes: ConfigSettingError

Instance Method Summary collapse

Constructor Details

#initialize(shell) ⇒ Git

Returns a new instance of Git.



22
23
24
# File 'lib/gitsu/git.rb', line 22

def initialize(shell)
    @shell = shell
end

Instance Method Details

#clear_user(scope) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/gitsu/git.rb', line 71

def clear_user(scope)
    unset_config(scope, "user.name")
    unset_config(scope, "user.email")
    if list_config(scope).select { |e| e =~ /^user\./ }.empty?
        remove_config_section(scope, "user")
    end
end

#color_output?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/gitsu/git.rb', line 79

def color_output?
    execute_config_command(:derived, "--get-colorbool color.ui")
end

#commit(file, message) ⇒ Object



98
99
100
# File 'lib/gitsu/git.rb', line 98

def commit(file, message)
    @shell.execute("git commit #{file} --message='#{message}'")
end

#edit_gitsu_configObject



67
68
69
# File 'lib/gitsu/git.rb', line 67

def edit_gitsu_config
    execute_config_command(:derived, "--edit --file #{File.expand_path '~/.gitsu'}")
end

#get_color(color_name) ⇒ Object



48
49
50
# File 'lib/gitsu/git.rb', line 48

def get_color(color_name)
    capture_config_command(:derived, "--get-color '' '#{color_name}'")
end

#get_config(scope, key) ⇒ Object



26
27
28
# File 'lib/gitsu/git.rb', line 26

def get_config(scope, key)
    capture_config_command(scope, key)
end

#list_config(scope) ⇒ Object



40
41
42
# File 'lib/gitsu/git.rb', line 40

def list_config(scope)
    capture_config_command(scope, "--list").chomp.split("\n")
end

#list_files(*options) ⇒ Object



102
103
104
105
# File 'lib/gitsu/git.rb', line 102

def list_files(*options)
    flags = options.map {|o| "--#{o}"}.join " "
    @shell.capture("git ls-files #{flags}").split "\n"
end

#remove_config_section(scope, section) ⇒ Object



44
45
46
# File 'lib/gitsu/git.rb', line 44

def remove_config_section(scope, section)
    capture_config_command(scope, "--remove-section #{section} 2>/dev/null")
end

#render(user) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/gitsu/git.rb', line 83

def render(user)
    if color_output?
        user_color = get_color "blue"
        email_color = get_color "green"
        reset_color = get_color "reset"
        user.to_ansi_s(user_color, email_color, reset_color)
    else
        user.to_s
    end
end

#render_user(scope) ⇒ Object



94
95
96
# File 'lib/gitsu/git.rb', line 94

def render_user(scope)
    render selected_user(scope)
end

#select_user(user, scope) ⇒ Object



52
53
54
55
# File 'lib/gitsu/git.rb', line 52

def select_user(user, scope)
    set_config(scope, "user.name", user.name) or raise ConfigSettingError, "Couldn't update user config in '#{scope}' scope"
    set_config(scope, "user.email", user.email)
end

#selected_user(scope) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/gitsu/git.rb', line 57

def selected_user(scope)
    name = get_config(scope, "user.name")
    if name.empty?
        User::NONE
    else
        email = get_config(scope, "user.email")
        User.new(name, email)
    end
end

#set_config(scope, key, value) ⇒ Object



30
31
32
33
34
# File 'lib/gitsu/git.rb', line 30

def set_config(scope, key, value)
    # replace <'> with <'\''>. E.g. O'Grady -> O'\''Grady
    escaped_value = value.gsub(/'/, "'\\\\\''")
    execute_config_command(scope, "#{key} '#{escaped_value}'")
end

#unset_config(scope, key) ⇒ Object



36
37
38
# File 'lib/gitsu/git.rb', line 36

def unset_config(scope, key)
    capture_config_command(scope, "--unset #{key}")
end