Allow setting custom default branch name (#192)

This commit is contained in:
Chris de Graaf 2020-06-17 11:06:11 -05:00 committed by GitHub
parent 970554be7f
commit 52bac95d66
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 4 deletions

View File

@ -1,7 +1,7 @@
name = "PkgTemplates" name = "PkgTemplates"
uuid = "14b8a8f1-9102-5b29-a752-f990bacb7fe1" uuid = "14b8a8f1-9102-5b29-a752-f990bacb7fe1"
authors = ["Chris de Graaf", "Invenia Technical Computing Corporation"] authors = ["Chris de Graaf", "Invenia Technical Computing Corporation"]
version = "0.7.4" version = "0.7.5"
[deps] [deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"

View File

@ -4,7 +4,7 @@ using Base: active_project, contractuser
using Dates: month, today, year using Dates: month, today, year
using InteractiveUtils: subtypes using InteractiveUtils: subtypes
using LibGit2: LibGit2, GitConfig, GitRemote, GitRepo using LibGit2: LibGit2, GitConfig, GitReference, GitRemote, GitRepo, delete_branch
using Pkg: Pkg, TOML, PackageSpec using Pkg: Pkg, TOML, PackageSpec
using REPL.TerminalMenus: MultiSelectMenu, RadioMenu, request using REPL.TerminalMenus: MultiSelectMenu, RadioMenu, request
using UUIDs: uuid4 using UUIDs: uuid4

View File

@ -3,6 +3,7 @@
ignore=String[], ignore=String[],
name=nothing, name=nothing,
email=nothing, email=nothing,
branch=nothing,
ssh=false, ssh=false,
jl=true, jl=true,
manifest=false, manifest=false,
@ -16,6 +17,7 @@ Creates a Git repository and a `.gitignore` file.
See also: [`gitignore`](@ref). See also: [`gitignore`](@ref).
- `name::AbstractString`: Your real name, if you have not set `user.name` with Git. - `name::AbstractString`: Your real name, if you have not set `user.name` with Git.
- `email::AbstractString`: Your email address, if you have not set `user.email` with Git. - `email::AbstractString`: Your email address, if you have not set `user.email` with Git.
- `branch::AbstractString`: The desired name of the repository's default branch.
- `ssh::Bool`: Whether or not to use SSH for the remote. - `ssh::Bool`: Whether or not to use SSH for the remote.
If left unset, HTTPS is used. If left unset, HTTPS is used.
- `jl::Bool`: Whether or not to add a `.jl` suffix to the remote URL. - `jl::Bool`: Whether or not to add a `.jl` suffix to the remote URL.
@ -28,6 +30,7 @@ Creates a Git repository and a `.gitignore` file.
ignore::Vector{String} = String[] ignore::Vector{String} = String[]
name::Union{String, Nothing} = nothing name::Union{String, Nothing} = nothing
email::Union{String, Nothing} = nothing email::Union{String, Nothing} = nothing
branch::Union{String, Nothing} = nothing
ssh::Bool = false ssh::Bool = false
jl::Bool = true jl::Bool = true
manifest::Bool = false manifest::Bool = false
@ -73,9 +76,15 @@ function prehook(p::Git, t::Template, pkg_dir::AbstractString)
else else
"https://$(t.host)/$(t.user)/$pkg$suffix" "https://$(t.host)/$(t.user)/$pkg$suffix"
end end
default = LibGit2.branch(repo)
branch = something(p.branch, default)
if branch != default
LibGit2.branch!(repo, branch)
delete_branch(GitReference(repo, "refs/heads/$default"))
end
LibGit2.with(GitRemote(repo, "origin", url)) do remote LibGit2.with(GitRemote(repo, "origin", url)) do remote
LibGit2.add_fetch!(repo, remote, "refs/heads/master") LibGit2.add_fetch!(repo, remote, "refs/heads/$branch")
LibGit2.add_push!(repo, remote, "refs/heads/master") LibGit2.add_push!(repo, remote, "refs/heads/$branch")
end end
end end
end end

View File

@ -57,6 +57,15 @@
end end
end end
@testset "With custom default branch" begin
t = tpl(; plugins=[Git(; branch="main")])
with_pkg(t) do pkg
LibGit2.with(GitRepo(joinpath(t.dir, pkg))) do repo
@test LibGit2.branch(repo) == "main"
end
end
end
@testset "Adds version to commit message" begin @testset "Adds version to commit message" begin
# We're careful to avoid a Pkg.update as it triggers Cassette#130. # We're careful to avoid a Pkg.update as it triggers Cassette#130.
t = tpl(; plugins=[Git(), !Tests]) t = tpl(; plugins=[Git(), !Tests])

View File

@ -40,6 +40,7 @@ end
ignore: String[] ignore: String[]
name: nothing name: nothing
email: nothing email: nothing
branch: nothing
ssh: false ssh: false
jl: true jl: true
manifest: false manifest: false