-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add GitShortHash type, remove some get methods #20104
Changes from 2 commits
fc6b868
ec8e2f6
2587850
8a02da9
dcde790
0b1ad7f
d5bbd02
2a2e936
3c19117
50261a3
d9d15a8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
# This file is a part of Julia. License is MIT: http://julialang.org/license | ||
|
||
function content(blob::GitBlob) | ||
return ccall((:git_blob_rawcontent, :libgit2), Ptr{Void}, (Ptr{Void},), blob.ptr) | ||
unsafe_string(ccall((:git_blob_rawcontent, :libgit2), Ptr{UInt8}, (Ptr{Void},), blob.ptr)) | ||
end | ||
|
||
function Base.length(blob::GitBlob) | ||
|
@@ -26,24 +26,29 @@ function lookup(repo::GitRepo, oid::GitHash) | |
return GitBlob(blob_ptr_ptr[]) | ||
end | ||
|
||
function GitBlob(repo::GitRepo, path::AbstractString) | ||
blob_id_ptr = Ref(GitHash()) | ||
""" | ||
LibGit2.addblob!(repo::GitRepo, path::AbstractString) | ||
|
||
Reads the file at `path` and adds it to the object database of `repo` as a loose blob. | ||
Returns the `GitHash` of the resulting blob. | ||
""" | ||
function addblob!(repo::GitRepo, path::AbstractString) | ||
id_ref = Ref{GitHash}() | ||
@check ccall((:git_blob_create_fromdisk, :libgit2), Cint, | ||
(Ptr{GitHash}, Ptr{Void}, Ptr{UInt8}), blob_id_ptr, | ||
repo.ptr, path) | ||
blob = get(GitBlob, repo, blob_id_ptr[]) | ||
return blob | ||
(Ptr{GitHash}, Ptr{Void}, Cstring), | ||
id_ref, repo.ptr, path) | ||
return id_ref[] | ||
end | ||
|
||
function Base.show(io::IO, blob::GitBlob) | ||
if !isbinary(blob) | ||
conts = split(content(blob), "\n") | ||
showlen = max(len(conts), 3) | ||
print(io, "GitBlob:\nBlob id: ", GitHash(blob.ptr), "\nContents:\n") | ||
showlen = max(length(conts), 3) | ||
print(io, "GitBlob:\nBlob id: ", GitHash(blob), "\nContents:\n") | ||
for i in 1:showlen | ||
print(io, conts[i],"\n") | ||
end | ||
else | ||
print(io, "GitBlob:\nBlob id: ", GitHash(blob.ptr), "\nContents are binary.") | ||
print(io, "GitBlob:\nBlob id: ", GitHash(blob), "\nContents are binary.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be println? along with the other prints in this function that have been adding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,7 +61,7 @@ end | |
function iscommit(id::AbstractString, repo::GitRepo) | ||
res = true | ||
try | ||
c = get(GitCommit, repo, id) | ||
c = GitCommit(repo, id) | ||
if c === nothing | ||
res = false | ||
else | ||
|
@@ -94,9 +94,8 @@ See `git diff-index <treeish> [-- <path>]` | |
""" | ||
function isdiff(repo::GitRepo, treeish::AbstractString, paths::AbstractString=""; cached::Bool=false) | ||
tree_oid = revparseid(repo, "$treeish^{tree}") | ||
iszero(tree_oid) && error("invalid treeish $treeish") # this can be removed by #20104 | ||
result = false | ||
tree = get(GitTree, repo, tree_oid) | ||
tree = GitTree(repo, tree_oid) | ||
try | ||
diff = diff_tree(repo, tree, paths, cached=cached) | ||
result = count(diff) > 0 | ||
|
@@ -112,8 +111,8 @@ function diff_files(repo::GitRepo, branch1::AbstractString, branch2::AbstractStr | |
filter::Set{Cint}=Set([Consts.DELTA_ADDED, Consts.DELTA_MODIFIED, Consts.DELTA_DELETED])) | ||
b1_id = revparseid(repo, branch1*"^{tree}") | ||
b2_id = revparseid(repo, branch2*"^{tree}") | ||
tree1 = get(GitTree, repo, b1_id) | ||
tree2 = get(GitTree, repo, b2_id) | ||
tree1 = GitTree(repo, b1_id) | ||
tree2 = GitTree(repo, b2_id) | ||
files = AbstractString[] | ||
try | ||
diff = diff_tree(repo, tree1, tree2) | ||
|
@@ -241,7 +240,7 @@ function branch!(repo::GitRepo, branch_name::AbstractString, | |
GitHash(commit) | ||
end | ||
iszero(commit_id) && return | ||
cmt = get(GitCommit, repo, commit_id) | ||
cmt = GitCommit(repo, commit_id) | ||
new_branch_ref = nothing | ||
try | ||
new_branch_ref = create_branch(repo, branch_name, cmt, force=force) | ||
|
@@ -298,28 +297,17 @@ function checkout!(repo::GitRepo, commit::AbstractString = ""; | |
end | ||
|
||
# search for commit to get a commit object | ||
obj = get(GitUnknownObject, repo, GitHash(commit)) | ||
obj === nothing && return | ||
try | ||
peeled = peel(obj, Consts.OBJ_COMMIT) | ||
peeled === nothing && return | ||
opts = force ? CheckoutOptions(checkout_strategy = Consts.CHECKOUT_FORCE) : | ||
CheckoutOptions() | ||
try | ||
# detach commit | ||
obj_oid = GitHash(peeled) | ||
ref = GitReference(repo, obj_oid, force=force, | ||
msg="libgit2.checkout: moving from $head_name to $(string(obj_oid))") | ||
close(ref) | ||
|
||
# checkout commit | ||
checkout_tree(repo, peeled, options = opts) | ||
finally | ||
close(peeled) | ||
end | ||
finally | ||
close(obj) | ||
end | ||
obj = GitObject(repo, GitHash(commit)) | ||
peeled = peel(GitCommit, obj) | ||
|
||
opts = force ? CheckoutOptions(checkout_strategy = Consts.CHECKOUT_FORCE) : CheckoutOptions() | ||
# detach commit | ||
obj_oid = GitHash(peeled) | ||
ref = GitReference(repo, obj_oid, force=force, | ||
msg="libgit2.checkout: moving from $head_name to $(string(obj_oid))") | ||
|
||
# checkout commit | ||
checkout_tree(repo, peeled, options = opts) | ||
end | ||
|
||
""" git clone [-b <branch>] [--bare] <url> <dir> """ | ||
|
@@ -343,42 +331,22 @@ end | |
|
||
""" git reset [<committish>] [--] <pathspecs>... """ | ||
function reset!(repo::GitRepo, committish::AbstractString, pathspecs::AbstractString...) | ||
obj = revparse(repo, !isempty(committish) ? committish : Consts.HEAD_FILE) | ||
obj = GitObject(repo, isempty(committish) ? Consts.HEAD_FILE : committish) | ||
# do not remove entries in the index matching the provided pathspecs with empty target commit tree | ||
obj === nothing && throw(GitError(Error.Object, Error.ERROR, "`$committish` not found")) | ||
try | ||
head = reset!(repo, Nullable(obj), pathspecs...) | ||
return head | ||
finally | ||
close(obj) | ||
end | ||
return head_oid(repo) | ||
reset!(repo, Nullable(obj), pathspecs...) | ||
end | ||
|
||
""" git reset [--soft | --mixed | --hard] <commit> """ | ||
function reset!(repo::GitRepo, commit::GitHash, mode::Cint = Consts.RESET_MIXED) | ||
obj = get(GitUnknownObject, repo, commit) | ||
# object must exist for reset | ||
obj === nothing && throw(GitError(Error.Object, Error.ERROR, "Commit `$(string(commit))` object not found")) | ||
try | ||
head = reset!(repo, obj, mode) | ||
return head | ||
finally | ||
close(obj) | ||
end | ||
return head_oid(repo) | ||
end | ||
""" git reset [--soft | --mixed | --hard] <id> """ | ||
reset!(repo::GitRepo, id::GitHash, mode::Cint = Consts.RESET_MIXED) = | ||
reset!(repo, GitObject(repo, id), mode) | ||
|
||
""" git cat-file <commit> """ | ||
function cat{T<:GitObject}(repo::GitRepo, ::Type{T}, object::AbstractString) | ||
obj_id = revparseid(repo, object) | ||
iszero(obj_id) && return nothing | ||
|
||
obj = get(T, repo, obj_id) | ||
function cat(repo::GitRepo, spec) | ||
obj = GitObject(repo, spec) | ||
if isa(obj, GitBlob) | ||
return unsafe_string(convert(Ptr{UInt8}, content(obj))) | ||
content(obj) | ||
else | ||
return nothing | ||
nothing | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what does this correspond to? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the resulting object is a commit, tag or tree. Arguably this is another function that could be removed altogether. |
||
end | ||
end | ||
|
||
|
@@ -442,10 +410,10 @@ function merge!(repo::GitRepo; | |
remotename = with(GitConfig, repo) do cfg | ||
LibGit2.get(String, cfg, "branch.$branchname.remote") | ||
end | ||
obj = with(GitReference(repo, "refs/remotes/$remotename/$branchname")) do ref | ||
oid = with(GitReference(repo, "refs/remotes/$remotename/$branchname")) do ref | ||
LibGit2.GitHash(ref) | ||
end | ||
with(get(GitCommit, repo, obj)) do cmt | ||
with(GitCommit(repo, oid)) do cmt | ||
LibGit2.create_branch(repo, branchname, cmt) | ||
end | ||
return true | ||
|
@@ -543,7 +511,7 @@ end | |
""" Returns all commit authors """ | ||
function authors(repo::GitRepo) | ||
return with(GitRevWalker(repo)) do walker | ||
map((oid,repo)->with(get(GitCommit, repo, oid)) do cmt | ||
map((oid,repo)->with(GitCommit(repo, oid)) do cmt | ||
author(cmt)::Signature | ||
end, | ||
walker) #, by = Consts.SORT_TIME) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if this is binary content, it could easily have embedded nuls or be invalid utf-8, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. We can deal with the NUL issue by calling it with a length argument (which I presume is
git_blob_rawsize
). But I don't know how we could deal with non-UTF8 data.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
have it return an array of bytes?