Skip to content

Commit ee84a3f

Browse files
authored
Merge pull request #19959 from JuliaLang/ksh/blobshow
Show + isbinary method for GitBlob, and a doc link
2 parents 73f8ead + 78133de commit ee84a3f

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

base/libgit2/blob.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,42 @@ function Base.length(blob::GitBlob)
88
return ccall((:git_blob_rawsize, :libgit2), Int64, (Ptr{Void},), blob.ptr)
99
end
1010

11+
"""
12+
Use a heuristic to guess if a file is binary: searching for NULL bytes and
13+
looking for a reasonable ratio of printable to non-printable characters among
14+
the first 8000 bytes.
15+
"""
16+
function isbinary(blob::GitBlob)
17+
bin_flag = ccall((:git_blob_is_binary, :libgit2), Int64, (Ptr{Void},), blob.ptr)
18+
return bin_flag == 1
19+
end
20+
1121
function lookup(repo::GitRepo, oid::GitHash)
1222
blob_ptr_ptr = Ref{Ptr{Void}}(C_NULL)
1323
@check ccall((:git_blob_lookup, :libgit2), Cint,
1424
(Ptr{Ptr{Void}}, Ptr{Void}, Ref{GitHash}),
1525
blob_ptr_ptr, repo.ptr, Ref(oid))
1626
return GitBlob(blob_ptr_ptr[])
1727
end
28+
29+
function GitBlob(repo::GitRepo, path::AbstractString)
30+
blob_id_ptr = Ref(GitHash())
31+
@check ccall((:git_blob_create_fromdisk, :libgit2), Cint,
32+
(Ptr{GitHash}, Ptr{Void}, Ptr{UInt8}), blob_id_ptr,
33+
repo.ptr, path)
34+
blob = get(GitBlob, repo, blob_id_ptr[])
35+
return blob
36+
end
37+
38+
function Base.show(io::IO, blob::GitBlob)
39+
if !isbinary(blob)
40+
conts = split(content(blob), "\n")
41+
showlen = max(len(conts), 3)
42+
print(io, "GitBlob:\nBlob id: ", GitHash(blob.ptr), "\nContents:\n")
43+
for i in 1:showlen
44+
print(io, conts[i],"\n")
45+
end
46+
else
47+
print(io, "GitBlob:\nBlob id: ", GitHash(blob.ptr), "\nContents are binary.")
48+
end
49+
end

base/libgit2/types.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ end
348348
LibGit2.RebaseOperation
349349
350350
Describes a single instruction/operation to be performed during the rebase.
351-
Matches the `git_rebase_operation` struct.
351+
Matches the [`git_rebase_operation`](https://libgit2.github.com/libgit2/#HEAD/type/git_rebase_operation_t) struct.
352352
"""
353353
immutable RebaseOperation
354354
optype::Cint
@@ -361,7 +361,7 @@ Base.show(io::IO, rbo::RebaseOperation) = print(io, "RebaseOperation($(string(rb
361361
LibGit2.StatusOptions
362362
363363
Options to control how `git_status_foreach_ext()` will issue callbacks.
364-
Matches the `git_status_options` struct.
364+
Matches the [`git_status_opt_t`](https://libgit2.github.com/libgit2/#HEAD/type/git_status_opt_t) struct.
365365
"""
366366
@kwdef immutable StatusOptions
367367
version::Cuint = 1

test/libgit2.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,24 @@ mktempdir() do dir
437437
close(repo)
438438
end
439439
end
440+
441+
@testset "blobs" begin
442+
repo = LibGit2.GitRepo(cache_repo)
443+
try
444+
# clear out the "GitHash( )" part
445+
hash_string = sprint(show, commit_oid1)[9:end]
446+
hash_string = strip(hash_string, ')')
447+
blob_file = joinpath(cache_repo,".git/objects", hash_string[1:2], hash_string[3:end])
448+
blob = LibGit2.GitBlob(repo, blob_file)
449+
@test LibGit2.isbinary(blob)
450+
blob_show_strs = split(sprint(show, blob), "\n")
451+
@test blob_show_strs[1] == "GitBlob:"
452+
@test contains(blob_show_strs[2], "Blob id:")
453+
@test blob_show_strs[3] == "Contents are binary."
454+
finally
455+
close(repo)
456+
end
457+
end
440458
end
441459

442460
@testset "Fetch from cache repository" begin

0 commit comments

Comments
 (0)