Skip to content

Commit

Permalink
Move LibGit2.prompt to Base.prompt
Browse files Browse the repository at this point in the history
As part of the supporting EOF update (#23092) we needed to deprecate
the original `LibGit2.prompt` since the return type changed. Since
packages like PkgDev make use of the `prompt` function and the function
itself isn't LibGit2 specific it seemed best to move it into Base. This
allows us to deprecate the original `prompt` without having to change
the function name or modify the parameters.
  • Loading branch information
omus committed Aug 4, 2017
1 parent c95792e commit 7a5c3ec
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 24 deletions.
10 changes: 10 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1601,6 +1601,16 @@ end
# issue #6466
# `write` on non-isbits arrays is deprecated in io.jl.

# PR #23092
@eval LibGit2 begin
function prompt(msg::AbstractString; default::AbstractString="", password::Bool=false)
Base.depwarn(string(
"`LibGit2.prompt(msg::AbstractString; default::AbstractString=\"\", password::Bool=false)` is deprecated, use ",
"`get(Base.prompt(msg, default=default, password=password), \"\")` instead."), :prompt)
Base.get(Base.prompt(msg, default=default, password=password), "")
end
end

# END 0.7 deprecations

# BEGIN 1.0 deprecations
Expand Down
12 changes: 6 additions & 6 deletions base/libgit2/callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function authenticate_ssh(creds::SSHCredentials, libgit2credptr::Ptr{Ptr{Void}},
if !isusedcreds
username = uname
else
response = prompt("Username for '$schema$host'", default=uname)
response = Base.prompt("Username for '$schema$host'", default=uname)
isnull(response) && return user_abort()
username = unsafe_get(response)
end
Expand All @@ -79,7 +79,7 @@ function authenticate_ssh(creds::SSHCredentials, libgit2credptr::Ptr{Ptr{Void}},
if isempty(keydefpath) && isfile(defaultkeydefpath)
keydefpath = defaultkeydefpath
else
response = prompt("Private key location for '$schema$username@$host'",
response = Base.prompt("Private key location for '$schema$username@$host'",
default=keydefpath)
isnull(response) && return user_abort()
keydefpath = unsafe_get(response)
Expand All @@ -102,7 +102,7 @@ function authenticate_ssh(creds::SSHCredentials, libgit2credptr::Ptr{Ptr{Void}},
keydefpath = privatekey*".pub"
end
if !isfile(keydefpath)
response = prompt("Public key location for '$schema$username@$host'",
response = Base.prompt("Public key location for '$schema$username@$host'",
default=keydefpath)
isnull(response) && return user_abort()
keydefpath = unsafe_get(response)
Expand Down Expand Up @@ -134,7 +134,7 @@ function authenticate_ssh(creds::SSHCredentials, libgit2credptr::Ptr{Ptr{Void}},
isnull(response) && return user_abort()
passdef = unsafe_get(response)[2]
else
response = prompt("Passphrase for $privatekey", password=true)
response = Base.prompt("Passphrase for $privatekey", password=true)
isnull(response) && return user_abort()
passdef = unsafe_get(response)
isempty(passdef) && return user_abort() # Ambiguous if EOF or newline
Expand Down Expand Up @@ -173,12 +173,12 @@ function authenticate_userpass(creds::UserPasswordCredentials, libgit2credptr::P
username, userpass = unsafe_get(response)
end
elseif isusedcreds
response = prompt("Username for '$schema$host'",
response = Base.prompt("Username for '$schema$host'",
default=isempty(username) ? urlusername : username)
isnull(response) && return user_abort()
username = unsafe_get(response)

response = prompt("Password for '$schema$username@$host'", password=true)
response = Base.prompt("Password for '$schema$username@$host'", password=true)
isnull(response) && return user_abort()
userpass = unsafe_get(response)
isempty(userpass) && return user_abort() # Ambiguous if EOF or newline
Expand Down
18 changes: 0 additions & 18 deletions base/libgit2/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,6 @@ isset(val::Integer, flag::Integer) = (val & flag == flag)
reset(val::Integer, flag::Integer) = (val &= ~flag)
toggle(val::Integer, flag::Integer) = (val |= flag)

function prompt(msg::AbstractString; default::AbstractString="", password::Bool=false)
if Sys.iswindows() && password
error("Command line prompt not supported for password entry on windows. Use winprompt instead")
end
msg = !isempty(default) ? msg*" [$default]:" : msg*":"
uinput = if password
Base.getpass(msg) # Automatically chomps. We cannot tell EOF from '\n'.
else
print(msg)
readline(chomp=false)
end
if !password
isempty(uinput) && return Nullable{String}() # Encountered EOF
uinput = chomp(uinput)
end
Nullable{String}(isempty(uinput) ? default : uinput)
end

function features()
feat = ccall((:git_libgit2_features, :libgit2), Cint, ())
res = Consts.GIT_FEATURE[]
Expand Down
26 changes: 26 additions & 0 deletions base/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,32 @@ else
getpass(prompt::AbstractString) = unsafe_string(ccall(:getpass, Cstring, (Cstring,), prompt))
end

"""
prompt(message; default="", password=false) -> Nullable{String}
Displays the `message` then waits for user input. Input is terminated when a newline (\\n)
is encountered or EOF (^D) character is entered on a blank line. If a `default` is provided
then the user can enter just a newline character to select the `default`. Alternatively,
when the `password` keyword is `true` the characters entered by the user will not be
displayed.
"""
function prompt(message::AbstractString; default::AbstractString="", password::Bool=false)
if Sys.iswindows() && password
error("Command line prompt not supported for password entry on windows. Use `Base.winprompt` instead")
end
msg = !isempty(default) ? "$message [$default]:" : "$message:"
if password
# `getpass` automatically chomps. We cannot tell an EOF from a '\n'.
uinput = getpass(msg)
else
print(msg)
uinput = readline(chomp=false)
isempty(uinput) && return Nullable{String}() # Encountered an EOF
uinput = chomp(uinput)
end
Nullable{String}(isempty(uinput) ? default : uinput)
end

# Windows authentication prompt
if Sys.iswindows()
struct CREDUI_INFO
Expand Down

0 comments on commit 7a5c3ec

Please sign in to comment.