Skip to content

Commit 0205e80

Browse files
author
Vicent Marti
committed
Merge pull request #491 from libgit2/vmg/loc
Implement Blob#loc
2 parents d29af88 + 0447616 commit 0205e80

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

Gemfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,6 @@ DEPENDENCIES
230230
rake-compiler (>= 0.9.0)
231231
rubysl (~> 2.0)
232232
rugged!
233+
234+
BUNDLED WITH
235+
1.10.3

ext/rugged/rugged_blob.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,44 @@ static VALUE rb_git_blob_from_io(int argc, VALUE *argv, VALUE klass)
317317
return rugged_create_oid(&oid);
318318
}
319319

320+
/*
321+
* call-seq:
322+
* blob.loc -> int
323+
*
324+
* Return the number of lines for this blob,
325+
* assuming the blob is plaintext (i.e. not binary)
326+
*/
327+
static VALUE rb_git_blob_loc(VALUE self)
328+
{
329+
git_blob *blob;
330+
const char *data, *data_end;
331+
size_t loc = 0;
332+
333+
Data_Get_Struct(self, git_blob, blob);
334+
335+
data = git_blob_rawcontent(blob);
336+
data_end = data + git_blob_rawsize(blob);
337+
338+
if (data == data_end)
339+
return INT2FIX(0);
340+
341+
for (; data < data_end; ++data) {
342+
if (data[0] == '\n') {
343+
loc++;
344+
}
345+
else if (data[0] == '\r') {
346+
if (data + 1 < data_end && data[1] == '\n')
347+
data++;
348+
loc++;
349+
}
350+
}
351+
352+
if (data[-1] != '\n' && data[-1] != '\r')
353+
loc++;
354+
355+
return INT2FIX(loc);
356+
}
357+
320358

321359
/*
322360
* call-seq:
@@ -583,6 +621,7 @@ void Init_rugged_blob(void)
583621
rb_define_method(rb_cRuggedBlob, "content", rb_git_blob_content_GET, -1);
584622
rb_define_method(rb_cRuggedBlob, "text", rb_git_blob_text_GET, -1);
585623
rb_define_method(rb_cRuggedBlob, "sloc", rb_git_blob_sloc, 0);
624+
rb_define_method(rb_cRuggedBlob, "loc", rb_git_blob_loc, 0);
586625
rb_define_method(rb_cRuggedBlob, "binary?", rb_git_blob_is_binary, 0);
587626
rb_define_method(rb_cRuggedBlob, "diff", rb_git_blob_diff, -1);
588627

test/blob_test.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,45 @@ def test_blob_is_binary
170170
end
171171
end
172172

173+
class BlobLOCTest < Rugged::TestCase
174+
include Rugged::TempRepositoryAccess
175+
176+
def write_blob(data)
177+
sha = Rugged::Blob.from_buffer(@repo, data)
178+
Rugged::Blob.lookup(@repo, sha)
179+
end
180+
181+
def test_loc_end_nl
182+
blob = write_blob("hello\nworld\nwhat\n")
183+
assert_equal 3, blob.loc
184+
end
185+
186+
def test_loc_no_end_nl
187+
blob = write_blob("hello\nworld\nwhat")
188+
assert_equal 3, blob.loc
189+
end
190+
191+
def test_loc_carriages
192+
blob = write_blob("hello\r\nworld\r\nwhat\r\n")
193+
assert_equal 3, blob.loc
194+
end
195+
196+
def test_loc_carriages_no_end_nl
197+
blob = write_blob("hello\r\nworld\r\nwhat")
198+
assert_equal 3, blob.loc
199+
end
200+
201+
def test_loc_mixed
202+
blob = write_blob("hello\nworld\rwhat\r")
203+
assert_equal 3, blob.loc
204+
end
205+
206+
def test_loc_mixed_no_end_nl
207+
blob = write_blob("hello\nworld\rwhat")
208+
assert_equal 3, blob.loc
209+
end
210+
end
211+
173212
class BlobDiffTest < Rugged::SandboxedTestCase
174213
def setup
175214
super

0 commit comments

Comments
 (0)