Class: CapitaGit::Repository
- Inherits:
-
Object
- Object
- CapitaGit::Repository
show all
- Defined in:
- lib/capita_git/repository.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(path, cli) ⇒ Repository
Returns a new instance of Repository.
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/capita_git/repository.rb', line 16
def initialize(path, cli)
@logger = cli
begin
log 'Opening repository'
@repository = Git.open path
rescue => e
raise CapitaGit::RepositoryError.new 'Can\'t access repository: ' + e.message
end
@git_remote = 'origin'
raise CapitaGit::UncleanError.new "Pending changes found!" if has_changes?
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args) ⇒ Object
12
13
14
|
# File 'lib/capita_git/repository.rb', line 12
def method_missing(m, *args)
@repository.send m, *args
end
|
Instance Attribute Details
#git_remote ⇒ Object
Returns the value of attribute git_remote.
5
6
7
|
# File 'lib/capita_git/repository.rb', line 5
def git_remote
@git_remote
end
|
#logger ⇒ Object
Returns the value of attribute logger.
6
7
8
|
# File 'lib/capita_git/repository.rb', line 6
def logger
@logger
end
|
Class Method Details
.open(path, cli = nil) ⇒ Object
8
9
10
|
# File 'lib/capita_git/repository.rb', line 8
def self.open(path, cli=nil)
new(path, cli)
end
|
Instance Method Details
#checkout_local_branch(name) ⇒ Object
67
68
69
70
|
# File 'lib/capita_git/repository.rb', line 67
def checkout_local_branch(name)
raise "Can't checkout #{name} since it doesn't exist!" unless has_local_branch?(name)
@repository.branch(name).checkout
end
|
#close_local_branch(branch) ⇒ Object
81
82
83
84
85
86
87
|
# File 'lib/capita_git/repository.rb', line 81
def close_local_branch(branch)
raise "Source branch '#{branch}' is not a feature branch, can't close!" unless is_local_feature_branch?(branch)
rebase_local_branch(branch)
checkout_local_branch source_branch(branch)
system "git merge #{branch}"
system "git branch -d #{branch}"
end
|
#create_local_branch(new_branch, source_branch, track = false) ⇒ Object
49
50
51
52
53
|
# File 'lib/capita_git/repository.rb', line 49
def create_local_branch(new_branch, source_branch, track=false)
raise "Branch '#{new_branch}' already exists!" if has_local_branch?(new_branch)
raise "Current branch '#{source_branch}' can't be used as a source for branching!" unless on_branchable_branch?(source_branch)
system "git branch #{track ? '--track' : ''} #{new_branch} #{source_branch}"
end
|
#create_local_fixbranch_for_version(version) ⇒ Object
130
131
132
|
# File 'lib/capita_git/repository.rb', line 130
def create_local_fixbranch_for_version(version)
create_local_branch("#{version}-fix", "origin/#{version}-fix", true)
end
|
#create_remote_fixbranch_for_version(version) ⇒ Object
124
125
126
127
128
|
# File 'lib/capita_git/repository.rb', line 124
def create_remote_fixbranch_for_version(version)
create_local_branch("local_#{version}-fix", version)
push_local_branch_to_remote('origin', "local_#{version}-fix", "#{version}-fix")
delete_local_branch("local_#{version}-fix")
end
|
#current_branch ⇒ Object
39
40
41
|
# File 'lib/capita_git/repository.rb', line 39
def current_branch
@repository.branches.local.select { |b| b.full == @repository.lib.branch_current }[0]
end
|
#delete_local_branch(name) ⇒ Object
63
64
65
|
# File 'lib/capita_git/repository.rb', line 63
def delete_local_branch(name)
system "git branch -d #{name}"
end
|
#fetch_remote_changes ⇒ Object
34
35
36
37
|
# File 'lib/capita_git/repository.rb', line 34
def fetch_remote_changes
log 'Fetching remote changes'
@repository.fetch(@git_remote)
end
|
#has_changes? ⇒ Boolean
43
44
45
46
47
|
# File 'lib/capita_git/repository.rb', line 43
def has_changes?
log 'Checking for pending changes'
changes = `git status --short`
!changes.empty?
end
|
#has_local_branch?(name) ⇒ Boolean
104
105
106
|
# File 'lib/capita_git/repository.rb', line 104
def has_local_branch?(name)
!@repository.branches.local.detect { |b| b.full =~ /^#{name}.*/ }.nil?
end
|
#has_local_fixbranch_for_version?(version) ⇒ Boolean
144
145
146
|
# File 'lib/capita_git/repository.rb', line 144
def has_local_fixbranch_for_version?(version)
!local_fixbranch_for_version?(version).nil?
end
|
#has_remote_branch?(name) ⇒ Boolean
108
109
110
|
# File 'lib/capita_git/repository.rb', line 108
def has_remote_branch?(name)
!@repository.branches.remote.detect { |b| b.full =~ /^#{name}.*/ }.nil?
end
|
#has_remote_fixbranch_for_version?(version) ⇒ Boolean
148
149
150
|
# File 'lib/capita_git/repository.rb', line 148
def has_remote_fixbranch_for_version?(version)
!remote_fixbranch_for_version?(version).nil?
end
|
#is_local_feature_branch?(name) ⇒ Boolean
112
113
114
|
# File 'lib/capita_git/repository.rb', line 112
def is_local_feature_branch?(name)
!name.match(/^#{user_shortcut}/).nil?
end
|
#is_local_fixbranch?(name) ⇒ Boolean
116
117
118
|
# File 'lib/capita_git/repository.rb', line 116
def is_local_fixbranch?(name)
local_fixbranch_for_version?(latest_major_release_tag).to_s == name
end
|
#latest_major_release_tag ⇒ Object
157
158
159
|
# File 'lib/capita_git/repository.rb', line 157
def latest_major_release_tag
major_release_tags.sort.last
end
|
#latest_minor_release_tag(major_release = nil) ⇒ Object
161
162
163
|
# File 'lib/capita_git/repository.rb', line 161
def latest_minor_release_tag(major_release=nil)
major_release.nil? ? minor_release_tags.sort.last : minor_release_tags.select { |tag| tag =~ /^#{major_release}\..*$/ }.sort.last
end
|
#local_fixbranch_for_version?(version) ⇒ Boolean
134
135
136
137
|
# File 'lib/capita_git/repository.rb', line 134
def local_fixbranch_for_version?(version)
branches = @repository.branches.local.select { |b| b.full =~ /^#{version}-fix$/ }
branches.nil? ? branches : branches[0]
end
|
#merge_fixbranch(branch) ⇒ Object
89
90
91
92
93
|
# File 'lib/capita_git/repository.rb', line 89
def merge_fixbranch(branch)
raise "Source branch '#{branch}' is not a fix branch!" unless is_local_fixbranch?(branch)
checkout_local_branch 'master'
system "git merge -m 'Backporting changes of fix branch \'#{branch}\' into master' #{branch}"
end
|
#name ⇒ Object
30
31
32
|
# File 'lib/capita_git/repository.rb', line 30
def name
dir.to_s.split(/\//).last
end
|
#on_branchable_branch?(branch) ⇒ Boolean
120
121
122
|
# File 'lib/capita_git/repository.rb', line 120
def on_branchable_branch?(branch)
branch == 'master' or not is_local_feature_branch?(branch)
end
|
#publish_branch(branch) ⇒ Object
95
96
97
98
99
100
101
102
|
# File 'lib/capita_git/repository.rb', line 95
def publish_branch(branch)
raise "Can't publish '#{branch}' since it doesn't exist!" unless has_local_branch?(branch)
raise "'#{remote_name(branch)}' already exists remotely!" if has_remote_branch?(remote_name(branch))
push_local_branch_to_remote('origin', branch, remote_name(branch))
create_local_branch(remote_name(branch), "origin/#{remote_name(branch)}", true)
checkout_local_branch remote_name(branch)
system "git branch -D #{branch}"
end
|
#push_local_branch_to_remote(remote, local_name, remote_name = nil) ⇒ Object
55
56
57
58
59
60
61
|
# File 'lib/capita_git/repository.rb', line 55
def push_local_branch_to_remote(remote, local_name, remote_name=nil)
if remote_name.nil?
system "git push #{remote} #{local_name}"
else
system "git push #{remote} #{local_name}:#{remote_name}"
end
end
|
#rebase_local_branch(branch) ⇒ Object
72
73
74
75
76
77
78
79
|
# File 'lib/capita_git/repository.rb', line 72
def rebase_local_branch(branch)
raise "Can't update #{branch} since it doesn't exist!" unless has_local_branch?(branch)
checkout_local_branch source_branch(branch)
system "git pull"
checkout_local_branch branch
system "git rebase #{source_branch(branch)}"
raise CapitaGit::UncleanError.new "Rebasing failed, please correct and issue 'git rebase --continue'" if has_changes?
end
|
#remote_fixbranch_for_version?(version) ⇒ Boolean
139
140
141
142
|
# File 'lib/capita_git/repository.rb', line 139
def remote_fixbranch_for_version?(version)
branches = @repository.branches.remote.select { |b| b.full =~ /#{version}-fix$/ }
branches.nil? ? branches : branches[0]
end
|
#remote_name(name) ⇒ Object
181
182
183
|
# File 'lib/capita_git/repository.rb', line 181
def remote_name(name)
name.gsub(/#{user_shortcut}_/, '').gsub(/_/,'-')
end
|
#source_branch(name) ⇒ Object
177
178
179
|
# File 'lib/capita_git/repository.rb', line 177
def source_branch(name)
name.split('_')[1]
end
|
#user_email ⇒ Object
169
170
171
|
# File 'lib/capita_git/repository.rb', line 169
def user_email
@user_email ||= `git config --get user.email`.strip.chomp
end
|
#user_name ⇒ Object
165
166
167
|
# File 'lib/capita_git/repository.rb', line 165
def user_name
@user_name ||= `git config --get user.name`.strip.chomp
end
|
#user_shortcut ⇒ Object
173
174
175
|
# File 'lib/capita_git/repository.rb', line 173
def user_shortcut
user_name.downcase.split(/\s/).map { |n| n[0, 1] }.join
end
|