Skip to content

Commit 1eadf4e

Browse files
committed
Revert "reomoved Git module and its tests"
This reverts commit 8a66dba.
1 parent 7d06371 commit 1eadf4e

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed

test/git.jl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# This file is a part of Julia. License is MIT: http://julialang.org/license
2+
3+
import Base.Git
4+
include("gitutils.jl")
5+
6+
@test Git.version() >= v"1.7.3"
7+
8+
dir = string("tmp.",randstring())
9+
@test !ispath(dir)
10+
mkdir(dir)
11+
@test isdir(dir)
12+
try cd(dir) do
13+
14+
run(`git init -q`)
15+
run(`git config user.name "Julia Tester"`)
16+
run(`git config user.email [email protected]`)
17+
run(`git commit -q --allow-empty -m "initial empty commit"`)
18+
git_verify(Dict(), Dict(), Dict())
19+
20+
# each path can have one of these content in each of head, index, work
21+
# for a total of length(contents)^3 = 4^3 = 64 combinations.
22+
# each path can be in any of these 64 "superpositions" before & after
23+
# for a total of 64^2 = 4096 files needed to test all transitions
24+
# between before and after superpositions of git repo states.
25+
26+
contents = [nothing, "foo", "bar", Dict{Any,Any}("baz"=>"qux")]
27+
b = length(contents)
28+
states = [ [ base(b,k,6) => contents[rem(div(k,b^p),b)+1] for k=0:(b^3)^2-1 ] for p=0:5 ]
29+
30+
git_setup(states[1:3]...)
31+
try Git.transact() do
32+
git_setup(states[4:6]...)
33+
throw(nothing)
34+
end catch x
35+
is(x,nothing) || rethrow()
36+
end
37+
git_verify(states[1:3]...)
38+
39+
end finally rm(dir, recursive=true) end

test/gitutils.jl

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# This file is a part of Julia. License is MIT: http://julialang.org/license
2+
3+
function write_and_readchomp(data, cmd::Cmd)
4+
r, w, p = readandwrite(cmd)
5+
print(w,data); close(w)
6+
v = readchomp(r)
7+
wait(p)
8+
return v
9+
end
10+
11+
function mktree(d::Dict)
12+
lstree = ""
13+
for (name, data) in d
14+
if isa(data, AbstractString)
15+
sha1 = write_and_readchomp(data, `git hash-object -w --stdin`)
16+
lstree *= "100644 blob $sha1\t$name\n"
17+
elseif isa(data, Dict)
18+
sha1 = mktree(data)
19+
lstree *= "040000 tree $sha1\t$name\n"
20+
elseif is(data, nothing)
21+
# ignore it
22+
else
23+
error("mktree: don't know what to do with $name => $data")
24+
end
25+
end
26+
write_and_readchomp(lstree, `git mktree`)
27+
end
28+
29+
function verify_tree(d::Dict, tree::AbstractString)
30+
# check that tree matches d
31+
seen = Set()
32+
for line in eachline(`git ls-tree $tree`)
33+
m = match(r"^(\d{6}) (\w+) ([0-9a-f]{40})\t(.*)$", line)
34+
@test m != nothing
35+
perm, kind, sha1, name = m.captures
36+
@test haskey(d,name)
37+
data = d[name]
38+
if isa(data, AbstractString)
39+
@test kind == "blob"
40+
@test data == readall(`git cat-file blob $sha1`)
41+
elseif isa(data, Dict)
42+
@test kind == "tree"
43+
verify_tree(data, sha1)
44+
else
45+
error("verify_tree: don't know what to do with $name => $data")
46+
end
47+
push!(seen, name)
48+
end
49+
# check that nothing was missing from tree
50+
for (name, data) in d
51+
@test is(data,nothing) || in(name,seen)
52+
end
53+
end
54+
55+
function verify_work(d::Dict)
56+
# check what's in d
57+
for (name, data) in d
58+
if is(data, nothing)
59+
@test !ispath(name)
60+
continue
61+
end
62+
@test ispath(name)
63+
if isa(data, AbstractString)
64+
@test isfile(name)
65+
@test readall(name) == data
66+
elseif isa(data, Dict)
67+
cd(name) do
68+
verify_work(data)
69+
end
70+
else
71+
error("verify_work: don't know what to do with $name => $data")
72+
end
73+
end
74+
# check for anything that's not in d
75+
for line in eachline(`ls -A`)
76+
name = chomp(line)
77+
@test name == ".git" || haskey(d,name)
78+
end
79+
end
80+
81+
function git_verify(h::Dict, i::Dict, w::Dict)
82+
verify_tree(h, "HEAD")
83+
verify_tree(i, readchomp(`git write-tree`))
84+
verify_work(w)
85+
end
86+
87+
function git_setup(h::Dict, i::Dict, w::Dict, parents::AbstractString...)
88+
# create tree objects
89+
headt = mktree(h)
90+
index = mktree(i)
91+
work = mktree(w)
92+
93+
# clear the repo
94+
for line in eachline(`ls -A`)
95+
name = chomp(line)
96+
name == ".git" || rm(name, recursive=true)
97+
end
98+
99+
# create the head commit
100+
commit_tree = `git commit-tree $headt`
101+
for parent in parents
102+
commit_tree = `$commit_tree -p $parent`
103+
end
104+
head = write_and_readchomp(headt, commit_tree)
105+
run(`git reset -q --soft $head`)
106+
107+
run(`git read-tree $work`) # read work into the index
108+
run(`git checkout-index -fa`) # check the index out
109+
run(`git read-tree $index`) # setup the index
110+
111+
# verify that everything is as expected
112+
git_verify(h, i, w)
113+
end
114+
git_setup(h::Dict, i::Dict, w::Dict) = git_setup(h, i, w, readchomp(`git rev-parse HEAD`))

0 commit comments

Comments
 (0)