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

JuliaLang / julia / #37638

01 Oct 2023 06:36PM UTC coverage: 87.461% (-0.1%) from 87.584%
#37638

push

local

web-flow
Update libc.jl compatability note (#51535)

Update libc.jl compatability note to julia 1.11

73897 of 84491 relevant lines covered (87.46%)

11729457.42 hits per line

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

96.08
/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✔
6
    sa = StrArrayStruct(Base.unsafe_convert(Ref{Cstring}, str_ref), 1)
6✔
7
    do_ref = Ref(DiffOptionsStruct(pathspec = sa))
6✔
8
    do_ref, str_ref
6✔
9
end
10
function Base.unsafe_convert(::Type{Ptr{DiffOptionsStruct}}, rr::Tuple{Ref{DiffOptionsStruct}, Ref{Cstring}})
×
11
    Base.unsafe_convert(Ptr{DiffOptionsStruct}, first(rr))
6✔
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)
52✔
27
    ensure_initialized()
26✔
28
    diff_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
26✔
29
    if cached
26✔
30
        @check ccall((:git_diff_tree_to_index, libgit2), Cint,
13✔
31
                     (Ptr{Ptr{Cvoid}}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{DiffOptionsStruct}),
32
                     diff_ptr_ptr, repo.ptr, tree.ptr, C_NULL, isempty(pathspecs) ? C_NULL : pathspecs)
33
    else
34
        @check ccall((:git_diff_tree_to_workdir_with_index, libgit2), Cint,
15✔
35
                     (Ptr{Ptr{Cvoid}}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{DiffOptionsStruct}),
36
                     diff_ptr_ptr, repo.ptr, tree.ptr, isempty(pathspecs) ? C_NULL : pathspecs)
37
    end
38
    return GitDiff(repo, diff_ptr_ptr[])
26✔
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)
3✔
52
    ensure_initialized()
3✔
53
    diff_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
3✔
54
    @check ccall((:git_diff_tree_to_tree, libgit2), Cint,
3✔
55
                  (Ptr{Ptr{Cvoid}}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{DiffOptionsStruct}),
56
                   diff_ptr_ptr, repo.ptr, oldtree.ptr, newtree.ptr, C_NULL)
57
    return GitDiff(repo, diff_ptr_ptr[])
3✔
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)
1✔
68
    ensure_initialized()
1✔
69
    diff_stat_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
1✔
70
    @check ccall((:git_diff_get_stats, libgit2), Cint,
1✔
71
                  (Ptr{Ptr{Cvoid}}, Ptr{Cvoid}),
72
                  diff_stat_ptr_ptr, diff.ptr)
73
    return GitDiffStats(diff.owner, diff_stat_ptr_ptr[])
1✔
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)
1✔
85
    ensure_initialized()
1✔
86
    return ccall((:git_diff_stats_files_changed, libgit2), Csize_t, (Ptr{Cvoid},), diff_stat.ptr)
1✔
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)
1✔
98
    ensure_initialized()
1✔
99
    return ccall((:git_diff_stats_insertions, libgit2), Csize_t, (Ptr{Cvoid},), diff_stat.ptr)
1✔
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)
1✔
111
    ensure_initialized()
1✔
112
    return ccall((:git_diff_stats_deletions, libgit2), Csize_t, (Ptr{Cvoid},), diff_stat.ptr)
1✔
113
end
114

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

120
function Base.getindex(diff::GitDiff, i::Integer)
7✔
121
    if i < 1 || i > count(diff)
13✔
122
        throw(BoundsError(diff, (i,)))
2✔
123
    end
124
    ensure_initialized()
5✔
125
    delta_ptr = ccall((:git_diff_get_delta, libgit2),
5✔
126
                      Ptr{DiffDelta},
127
                      (Ptr{Cvoid}, Csize_t), diff.ptr, i-1)
128
    return unsafe_load(delta_ptr)
5✔
129
end
130

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

138
function Base.show(io::IO, diff::GitDiff)
1✔
139
    println(io, "GitDiff:")
1✔
140
    println(io, "Number of deltas: $(count(diff))")
1✔
141
    show(io, GitDiffStats(diff))
1✔
142
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

© 2026 Coveralls, Inc