Skip to content

Commit 2f23280

Browse files
Add Rugged::Config#snapshot.
1 parent e0e8f1c commit 2f23280

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

ext/rugged/rugged_config.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,28 @@ static VALUE rb_git_config_open_default(VALUE klass)
306306
return rugged_config_new(klass, Qnil, cfg);
307307
}
308308

309+
/*
310+
* call-seq:
311+
* config.snapshot -> snapshot
312+
*
313+
* Create a snapshot of the configuration.
314+
*
315+
* Provides a consistent, read-only view of the configuration for
316+
* looking up complex values from a configuration.
317+
*/
318+
static VALUE rb_git_config_snapshot(VALUE self)
319+
{
320+
git_config *config, *snapshot;
321+
322+
Data_Get_Struct(self, git_config, config);
323+
324+
rugged_exception_check(
325+
git_config_snapshot(&snapshot, config)
326+
);
327+
328+
return rugged_config_new(rb_obj_class(self), Qnil, snapshot);
329+
}
330+
309331
void Init_rugged_config(void)
310332
{
311333
/*
@@ -330,4 +352,5 @@ void Init_rugged_config(void)
330352
rb_define_method(rb_cRuggedConfig, "each", rb_git_config_each_pair, 0);
331353
rb_define_method(rb_cRuggedConfig, "to_hash", rb_git_config_to_hash, 0);
332354

355+
rb_define_method(rb_cRuggedConfig, "snapshot", rb_git_config_snapshot, 0);
333356
}

test/config_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ def test_read_global_config_file
2121
assert config['user.name'] != nil
2222
assert_nil config['core.bare']
2323
end
24+
25+
def test_snapshot
26+
config = Rugged::Config.new(File.join(@repo.path, 'config'))
27+
config['old.value'] = 5
28+
29+
snapshot = config.snapshot
30+
assert_equal '5', snapshot['old.value']
31+
32+
config['new.value'] = 42
33+
config['old.value'] = 1337
34+
35+
assert_equal '5', snapshot['old.value']
36+
assert_nil snapshot['new.value']
37+
end
2438
end
2539

2640
class ConfigWriteTest < Rugged::TestCase

0 commit comments

Comments
 (0)