Skip to content

Commit

Permalink
Add a method of git() that bypasses cmd parsing. (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
GunnarFarneback authored Mar 24, 2021
1 parent b941046 commit 46ce401
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Git"
uuid = "d7ba0133-e1db-5d97-8f8c-041e4b3a1eb2"
authors = ["Dilum Aluthge", "contributors"]
version = "1.1.0"
version = "1.2.0"

[deps]
Git_jll = "f8c6e375-362e-5223-8a59-34ff63f689eb"
Expand Down
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,29 @@ julia> using Git
julia> run(`$(git()) clone https://github.com/JuliaRegistries/General`)
```

This can equivalently be written with explicitly split arguments as

```
julia> run(git(["clone", "https://github.com/JuliaRegistries/General"]))
```

to bypass the parsing of the command string.

To read a single line of output from a git command you can use `readchomp`,

```
julia> cd("General")
julia> readchomp(`$(git()) remote get-url origin`)
"https://github.com/JuliaRegistries/General"
```

and `readlines` for multiple lines.

```
julia> readlines(`$(git()) log`)
```

## Acknowledgements

- This work was supported in part by National Institutes of Health grants U54GM115677, R01LM011963, and R25MH116440. The content is solely the responsibility of the authors and does not necessarily represent the official views of the National Institutes of Health.
5 changes: 5 additions & 0 deletions src/Git.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
"""
The Git module allows you to use command-line Git in your Julia
packages. This is implemented with the `git` function, which returns a
`Cmd` object giving access to command-line Git.
"""
module Git

import Git_jll
Expand Down
16 changes: 15 additions & 1 deletion src/git_function.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@ Return a `Cmd` for running Git.
## Example
```julia
julia> run(`$(git()) clone https://github.com/JuliaRegistries/General`)
julia> run(`\$(git()) clone https://github.com/JuliaRegistries/General`)
```
This can equivalently be written with explicitly split arguments as
```
julia> run(git(["clone", "https://github.com/JuliaRegistries/General"]))
```
to bypass the parsing of the command string.
"""
function git(; adjust_PATH::Bool = true, adjust_LIBPATH::Bool = true)
@static if Sys.iswindows()
Expand All @@ -33,3 +41,9 @@ function git(; adjust_PATH::Bool = true, adjust_LIBPATH::Bool = true)
return addenv(original_cmd, env_mapping...)::Cmd
end
end

function git(args::AbstractVector{<:AbstractString}; kwargs...)
cmd = git(; kwargs...)
append!(cmd.exec, args)
return cmd
end
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ end
withtempdir() do tmp_dir
@test !isdir("Git.jl")
@test !isfile(joinpath("Git.jl", "Project.toml"))
run(`$(git()) clone https://github.com/JuliaVersionControl/Git.jl`)
run(git(["clone", "https://github.com/JuliaVersionControl/Git.jl"]))
@test isdir("Git.jl")
@test isfile(joinpath("Git.jl", "Project.toml"))
end
Expand Down

2 comments on commit 46ce401

@DilumAluthge
Copy link
Member

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/32748

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.2.0 -m "<description of version>" 46ce401be40440858e29f6729b5c862539f2c832
git push origin v1.2.0

Please sign in to comment.