From e623bde7a914d700b819a86871e2b7bdd7d1b997 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Tue, 25 Jan 2022 14:32:28 +0100 Subject: [PATCH] Automatically detect "edit branch", and let users override using edit_url kwarg, closes #179. --- CHANGELOG.md | 7 +++++++ src/Literate.jl | 23 ++++++++++++++++++----- test/runtests.jl | 8 ++++++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a5dba40..615ec42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added +- Literate now tries to figure out the branch/commit that `EditURL` should point to + automatically instead of always defaulting to `"master"`. For typical setups the + auto-detection should be sufficient, but you can also set it explicitly by passing + `edit_commit`, for example `edit_commit = "main"`. ([#179][github-179], [#184][github-184]) ## [2.10.0] - 2022-01-24 ### Added @@ -159,8 +164,10 @@ https://discourse.julialang.org/t/ann-literate-jl/10651 for release announcement [github-169]: https://github.com/fredrikekre/Literate.jl/pull/169 [github-171]: https://github.com/fredrikekre/Literate.jl/issues/171 [github-172]: https://github.com/fredrikekre/Literate.jl/pull/172 +[github-179]: https://github.com/fredrikekre/Literate.jl/issues/179 [github-182]: https://github.com/fredrikekre/Literate.jl/issues/182 [github-183]: https://github.com/fredrikekre/Literate.jl/pull/183 +[github-184]: https://github.com/fredrikekre/Literate.jl/pull/184 [Unreleased]: https://github.com/fredrikekre/Literate.jl/compare/v2.9.4...HEAD [2.9.4]: https://github.com/fredrikekre/Literate.jl/compare/v2.9.3...v2.9.4 diff --git a/src/Literate.jl b/src/Literate.jl index 37350f1..76109af 100644 --- a/src/Literate.jl +++ b/src/Literate.jl @@ -264,8 +264,21 @@ function create_configuration(inputfile; user_config, user_kwargs, type=nothing) ("````@example $(get(user_config, "name", replace(cfg["name"], r"\s" => "_")))" => "````") : ("````julia" => "````") cfg["image_formats"] = _DEFAULT_IMAGE_FORMATS - # Guess the package (or repository) root url - edit_commit = "master" # TODO: Make this configurable like Documenter? + # Guess the package (or repository) root url with "master" as fallback + # see JuliaDocs/Documenter.jl#1751 + fallback_edit_commit = "master" + if (git = Sys.which("git"); git !== nothing) + try + str = read(pipeline(ignorestatus( + setenv(`$(git) remote show origin`; dir=dirname(inputfile)) + ), stderr=devnull), String) + if (m = match(r"^\s*HEAD branch:\s*(.*)$"m, str); m !== nothing) + fallback_edit_commit = String(m[1]) + end + catch + end + end + cfg["edit_commit"] = get(user_config, "edit_commit", fallback_edit_commit) deploy_branch = "gh-pages" # TODO: Make this configurable like Documenter? # Strip build version from a tag (cf. JuliaDocs/Documenter.jl#1298, Literate.jl#162) function version_tag_strip_build(tag) @@ -288,7 +301,7 @@ function create_configuration(inputfile; user_config, user_kwargs, type=nothing) else "previews/PR$(get(ENV, "TRAVIS_PULL_REQUEST", "##"))" end - cfg["repo_root_url"] = "https://github.com/$(repo_slug)/blob/$(edit_commit)" + cfg["repo_root_url"] = "https://github.com/$(repo_slug)/blob/$(cfg["edit_commit"])" cfg["nbviewer_root_url"] = "https://nbviewer.jupyter.org/github/$(repo_slug)/blob/$(deploy_branch)/$(deploy_folder)" cfg["binder_root_url"] = "https://mybinder.org/v2/gh/$(repo_slug)/$(deploy_branch)?filepath=$(deploy_folder)" if (dir = get(ENV, "TRAVIS_BUILD_DIR", nothing)) !== nothing @@ -307,7 +320,7 @@ function create_configuration(inputfile; user_config, user_kwargs, type=nothing) else "dev" end - cfg["repo_root_url"] = "https://github.com/$(repo_slug)/blob/$(edit_commit)" + cfg["repo_root_url"] = "https://github.com/$(repo_slug)/blob/$(cfg["edit_commit"])" cfg["nbviewer_root_url"] = "https://nbviewer.jupyter.org/github/$(repo_slug)/blob/$(deploy_branch)/$(deploy_folder)" cfg["binder_root_url"] = "https://mybinder.org/v2/gh/$(repo_slug)/$(deploy_branch)?filepath=$(deploy_folder)" if (dir = get(ENV, "GITHUB_WORKSPACE", nothing)) !== nothing @@ -315,7 +328,7 @@ function create_configuration(inputfile; user_config, user_kwargs, type=nothing) end elseif haskey(ENV, "GITLAB_CI") if (url = get(ENV, "CI_PROJECT_URL", nothing)) !== nothing - cfg["repo_root_url"] = "$(url)/blob/$(edit_commit)" + cfg["repo_root_url"] = "$(url)/blob/$(cfg["edit_commit"])" end if (url = get(ENV, "CI_PAGES_URL", nothing)) !== nothing && (m = match(r"https://(.+)", url)) !== nothing diff --git a/test/runtests.jl b/test/runtests.jl index c229c7e..9d34427 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -762,6 +762,14 @@ end end markdown = read(joinpath(outdir, "inputfile.md"), String) @test !occursin("md\"\"\"", markdown) + # edit_commit + withenv(ACTIONS_ENV...) do + Literate.markdown(inputfile, outdir; edit_commit="retsam") + end + markdown = read(joinpath(outdir, "inputfile.md"), String) + @test occursin("blob/retsam/", markdown) + @test !occursin("blob/master/", markdown) + # execute write(inputfile, """ using DisplayAs