Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#387 #398

Merged
merged 12 commits into from
Sep 28, 2023
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 2.13.0
- Add `commmit_message` option in `tag!`, which add an additional `"gitmessage"` field in dictionary `d` and include the git message associated with the commit.

# 2.12.6
- Crucial bugfix to `produce_or_load`. When used with a prefix, it attached double prefix to the file (one coming as a duplicate from `savename`). This is now fixed, but it means that some files produced with prefix and `produce_or_load` in v2.12 may be re-produced after this update.

Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "DrWatson"
uuid = "634d3b9d-ee7a-5ddf-bec9-22491ea816e1"
repo = "https://github.com/JuliaDynamics/DrWatson.jl.git"
version = "2.12.7"
version = "2.13.0"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
22 changes: 19 additions & 3 deletions src/saving_tools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function read_stdout_stderr(cmd::Cmd)
return (exception = exception, out=read(out,String), err=read(err,String))
end

"""
"""
gitpatch(gitpath = projectdir())

Generates a patch describing the changes of a dirty repository
Expand Down Expand Up @@ -155,6 +155,7 @@ function gitpatch(path = projectdir(); try_submodule_diff=true)
return nothing
end


########################################################################################
# Tagging
########################################################################################
Expand All @@ -167,7 +168,9 @@ the project's gitpath). Do nothing if a key `gitcommit` already exists
repository is not found. If the git repository is dirty, i.e. there
are un-commited changes, and `storepatch` is true, then the output of `git diff HEAD` is stored
in the field `gitpatch`. Note that patches for binary files are not
stored. You can use [`isdirty`](@ref) to check if a repo is dirty.
stored. You can use [`isdirty`](@ref) to check if a repo is dirty.
If the `commit message` is set to `true`,
then the dictionary `d` will include an additional field `"gitmessage"` and will contain the git message associated with the commit.

Notice that the key-type of the dictionary must be `String` or `Symbol`.
If `String` is a subtype of the _value_ type of the dictionary, this operation is
Expand All @@ -190,16 +193,18 @@ Dict{Symbol,Int64} with 2 entries:
:y => 4
:x => 3

julia> tag!(d)
julia> tag!(d; commit_message=true)
Dict{Symbol,Any} with 3 entries:
:y => 4
:gitmessage => "File set up by DrWatson"
:gitcommit => "96df587e45b29e7a46348a3d780db1f85f41de04"
:x => 3
```
"""
function tag!(d::AbstractDict{K,T};
gitpath = projectdir(), force = false, source = nothing,
storepatch::Bool = readenv("DRWATSON_STOREPATCH", false),
commit_message::Bool = false
Datseris marked this conversation as resolved.
Show resolved Hide resolved
) where {K,T}
@assert (K <: Union{Symbol,String}) "We only know how to tag dictionaries that have keys that are strings or symbols"
c = gitdescribe(gitpath)
Expand All @@ -208,6 +213,7 @@ function tag!(d::AbstractDict{K,T};
# Get the appropriate keys
commitname = keyname(d, :gitcommit)
patchname = keyname(d, :gitpatch)
message_name = keyname(d, :gitmessage)

if haskey(d, commitname) && !force
@warn "The dictionary already has a key named `gitcommit`. We won't "*
Expand All @@ -222,6 +228,14 @@ function tag!(d::AbstractDict{K,T};
d[patchname] = patch
end
end
if commit_message
repo = LibGit2.GitRepoExt(gitpath)
mssgcommit = LibGit2.GitCommit(repo, "HEAD")
msg = LibGit2.message(mssgcommit)
if (msg !== nothing) && (msg != "")
d[message_name] = msg
end
end
end

# Include source file and line number info if given.
Expand All @@ -232,6 +246,8 @@ function tag!(d::AbstractDict{K,T};
return d
end



"""
keyname(d::AbstractDict{K,T}, key) where {K<:Union{Symbol,String},T}

Expand Down
8 changes: 8 additions & 0 deletions test/stools_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ d2 = Dict("x" => 3, "y" => 4)
_test_tag!(d, dpath, true, "true") # variable parses as true
_test_tag!(d, dpath, true, "1") # variable parses as true
end
@testset "message" begin
Datseris marked this conversation as resolved.
Show resolved Hide resolved
d = copy(d1)
path = cpath
d = tag!(d; gitpath=path, commit_message = true)
message_name = keytype(d)(:gitmessage)
@test haskey(d, message_name)
@test d[message_name] == "tmp repo commit"
end
end

# Ensure that above tests operated out-of-place.
Expand Down
Loading