-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,64 +3,80 @@ module Git | |
# some utility functions for working with git repos | ||
# | ||
|
||
dir() = readchomp(`git rev-parse --git-dir`) | ||
modules(args::Cmd) = readchomp(`git config -f .gitmodules $args`) | ||
dir(d) = cd(dir,d) | ||
dir() = Base.readchomp(`git rev-parse --git-dir`) | ||
|
||
function git(d) | ||
isempty(d) && return `git` | ||
work_tree = abspath(d) | ||
git_dir = joinpath(work_tree, dir(work_tree)) | ||
`git --work-tree=$work_tree --git-dir=$git_dir` | ||
end | ||
|
||
run(args; dir="") = Base.run(`$(git(dir)) $args`) | ||
success(args; dir="") = Base.success(`$(git(dir)) $args`) | ||
readall(args; dir="") = Base.readall(`$(git(dir)) $args`) | ||
readchomp(args; dir="") = Base.readchomp(`$(git(dir)) $args`) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
StefanKarpinski
Author
Member
|
||
|
||
modules(args::Cmd) = readchomp(`config -f .gitmodules $args`) | ||
different(verA::String, verB::String, path::String) = | ||
!success(`git diff --quiet $verA $verB -- $path`) | ||
!success(`diff --quiet $verA $verB -- $path`) | ||
|
||
dirty() = !success(`git diff --quiet HEAD`) | ||
staged() = !success(`git diff --quiet --cached`) | ||
unstaged() = !success(`git diff --quiet`) | ||
dirty(paths) = !success(`git diff --quiet HEAD -- $paths`) | ||
staged(paths) = !success(`git diff --quiet --cached -- $paths`) | ||
unstaged(paths) = !success(`git diff --quiet -- $paths`) | ||
iscommit(name) = success(`git cat-file commit $name`) | ||
dirty(; dir="") = !success(`diff --quiet HEAD`, dir=dir) | ||
staged(; dir="") = !success(`diff --quiet --cached`, dir=dir) | ||
unstaged(; dir="") = !success(`diff --quiet`, dir=dir) | ||
dirty(paths; dir="") = !success(`diff --quiet HEAD -- $paths`, dir=dir) | ||
staged(paths; dir="") = !success(`diff --quiet --cached -- $paths`, dir=dir) | ||
unstaged(paths; dir="") = !success(`diff --quiet -- $paths`, dir=dir) | ||
iscommit(name; dir="") = success(`cat-file commit $name`, dir=dir) | ||
|
||
attached() = success(`git symbolic-ref -q HEAD` |> SpawnNullStream()) | ||
branch() = readchomp(`git rev-parse --symbolic-full-name --abbrev-ref HEAD`) | ||
head() = readchomp(`git rev-parse HEAD`) | ||
attached(; dir="") = success(`symbolic-ref -q HEAD` |> SpawnNullStream(); dir=dir) | ||
branch(; dir="") = readchomp(`rev-parse --symbolic-full-name --abbrev-ref HEAD`, dir=dir) | ||
head(; dir="") = readchomp(`rev-parse HEAD`, dir=dir) | ||
|
||
immutable State | ||
head::ASCIIString | ||
index::ASCIIString | ||
work::ASCIIString | ||
end | ||
|
||
function snapshot() | ||
head = readchomp(`git rev-parse HEAD`) | ||
index = readchomp(`git write-tree`) | ||
function snapshot(; dir="") | ||
head = readchomp(`rev-parse HEAD`, dir=dir) | ||
index = readchomp(`write-tree`, dir=dir) | ||
work = try | ||
run(`git add --all`) | ||
run(`git add .`) | ||
readchomp(`git write-tree`) | ||
run(`add --all`, dir=dir) | ||
run(`add .`, dir=dir) | ||
readchomp(`write-tree`, dir=dir) | ||
finally | ||
run(`git read-tree $index`) # restore index | ||
run(`read-tree $index`, dir=dir) # restore index | ||
end | ||
State(head, index, work) | ||
end | ||
|
||
function restore(s::State) | ||
run(`git reset -q --`) # unstage everything | ||
run(`git read-tree $(s.work)`) # move work tree to index | ||
run(`git checkout-index -fa`) # check the index out to work | ||
run(`git clean -qdf`) # remove everything else | ||
run(`git read-tree $(s.index)`) # restore index | ||
run(`git reset -q --soft $(s.head)`) # restore head | ||
function restore(s::State; dir="") | ||
run(`reset -q --`, dir=dir) # unstage everything | ||
run(`read-tree $(s.work)`, dir=dir) # move work tree to index | ||
run(`checkout-index -fa`, dir=dir) # check the index out to work | ||
run(`clean -qdf`, dir=dir) # remove everything else | ||
run(`read-tree $(s.index)`, dir=dir) # restore index | ||
run(`reset -q --soft $(s.head)`, dir=dir) # restore head | ||
end | ||
|
||
function transact(f::Function) | ||
state = snapshot() | ||
function transact(f::Function; dir="") | ||
state = snapshot(dir=dir) | ||
try f() catch | ||
restore(state) | ||
restore(state, dir=dir) | ||
rethrow() | ||
end | ||
end | ||
|
||
function is_ancestor_of(a::String, b::String) | ||
A = readchomp(`git rev-parse $a`) | ||
readchomp(`git merge-base $A $b`) == A | ||
function is_ancestor_of(a::String, b::String; dir="") | ||
A = readchomp(`rev-parse $a`, dir=dir) | ||
readchomp(`merge-base $A $b`, dir=dir) == A | ||
end | ||
|
||
## below here to be retired ## | ||
|
||
function each_tagged_version() | ||
git_dir = abspath(dir()) | ||
@task for line in eachline(`git --git-dir=$git_dir show-ref --tags`) | ||
|
I find these kind of obfuscated. How about having
git()
construct git commands, so you'd writereadchomp(git(
config remote.origin.url))
wherereadchomp
is the usual one from Base.