Git: Allow setting custom user.name and user.email (#158)
This commit is contained in:
parent
ad0c012e88
commit
e14d672b60
|
@ -3,7 +3,7 @@ module PkgTemplates
|
|||
using Base: active_project, contractuser
|
||||
|
||||
using Dates: month, today, year
|
||||
using LibGit2: LibGit2, GitRemote, GitRepo
|
||||
using LibGit2: LibGit2, GitConfig, GitRemote, GitRepo
|
||||
using Pkg: Pkg, TOML, PackageSpec
|
||||
using UUIDs: uuid4
|
||||
|
||||
|
|
|
@ -1,11 +1,20 @@
|
|||
"""
|
||||
Git(; ignore=String[], ssh=false, manifest=false, gpgsign=false)
|
||||
Git(;
|
||||
ignore=String[],
|
||||
name=nothing,
|
||||
email=nothing,
|
||||
ssh=false,
|
||||
manifest=false,
|
||||
gpgsign=false,
|
||||
)
|
||||
|
||||
Creates a Git repository and a `.gitignore` file.
|
||||
|
||||
## Keyword Arguments
|
||||
- `ignore::Vector{<:AbstractString}`: Patterns to add to the `.gitignore`.
|
||||
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.
|
||||
- `ssh::Bool`: Whether or not to use SSH for the remote.
|
||||
If left unset, HTTPS is used.
|
||||
- `manifest::Bool`: Whether or not to commit `Manifest.toml`.
|
||||
|
@ -15,6 +24,8 @@ Creates a Git repository and a `.gitignore` file.
|
|||
"""
|
||||
@with_kw_noshow struct Git <: Plugin
|
||||
ignore::Vector{String} = String[]
|
||||
name::Union{String, Nothing} = nothing
|
||||
email::Union{String, Nothing} = nothing
|
||||
ssh::Bool = false
|
||||
manifest::Bool = false
|
||||
gpgsign::Bool = false
|
||||
|
@ -36,9 +47,10 @@ function validate(p::Git, t::Template)
|
|||
throw(ArgumentError("Git: gpgsign is set but the Git CLI is not installed"))
|
||||
end
|
||||
|
||||
foreach(("user.name", "user.email")) do k
|
||||
if isempty(LibGit2.getconfig(k, ""))
|
||||
throw(ArgumentError("Git: Global Git config is missing required value '$k'"))
|
||||
foreach((:name, :email)) do k
|
||||
user_k = "user.$k"
|
||||
if getproperty(p, k) === nothing && isempty(LibGit2.getconfig(user_k, ""))
|
||||
throw(ArgumentError("Git: Global Git config is missing required value '$user_k'"))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -46,6 +58,12 @@ end
|
|||
# Set up the Git repository.
|
||||
function prehook(p::Git, t::Template, pkg_dir::AbstractString)
|
||||
LibGit2.with(LibGit2.init(pkg_dir)) do repo
|
||||
LibGit2.with(GitConfig(repo)) do config
|
||||
foreach((:name, :email)) do k
|
||||
v = getproperty(p, k)
|
||||
v === nothing || LibGit2.set!(config, "user.$k", v)
|
||||
end
|
||||
end
|
||||
commit(p, repo, pkg_dir, "Initial commit")
|
||||
pkg = basename(pkg_dir)
|
||||
url = if p.ssh
|
||||
|
|
10
test/git.jl
10
test/git.jl
|
@ -35,6 +35,16 @@
|
|||
end
|
||||
end
|
||||
|
||||
@testset "With custom name/email" begin
|
||||
t = tpl(; plugins=[Git(; name="me", email="a@b.c")])
|
||||
with_pkg(t) do pkg
|
||||
LibGit2.with(GitRepo(joinpath(t.dir, pkg))) do repo
|
||||
@test LibGit2.getconfig(repo, "user.name", "") == "me"
|
||||
@test LibGit2.getconfig(repo, "user.email", "") == "a@b.c"
|
||||
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(; disable_defaults=[Tests], plugins=[Git()])
|
||||
|
|
|
@ -27,6 +27,8 @@ const LICENSES_DIR = joinpath(TEMPLATES_DIR, "licenses")
|
|||
cron: "0 0 * * *"
|
||||
Git:
|
||||
ignore: String[]
|
||||
name: nothing
|
||||
email: nothing
|
||||
ssh: false
|
||||
manifest: false
|
||||
gpgsign: false
|
||||
|
|
Loading…
Reference in New Issue