Skip to content

Commit

Permalink
add force option to obj.create_note
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolai Vladimirov committed Jan 3, 2013
1 parent 2f6b301 commit 35a26de
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
10 changes: 8 additions & 2 deletions ext/rugged/rugged_note.c
Expand Up @@ -119,6 +119,7 @@ static VALUE rb_git_note_lookup(int argc, VALUE *argv, VALUE self)
* - +:committer+: a hash with the signature for the committer
* - +:author+: a hash with the signature for the author
* - +:ref+: (optional): cannonical name of the reference to use, defaults to "refs/notes/commits"
* - +:force+: (optional): overwrite existing note (disabled by default)
*
* When the note is successfully written to disk, its +oid+ will be
* returned as a hex +String+.
Expand All @@ -134,7 +135,7 @@ static VALUE rb_git_note_lookup(int argc, VALUE *argv, VALUE self)
*/
static VALUE rb_git_note_create(VALUE self, VALUE rb_data)
{
VALUE rb_ref, rb_message;
VALUE rb_ref, rb_message, rb_force;
git_repository *repo = NULL;
const char *notes_ref = NULL;

Expand All @@ -145,6 +146,7 @@ static VALUE rb_git_note_create(VALUE self, VALUE rb_data)

git_object *target = NULL;
int error = 0;
int force = 0;

Check_Type(rb_data, T_HASH);

Expand All @@ -155,6 +157,10 @@ static VALUE rb_git_note_create(VALUE self, VALUE rb_data)

rb_ref = rb_hash_aref(rb_data, CSTR2SYM("ref"));

rb_force = rb_hash_aref(rb_data, CSTR2SYM("force"));
if (!NIL_P(rb_force))
force = rugged_parse_bool(rb_force);

if (!NIL_P(rb_ref)) {
Check_Type(rb_ref, T_STRING);
notes_ref = StringValueCStr(rb_ref);
Expand All @@ -179,7 +185,7 @@ static VALUE rb_git_note_create(VALUE self, VALUE rb_data)
notes_ref,
git_object_id(target),
StringValueCStr(rb_message),
0);
force);


git_signature_free(author);
Expand Down
46 changes: 46 additions & 0 deletions test/note_test.rb
Expand Up @@ -77,6 +77,52 @@ def test_create_note
assert_equal note[:message], message
end

def test_create_note_on_object_with_notes_raises
person = {:name => 'Scott', :email => 'schacon@gmail.com', :time => Time.now }
oid = "8496071c1b46c854b31185ea97743be6a8774479"
message ="This is the note message\n\nThis note is created from Rugged"
obj = @repo.lookup(oid)
obj.create_note(
:message => message,
:committer => person,
:author => person,
:ref => 'refs/notes/test'
)

assert_raises Rugged::RepositoryError do
obj.create_note(
:message => message,
:committer => person,
:author => person,
:ref => 'refs/notes/test'
)
end
end

def test_overwrite_object_note
person = {:name => 'Scott', :email => 'schacon@gmail.com', :time => Time.now }
oid = "8496071c1b46c854b31185ea97743be6a8774479"
message ="This is the note message\n\nThis note is created from Rugged"
obj = @repo.lookup(oid)
obj.create_note(
:message => message,
:committer => person,
:author => person,
:ref => 'refs/notes/test'
)

obj.create_note(
:message => 'new message',
:committer => person,
:author => person,
:ref => 'refs/notes/test',
:force => true
)

note = obj.notes('refs/notes/test')
assert_equal note[:message], 'new message'
end

def test_remove_note
oid = "36060c58702ed4c2a40832c51758d5344201d89a"
person = {:name => 'Scott', :email => 'schacon@gmail.com', :time => Time.now }
Expand Down

0 comments on commit 35a26de

Please sign in to comment.