-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathremote.jl
399 lines (322 loc) · 12.8 KB
/
remote.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# This file is a part of Julia. License is MIT: https://julialang.org/license
"""
GitRemote(repo::GitRepo, rmt_name::AbstractString, rmt_url::AbstractString) -> GitRemote
Look up a remote git repository using its name and URL. Uses the default fetch refspec.
# Examples
```julia
repo = LibGit2.init(repo_path)
remote = LibGit2.GitRemote(repo, "upstream", repo_url)
```
"""
function GitRemote(repo::GitRepo, rmt_name::AbstractString, rmt_url::AbstractString)
rmt_ptr_ptr = Ref{Ptr{Void}}(C_NULL)
@check ccall((:git_remote_create, :libgit2), Cint,
(Ptr{Ptr{Void}}, Ptr{Void}, Cstring, Cstring),
rmt_ptr_ptr, repo.ptr, rmt_name, rmt_url)
return GitRemote(repo, rmt_ptr_ptr[])
end
"""
GitRemote(repo::GitRepo, rmt_name::AbstractString, rmt_url::AbstractString, fetch_spec::AbstractString) -> GitRemote
Look up a remote git repository using the repository's name and URL,
as well as specifications for how to fetch from the remote
(e.g. which remote branch to fetch from).
# Examples
```julia
repo = LibGit2.init(repo_path)
refspec = "+refs/heads/mybranch:refs/remotes/origin/mybranch"
remote = LibGit2.GitRemote(repo, "upstream", repo_url, refspec)
```
"""
function GitRemote(repo::GitRepo, rmt_name::AbstractString, rmt_url::AbstractString, fetch_spec::AbstractString)
rmt_ptr_ptr = Ref{Ptr{Void}}(C_NULL)
@check ccall((:git_remote_create_with_fetchspec, :libgit2), Cint,
(Ptr{Ptr{Void}}, Ptr{Void}, Cstring, Cstring, Cstring),
rmt_ptr_ptr, repo.ptr, rmt_name, rmt_url, fetch_spec)
return GitRemote(repo, rmt_ptr_ptr[])
end
"""
GitRemoteAnon(repo::GitRepo, url::AbstractString) -> GitRemote
Look up a remote git repository using only its URL, not its name.
# Examples
```julia
repo = LibGit2.init(repo_path)
remote = LibGit2.GitRemoteAnon(repo, repo_url)
```
"""
function GitRemoteAnon(repo::GitRepo, url::AbstractString)
rmt_ptr_ptr = Ref{Ptr{Void}}(C_NULL)
@check ccall((:git_remote_create_anonymous, :libgit2), Cint,
(Ptr{Ptr{Void}}, Ptr{Void}, Cstring),
rmt_ptr_ptr, repo.ptr, url)
return GitRemote(repo, rmt_ptr_ptr[])
end
"""
lookup_remote(repo::GitRepo, remote_name::AbstractString) -> Nullable{GitRemote}
Determine if the `remote_name` specified exists within the `repo`. Returns a
[`Nullable`](@ref), which will be null if the requested remote does not exist. If the remote
does exist, the `Nullable` contains a [`GitRemote`](@ref) to the remote name.
# Examples
```julia
repo = LibGit2.GitRepo(path)
remote_name = "test"
isnull(LibGit2.lookup_remote(repo, remote_name)) # will return true
```
"""
function lookup_remote(repo::GitRepo, remote_name::AbstractString)
rmt_ptr_ptr = Ref{Ptr{Void}}(C_NULL)
err = ccall((:git_remote_lookup, :libgit2), Cint,
(Ptr{Ptr{Void}}, Ptr{Void}, Cstring),
rmt_ptr_ptr, repo.ptr, remote_name)
if err == Int(Error.GIT_OK)
return Nullable{GitRemote}(GitRemote(repo, rmt_ptr_ptr[]))
elseif err == Int(Error.ENOTFOUND)
return Nullable{GitRemote}()
else
throw(Error.GitError(err))
end
end
function get(::Type{GitRemote}, repo::GitRepo, rmt_name::AbstractString)
rmt_ptr_ptr = Ref{Ptr{Void}}(C_NULL)
@check ccall((:git_remote_lookup, :libgit2), Cint,
(Ptr{Ptr{Void}}, Ptr{Void}, Cstring),
rmt_ptr_ptr, repo.ptr, rmt_name)
return GitRemote(repo, rmt_ptr_ptr[])
end
"""
url(rmt::GitRemote)
Get the fetch URL of a remote git repository.
# Examples
```julia-repl
julia> repo_url = "https://github.com/JuliaLang/Example.jl";
julia> repo = LibGit2.init(mktempdir());
julia> remote = LibGit2.GitRemote(repo, "origin", repo_url);
julia> LibGit2.url(remote)
"https://github.com/JuliaLang/Example.jl"
```
"""
function url(rmt::GitRemote)
url_ptr = ccall((:git_remote_url, :libgit2), Cstring, (Ptr{Void},), rmt.ptr)
url_ptr == C_NULL && return ""
return unsafe_string(url_ptr)
end
"""
push_url(rmt::GitRemote)
Get the push URL of a remote git repository.
# Examples
```julia-repl
julia> repo_url = "https://github.com/JuliaLang/Example.jl";
julia> repo = LibGit2.init(mktempdir());
julia> LibGit2.set_remote_push_url(repo, "origin", repo_url);
julia> LibGit2.push_url(LibGit2.get(LibGit2.GitRemote, repo, "origin"))
"https://github.com/JuliaLang/Example.jl"
```
"""
function push_url(rmt::GitRemote)
url_ptr = ccall((:git_remote_pushurl, :libgit2), Cstring, (Ptr{Void},), rmt.ptr)
url_ptr == C_NULL && return ""
return unsafe_string(url_ptr)
end
"""
name(rmt::GitRemote)
Get the name of a remote repository, for instance `"origin"`.
If the remote is anonymous (see [`GitRemoteAnon`](@ref))
the name will be an empty string `""`.
# Examples
```julia-repl
julia> repo_url = "https://github.com/JuliaLang/Example.jl";
julia> repo = LibGit2.clone(cache_repo, "test_directory");
julia> remote = LibGit2.GitRemote(repo, "origin", repo_url);
julia> name(remote)
"origin"
```
"""
function name(rmt::GitRemote)
name_ptr = ccall((:git_remote_name, :libgit2), Cstring, (Ptr{Void},), rmt.ptr)
name_ptr == C_NULL && return ""
return unsafe_string(name_ptr)
end
"""
fetch_refspecs(rmt::GitRemote) -> Vector{String}
Get the *fetch* refspecs for the specified `rmt`. These refspecs contain
information about which branch(es) to fetch from.
# Examples
```julia-repl
julia> remote = LibGit2.get(LibGit2.GitRemote, repo, "upstream");
julia> LibGit2.add_fetch!(repo, remote, "upstream");
julia> LibGit2.fetch_refspecs(remote)
String["+refs/heads/*:refs/remotes/upstream/*"]
```
"""
function fetch_refspecs(rmt::GitRemote)
sa_ref = Ref(StrArrayStruct())
@check ccall((:git_remote_get_fetch_refspecs, :libgit2), Cint,
(Ptr{StrArrayStruct}, Ptr{Void}), sa_ref, rmt.ptr)
res = convert(Vector{String}, sa_ref[])
free(sa_ref)
res
end
"""
push_refspecs(rmt::GitRemote) -> Vector{String}
Get the *push* refspecs for the specified `rmt`. These refspecs contain
information about which branch(es) to push to.
# Examples
```julia-repl
julia> remote = LibGit2.get(LibGit2.GitRemote, repo, "upstream");
julia> LibGit2.add_push!(repo, remote, "refs/heads/master");
julia> close(remote);
julia> remote = LibGit2.get(LibGit2.GitRemote, repo, "upstream");
julia> LibGit2.push_refspecs(remote)
String["refs/heads/master"]
```
"""
function push_refspecs(rmt::GitRemote)
sa_ref = Ref(StrArrayStruct())
@check ccall((:git_remote_get_push_refspecs, :libgit2), Cint,
(Ptr{StrArrayStruct}, Ptr{Void}), sa_ref, rmt.ptr)
res = convert(Vector{String}, sa_ref[])
free(sa_ref)
res
end
"""
add_fetch!(repo::GitRepo, rmt::GitRemote, fetch_spec::String)
Add a *fetch* refspec for the specified `rmt`. This refspec will contain
information about which branch(es) to fetch from.
# Examples
```julia-repl
julia> LibGit2.add_fetch!(repo, remote, "upstream");
julia> LibGit2.fetch_refspecs(remote)
String["+refs/heads/*:refs/remotes/upstream/*"]
```
"""
function add_fetch!(repo::GitRepo, rmt::GitRemote, fetch_spec::String)
@check ccall((:git_remote_add_fetch, :libgit2), Cint,
(Ptr{Void}, Cstring, Cstring), repo.ptr,
name(rmt), fetch_spec)
end
"""
add_push!(repo::GitRepo, rmt::GitRemote, push_spec::String)
Add a *push* refspec for the specified `rmt`. This refspec will contain
information about which branch(es) to push to.
# Examples
```julia-repl
julia> LibGit2.add_push!(repo, remote, "refs/heads/master");
julia> remote = LibGit2.get(LibGit2.GitRemote, repo, branch);
julia> LibGit2.push_refspecs(remote)
String["refs/heads/master"]
```
!!! note
You may need to [`close`](@ref) and reopen the `GitRemote`
in question after updating its push refspecs in order for
the change to take effect and for calls to [`push`](@ref)
to work.
"""
function add_push!(repo::GitRepo, rmt::GitRemote, push_spec::String)
@check ccall((:git_remote_add_push, :libgit2), Cint,
(Ptr{Void}, Cstring, Cstring), repo.ptr,
name(rmt), push_spec)
end
"""
fetch(rmt::GitRemote, refspecs; options::FetchOptions=FetchOptions(), msg="")
Fetch from the specified `rmt` remote git repository, using `refspecs` to
determine which remote branch(es) to fetch.
The keyword arguments are:
* `options`: determines the options for the fetch, e.g. whether to prune afterwards.
See [`FetchOptions`](@ref) for more information.
* `msg`: a message to insert into the reflogs.
"""
function fetch(rmt::GitRemote, refspecs::Vector{<:AbstractString};
options::FetchOptions = FetchOptions(),
msg::AbstractString="")
msg = "libgit2.fetch: $msg"
@check ccall((:git_remote_fetch, :libgit2), Cint,
(Ptr{Void}, Ptr{StrArrayStruct}, Ptr{FetchOptions}, Cstring),
rmt.ptr, isempty(refspecs) ? C_NULL : refspecs, Ref(options), msg)
end
"""
push(rmt::GitRemote, refspecs; force::Bool=false, options::PushOptions=PushOptions())
Push to the specified `rmt` remote git repository, using `refspecs` to
determine which remote branch(es) to push to.
The keyword arguments are:
* `force`: if `true`, a force-push will occur, disregarding conflicts.
* `options`: determines the options for the push, e.g. which proxy headers to use.
See [`PushOptions`](@ref) for more information.
!!! note
You can add information about the push refspecs in two other ways: by setting
an option in the repository's `GitConfig` (with `push.default` as the key) or
by calling [`add_push!`](@ref). Otherwise you will need to explicitly specify
a push refspec in the call to `push` for it to have any effect, like so:
`LibGit2.push(repo, refspecs=["refs/heads/master"])`.
"""
function push(rmt::GitRemote, refspecs::Vector{<:AbstractString};
force::Bool = false, options::PushOptions = PushOptions())
@check ccall((:git_remote_push, :libgit2), Cint,
(Ptr{Void}, Ptr{StrArrayStruct}, Ptr{PushOptions}),
rmt.ptr, isempty(refspecs) ? C_NULL : refspecs, Ref(options))
end
"""
remote_delete(repo::GitRepo, remote_name::AbstractString) -> Void
Delete the `remote_name` from the git `repo`.
"""
function remote_delete(repo::GitRepo, remote_name::AbstractString)
@check ccall((:git_remote_delete, :libgit2), Cint,
(Ptr{Void}, Cstring),
repo.ptr, remote_name)
end
Base.show(io::IO, rmt::GitRemote) = print(io, "GitRemote:\nRemote name: ", name(rmt), " url: ", url(rmt))
"""
set_remote_fetch_url(repo::GitRepo, remote_name, url)
set_remote_fetch_url(path::String, remote_name, url)
Set the fetch `url` for the specified `remote_name` for the [`GitRepo`](@ref) or the git repository
located at `path`. Typically git repos use `"origin"` as the remote name.
"""
function set_remote_fetch_url end
function set_remote_fetch_url(repo::GitRepo, remote_name::AbstractString, url::AbstractString)
@check ccall((:git_remote_set_url, :libgit2), Cint,
(Ptr{Void}, Cstring, Cstring),
repo.ptr, remote_name, url)
end
function set_remote_fetch_url(path::AbstractString, remote_name::AbstractString, url::AbstractString)
with(GitRepo, path) do repo
set_remote_fetch_url(repo, remote_name, url)
end
end
"""
set_remote_push_url(repo::GitRepo, remote_name, url)
set_remote_push_url(path::String, remote_name, url)
Set the push `url` for the specified `remote_name` for the [`GitRepo`](@ref) or the git repository
located at `path`. Typically git repos use `"origin"` as the remote name.
"""
function set_remote_push_url end
function set_remote_push_url(repo::GitRepo, remote_name::AbstractString, url::AbstractString)
@check ccall((:git_remote_set_pushurl, :libgit2), Cint,
(Ptr{Void}, Cstring, Cstring),
repo.ptr, remote_name, url)
end
function set_remote_push_url(path::AbstractString, remote_name::AbstractString, url::AbstractString)
with(GitRepo, path) do repo
set_remote_push_url(repo, remote_name, url)
end
end
"""
set_remote_url(repo::GitRepo, remote_name, url)
set_remote_url(repo::String, remote_name, url)
Set both the fetch and push `url` for `remote_name` for the [`GitRepo`](@ref) or the git repository
located at `path`. Typically git repos use `"origin"` as the remote name.
# Examples
```julia
repo_path = joinpath(tempdir(), "Example")
repo = LibGit2.init(repo_path)
LibGit2.set_remote_url(repo, "upstream", "https://github.com/JuliaLang/Example.jl")
LibGit2.set_remote_url(repo_path, "upstream2", "https://github.com/JuliaLang/Example2.jl")
```
"""
function set_remote_url end
function set_remote_url(repo::GitRepo, remote_name::AbstractString, url::AbstractString)
set_remote_fetch_url(repo, remote_name, url)
set_remote_push_url(repo, remote_name, url)
end
function set_remote_url(path::AbstractString, remote_name::AbstractString, url::AbstractString)
with(GitRepo, path) do repo
set_remote_url(repo, remote_name, url)
end
end