Class: BigKeeper::GitService

Inherits:
Object
  • Object
show all
Defined in:
lib/big_keeper/service/git_service.rb

Overview

Operator for got

Instance Method Summary collapse

Instance Method Details

#branchs_with_type(path, type) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/big_keeper/service/git_service.rb', line 161

def branchs_with_type(path, type)
  branchs = []
  Dir.chdir(path) do
    IO.popen('git branch -r') do | io |
      io.each do | line |
        branchs << line.gsub(/\s/, '') if line =~ /[\s\S]*#{GitflowType.name(type)}*/
      end
    end
  end
  branchs
end

#current_branch_type(path) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/big_keeper/service/git_service.rb', line 148

def current_branch_type(path)
  branch_name = GitOperator.new.current_branch(path)
  if branch_name =~ /^feature\/S*/
    GitflowType::FEATURE
  elsif branch_name =~ /^hotfix\/S*/
    GitflowType::HOTFIX
  elsif branch_name =~ /^release\/S*/
    GitflowType::RELEASE
  else
    GitflowType::FEATURE
  end
end

#pull(path, branch_name) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/big_keeper/service/git_service.rb', line 173

def pull(path, branch_name)
  git = GitOperator.new
  current_branch_name = git.current_branch(path)

  p "----------current_branch_name:#{current_branch_name}"
  p "----------branch_name:#{branch_name}"
  if current_branch_name == branch_name
    git.pull(path)
  else
    git.checkout(path, branch_name)
    git.pull(path)
    git.checkout(path, current_branch_name)
  end
end

#start(path, name, type) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/big_keeper/service/git_service.rb', line 43

def start(path, name, type)
  git = GitOperator.new

  branch_name = "#{GitflowType.name(type)}/#{name}"
  if !git.has_remote_branch(path, branch_name) && !git.has_local_branch(path, branch_name)

    verify_special_branch(path, 'master')
    #verify_special_branch(path, 'develop')

    #GitflowOperator.new.verify_git_flow(path)

    #GitflowOperator.new.start(path, name, type)

    verify_checkout(path, branch_name)
    git.push_to_remote(path, branch_name)
  else
    verify_checkout(path, branch_name)

    if !git.has_remote_branch(path, branch_name)
      git.push_to_remote(path, branch_name)
    end
  end
end

#startHome(path, name, type) ⇒ Object



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
# File 'lib/big_keeper/service/git_service.rb', line 11

def startHome(path, name, type)
  p "--------------------11.1.startHome"
  git = GitOperator.new
  branch_name = "#{GitflowType.name(type)}/#{name}"
  p "--------------------11.2branch_name:#{branch_name}"
  if !git.has_remote_branch(path, branch_name) && !git.has_local_branch(path, branch_name)
    p "------------------------------11.2.1如果远程和本地没有#{branch_name}分支"
    verify_special_branch(path, 'master')


    #verify_special_branch(path, 'develop')

    #GitflowOperator.new.verify_git_flow(path)

    #GitflowOperator.new.start(path, name, type)
    #
    Dir.chdir(path) do
      p "------------------------------11.2.2git checkout -b #{branch_name}"
      `git checkout -b #{branch_name}`
    end
    git.push_to_remote(path, branch_name)
  else
    verify_checkout(path, branch_name)

    if !git.has_remote_branch(path, branch_name)
      p "------------------------------11.3.1如果远程没有 #{branch_name}"
      git.push_to_remote(path, branch_name)
    end
  end
end

#verify_checkout(path, branch_name) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/big_keeper/service/git_service.rb', line 67

def verify_checkout(path, branch_name)
  p "++++++++++verify_checkout:#{path} #{branch_name}"
  Dir.chdir(path) do
    p "++++++++++cd:#{path}"
    cmd = "git checkout -b #{branch_name}"
    if GitOperator.new.has_branch(path, branch_name)
      p "++++++++++有#{branch_name}"
      cmd = "git checkout #{branch_name}"
    end
    p "++++++++++cmd:#{cmd}"
    IO.popen(cmd) do |io|
      io.each do |line|
        Logger.error("Checkout #{branch_name} failed.") if line.include? 'error'
      end
    end
  end
end

#verify_checkout_pull(path, branch_name) ⇒ Object



85
86
87
88
# File 'lib/big_keeper/service/git_service.rb', line 85

def verify_checkout_pull(path, branch_name)
  GitService.new.verify_checkout(path, branch_name)
  GitService.new.pull(path, branch_name)
end

#verify_del(path, branch_name, name, type) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/big_keeper/service/git_service.rb', line 188

