-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.rb
89 lines (76 loc) · 2.23 KB
/
repository.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
module Saddler
module Reporter
module Support
module Git
class Repository
attr_reader :git
def initialize(path, options = {})
@git = ::Git.open(path, options)
end
def slug
slug_regex = %r{\A/(?<slug>.*?)(?:\.git)?\Z}
remote_urls.map do |url|
uri = GitCloneUrl.parse(url)
match = slug_regex.match(uri.path)
match[:slug] if match
end.compact.first
end
def remote_urls
@git
.remotes
.map(&:url)
end
def current_branch
env_current_branch || @git.current_branch
end
def head
@git.object('HEAD')
end
def merging_sha
merging_object.sha
end
# This for GitHub pull request diff file.
# if head is commit which already merged,
# head's parent objects include merging object
# and (master or origin/master)
def merging_object
return head unless merge_commit?(head)
commit = head.parents.select do |parent|
![master.sha, origin_master.sha].include?(parent.sha)
end
return commit.last if commit.count == 1
head # fallback
end
def master
@git.object('master')
end
def origin_master
@git.object('origin/master')
end
def merge_commit?(commit)
commit.parents.count == 2
end
def push_endpoint
(env_push_endpoint || 'github.com').chomp('/')
end
# e.g. 'github.com'
# git@github.com:packsaddle/ruby-saddler-reporter-support-git.git
def env_push_endpoint
if ENV['PUSH_ENDPOINT']
ENV['PUSH_ENDPOINT']
end
end
def env_current_branch
if ENV['CURRENT_BRANCH']
ENV['CURRENT_BRANCH']
elsif ENV['TRAVIS_BRANCH']
ENV['TRAVIS_BRANCH']
elsif ENV['CIRCLE_BRANCH']
ENV['CIRCLE_BRANCH']
end
end
end
end
end
end
end