Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically determine edit_link default #1830

Merged
merged 20 commits into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Version `v0.28.0` (unreleased)

* ![Enhancement][badge-enhancement] The `ansicolor` keyword to `HTML()` now defaults to true, meaning that executed outputs from `@example`- and `@repl`-blocks are now by default colored (if they emit colored output). ([#1828][github-1828])
* ![Enhancement][badge-enhancement] The `edit_link` keyword to `HTML()` automatically tries to figure out if the remote default branch is `main`, `master`, or something else. Documenter will now print a warning if it is unable to unreliably automatically determine `edit_link` or `devbranch` (for `deploydocs`). ([#1827][github-1827], [#1829][github-1829])

## Version `v0.27.18`

Expand Down Expand Up @@ -1041,7 +1042,9 @@
[github-1818]: https://github.com/JuliaDocs/Documenter.jl/pull/1818
[github-1821]: https://github.com/JuliaDocs/Documenter.jl/pull/1821
[github-1825]: https://github.com/JuliaDocs/Documenter.jl/pull/1825
[github-1827]: https://github.com/JuliaDocs/Documenter.jl/issues/1827
[github-1828]: https://github.com/JuliaDocs/Documenter.jl/pull/1828
[github-1829]: https://github.com/JuliaDocs/Documenter.jl/pull/1829
<!-- end of issue link definitions -->

[julia-38079]: https://github.com/JuliaLang/julia/issues/38079
Expand Down
13 changes: 1 addition & 12 deletions src/Documenter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -532,18 +532,7 @@ function deploydocs(;

# Try to figure out default branch (see #1443 and #1727)
if devbranch === nothing
env = copy(ENV)
env["GIT_TERMINAL_PROMPT"] = "0"
env["GIT_SSH_COMMAND"] = get(env, "GIT_SSH_COMMAND", "ssh -o \"BatchMode yes\"")
str = try
read(pipeline(ignorestatus(
setenv(`git remote show origin`, env; dir=root)
); stderr=devnull), String)
catch
""
end
m = match(r"^\s*HEAD branch:\s*(.*)$"m, str)
devbranch = m === nothing ? "master" : String(m[1])
devbranch = Utilities.git_remote_head_branch("deploydocs(devbranch = ...)", root)
end

deploy_decision = deploy_folder(deploy_config;
Expand Down
39 changes: 39 additions & 0 deletions src/Utilities/Utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,45 @@ function check_strict_kw(strict)
return nothing
end

"""
Calls `git remote show \$(remotename)` to try to determine the main (development) branch
of the remote repository. Returns `master` and prints a warning if it was unable to figure
it out automatically.
"""
function git_remote_head_branch(varname, root; remotename = "origin", fallback = "master")
env = copy(ENV)
env["GIT_TERMINAL_PROMPT"] = "0"
env["GIT_SSH_COMMAND"] = get(env, "GIT_SSH_COMMAND", "ssh -o \"BatchMode yes\"")
cmd = `git remote show $(remotename)`
stderr_output = IOBuffer()
git_remote_output = try
read(pipeline(setenv(cmd, env; dir=root); stderr=stderr_output), String)
mortenpi marked this conversation as resolved.
Show resolved Hide resolved
catch e
@warn """
Unable to determine $(varname) from remote HEAD branch, defaulting to "$(fallback)".
Calling `git remote` failed with an exception. Set JULIA_DEBUG=Documenter to see the error.
Unless this is due to a configuration error, the relevant variable should be set explicitly.
"""
@debug "Command: $cmd" exception = (e, catch_backtrace()) stderr = String(take!(stderr_output))
return fallback
end
m = match(r"^\s*HEAD branch:\s*(.*)$"m, git_remote_output)
if m === nothing
@warn """
Unable to determine $(varname) from remote HEAD branch, defaulting to "$(fallback)".
Failed to parse the `git remote` output. Set JULIA_DEBUG=Documenter to see the output.
Unless this is due to a configuration error, the relevant variable should be set explicitly.
"""
@debug """
stdout from $cmd:
$(git_remote_output)
"""
fallback
else
String(m[1])
end
end

include("DOM.jl")
include("MDFlatten.jl")
include("TextDiff.jl")
Expand Down
2 changes: 1 addition & 1 deletion src/Writers/HTMLWriter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ struct HTML <: Documenter.Writer
function HTML(;
prettyurls :: Bool = true,
disable_git :: Bool = false,
edit_link :: Union{String, Symbol, Nothing, Default} = Default("master"),
edit_link :: Union{String, Symbol, Nothing, Default} = Default(Utilities.git_remote_head_branch("HTML(edit_link = ...)", Utilities.currentdir())),
canonical :: Union{String, Nothing} = nothing,
assets :: Vector = String[],
analytics :: String = "",
Expand Down