Skip to content

Commit 010cc8d

Browse files
authored
Linkify commit hashes (#4)
1 parent 98c2c4f commit 010cc8d

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
<!-- ## [Unreleased] -->
99

10+
## [v1.1.0] - 2023-11-13
11+
### Added
12+
- Links of the form `[<commit hash>]`, where `<commit hash>` is a commit hash
13+
of length 7 or 40, are now linkified. ([#4])
14+
1015
## [v1.0.0] - 2023-11-13
1116
First release. See
1217
[README.md](https://github.com/JuliaDocs/Changelog.jl/blob/master/README.md)
@@ -16,3 +21,5 @@ for currently supported functionality.
1621
<!-- Links generated by Changelog.jl -->
1722

1823
[v1.0.0]: https://github.com/JuliaDocs/Changelog.jl/releases/tag/v1.0.0
24+
[v1.1.0]: https://github.com/JuliaDocs/Changelog.jl/releases/tag/v1.1.0
25+
[#4]: https://github.com/JuliaDocs/Changelog.jl/issues/4

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "Changelog"
22
uuid = "5217a498-cd5d-4ec6-b8c2-9b85a09b6e3e"
3-
version = "1.0.0"
3+
version = "1.1.0"
44

55
[compat]
66
julia = "1.6"

src/Changelog.jl

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ function collect_links(inputfile::String, repo::String)
5353
linkmap[m["token"]] = "https://github.com/$(repo)/releases/tag/$(m["tag"])"
5454
end
5555

56+
# Rule: [<commit hash>] -> https://github.com/url/commit/<commit hash>
57+
# Description: Replace short (7) or long (40) commit hash with a link to the commit
58+
# Example: [98c2c4f] -> https://github.com/JuliaDocs/Changelog.jl/commit/98c2c4f
59+
for m in eachmatch(r"(?<!\])\[(?<commit>[a-f0-9]{7}|[a-f0-9]{40})\](?![\[\(])", content)
60+
linkmap[m.match] = "https://github.com/$(repo)/commit/$(m["commit"])"
61+
end
62+
5663
return linkmap
5764
end
5865

@@ -74,6 +81,10 @@ The following modifications and replacements are performed:
7481
`[#123](https://github.com/JuliaDocs/Changelog.jl/issues/123)` (with
7582
`repo = "JuliaDocs/Changelog.jl"`).
7683
84+
- `[<commit hash>]` (where `<commit hash>` is either of length 7 or 40) is replaced with
85+
`[<commit hash>](https://github.com/\$repo/commit/<commit hash>)` where `repo` is the
86+
input keyword argument.
87+
7788
- `[abc#XYZ]` is replaced with `[abc#XYZ](https://github.com/abc/issues/XYZ)`. For example,
7889
`[JuliaLang/julia#265]` becomes
7990
`[JuliaLang/julia#265](https://github.com/JuliaLang/julia/issues/265)`.
@@ -186,6 +197,10 @@ The following link tokens are discovered:
186197
`[#123]: https://github.com/JuliaDocs/Changelog.jl/issues/123` to the list (with
187198
`repo = "JuliaDocs/Changelog.jl"`).
188199
200+
- `[<commit hash>]` (where `<commit hash>` is either of length 7 or 40) results in the link
201+
`[<commit hash>]: https://github.com/\$repo/commit/<commit hash>` where `repo` is the
202+
input keyword argument.
203+
189204
- `[abc#XYZ]` results in the link `[abc#XYZ]: https://github.com/abc/issues/XYZ`. For
190205
example, `[JuliaLang/julia#265]` adds
191206
`[JuliaLang/julia#265]: https://github.com/JuliaLang/julia/issues/265` to the list.
@@ -207,7 +222,12 @@ function generate(
207222
# Get the map of token to full URL
208223
linkmap = collect(collect_links(inputfile, repo))
209224

210-
# Sort releases first, then own issues, then external issues, then other things
225+
# Sorting order:
226+
# 1. Releases by version
227+
# 2. Own issues by issue number
228+
# 3. Own commits by hash
229+
# 4. External issues by issue number
230+
# 5. Other things by link token
211231
sort!(linkmap; by = function(x)
212232
k, v = x
213233
if occursin("/releases/tag/", v)
@@ -217,13 +237,16 @@ function generate(
217237
# Sort issues by number
218238
n = parse(Int, match(r"\[\#(?<id>\d+)\]", k)["id"])
219239
return (2, n)
240+
elseif occursin("github.com/$(repo)/commit/", v)
241+
# Sort commit by hash (url)
242+
return (3, v)
220243
elseif occursin(r"github\.com/.*/issues/", v)
221244
# Sort by repo name, then issues by number
222245
m = match(r"\[(?<repo>.*)\#(?<id>\d+)\]", k)
223246
n = parse(Int, m["id"])
224-
return (3, m["repo"], n)
247+
return (4, m["repo"], n)
225248
else
226-
return (4,)
249+
return (5, k)
227250
end
228251
end)
229252

test/runtests.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ using Changelog, Test
33
const CHANGELOG = """
44
## Version [v1.2.3] - link to GitHub release
55
- Link to issue/pull request in own repository: [#123]
6+
- Link to short commit in own repository: [abcd123]
7+
- Link to long commit in own repository: [abcdef0123456789abcdef0123456789abcdef12]
68
- Link to issue/pull request in another GitHub repository:
79
[JuliaLang/julia#123], [JuliaDocs/Documenter.jl#123]
810
- Link to GitHub user: [@octocat]
@@ -22,6 +24,8 @@ EditURL = "https://github.com/JuliaDocs/Changelog.jl/blob/master/CHANGELOG.md"
2224
2325
## Version [v1.2.3](https://github.com/JuliaDocs/Changelog.jl/releases/tag/v1.2.3) - link to GitHub release
2426
- Link to issue/pull request in own repository: [#123](https://github.com/JuliaDocs/Changelog.jl/issues/123)
27+
- Link to short commit in own repository: [abcd123](https://github.com/JuliaDocs/Changelog.jl/commit/abcd123)
28+
- Link to long commit in own repository: [abcdef0123456789abcdef0123456789abcdef12](https://github.com/JuliaDocs/Changelog.jl/commit/abcdef0123456789abcdef0123456789abcdef12)
2529
- Link to issue/pull request in another GitHub repository:
2630
[JuliaLang/julia#123](https://github.com/JuliaLang/julia/issues/123), [JuliaDocs/Documenter.jl#123](https://github.com/JuliaDocs/Documenter.jl/issues/123)
2731
- Link to GitHub user: [@octocat](https://github.com/octocat)
@@ -35,6 +39,8 @@ EditURL = "https://github.com/JuliaDocs/Changelog.jl/blob/master/CHANGELOG.md"
3539
const GITHUB_OUTPUT = """
3640
## Version [v1.2.3] - link to GitHub release
3741
- Link to issue/pull request in own repository: [#123]
42+
- Link to short commit in own repository: [abcd123]
43+
- Link to long commit in own repository: [abcdef0123456789abcdef0123456789abcdef12]
3844
- Link to issue/pull request in another GitHub repository:
3945
[JuliaLang/julia#123], [JuliaDocs/Documenter.jl#123]
4046
- Link to GitHub user: [@octocat]
@@ -51,6 +57,8 @@ $(Changelog.CHANGELOG_LINK_SEPARATOR)
5157
5258
[v1.2.3]: https://github.com/JuliaDocs/Changelog.jl/releases/tag/v1.2.3
5359
[#123]: https://github.com/JuliaDocs/Changelog.jl/issues/123
60+
[abcd123]: https://github.com/JuliaDocs/Changelog.jl/commit/abcd123
61+
[abcdef0123456789abcdef0123456789abcdef12]: https://github.com/JuliaDocs/Changelog.jl/commit/abcdef0123456789abcdef0123456789abcdef12
5462
[JuliaDocs/Documenter.jl#123]: https://github.com/JuliaDocs/Documenter.jl/issues/123
5563
[JuliaLang/julia#123]: https://github.com/JuliaLang/julia/issues/123
5664
[@octocat]: https://github.com/octocat

0 commit comments

Comments
 (0)