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
117
118
119
|
# File 'lib/capita_git/repository.rb', line 117
def create_local_fixbranch_for_version(version)
create_local_branch("#{version}-fix", "origin/#{version}-fix", true)
end
|
#create_remote_fixbranch_for_version(version) ⇒ Object
111
112
113
114
115
|
# File 'lib/capita_git/repository.rb', line 111
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
95
96
97
|
# File 'lib/capita_git/repository.rb', line 95
def has_local_branch?(name)
!@repository.branches.local.detect { |b| b.full =~ /#{name.gsub('/', '\/')}/ }.nil?
end
|
#has_local_fixbranch_for_version?(version) ⇒ Boolean
131
132
133
|
# File 'lib/capita_git/repository.rb', line 131
def has_local_fixbranch_for_version?(version)
!local_fixbranch_for_version?(version).nil?
end
|
#has_remote_fixbranch_for_version?(version) ⇒ Boolean
135
136
137
|
# File 'lib/capita_git/repository.rb', line 135
def has_remote_fixbranch_for_version?(version)
!remote_fixbranch_for_version?(version).nil?
end
|
#is_local_feature_branch?(name) ⇒ Boolean
99
100
101
|
# File 'lib/capita_git/repository.rb', line 99
def is_local_feature_branch?(name)
!name.match(/^#{user_shortcut}/).nil?
end
|
#is_local_fixbranch?(name) ⇒ Boolean
103
104
105
|
# File 'lib/capita_git/repository.rb', line 103
def is_local_fixbranch?(name)
local_fixbranch_for_version?(latest_major_release_tag).to_s == name
end
|
#latest_major_release_tag ⇒ Object
144
145
146
|
# File 'lib/capita_git/repository.rb', line 144
def latest_major_release_tag
major_release_tags.sort.last
end
|
#latest_minor_release_tag(major_release = nil) ⇒ Object
148
149
150
|
# File 'lib/capita_git/repository.rb', line 148
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
121
122
123
124
|
# File 'lib/capita_git/repository.rb', line 121
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
107
108
109
|
# File 'lib/capita_git/repository.rb', line 107
def on_branchable_branch?(branch)
branch == 'master' or not is_local_feature_branch?(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
126
127
128
129
|
# File 'lib/capita_git/repository.rb', line 126
def remote_fixbranch_for_version?(version)
branches = @repository.branches.remote.select { |b| b.full =~ /#{version}-fix$/ }
branches.nil? ? branches : branches[0]
end
|
#source_branch(name) ⇒ Object
164
165
166
|
# File 'lib/capita_git/repository.rb', line 164
def source_branch(name)
name.split('_')[1]
end
|
#user_email ⇒ Object
156
157
158
|
# File 'lib/capita_git/repository.rb', line 156
def user_email
@repository.config('user.email')
end
|
#user_name ⇒ Object
152
153
154
|
# File 'lib/capita_git/repository.rb', line 152
def user_name
@repository.config('user.name')
end
|
#user_shortcut ⇒ Object
160
161
162
|
# File 'lib/capita_git/repository.rb', line 160
def user_shortcut
user_name.downcase.split(/\s/).map { |n| n[0, 1] }.join
end
|