Skip to content

Commit 33873e5

Browse files
author
Vicent Martí
authored
Merge pull request libgit2#790 from repotag/diff_string
[Feature] Expose libgit2's git_diff_from_buffer in rugged through Rugged::Repository#diff_from_buffer(buffer)
2 parents 551a0fb + 3f614b1 commit 33873e5

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

ext/rugged/rugged_repo.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2710,6 +2710,29 @@ static VALUE rb_git_repo_cherrypick_commit(int argc, VALUE *argv, VALUE self)
27102710
return rugged_index_new(rb_cRuggedIndex, self, index);
27112711
}
27122712

2713+
/*
2714+
* call-seq: repo.diff_from_buffer(buffer) -> Rugged::Diff object
2715+
*
2716+
* Where +buffer+ is a +String+.
2717+
* Returns A Rugged::Diff object
2718+
*/
2719+
static VALUE rb_git_diff_from_buffer(VALUE self, VALUE rb_buffer)
2720+
{
2721+
git_diff *diff = NULL;
2722+
const char *buffer;
2723+
size_t len;
2724+
int error;
2725+
2726+
Check_Type(rb_buffer, T_STRING);
2727+
buffer = RSTRING_PTR(rb_buffer);
2728+
len = RSTRING_LEN(rb_buffer);
2729+
2730+
error = git_diff_from_buffer(&diff, buffer, len);
2731+
rugged_exception_check(error);
2732+
2733+
return rugged_diff_new(rb_cRuggedDiff, self, diff);
2734+
}
2735+
27132736
void Init_rugged_repo(void)
27142737
{
27152738
id_call = rb_intern("call");
@@ -2768,6 +2791,8 @@ void Init_rugged_repo(void)
27682791
rb_define_method(rb_cRuggedRepo, "apply", rb_git_repo_apply, -1);
27692792

27702793
rb_define_method(rb_cRuggedRepo, "revert_commit", rb_git_repo_revert_commit, -1);
2794+
2795+
rb_define_method(rb_cRuggedRepo, "diff_from_buffer", rb_git_diff_from_buffer, 1);
27712796

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

test/diff_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ def test_from_strings_with_custom_paths
6565
end
6666

6767
class RepoDiffTest < Rugged::TestCase
68+
def test_new_from_buffer
69+
repo = FixtureRepo.from_libgit2("attr")
70+
patch1 = Rugged::Patch.from_strings("deleted\n", "added\n", old_path: "old", new_path: "new").to_s
71+
diff1 = repo.diff_from_buffer(patch1)
72+
assert_equal diff1.patch, patch1
73+
74+
diff2 = repo.diff("605812a", "370fe9ec22", :context_lines => 1, :interhunk_lines => 1)
75+
patch2 = diff2.patch
76+
diff3 = repo.diff_from_buffer(patch2)
77+
assert_equal diff3.patch, patch2
78+
end
79+
6880
def test_with_oid_string
6981
repo = FixtureRepo.from_libgit2("attr")
7082
diff = repo.diff("605812a", "370fe9ec22", :context_lines => 1, :interhunk_lines => 1)

0 commit comments

Comments
 (0)