def verify_del(path, branch_name, name, type)
  git = GitOperator.new

  if git.has_local_branch(path, branch_name)
    Logger.highlight("Delete local branch '#{branch_name}' for '#{name}'...")

    if git.current_branch(path) == branch_name
      git.discard(path)
      git.checkout(path, GitflowType.base_branch(type))
    end
    git.del_local(path, branch_name)
  end

  if git.has_remote_branch(path, branch_name)
    Logger.highlight("Delete remote branch '#{branch_name}' for '#{name}'...")
    git.del_remote(path, branch_name)
  end
end

#verify_home_branch(path, branch_name, type) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/big_keeper/service/git_service.rb', line 114

def verify_home_branch(path, branch_name, type)

  p "--------------------7.1.从远程同步本地分支"
  Logger.highlight('Sync local branchs from remote, waiting...')
  git = GitOperator.new
  p "--------------------7.2.git fetch origin"
  git.fetch(path)

  if OperateType::START == type
    if git.current_branch(path) == branch_name
      Logger.error(%(Current branch is '#{branch_name}' already. Use 'update' please))
    end
    if git.has_branch(path, branch_name)
      Logger.error(%(Branch '#{branch_name}' already exists. Use 'switch' please))
    end
  elsif OperateType::SWITCH == type
    if !git.has_branch(path, branch_name)
      Logger.error(%(Can't find a branch named '#{branch_name}'. Use 'start' please))
    end
    if git.current_branch(path) == branch_name
      Logger.error(%(Current branch is '#{branch_name}' already. Use 'update' please))
    end
  elsif OperateType::UPDATE == type
    if !git.has_branch(path, branch_name)
      Logger.error(%(Can't find a branch named '#{branch_name}'. Use 'start' please))
    end
    if git.current_branch(path) != branch_name
      Logger.error(%(Current branch is not '#{branch_name}'. Use 'switch' please))
    end
  else
    Logger.error(%(Not a valid command for '#{branch_name}'.))
  end
end

#verify_push(path, comment, branch_name, name) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/big_keeper/service/git_service.rb', line 207

def verify_push(path, comment, branch_name, name)
  git = GitOperator.new
  if git.has_changes(path) || git.has_commits(path, branch_name)

    git.commit(path, comment) if git.has_changes(path)

    if git.has_remote_branch(path, branch_name)
      Dir.chdir(path) do
        `git push`
      end
    else
      git.push_to_remote(path, branch_name)
    end

    GitOperator.new.check_push_success(path, branch_name, "origin/#{branch_name}")
  else
    Logger.default("Nothing to push for '#{name}'.")
  end
end

#verify_rebase(path, branch_name, name) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/big_keeper/service/git_service.rb', line 227

def verify_rebase(path, branch_name, name)

  # pull rebased branch
  pull(path, branch_name)

  Dir.chdir(path) do
    IO.popen("git rebase #{branch_name} --ignore-whitespace") do |io|
      unless io.gets
        Logger.error("#{name} is already in a rebase-apply, Please:\n"\
                     "  1.Resolve it;\n"\
                     "  2.Commit the changes;\n"\
                     "  3.Push to remote;\n"\
                     "  4.Create a MR;\n"\
                     "  5.Run 'finish' again.")
      end
      io.each do |line|
        next unless line.include? 'Merge conflict'
        Logger.error("Merge conflict in #{name}, Please:\n"\
                     "  1.Resolve it;\n"\
                     "  2.Commit the changes;\n"\
                     "  3.Push to remote;\n"\
                     "  4.Create a MR;\n"\
                     "  5.Run 'finish' again.")
      end
    end
    if GitOperator.new.current_branch(path) != 'develop' && GitOperator.new.current_branch(path) != 'master'
      `git push -f`
    else
      Logger.error("You should not push 'master' or 'develop'")
    end
  end
end

#verify_special_branch(path, name) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/big_keeper/service/git_service.rb', line 90

def verify_special_branch(path, name)
  git = GitOperator.new
  p "----------检查#{name}分支"
  if git.has_remote_branch(path, name)
    p "--------------------有远程#{name}分支"
    if git.has_local_branch(path, name)
      p "------------------------------有本地#{name}分支"
      if git.has_commits(path, name)
        p "----------------------------------------有commits"
        Logger.error(%Q('#{name}' has unpushed commits, you should fix it manually...))
      end
      p "------------------------------git pull"
      pull(path, name)
    else
      p "------------------------------没有本地#{name}分支"
      git.checkout(path, name)
    end
  else
    p "--------------------没有远程#{name}分支"
    verify_checkout(path, name)
    git.push_to_remote(path, name)
  end
end