Skip to content

Commit

Permalink
Merge pull request #790 from repotag/diff_string
Browse files Browse the repository at this point in the history
[Feature] Expose libgit2's git_diff_from_buffer in rugged through Rugged::Repository#diff_from_buffer(buffer)
  • Loading branch information
Vicent Martí authored Apr 9, 2019
2 parents 551a0fb + 3f614b1 commit 33873e5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
25 changes: 25 additions & 0 deletions ext/rugged/rugged_repo.c
Original file line number Diff line number Diff line change
Expand Up @@ -2710,6 +2710,29 @@ static VALUE rb_git_repo_cherrypick_commit(int argc, VALUE *argv, VALUE self)
return rugged_index_new(rb_cRuggedIndex, self, index);
}

/*
* call-seq: repo.diff_from_buffer(buffer) -> Rugged::Diff object
*
* Where +buffer+ is a +String+.
* Returns A Rugged::Diff object
*/
static VALUE rb_git_diff_from_buffer(VALUE self, VALUE rb_buffer)
{
git_diff *diff = NULL;
const char *buffer;
size_t len;
int error;

Check_Type(rb_buffer, T_STRING);
buffer = RSTRING_PTR(rb_buffer);
len = RSTRING_LEN(rb_buffer);

error = git_diff_from_buffer(&diff, buffer, len);
rugged_exception_check(error);

return rugged_diff_new(rb_cRuggedDiff, self, diff);
}

void Init_rugged_repo(void)
{
id_call = rb_intern("call");
Expand Down Expand Up @@ -2768,6 +2791,8 @@ void Init_rugged_repo(void)
rb_define_method(rb_cRuggedRepo, "apply", rb_git_repo_apply, -1);

rb_define_method(rb_cRuggedRepo, "revert_commit", rb_git_repo_revert_commit, -1);

rb_define_method(rb_cRuggedRepo, "diff_from_buffer", rb_git_diff_from_buffer, 1);

rb_define_method(rb_cRuggedRepo, "path_ignored?", rb_git_repo_is_path_ignored, 1);

Expand Down
12 changes: 12 additions & 0 deletions test/diff_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ def test_from_strings_with_custom_paths
end

class RepoDiffTest < Rugged::TestCase
def test_new_from_buffer
repo = FixtureRepo.from_libgit2("attr")
patch1 = Rugged::Patch.from_strings("deleted\n", "added\n", old_path: "old", new_path: "new").to_s
diff1 = repo.diff_from_buffer(patch1)
assert_equal diff1.patch, patch1

diff2 = repo.diff("605812a", "370fe9ec22", :context_lines => 1, :interhunk_lines => 1)
patch2 = diff2.patch
diff3 = repo.diff_from_buffer(patch2)
assert_equal diff3.patch, patch2
end

def test_with_oid_string
repo = FixtureRepo.from_libgit2("attr")
diff = repo.diff("605812a", "370fe9ec22", :context_lines => 1, :interhunk_lines => 1)
Expand Down

0 comments on commit 33873e5

Please sign in to comment.