Skip to content

Commit c121506

Browse files
Add setter and getter for the Repository ident.
1 parent 18247c0 commit c121506

File tree

4 files changed

+140
-6
lines changed

4 files changed

+140
-6
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11

2+
* Add accessors for the Repository ident.
3+
4+
Added `Repository#ident` and `Repository#ident=` to read and set the
5+
identity that is used when writing reflog entries.
6+
7+
*Arthur Schreiber*
8+
29
* Updated the API of reflog modifying methods.
310

411
This removes both the optional `:message` as well as `:signature` options from

ext/rugged/rugged_repo.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,75 @@ static VALUE rb_git_repo_get_config(VALUE self)
593593
RB_GIT_REPO_OWNED_GET(rb_cRuggedConfig, config);
594594
}
595595

596+
/*
597+
* call-seq:
598+
* repo.ident = ident
599+
*
600+
* Set the identity to be used for writing reflogs.
601+
*
602+
* +ident+ can be either +nil+ or a Hash containing +name+ and/or +email+ entries.
603+
*/
604+
static VALUE rb_git_repo_set_ident(VALUE self, VALUE rb_ident) {
605+
VALUE rb_val;
606+
607+
git_repository *repo;
608+
const char *name = NULL, *email = NULL;
609+
610+
Data_Get_Struct(self, git_repository, repo);
611+
612+
if (!NIL_P(rb_ident)) {
613+
Check_Type(rb_ident, T_HASH);
614+
615+
if (!NIL_P(rb_val = rb_hash_aref(rb_ident, CSTR2SYM("name")))) {
616+
Check_Type(rb_val, T_STRING);
617+
name = StringValueCStr(rb_val);
618+
}
619+
620+
if (!NIL_P(rb_val = rb_hash_aref(rb_ident, CSTR2SYM("email")))) {
621+
Check_Type(rb_val, T_STRING);
622+
email = StringValueCStr(rb_val);
623+
}
624+
}
625+
626+
rugged_exception_check(
627+
git_repository_set_ident(repo, name, email)
628+
);
629+
630+
return Qnil;
631+
}
632+
633+
/*
634+
* call-seq:
635+
* repo.ident -> ident
636+
*
637+
* Return a Hash containing the identity that is used to write reflogs.
638+
*
639+
* +ident+ is a Hash containing +name+ and/or +email+ entries, or `nil`.
640+
*/
641+
static VALUE rb_git_repo_get_ident(VALUE self)
642+
{
643+
VALUE rb_ident = rb_hash_new();
644+
645+
git_repository *repo;
646+
const char *name = NULL, *email = NULL;
647+
648+
Data_Get_Struct(self, git_repository, repo);
649+
650+
rugged_exception_check(
651+
git_repository_ident(&name, &email, repo)
652+
);
653+
654+
if (name) {
655+
rb_hash_aset(rb_ident, CSTR2SYM("name"), rb_str_new_utf8(name));
656+
}
657+
658+
if (email) {
659+
rb_hash_aset(rb_ident, CSTR2SYM("email"), rb_str_new_utf8(email));
660+
}
661+
662+
return rb_ident;
663+
}
664+
596665
/*
597666
* call-seq:
598667
* repo.merge_base(oid1, oid2, ...)
@@ -2386,6 +2455,9 @@ void Init_rugged_repo(void)
23862455
rb_define_method(rb_cRuggedRepo, "config", rb_git_repo_get_config, 0);
23872456
rb_define_method(rb_cRuggedRepo, "config=", rb_git_repo_set_config, 1);
23882457

2458+
rb_define_method(rb_cRuggedRepo, "ident", rb_git_repo_get_ident, 0);
2459+
rb_define_method(rb_cRuggedRepo, "ident=", rb_git_repo_set_ident, 1);
2460+
23892461
rb_define_method(rb_cRuggedRepo, "bare?", rb_git_repo_is_bare, 0);
23902462
rb_define_method(rb_cRuggedRepo, "shallow?", rb_git_repo_is_shallow, 0);
23912463
rb_define_method(rb_cRuggedRepo, "empty?", rb_git_repo_is_empty, 0);

test/reference_test.rb

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,12 @@ def test_create_default_log
324324
assert_kind_of Time, reflog[0][:committer][:time]
325325
end
326326

327-
def test_create_default_log_custom_signature
327+
def test_create_default_log_custom_ident
328+
@repo.ident = {
329+
name: 'Other User',
330+
331+
}
332+
328333
ref = @repo.references.create("refs/heads/test-reflog-default",
329334
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750")
330335
reflog = ref.log
@@ -334,8 +339,8 @@ def test_create_default_log_custom_signature
334339
assert_equal '0000000000000000000000000000000000000000', reflog[0][:id_old]
335340
assert_equal 'a65fedf39aefe402d3bb6e24df4d4f5fe4547750', reflog[0][:id_new]
336341
assert_equal nil, reflog[0][:message]
337-
assert_equal @ident[:name], reflog[0][:committer][:name]
338-
assert_equal @ident[:email], reflog[0][:committer][:email]
342+
assert_equal 'Other User', reflog[0][:committer][:name]
343+
assert_equal '[email protected]', reflog[0][:committer][:email]
339344
assert_kind_of Time, reflog[0][:committer][:time]
340345
end
341346

@@ -357,7 +362,12 @@ def test_create_default_log_custom_log_message
357362
assert_kind_of Time, reflog[0][:committer][:time]
358363
end
359364

360-
def test_create_default_log_custom_signature_and_log_message
365+
def test_create_default_log_custom_ident_and_log_message
366+
@repo.ident = {
367+
name: 'Other User',
368+
369+
}
370+
361371
ref = @repo.references.create(
362372
"refs/heads/test-reflog-default",
363373
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750", {
@@ -370,8 +380,8 @@ def test_create_default_log_custom_signature_and_log_message
370380
assert_equal '0000000000000000000000000000000000000000', reflog[0][:id_old]
371381
assert_equal 'a65fedf39aefe402d3bb6e24df4d4f5fe4547750', reflog[0][:id_new]
372382
assert_equal "reference created", reflog[0][:message]
373-
assert_equal @ident[:name], reflog[0][:committer][:name]
374-
assert_equal @ident[:email], reflog[0][:committer][:email]
383+
assert_equal 'Other User', reflog[0][:committer][:name]
384+
assert_equal '[email protected]', reflog[0][:committer][:email]
375385
assert_kind_of Time, reflog[0][:committer][:time]
376386
end
377387

@@ -389,6 +399,25 @@ def test_set_target_default_log
389399
assert_kind_of Time, reflog[1][:committer][:time]
390400
end
391401

402+
def test_set_target_default_log_custom_signature
403+
@repo.ident = {
404+
name: "Other User",
405+
406+
}
407+
408+
@repo.references.update(@ref, "5b5b025afb0b4c913b4c338a42934a3863bf3644")
409+
410+
reflog = @ref.log
411+
assert_equal reflog.size, 2
412+
413+
assert_equal 'a65fedf39aefe402d3bb6e24df4d4f5fe4547750', reflog[1][:id_old]
414+
assert_equal '5b5b025afb0b4c913b4c338a42934a3863bf3644', reflog[1][:id_new]
415+
assert_equal nil, reflog[1][:message]
416+
assert_equal "Other User", reflog[1][:committer][:name]
417+
assert_equal "[email protected]", reflog[1][:committer][:email]
418+
assert_kind_of Time, reflog[1][:committer][:time]
419+
end
420+
392421
def test_set_target_default_log_custom_log_message
393422
@repo.references.update(@ref, "5b5b025afb0b4c913b4c338a42934a3863bf3644", {
394423
message: "reference updated"

test/repo_test.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,32 @@ def test_default_signature
397397
assert_equal name, @repo.default_signature[:name]
398398
assert_equal email, @repo.default_signature[:email]
399399
end
400+
401+
def test_ident
402+
assert_equal(nil, @repo.ident[:name])
403+
assert_equal(nil, @repo.ident[:email])
404+
405+
@repo.ident = { name: "Other User" }
406+
assert_equal("Other User", @repo.ident[:name])
407+
assert_equal(nil, @repo.ident[:email])
408+
409+
@repo.ident = { email: "[email protected]" }
410+
assert_equal(nil, @repo.ident[:name])
411+
assert_equal("[email protected]", @repo.ident[:email])
412+
413+
@repo.ident = { name: "Other User", email: "[email protected]" }
414+
assert_equal("Other User", @repo.ident[:name])
415+
assert_equal("[email protected]", @repo.ident[:email])
416+
417+
@repo.ident = {}
418+
assert_equal(nil, @repo.ident[:name])
419+
assert_equal(nil, @repo.ident[:email])
420+
421+
@repo.ident = { name: "Other User", email: "[email protected]" }
422+
@repo.ident = nil
423+
assert_equal(nil, @repo.ident[:name])
424+
assert_equal(nil, @repo.ident[:email])
425+
end
400426
end
401427

402428
class RepositoryDiscoverTest < Rugged::TestCase

0 commit comments

Comments
 (0)