@@ -328,6 +328,47 @@ static VALUE rb_git_config_snapshot(VALUE self)
328
328
return rugged_config_new (rb_obj_class (self ), Qnil , snapshot );
329
329
}
330
330
331
+ /*
332
+ * call-seq:
333
+ * config.transaction { |config| }
334
+ *
335
+ * Perform configuration changes in a transaction.
336
+ *
337
+ * Locks the configuration, executes the given block and stores
338
+ * any changes that were made to the configuration. If the block
339
+ * throws an exception, all changes are rolled back automatically.
340
+ *
341
+ * During the execution of the block, configuration changes don't
342
+ * get stored to disk immediately, so reading from the configuration
343
+ * will continue to return the values that were stored in the configuration
344
+ * when the transaction was started.
345
+ */
346
+ static VALUE rb_git_config_transaction (VALUE self )
347
+ {
348
+ git_config * config ;
349
+ git_transaction * tx ;
350
+ VALUE rb_result ;
351
+ int error = 0 , exception = 0 ;
352
+
353
+ Data_Get_Struct (self , git_config , config );
354
+
355
+ git_config_lock (& tx , config );
356
+
357
+ rb_result = rb_protect (rb_yield , self , & exception );
358
+
359
+ if (!exception )
360
+ error = git_transaction_commit (tx );
361
+
362
+ git_transaction_free (tx );
363
+
364
+ if (exception )
365
+ rb_jump_tag (exception );
366
+ else if (error )
367
+ rugged_exception_check (error );
368
+
369
+ return rb_result ;
370
+ }
371
+
331
372
void Init_rugged_config (void )
332
373
{
333
374
/*
@@ -353,4 +394,5 @@ void Init_rugged_config(void)
353
394
rb_define_method (rb_cRuggedConfig , "to_hash" , rb_git_config_to_hash , 0 );
354
395
355
396
rb_define_method (rb_cRuggedConfig , "snapshot" , rb_git_config_snapshot , 0 );
397
+ rb_define_method (rb_cRuggedConfig , "transaction" , rb_git_config_transaction , 0 );
356
398
}
0 commit comments