-
Notifications
You must be signed in to change notification settings - Fork 1
/
shared.rb
72 lines (58 loc) · 1.66 KB
/
shared.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
CONST_BRANCHES = ["master","staging"]
def show_heading(msg)
puts "\n**** #{msg} ****"
end
# execute, ignoring and hiding all output, and return exit status code
def pexec(cmd)
`#{cmd} 2>&1`
$?
end
def __branch_cmd(cmd)
`git branch #{cmd} | sed -e 's/[ \*]//g'`.split("\n").sort
end
def local_branches
__branches("-l")
end
def remote_branches
__branches("-r")
end
# get all branches which are fully contained in the given branch
def get_merged(br)
__branches "-l --merged #{br}"
end
def __branches(arg)
branches = __branch_cmd arg
branches.each do |branch|
branch.gsub!("origin/","")
branch.strip!
end
#the =~ master gets rid of HEAD->master
branches.delete_if {|branch| CONST_BRANCHES.include? branch or branch =~ /master/ }
end
def git_count_commits(from, to)
`git log --oneline #{from}..#{to}`.split("\n").length
end
def git_state()
`git status --porcelain`.strip.split("\n")
end
def git_changes(branch)
pexec "git checkout #{branch}"
`git status --porcelain`.strip.split("\n")
end
def git_merge(from, to)
# git merge doesn't output anything useful for tracking number of commits merged on ff merges
# just compare the number of commits between before and after the merge (number of lines on a log --online between before and after version)
pexec "git checkout #{to}"
version = `git rev-parse #{to}`.strip
output = `git merge --ff-only #{from} 2>&1`
count = git_count_commits version, to
if output.include? "CONFLICT"
puts "merge #{from} => #{to} [#{count} commits: CONFLICT!!]"
else
if count > 0
puts "merge #{from} => #{to} [#{count} commits]"
else
puts "merge #{from} => #{to} [None]"
end
end
end