• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JuliaLang / julia / #37889

31 Aug 2024 02:18AM UTC coverage: 86.287% (-1.6%) from 87.882%
#37889

push

local

web-flow
Fix an assertion in new binding code (#55636)

This snuck by me late in the new bindings PR and I didn't notice the
assert builder handn't finished yet.

76579 of 88749 relevant lines covered (86.29%)

16183774.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/stdlib/LibGit2/src/diff.jl
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2

3
# TODO: make this a general purpose solution
4
function Base.cconvert(::Type{Ptr{DiffOptionsStruct}}, pathspecs::AbstractString)
5
    str_ref = Base.cconvert(Ref{Cstring}, [pathspecs])
×
6
    sa = StrArrayStruct(Base.unsafe_convert(Ref{Cstring}, str_ref), 1)
×
7
    do_ref = Ref(DiffOptionsStruct(pathspec = sa))
×
8
    do_ref, str_ref
×
9
end
10
function Base.unsafe_convert(::Type{Ptr{DiffOptionsStruct}}, rr::Tuple{Ref{DiffOptionsStruct}, Ref{Cstring}})
11
    Base.unsafe_convert(Ptr{DiffOptionsStruct}, first(rr))
×
12
end
13

14
"""
15
    diff_tree(repo::GitRepo, tree::GitTree, pathspecs::AbstractString=""; cached::Bool=false)
16

17
Generate a [`GitDiff`](@ref) between `tree` (which will be used for the "old"
18
side of the [`DiffDelta`](@ref)) and `repo` (which will be used for the "new" side).
19
If `repo` is `cached`, calls [`git_diff_tree_to_index`](https://libgit2.org/libgit2/#HEAD/group/diff/git_diff_tree_to_index).
20
The `cached` version is generally used to examine the diff for staged changes from one
21
commit to the next. If `cached` is `false`, calls
22
[`git_diff_tree_to_workdir_with_index`](https://libgit2.org/libgit2/#HEAD/group/diff/git_diff_tree_to_workdir_with_index).
23
This compares the current working directory against the [`GitIndex`](@ref) and can,
24
for example, be used to examine the changes in staged files before a commit.
25
"""
26
function diff_tree(repo::GitRepo, tree::GitTree, pathspecs::AbstractString=""; cached::Bool=false)
×
27
    ensure_initialized()
×
28
    diff_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
×
29
    if cached
×
30
        @check ccall((:git_diff_tree_to_index, libgit2), Cint,
×
31
                     (Ptr{Ptr{Cvoid}}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{DiffOptionsStruct}),
32
                     diff_ptr_ptr, repo, tree, C_NULL, isempty(pathspecs) ? C_NULL : pathspecs)
33
    else
34
        @check ccall((:git_diff_tree_to_workdir_with_index, libgit2), Cint,
×
35
                     (Ptr{Ptr{Cvoid}}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{DiffOptionsStruct}),
36
                     diff_ptr_ptr, repo, tree, isempty(pathspecs) ? C_NULL : pathspecs)
37
    end
38
    return GitDiff(repo, diff_ptr_ptr[])
×
39
end
40

41
"""
42
    diff_tree(repo::GitRepo, oldtree::GitTree, newtree::GitTree)
43

44
Generate a [`GitDiff`](@ref) between `oldtree` (which will be used for the "old"
45
side of the [`DiffDelta`](@ref)) and `newtree` (which will be used for the "new"
46
side of the `DiffDelta`). Equivalent to [`git_diff_tree_to_tree`](https://libgit2.org/libgit2/#HEAD/group/diff/git_diff_tree_to_tree).
47
This can be used to generate a diff between two commits. For instance, it could
48
be used to compare a commit made 2 months ago with the current latest commit, or
49
to compare a commit on another branch with the current latest commit on `master`.
50
"""
51
function diff_tree(repo::GitRepo, oldtree::GitTree, newtree::GitTree)
×
52
    ensure_initialized()
×
53
    diff_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
×
54
    @check ccall((:git_diff_tree_to_tree, libgit2), Cint,
×
55
                  (Ptr{Ptr{Cvoid}}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{DiffOptionsStruct}),
56
                   diff_ptr_ptr, repo, oldtree, newtree, C_NULL)
57
    return GitDiff(repo, diff_ptr_ptr[])
×
58
end
59

60
"""
61
    GitDiffStats(diff::GitDiff)
62

63
Get the diff statistics from the [`GitDiff`](@ref) `diff`. This object records a
64
summary of changes made across the `diff`. In particular, it records how many
65
files were changed, how many insertions were made, and how many deletions were made.
66
"""
67
function GitDiffStats(diff::GitDiff)
×
68
    ensure_initialized()
×
69
    diff_stat_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
×
70
    @check ccall((:git_diff_get_stats, libgit2), Cint,
×
71
                  (Ptr{Ptr{Cvoid}}, Ptr{Cvoid}),
72
                  diff_stat_ptr_ptr, diff)
73
    return GitDiffStats(diff.owner, diff_stat_ptr_ptr[])
×
74
end
75

76
"""
77
    files_changed(diff_stat::GitDiffStats) -> Csize_t
78

79
Return how many files were changed (added/modified/deleted) in the [`GitDiff`](@ref)
80
summarized by `diff_stat`. The result may vary depending on the [`DiffOptionsStruct`](@ref)
81
used to generate the parent `GitDiff` of `diff_stat` (for instance, whether ignored files
82
are to be included or not).
83
"""
84
function files_changed(diff_stat::GitDiffStats)
×
85
    ensure_initialized()
×
86
    return ccall((:git_diff_stats_files_changed, libgit2), Csize_t, (Ptr{Cvoid},), diff_stat)
×
87
end
88

89
"""
90
    insertions(diff_stat::GitDiffStats) -> Csize_t
91

92
Return the total number of insertions (lines added) in the [`GitDiff`](@ref)
93
summarized by `diff_stat`. The result may vary depending on the [`DiffOptionsStruct`](@ref)
94
used to generate the parent `GitDiff` of `diff_stat` (for instance, whether ignored files
95
are to be included or not).
96
"""
97
function insertions(diff_stat::GitDiffStats)
×
98
    ensure_initialized()
×
99
    return ccall((:git_diff_stats_insertions, libgit2), Csize_t, (Ptr{Cvoid},), diff_stat)
×
100
end
101

102
"""
103
    deletions(diff_stat::GitDiffStats) -> Csize_t
104

105
Return the total number of deletions (lines removed) in the [`GitDiff`](@ref)
106
summarized by `diff_stat`. The result may vary depending on the [`DiffOptionsStruct`](@ref)
107
used to generate the parent `GitDiff` of `diff_stat` (for instance, whether ignored files
108
are to be included or not).
109
"""
110
function deletions(diff_stat::GitDiffStats)
×
111
    ensure_initialized()
×
112
    return ccall((:git_diff_stats_deletions, libgit2), Csize_t, (Ptr{Cvoid},), diff_stat)
×
113
end
114

115
function count(diff::GitDiff)
116
    ensure_initialized()
×
117
    return ccall((:git_diff_num_deltas, libgit2), Cint, (Ptr{Cvoid},), diff)
×
118
end
119

120
function Base.getindex(diff::GitDiff, i::Integer)
×
121
    if i < 1 || i > count(diff)
×
122
        throw(BoundsError(diff, (i,)))
×
123
    end
124
    ensure_initialized()
×
125
    GC.@preserve diff begin # preserve `diff` object until return of `unsafe_load`
×
126
        delta_ptr = ccall((:git_diff_get_delta, libgit2),
×
127
                          Ptr{DiffDelta},
128
                          (Ptr{Cvoid}, Csize_t), diff, i-1)
129
        return unsafe_load(delta_ptr)
×
130
    end
131
end
132

133
function Base.show(io::IO, diff_stat::GitDiffStats)
×
134
    println(io, "GitDiffStats:")
×
135
    println(io, "Files changed: $(files_changed(diff_stat))")
×
136
    println(io, "Insertions: $(insertions(diff_stat))")
×
137
    println(io, "Deletions: $(deletions(diff_stat))")
×
138
end
139

140
function Base.show(io::IO, diff::GitDiff)
×
141
    println(io, "GitDiff:")
×
142
    println(io, "Number of deltas: $(count(diff))")
×
143
    show(io, GitDiffStats(diff))
×
144
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc