class Rugged::Config

Config

Public Class Methods

global() → new_config click to toggle source

Open the default global config file as a new Rugged::Config object. An exception will be raised if the global config file doesn't exist.

static VALUE rb_git_config_open_default(VALUE klass)
{
        git_config *cfg;
        int error;

        error = git_config_open_default(&cfg);
        rugged_exception_check(error);

        return rugged_config_new(klass, Qnil, cfg);
}
new(path) → new_config click to toggle source

Open the file specified in path as a Rugged::Config file. If path cannot be found, or the file is an invalid Git config, an exception will be raised.

static VALUE rb_git_config_new(VALUE klass, VALUE rb_path)
{
        git_config *config = NULL;
        int error, i;

        if (TYPE(rb_path) == T_ARRAY) {
                error = git_config_new(&config);
                rugged_exception_check(error);

                for (i = 0; i < RARRAY_LEN(rb_path); ++i) {
                        VALUE f = rb_ary_entry(rb_path, i);
                        Check_Type(f, T_STRING);
                        error = git_config_add_file_ondisk(config, StringValueCStr(f), i + 1, 1);
                        rugged_exception_check(error);
                }
        } else if (TYPE(rb_path) == T_STRING) {
                error = git_config_open_ondisk(&config, StringValueCStr(rb_path));
                rugged_exception_check(error);
        } else {
                rb_raise(rb_eTypeError, "Expecting a filename or an array of filenames");
        }

        return rugged_config_new(klass, Qnil, config);
}
open_global() → new_config click to toggle source

Open the default global config file as a new Rugged::Config object. An exception will be raised if the global config file doesn't exist.

static VALUE rb_git_config_open_default(VALUE klass)
{
        git_config *cfg;
        int error;

        error = git_config_open_default(&cfg);
        rugged_exception_check(error);

        return rugged_config_new(klass, Qnil, cfg);
}

Public Instance Methods

cfg[key] → value click to toggle source

Get the value for the given config key. Values are always returned as String, or nil if the given key doesn't exist in the Config file.

cfg['apply.whitespace'] #=> 'fix'
cfg['diff.renames'] #=> 'true'
static VALUE rb_git_config_get(VALUE self, VALUE rb_key)
{
        git_config *config;
        git_buf buf = { NULL };
        int error;
        VALUE rb_result;

        Data_Get_Struct(self, git_config, config);
        Check_Type(rb_key, T_STRING);

        error = git_config_get_string_buf(&buf, config, StringValueCStr(rb_key));
        if (error == GIT_ENOTFOUND)
                return Qnil;

        rugged_exception_check(error);
        rb_result = rb_str_new_utf8(buf.ptr);
        git_buf_free(&buf);

        return rb_result;
}
cfg[key] = value click to toggle source

Store the given value in the Config file, under the section and name specified by key. Value can be any of the following Ruby types: String, true, false and Fixnum.

The config file will be automatically stored to disk.

cfg['apply.whitespace'] = 'fix'
cfg['diff.renames'] = true
cfg['gc.reflogexpre'] = 90
static VALUE rb_git_config_store(VALUE self, VALUE rb_key, VALUE rb_val)
{
        git_config *config;
        const char *key;
        int error;

        Data_Get_Struct(self, git_config, config);
        Check_Type(rb_key, T_STRING);

        key = StringValueCStr(rb_key);

        switch (TYPE(rb_val)) {
        case T_STRING:
                error = git_config_set_string(config, key, StringValueCStr(rb_val));
                break;

        case T_TRUE:
        case T_FALSE:
                error = git_config_set_bool(config, key, (rb_val == Qtrue));
                break;

        case T_FIXNUM:
                error = git_config_set_int32(config, key, FIX2INT(rb_val));
                break;

        default:
                rb_raise(rb_eTypeError,
                        "Invalid value; config files can only store string, bool or int keys");
        }

        rugged_exception_check(error);
        return Qnil;
}
delete(key) → true or false click to toggle source

Delete the given key from the config file. Return true if the deletion was successful, or false if the key was not found in the Config file.

The config file is immediately updated on disk.

static VALUE rb_git_config_delete(VALUE self, VALUE rb_key)
{
        git_config *config;
        int error;

        Data_Get_Struct(self, git_config, config);
        Check_Type(rb_key, T_STRING);

        error = git_config_delete_entry(config, StringValueCStr(rb_key));
        if (error == GIT_ENOTFOUND)
                return Qfalse;

        rugged_exception_check(error);
        return Qtrue;
}
each_pair { |key, value| block } click to toggle source
each_pair → enumerator
each { |key, value| block }
each → enumerator

