forked from integrity/integrity
/
git_helper.rb
99 lines (82 loc) · 2.27 KB
/
git_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
module GitHelper
@@_git_repositories = Hash.new {|h,k| h[k] = Repo.new(k) }
def git_repo(name)
@@_git_repositories[name]
end
def destroy_all_git_repos
@@_git_repositories.each {|n,r| r.destroy }
@@_git_repositories.clear
end
class Repo
attr_reader :path
def initialize(name)
@name = name
@path = "/tmp" / @name.to_s
create
end
def path
@path / ".git"
end
def create
destroy
FileUtils.mkdir_p @path
Dir.chdir(@path) do
system 'git init &>/dev/null'
system 'git config user.name "John Doe"'
system 'git config user.email "johndoe@example.org"'
system 'echo "just a test repo" >> README'
system 'git add README &>/dev/null'
system 'git commit -m "First commit" &>/dev/null'
end
add_successful_commit
end
def commits
Dir.chdir(@path) do
commits = `git log --pretty=oneline`.collect { |line| line.split(" ").first }
commits.inject([]) do |commits, sha1|
format = %Q(---%n:message: >-%n %s%n:timestamp: %ci%n:id: %H%n:author: %n :name: %an%n :email: %ae%n)
commits << YAML.load(`git show -s --pretty=format:"#{format}" #{sha1}`)
end
end
end
def add_commit(message, &action)
Dir.chdir(@path) do
yield action
system %Q(git commit -m "#{message}" &>/dev/null)
end
end
def add_failing_commit
add_commit "This commit will fail" do
system %Q(echo '#{build_script(false)}' > test)
system %Q(chmod +x test)
system %Q(git add test &>/dev/null)
end
end
def add_successful_commit
add_commit "This commit will work" do
system %Q(echo '#{build_script(true)}' > test)
system %Q(chmod +x test)
system %Q(git add test &>/dev/null)
end
end
def head
Dir.chdir(@path) do
`git log --pretty=format:%H | head -1`.chomp
end
end
def short_head
head[0..6]
end
def destroy
FileUtils.rm_rf @path if File.directory?(@path)
end
protected
def build_script(successful=true)
<<-script
#!/bin/sh
echo "Running tests..."
exit #{successful ? 0 : 1}
script
end
end
end