Skip to content

Commit 8d926c2

Browse files
committed
Introduce Commit::create_buffer()
Similar to Commit::create() but this returns the commit as a buffer for further processing by the caller.
1 parent 7fea733 commit 8d926c2

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

ext/rugged/rugged_commit.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,11 +693,77 @@ static VALUE rb_git_commit_extract_signature(int argc, VALUE *argv, VALUE self)
693693
return ret;
694694
}
695695

696+
/*
697+
* call-seq:
698+
* Commit.create_to_s(repository, data = {}) -> str
699+
*
700+
* Create a string with the contents of the commit, created with the
701+
* given +data+ arguments, passed as a +Hash+:
702+
*
703+
* - +:message+: a string with the full text for the commit's message
704+
* - +:committer+ (optional): a hash with the signature for the committer,
705+
* defaults to the signature from the configuration
706+
* - +:author+ (optional): a hash with the signature for the author,
707+
* defaults to the signature from the configuration
708+
* - +:parents+: an +Array+ with zero or more parents for this commit,
709+
* represented as <tt>Rugged::Commit</tt> instances, or OID +String+.
710+
* - +:tree+: the tree for this commit, represented as a <tt>Rugged::Tree</tt>
711+
* instance or an OID +String+.
712+
*
713+
* author = {:email=>"[email protected]", :time=>Time.now, :name=>"Vicent Mart\303\255"}
714+
*
715+
* Rugged::Commit.create(r,
716+
* :author => author,
717+
* :message => "Hello world\n\n",
718+
* :committer => author,
719+
* :parents => ["2cb831a8aea28b2c1b9c63385585b864e4d3bad1"],
720+
* :tree => some_tree) #=> "tree some_tree\nparent 2cb831...."
721+
*/
722+
static VALUE rb_git_commit_create_to_s(VALUE self, VALUE rb_repo, VALUE rb_data)
723+
{
724+
int error = 0;
725+
struct commit_data commit_data = { Qnil };
726+
git_repository *repo;
727+
git_buf buf = { 0 };
728+
729+
Check_Type(rb_data, T_HASH);
730+
731+
rugged_check_repo(rb_repo);
732+
Data_Get_Struct(rb_repo, git_repository, repo);
733+
734+
if ((error = parse_commit_options(&commit_data, repo, rb_data)) < 0)
735+
goto cleanup;
736+
737+
error = git_commit_create_buffer(
738+
&buf,
739+
repo,
740+
commit_data.author,
741+
commit_data.committer,
742+
NULL,
743+
commit_data.message,
744+
commit_data.tree,
745+
commit_data.parent_count,
746+
commit_data.parents);
747+
748+
cleanup:
749+
free_commit_options(&commit_data);
750+
if (!NIL_P(commit_data.rb_err_obj))
751+
rb_exc_raise(commit_data.rb_err_obj);
752+
753+
rugged_exception_check(error);
754+
755+
VALUE ret = rb_str_new_utf8(buf.ptr);
756+
git_buf_free(&buf);
757+
758+
return ret;
759+
}
760+
696761
void Init_rugged_commit(void)
697762
{
698763
rb_cRuggedCommit = rb_define_class_under(rb_mRugged, "Commit", rb_cRuggedObject);
699764

700765
rb_define_singleton_method(rb_cRuggedCommit, "create", rb_git_commit_create, 2);
766+
rb_define_singleton_method(rb_cRuggedCommit, "create_to_s", rb_git_commit_create_to_s, 2);
701767
rb_define_singleton_method(rb_cRuggedCommit, "extract_signature", rb_git_commit_extract_signature, -1);
702768

703769
rb_define_method(rb_cRuggedCommit, "message", rb_git_commit_message_GET, 0);

test/commit_test.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,27 @@ def test_write_empty_email_fails
369369
:tree => "c4dc1555e4d4fa0e0c9c3fc46734c7c35b3ce90b")
370370
end
371371
end
372+
373+
def test_create_commit_to_s
374+
person = {:name => 'Scott', :email => '[email protected]', :time => Time.now }
375+
376+
id = Rugged::Commit.create(@repo,
377+
:message => "This is the commit message\n\nThis commit is created from Rugged",
378+
:committer => person,
379+
:author => person,
380+
:parents => [@repo.head.target],
381+
:tree => "c4dc1555e4d4fa0e0c9c3fc46734c7c35b3ce90b")
382+
383+
buffer = Rugged::Commit.create_to_s(@repo,
384+
:message => "This is the commit message\n\nThis commit is created from Rugged",
385+
:committer => person,
386+
:author => person,
387+
:parents => [@repo.head.target],
388+
:tree => "c4dc1555e4d4fa0e0c9c3fc46734c7c35b3ce90b")
389+
390+
commit = @repo.lookup(id)
391+
assert_equal buffer, commit.read_raw.data
392+
end
372393
end
373394

374395
class CommitToMboxTest < Rugged::TestCase

0 commit comments

Comments
 (0)