diff --git a/Project.toml b/Project.toml index 897719b..c13461c 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "PkgTemplates" uuid = "14b8a8f1-9102-5b29-a752-f990bacb7fe1" authors = ["Chris de Graaf", "Invenia Technical Computing Corporation"] -version = "0.7.4" +version = "0.7.5" [deps] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" diff --git a/src/PkgTemplates.jl b/src/PkgTemplates.jl index 1121287..bf6bfad 100644 --- a/src/PkgTemplates.jl +++ b/src/PkgTemplates.jl @@ -4,7 +4,7 @@ using Base: active_project, contractuser using Dates: month, today, year using InteractiveUtils: subtypes -using LibGit2: LibGit2, GitConfig, GitRemote, GitRepo +using LibGit2: LibGit2, GitConfig, GitReference, GitRemote, GitRepo, delete_branch using Pkg: Pkg, TOML, PackageSpec using REPL.TerminalMenus: MultiSelectMenu, RadioMenu, request using UUIDs: uuid4 diff --git a/src/plugins/git.jl b/src/plugins/git.jl index a91003d..b45c1ca 100644 --- a/src/plugins/git.jl +++ b/src/plugins/git.jl @@ -3,6 +3,7 @@ ignore=String[], name=nothing, email=nothing, + branch=nothing, ssh=false, jl=true, manifest=false, @@ -16,6 +17,7 @@ Creates a Git repository and a `.gitignore` file. See also: [`gitignore`](@ref). - `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. +- `branch::AbstractString`: The desired name of the repository's default branch. - `ssh::Bool`: Whether or not to use SSH for the remote. If left unset, HTTPS is used. - `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[] name::Union{String, Nothing} = nothing email::Union{String, Nothing} = nothing + branch::Union{String, Nothing} = nothing ssh::Bool = false jl::Bool = true manifest::Bool = false @@ -73,9 +76,15 @@ function prehook(p::Git, t::Template, pkg_dir::AbstractString) else "https://$(t.host)/$(t.user)/$pkg$suffix" 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.add_fetch!(repo, remote, "refs/heads/master") - LibGit2.add_push!(repo, remote, "refs/heads/master") + LibGit2.add_fetch!(repo, remote, "refs/heads/$branch") + LibGit2.add_push!(repo, remote, "refs/heads/$branch") end end end diff --git a/test/git.jl b/test/git.jl index 5878956..b580520 100644 --- a/test/git.jl +++ b/test/git.jl @@ -57,6 +57,15 @@ 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 # We're careful to avoid a Pkg.update as it triggers Cassette#130. t = tpl(; plugins=[Git(), !Tests]) diff --git a/test/show.jl b/test/show.jl index 207d37a..8bcaa97 100644 --- a/test/show.jl +++ b/test/show.jl @@ -40,6 +40,7 @@ end ignore: String[] name: nothing email: nothing + branch: nothing ssh: false jl: true manifest: false