Call the given block once for each key/value pair in the config file. If no block is given, an enumerator is returned.

cfg.each do |key, value|
  puts "#{key} => #{value}"
end
static VALUE rb_git_config_each_pair(VALUE self)
{
        git_config *config;
        int error;

        Data_Get_Struct(self, git_config, config);

        if (!rb_block_given_p())
                return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each_pair"));

        error = git_config_foreach(config, &cb_config__each_pair, (void *)rb_block_proc());
        rugged_exception_check(error);
        return Qnil;
}
each_key { |key| block } click to toggle source
each_key → enumarator

Call the given block once for each key in the config file. If no block is given, an enumerator is returned.

cfg.each_key do |key|
  puts key
end
static VALUE rb_git_config_each_key(VALUE self)
{
        git_config *config;
        int error;

        Data_Get_Struct(self, git_config, config);

        if (!rb_block_given_p())
                return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each_key"));

        error = git_config_foreach(config, &cb_config__each_key, (void *)rb_block_proc());
        rugged_exception_check(error);
        return Qnil;
}
each_pair { |key, value| block } click to toggle source
each_pair → enumerator

Call the given block once for each key/value pair in the config file. If no block is given, an enumerator is returned.

cfg.each do |key, value|
  puts "#{key} => #{value}"
end
static VALUE rb_git_config_each_pair(VALUE self)
{
        git_config *config;
        int error;

        Data_Get_Struct(self, git_config, config);

        if (!rb_block_given_p())
                return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each_pair"));

        error = git_config_foreach(config, &cb_config__each_pair, (void *)rb_block_proc());
        rugged_exception_check(error);
        return Qnil;
}
get(key) → value click to toggle source

Get the value for the given config key. Values are always returned as String, or nil if the given key doesn't exist in the Config file.

cfg['apply.whitespace'] #=> 'fix'
cfg['diff.renames'] #=> 'true'
static VALUE rb_git_config_get(VALUE self, VALUE rb_key)
{
        git_config *config;
        git_buf buf = { NULL };
        int error;
        VALUE rb_result;

        Data_Get_Struct(self, git_config, config);
        Check_Type(rb_key, T_STRING);

        error = git_config_get_string_buf(&buf, config, StringValueCStr(rb_key));
        if (error == GIT_ENOTFOUND)
                return Qnil;

        rugged_exception_check(error);
        rb_result = rb_str_new_utf8(buf.ptr);
        git_buf_free(&buf);

        return rb_result;
}
store(key, value) click to toggle source

Store the given value in the Config file, under the section and name specified by key. Value can be any of the following Ruby types: String, true, false and Fixnum.

The config file will be automatically stored to disk.

cfg['apply.whitespace'] = 'fix'
cfg['diff.renames'] = true
cfg['gc.reflogexpre'] = 90
static VALUE rb_git_config_store(VALUE self, VALUE rb_key, VALUE rb_val)
{
        git_config *config;
        const char *key;
        int error;

        Data_Get_Struct(self, git_config, config);
        Check_Type(rb_key, T_STRING);

        key = StringValueCStr(rb_key);

        switch (TYPE(rb_val)) {
        case T_STRING:
                error = git_config_set_string(config, key, StringValueCStr(rb_val));
                break;

        case T_TRUE:
        case T_FALSE:
                error = git_config_set_bool(config, key, (rb_val == Qtrue));
                break;

        case T_FIXNUM:
                error = git_config_set_int32(config, key, FIX2INT(rb_val));
                break;

        default:
                rb_raise(rb_eTypeError,
                        "Invalid value; config files can only store string, bool or int keys");
        }

        rugged_exception_check(error);
        return Qnil;
}
to_hash → hash click to toggle source

Returns the config file represented as a Ruby hash, where each configuration entry appears as a key with its corresponding value.

cfg.to_hash #=> {"core.autolf" => "true", "core.bare" => "true"}
static VALUE rb_git_config_to_hash(VALUE self)
{
        git_config *config;
        int error;
        VALUE hash;

        Data_Get_Struct(self, git_config, config);
        hash = rb_hash_new();

        error = git_config_foreach(config, &cb_config__to_hash, (void *)hash);
        rugged_exception_check(error);
        return hash;
}