Skip to content

Commit c187668

Browse files
committed
Find and save location of Git in a deps/build.jl script
WIP towards using BinDeps to download git if not found
1 parent 08d5165 commit c187668

File tree

6 files changed

+57
-18
lines changed

6 files changed

+57
-18
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
*.jl.cov
22
*.jl.*.cov
33
*.jl.mem
4+
deps/downloads/
5+
deps/deps.jl

REQUIRE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
julia 0.4
22
Compat 0.8.4
3+
BinDeps
4+
@osx Homebrew

deps/build.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Compat
2+
using BinDeps # just for download_cmd, unpack_cmd
3+
if is_apple()
4+
using Homebrew
5+
end
6+
7+
gitcmd = `git`
8+
gitversion = "notfound"
9+
try
10+
gitversion = readchomp(`$gitcmd --version`)
11+
end
12+
if gitversion == "notfound"
13+
# TODO download it, or use homebrew's
14+
gitversion = ""
15+
downloadurl = ""
16+
info("Downloading git version $gitversion from $downloadurl")
17+
else
18+
try
19+
# this is in a try because some environments like centos 7
20+
# docker containers don't have `which` installed by default
21+
gitpath = readchomp(is_windows() ? `where git` : `which git`)
22+
gitcmd = `$gitpath`
23+
end
24+
info("Using $gitversion found on path" * (gitcmd == `git` ?
25+
"" : " at $gitcmd"))
26+
end
27+
open("deps.jl", "w") do f
28+
println(f, "gitcmd = $gitcmd")
29+
end

src/Git.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,25 @@ module Git
66
#
77
using Compat
88
import Base: shell_escape
9+
export gitcmd # determined by deps/build.jl and saved in deps/deps.jl
10+
11+
depsjl = joinpath(dirname(@__FILE__), "..", "deps", "deps.jl")
12+
isfile(depsjl) ? include(depsjl) : error("Git.jl not properly installed. " *
13+
"Please run\nPkg.build(\"Git\")")
914

1015
function dir(d)
1116
g = joinpath(d,".git")
1217
isdir(g) && return g
13-
normpath(d, Base.readchomp(setenv(`git rev-parse --git-dir`, dir=d)))
18+
normpath(d, Base.readchomp(setenv(`$gitcmd rev-parse --git-dir`, dir=d)))
1419
end
1520

21+
git() = gitcmd
1622
function git(d)
17-
isempty(d) && return `git`
23+
isempty(d) && return gitcmd
1824
work_tree = abspath(d)
1925
git_dir = joinpath(work_tree, dir(work_tree))
2026
normpath(work_tree, ".") == normpath(git_dir, ".") ? # is it a bare repo?
21-
`git --git-dir=$work_tree` : `git --work-tree=$work_tree --git-dir=$git_dir`
27+
`$gitcmd --git-dir=$work_tree` : `$gitcmd --work-tree=$work_tree --git-dir=$git_dir`
2228
end
2329

2430
cmd(args::Cmd; dir="") = `$(git(dir)) $args`

test/gitutils.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function mktree(d::Dict)
1414
lstree = ""
1515
for (name, data) in d
1616
if isa(data, AbstractString)
17-
sha1 = write_and_readchomp(data, `git hash-object -w --stdin`)
17+
sha1 = write_and_readchomp(data, `$gitcmd hash-object -w --stdin`)
1818
lstree *= "100644 blob $sha1\t$name\n"
1919
elseif isa(data, Dict)
2020
sha1 = mktree(data)
@@ -25,21 +25,21 @@ function mktree(d::Dict)
2525
error("mktree: don't know what to do with $name => $data")
2626
end
2727
end
28-
write_and_readchomp(lstree, `git mktree`)
28+
write_and_readchomp(lstree, `$gitcmd mktree`)
2929
end
3030

3131
function verify_tree(d::Dict, tree::AbstractString)
3232
# check that tree matches d
3333
seen = Set()
34-
for line in eachline(`git ls-tree $tree`)
34+
for line in eachline(`$gitcmd ls-tree $tree`)
3535
m = match(r"^(\d{6}) (\w+) ([0-9a-f]{40})\t(.*)$", line)
3636
@test m != nothing
3737
perm, kind, sha1, name = m.captures
3838
@test haskey(d,name)
3939
data = d[name]
4040
if isa(data, AbstractString)
4141
@test kind == "blob"
42-
@test data == readstring(`git cat-file blob $sha1`)
42+
@test data == readstring(`$gitcmd cat-file blob $sha1`)
4343
elseif isa(data, Dict)
4444
@test kind == "tree"
4545
verify_tree(data, sha1)
@@ -82,7 +82,7 @@ end
8282

8383
function git_verify(h::Dict, i::Dict, w::Dict)
8484
verify_tree(h, "HEAD")
85-
verify_tree(i, readchomp(`git write-tree`))
85+
verify_tree(i, readchomp(`$gitcmd write-tree`))
8686
verify_work(w)
8787
end
8888

@@ -99,18 +99,18 @@ function git_setup(h::Dict, i::Dict, w::Dict, parents::AbstractString...)
9999
end
100100

101101
# create the head commit
102-
commit_tree = `git commit-tree $headt`
102+
commit_tree = `$gitcmd commit-tree $headt`
103103
for parent in parents
104104
commit_tree = `$commit_tree -p $parent`
105105
end
106106
head = write_and_readchomp(headt, commit_tree)
107-
run(`git reset -q --soft $head`)
107+
run(`$gitcmd reset -q --soft $head`)
108108

109-
run(`git read-tree $work`) # read work into the index
110-
run(`git checkout-index -fa`) # check the index out
111-
run(`git read-tree $index`) # setup the index
109+
run(`$gitcmd read-tree $work`) # read work into the index
110+
run(`$gitcmd checkout-index -fa`) # check the index out
111+
run(`$gitcmd read-tree $index`) # setup the index
112112

113113
# verify that everything is as expected
114114
git_verify(h, i, w)
115115
end
116-
git_setup(h::Dict, i::Dict, w::Dict) = git_setup(h, i, w, readchomp(`git rev-parse HEAD`))
116+
git_setup(h::Dict, i::Dict, w::Dict) = git_setup(h, i, w, readchomp(`$gitcmd rev-parse HEAD`))

test/runtests.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ dir = string("tmp.",randstring())
1212
mkdir(dir)
1313
@test isdir(dir)
1414
try cd(dir) do
15-
run(`git init -q`)
16-
run(`git config user.name "Julia Tester"`)
17-
run(`git config user.email [email protected]`)
18-
run(`git commit -q --allow-empty -m "initial empty commit"`)
15+
run(`$gitcmd init -q`)
16+
run(`$gitcmd config user.name "Julia Tester"`)
17+
run(`$gitcmd config user.email [email protected]`)
18+
run(`$gitcmd commit -q --allow-empty -m "initial empty commit"`)
1919
git_verify(Dict(), Dict(), Dict())
2020

2121
# each path can have one of these content in each of head, index, work

0 commit comments

Comments
 (0)