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
23 changes: 19 additions & 4 deletions src/saving_tools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ function read_stdout_stderr(cmd::Cmd)
return (exception = exception, out=read(out,String), err=read(err,String))
end

"""
gitpatch(gitpath = projectdir())
"""
gitpatch(gitpath = projectdir())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incorrect. Restore the 4 spaces of identation, otherwise the documentation string will not register correctly.


Generates a patch describing the changes of a dirty repository
compared to its last commit; i.e. what `git diff HEAD` produces.
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,8 @@ 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 output will display the commit message.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When referring to code, don't use english words. Use code, formatted as monotype font. Don't write "If the commit message is ...". Write instead "If the commit_message keyword is set to true, then...".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sentence "then the output will display the commit message." should be imporved. What is correct is that "the dinctionary 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 +192,18 @@ Dict{Symbol,Int64} with 2 entries:
:y => 4
:x => 3

julia> tag!(d)
julia> tag!(d, commit_message=true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
julia> tag!(d, commit_message=true)
julia> tag!(d; commit_message=true)

it is better syntax to separate keywords by ;.

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 +212,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 +227,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 +245,8 @@ function tag!(d::AbstractDict{K,T};
return d
end



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

Expand Down
24 changes: 19 additions & 5 deletions test/stools_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,32 @@ d2 = Dict("x" => 3, "y" => 4)
@testset "tag! ($(keytype(d)))" for d in (d1, d2)
@testset "no patch ($(dirty ? "dirty" : "clean"))" for dirty in (true, false)
path = dirty ? dpath : cpath
_test_tag!(d, path, false, nothing) # variable unset
_test_tag!(d, path, false, "") # variable set but empty
_test_tag!(d, path, false, nothing) # variable unset
_test_tag!(d, path, false, "") # variable set but empty
_test_tag!(d, path, false, "false") # variable parses as false
_test_tag!(d, path, false, "0") # variable parses as false
_test_tag!(d, path, false, "rubbish") # variable not a Bool
_test_tag!(d, path, false, "0") # variable parses as false
_test_tag!(d, path, false, "rubbish") # variable not a Bool
end
@testset "patch" begin
_test_tag!(d, dpath, true, "true") # variable parses as true
_test_tag!(d, dpath, true, "1") # variable parses as true
_test_tag!(d, dpath, true, "1") # variable parses as true
end
@testset "message_name" begin
path = mktempdir(cleanup=true) # delete path on process exit
repo = LibGit2.init(path)
LibGit2.commit(repo, "tmp repo commit")
message_commit_test = LibGit2.GitCommit(repo, "HEAD")
message_name = LibGit2.message(message_commit_test)
if (message_name !== nothing) && (message_name != "")
print(message_name)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test block is wrong unfortunately. You need to test the tag! function. This is what DrWatson provides. We don't care about testing Lib2Git.

Please call the tag! function, with the keyword commit_message to true. Then manually examine the tagged dictionary, and manually write the test

@test d["gitmessage"] == "the mesage you expecet"

Not calling the @test macro means you are not testing anything. Printing is not testing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, this might be a dumb question, but will it also work if instead I add the @test d["gitmessage"] == "the mesage you expect" in the _test_tag! function? So something like this? I'm terribly sorry for the beginner mistakes..

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't alter any of the previous code. It is simply not worth it. Why not just add this new test code block, that just has 5 lines of code, and does the test? Simpler = better.

(Sure, there could be ways to alter the previous code, but why do it though? just make a new @testset codeblock with 4 lines of code that call the tag! function and read the message and call an @test on the read message.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somehow it didn't work for me.............

end
end

# Ensure that the correct git message show up
d_new = tag!(d, gitpath=dpath, commit_message = true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't test anything, see above.



# Ensure that above tests operated out-of-place.
@test d1 == Dict(:x => 3, :y => 4)
@test d2 == Dict("x" => 3, "y" => 4)
Expand Down
Loading