Class: Rugged::Config
- Inherits:
-
Object
- Object
- Rugged::Config
- Defined in:
- ext/rugged/rugged_config.c
Class Method Summary collapse
-
.global ⇒ Object
Open the default global config file as a new
Rugged::Configobject. -
.new(path) ⇒ Object
Open the file specified in
pathas aRugged::Configfile. -
.open_global ⇒ Object
Open the default global config file as a new
Rugged::Configobject.
Instance Method Summary collapse
-
#[](rb_key) ⇒ Object
Get the value for the given config
key. -
#[]=(rb_key, rb_val) ⇒ Object
Store the given
valuein the Config file, under the section and name specified bykey. -
#delete(key) ⇒ Boolean
Delete the given
keyfrom the config file. -
#each ⇒ Object
Call the given block once for each key/value pair in the config file.
-
#each_key ⇒ Object
Call the given block once for each key in the config file.
-
#each_pair ⇒ Object
Call the given block once for each key/value pair in the config file.
-
#get(rb_key) ⇒ Object
Get the value for the given config
key. -
#get_all(key) ⇒ Array
Get a list of values for the given config
key. -
#snapshot ⇒ Object
Create a snapshot of the configuration.
-
#store(rb_key, rb_val) ⇒ Object
Store the given
valuein the Config file, under the section and name specified bykey. -
#to_hash ⇒ Hash
Returns the config file represented as a Ruby hash, where each configuration entry appears as a key with its corresponding value.
-
#transaction {|config| ... } ⇒ Object
Perform configuration changes in a transaction.
Class Method Details
.global ⇒ Object .open_global ⇒ Object
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.
305 306 307 308 309 310 311 312 313 314 |
# File 'ext/rugged/rugged_config.c', line 305
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) ⇒ Object
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.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'ext/rugged/rugged_config.c', line 42
static VALUE rb_git_config_new(VALUE klass, VALUE rb_path)
{
git_config *config = NULL;
if (TYPE(rb_path) == T_ARRAY) {
int error, i;
error = git_config_new(&config);
rugged_exception_check(error);
for (i = 0; i < RARRAY_LEN(rb_path) && !error; ++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, NULL, 1);
}
if (error) {
git_config_free(config);
rugged_exception_check(error);
}
} else if (TYPE(rb_path) == T_STRING) {
rugged_exception_check(
git_config_open_ondisk(&config, StringValueCStr(rb_path))
);
} else {
rb_raise(rb_eTypeError, "Expecting a filename or an array of filenames");
}
return rugged_config_new(klass, Qnil, config);
}
|
.global ⇒ Object .open_global ⇒ Object
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.
305 306 307 308 309 310 311 312 313 314 |
# File 'ext/rugged/rugged_config.c', line 305
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);
}
|
Instance Method Details
#get(key) ⇒ Object #[](key) ⇒ Object
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'
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'ext/rugged/rugged_config.c', line 85
static VALUE rb_git_config_get(VALUE self, VALUE rb_key)
{
git_config *config;
git_buf buf = { NULL };
int error;
VALUE rb_result;
TypedData_Get_Struct(self, git_config, &rugged_config_type, 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_dispose(&buf);
return rb_result;
}
|
#store(key, value) ⇒ Object #[]=(key) ⇒ Object
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
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'ext/rugged/rugged_config.c', line 121
static VALUE rb_git_config_store(VALUE self, VALUE rb_key, VALUE rb_val)
{
git_config *config;
const char *key;
int error;
TypedData_Get_Struct(self, git_config, &rugged_config_type, 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) ⇒ Boolean
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.
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'ext/rugged/rugged_config.c', line 165
static VALUE rb_git_config_delete(VALUE self, VALUE rb_key)
{
git_config *config;
int error;
TypedData_Get_Struct(self, git_config, &rugged_config_type, 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| ... } ⇒ Object #each_pair ⇒ Object #each {|key, value| ... } ⇒ Object #each ⇒ Object
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
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'ext/rugged/rugged_config.c', line 256
static VALUE rb_git_config_each_pair(VALUE self)
{
git_config *config;
int error, exception;
RETURN_ENUMERATOR(self, 0, 0);
TypedData_Get_Struct(self, git_config, &rugged_config_type, config);
error = git_config_foreach(config, &cb_config__each_pair, &exception);
if (error == GIT_EUSER)
rb_jump_tag(exception);
rugged_exception_check(error);
return Qnil;
}
|
#each_key {|key| ... } ⇒ Object #each_key ⇒ Object
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
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'ext/rugged/rugged_config.c', line 226
static VALUE rb_git_config_each_key(VALUE self)
{
git_config *config;
int error, exception;
RETURN_ENUMERATOR(self, 0, 0);
TypedData_Get_Struct(self, git_config, &rugged_config_type, config);
error = git_config_foreach(config, &cb_config__each_key, &exception);
if (error == GIT_EUSER)
rb_jump_tag(exception);
rugged_exception_check(error);
return Qnil;
}
|
#each_pair {|key, value| ... } ⇒ Object #each_pair ⇒ Object #each {|key, value| ... } ⇒ Object #each ⇒ Object
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
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'ext/rugged/rugged_config.c', line 256
static VALUE rb_git_config_each_pair(VALUE self)
{
git_config *config;
int error, exception;
RETURN_ENUMERATOR(self, 0, 0);
TypedData_Get_Struct(self, git_config, &rugged_config_type, config);
error = git_config_foreach(config, &cb_config__each_pair, &exception);
if (error == GIT_EUSER)
rb_jump_tag(exception);
rugged_exception_check(error);
return Qnil;
}
|
#get(key) ⇒ Object #[](key) ⇒ Object
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'
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'ext/rugged/rugged_config.c', line 85
static VALUE rb_git_config_get(VALUE self, VALUE rb_key)
{
git_config *config;
git_buf buf = { NULL };
int error;
VALUE rb_result;
TypedData_Get_Struct(self, git_config, &rugged_config_type, 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_dispose(&buf);
return rb_result;
}
|
#get_all(key) ⇒ Array
Get a list of values for the given config key. Values are always
returned as an Array of String, or nil if the given key doesn't exist
in the Config file.
cfg['apply.whitespace'] #=> ['fix']
cfg['diff.renames'] #=> ['true']
cfg['remote.origin.fetch'] #=> ["+refs/heads/*:refs/remotes/origin/*", "+refs/heads/*:refs/lolol/origin/*"]
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
# File 'ext/rugged/rugged_config.c', line 398
static VALUE rb_git_config_get_all(VALUE self, VALUE key)
{
git_config *config;
VALUE list;
int error;
TypedData_Get_Struct(self, git_config, &rugged_config_type, config);
list = rb_ary_new();
error = git_config_get_multivar_foreach(
config, StringValueCStr(key), NULL, each_config_value, (void *)list);
if (error == GIT_ENOTFOUND)
return Qnil;
rugged_exception_check(error);
return list;
}
|
#snapshot ⇒ Object
Create a snapshot of the configuration.
Provides a consistent, read-only view of the configuration for looking up complex values from a configuration.
325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'ext/rugged/rugged_config.c', line 325
static VALUE rb_git_config_snapshot(VALUE self)
{
git_config *config, *snapshot;
TypedData_Get_Struct(self, git_config, &rugged_config_type, config);
rugged_exception_check(
git_config_snapshot(&snapshot, config)
);
return rugged_config_new(rb_obj_class(self), Qnil, snapshot);
}
|
#store(key, value) ⇒ Object #[]=(key) ⇒ Object
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
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'ext/rugged/rugged_config.c', line 121
static VALUE rb_git_config_store(VALUE self, VALUE rb_key, VALUE rb_val)
{
git_config *config;
const char *key;
int error;
TypedData_Get_Struct(self, git_config, &rugged_config_type, 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
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"}
282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'ext/rugged/rugged_config.c', line 282
static VALUE rb_git_config_to_hash(VALUE self)
{
git_config *config;
int error;
VALUE hash;
TypedData_Get_Struct(self, git_config, &rugged_config_type, config);
hash = rb_hash_new();
error = git_config_foreach(config, &cb_config__to_hash, (void *)hash);
rugged_exception_check(error);
return hash;
}
|
#transaction {|config| ... } ⇒ Object
Perform configuration changes in a transaction.
Locks the configuration, executes the given block and stores any changes that were made to the configuration. If the block throws an exception, all changes are rolled back automatically.
During the execution of the block, configuration changes don't get stored to disk immediately, so reading from the configuration will continue to return the values that were stored in the configuration when the transaction was started.
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'ext/rugged/rugged_config.c', line 353
static VALUE rb_git_config_transaction(VALUE self)
{
git_config *config;
git_transaction *tx;
VALUE rb_result;
int error = 0, exception = 0;
TypedData_Get_Struct(self, git_config, &rugged_config_type, config);
git_config_lock(&tx, config);
rb_result = rb_protect(rb_yield, self, &exception);
if (!exception)
error = git_transaction_commit(tx);
git_transaction_free(tx);
if (exception)
rb_jump_tag(exception);
else if (error)
rugged_exception_check(error);
return rb_result;
}
|