Class: Rugged::Config

Inherits:
Object
  • Object
show all
Defined in:
ext/rugged/rugged_config.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.globalObject .open_globalObject

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.



287
288
289
290
291
292
293
294
295
296
# File 'ext/rugged/rugged_config.c', line 287

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.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'ext/rugged/rugged_config.c', line 33

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, 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);
}

.globalObject .open_globalObject

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.



287
288
289
290
291
292
293
294
295
296
# File 'ext/rugged/rugged_config.c', line 287

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'


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'ext/rugged/rugged_config.c', line 76

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) ⇒ 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


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'ext/rugged/rugged_config.c', line 112

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) ⇒ 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.

Returns:

  • (Boolean)


156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'ext/rugged/rugged_config.c', line 156

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| ... } ⇒ Object #each_pairObject #each {|key, value| ... } ⇒ Object #eachObject

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

Overloads:

  • #each_pair {|key, value| ... } ⇒ Object

    Yields:

    • (key, value)
  • #each {|key, value| ... } ⇒ Object

    Yields:

    • (key, value)


239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'ext/rugged/rugged_config.c', line 239

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| ... } ⇒ Object #each_keyObject

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

Overloads:

  • #each_key {|key| ... } ⇒ Object

    Yields:

    • (key)


210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'ext/rugged/rugged_config.c', line 210

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| ... } ⇒ Object #each_pairObject #each {|key, value| ... } ⇒ Object #eachObject

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

Overloads:

  • #each_pair {|key, value| ... } ⇒ Object

    Yields:

    • (key, value)
  • #each {|key, value| ... } ⇒ Object

    Yields:

    • (key, value)


239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'ext/rugged/rugged_config.c', line 239

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) ⇒ 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'


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'ext/rugged/rugged_config.c', line 76

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;
}

#snapshotObject

Create a snapshot of the configuration.

Provides a consistent, read-only view of the configuration for looking up complex values from a configuration.



307
308
309
310
311
312
313
314
315
316
317
318
# File 'ext/rugged/rugged_config.c', line 307

static VALUE rb_git_config_snapshot(VALUE self)
{
	git_config *config, *snapshot;

	Data_Get_Struct(self, git_config, 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


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'ext/rugged/rugged_config.c', line 112

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_hashHash

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"}

Returns:

  • (Hash)


264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'ext/rugged/rugged_config.c', line 264

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;
}

#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.

Yields:

  • (config)


335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'ext/rugged/rugged_config.c', line 335

static VALUE rb_git_config_transaction(VALUE self)
{
	git_config *config;
	git_transaction *tx;
	VALUE rb_result;
	int error = 0, exception = 0;

	Data_Get_Struct(self, git_config, 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;
}