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

JuliaLang / julia / 1300

09 Oct 2025 07:15AM UTC coverage: 76.746% (-0.03%) from 76.775%
1300

push

buildkite

web-flow
fix prefix IntrusiveLinkedList with Base in a REPL test util (#59782)

61123 of 79643 relevant lines covered (76.75%)

13678184.85 hits per line

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

98.04
/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])
12✔
6
    sa = StrArrayStruct(Base.unsafe_convert(Ref{Cstring}, str_ref), 1)
12✔
7
    do_ref = Ref(DiffOptionsStruct(pathspec = sa))
12✔
8
    do_ref, str_ref
12✔
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✔
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)
100✔
27
    ensure_initialized()
50✔
28
    diff_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
50✔
29
    if cached
50✔
30
        @check ccall((:git_diff_tree_to_index, libgit2), Cint,
26✔
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,
28✔
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(diff_ptr_ptr[])
50✔
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)
8✔
52
    ensure_initialized()
8✔
53
    diff_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
8✔
54
    @check ccall((:git_diff_tree_to_tree, libgit2), Cint,
8✔
55
                  (Ptr{Ptr{Cvoid}}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{DiffOptionsStruct}),
56
                   diff_ptr_ptr, repo, oldtree, newtree, C_NULL)
57
    return GitDiff(diff_ptr_ptr[])
8✔
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)
2✔
68
    ensure_initialized()
2✔
69
    diff_stat_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
2✔
70
    @check ccall((:git_diff_get_stats, libgit2), Cint,
2✔
71
                  (Ptr{Ptr{Cvoid}}, Ptr{Cvoid}),
72
                  diff_stat_ptr_ptr, diff)
73
    return GitDiffStats(diff_stat_ptr_ptr[])
2✔
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()
2✔
86
    return ccall((:git_diff_stats_files_changed, libgit2), Csize_t, (Ptr{Cvoid},), diff_stat)
2✔
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()
2✔
99
    return ccall((:git_diff_stats_insertions, libgit2), Csize_t, (Ptr{Cvoid},), diff_stat)
2✔
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()
2✔
112
    return ccall((:git_diff_stats_deletions, libgit2), Csize_t, (Ptr{Cvoid},), diff_stat)
2✔
113
end
114

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

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

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

140
function Base.show(io::IO, diff::GitDiff)
2✔
141
    println(io, "GitDiff:")
2✔
142
    println(io, "Number of deltas: $(count(diff))")
2✔
143
    show(io, GitDiffStats(diff))
2✔
144
end
145

146
"""
147
    GitDiff(content::AbstractString)
148

149
Parse a diff from a buffer. The `content` should be in unified diff format.
150
Returns a [`GitDiff`](@ref) object.
151

152
This is equivalent to [`git_diff_from_buffer`](https://libgit2.org/libgit2/#HEAD/group/diff/git_diff_from_buffer).
153

154
# Examples
155
```julia
156
diff_str = \"\"\"
157
diff --git a/file.txt b/file.txt
158
index 1234567..abcdefg 100644
159
--- a/file.txt
160
+++ b/file.txt
161
@@ -1 +1 @@
162
-old content
163
+new content
164
\"\"\"
165
diff = LibGit2.GitDiff(diff_str)
166
```
167
"""
168
function GitDiff(content::AbstractString)
2✔
169
    ensure_initialized()
2✔
170
    diff_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
2✔
171
    @check ccall((:git_diff_from_buffer, libgit2), Cint,
2✔
172
                 (Ptr{Ptr{Cvoid}}, Cstring, Csize_t),
173
                 diff_ptr_ptr, content, sizeof(content))
174
    return GitDiff(diff_ptr_ptr[])
×
175
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