Skip to content

Commit b29a556

Browse files
authored
[Sources] Add optional progress bar to git clone (#240)
1 parent 1799bba commit b29a556

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

Manifest.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ version = "0.7.0"
2222
deps = ["Printf"]
2323
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"
2424

25+
[[deps.Distributed]]
26+
deps = ["Random", "Serialization", "Sockets"]
27+
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"
28+
2529
[[deps.Downloads]]
2630
deps = ["ArgTools", "LibCURL", "NetworkOptions"]
2731
uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
@@ -115,6 +119,12 @@ version = "1.2.5"
115119
deps = ["Unicode"]
116120
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"
117121

122+
[[deps.ProgressMeter]]
123+
deps = ["Distributed", "Printf"]
124+
git-tree-sha1 = "d7a7aef8f8f2d537104f170139553b14dfe39fe9"
125+
uuid = "92933f4c-e287-5a05-a399-4b506db050ca"
126+
version = "1.7.2"
127+
118128
[[deps.REPL]]
119129
deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"]
120130
uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "BinaryBuilderBase"
22
uuid = "7f725544-6523-48cd-82d1-3fa08ff4056e"
33
authors = ["Elliot Saba <staticfloat@gmail.com>"]
4-
version = "1.9.0"
4+
version = "1.10.0"
55

66
[deps]
77
CodecZlib = "944b1d66-785c-5afd-91f1-9de20f533193"
@@ -15,6 +15,7 @@ Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
1515
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
1616
OutputCollectors = "6c11c7d4-943b-4e2b-80de-f2cfc2930a8c"
1717
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
18+
ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca"
1819
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1920
SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce"
2021
Scratch = "6c6a2e73-6563-6170-7368-637461726353"
@@ -30,6 +31,7 @@ CodecZlib = "0.5, 0.6, 0.7"
3031
JSON = "0.21"
3132
OrderedCollections = "1.4.1"
3233
OutputCollectors = "0.1"
34+
ProgressMeter = "1"
3335
Scratch = "1.0"
3436
SimpleBufferStream = "1"
3537
Tar = "1.7"

src/Sources.jl

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using LibGit2
2+
using ProgressMeter
3+
const update! = ProgressMeter.update!
24

35
export ArchiveSource, FileSource, GitSource, DirectorySource
46

@@ -142,10 +144,34 @@ function download_source(source::T; verbose::Bool = false, downloads_dir = stora
142144
return SetupSource{T}(src_path, source.hash, gettarget(source))
143145
end
144146

147+
struct GitTransferProgress
148+
total_objects::Cuint
149+
indexed_objects::Cuint
150+
received_objects::Cuint
151+
local_objects::Cuint
152+
total_deltas::Cuint
153+
indexed_deltas::Cuint
154+
received_bytes::Csize_t
155+
end
156+
157+
function transfer_progress(progress::Ptr{GitTransferProgress}, p::Any)
158+
progress = unsafe_load(progress)
159+
p.n = progress.total_objects
160+
if progress.total_deltas != 0
161+
p.desc = "Resolving Deltas: "
162+
p.n = progress.total_deltas
163+
update!(p, Int(max(1, progress.indexed_deltas)))
164+
else
165+
update!(p, Int(max(1, progress.received_objects)))
166+
end
167+
return Cint(0)
168+
end
169+
145170
function cached_git_clone(url::String;
146171
hash_to_check::Union{Nothing, String} = nothing,
147172
downloads_dir::String = storage_dir("downloads"),
148173
verbose::Bool = false,
174+
progressbar::Bool = false,
149175
)
150176
repo_path = joinpath(downloads_dir, "clones", string(basename(url), "-", bytes2hex(sha256(url))))
151177
if isdir(repo_path)
@@ -163,11 +189,29 @@ function cached_git_clone(url::String;
163189
end
164190
end
165191
else
192+
# If there is no repo_path yet, clone it down into a bare repository
166193
if verbose
167194
@info("Cloning git repository", url, repo_path)
168195
end
169-
# If there is no repo_path yet, clone it down into a bare repository
170-
LibGit2.clone(url, repo_path; isbare=true)
196+
if progressbar
197+
# Clone with a progress bar
198+
p = Progress(0, 1, "Cloning: ")
199+
GC.@preserve p begin
200+
callbacks = LibGit2.RemoteCallbacks(
201+
transfer_progress=@cfunction(
202+
transfer_progress,
203+
Cint,
204+
(Ptr{GitTransferProgress}, Any)
205+
),
206+
payload = p
207+
)
208+
fetch_opts = LibGit2.FetchOptions(; callbacks)
209+
clone_opts = LibGit2.CloneOptions(; fetch_opts, bare = Cint(true))
210+
LibGit2.clone(url, repo_path, clone_opts)
211+
end
212+
else
213+
LibGit2.clone(url, repo_path; isbare=true)
214+
end
171215
end
172216
return repo_path
173217
end

0 commit comments

Comments
 (0)