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



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/big_keeper/service/git_service.rb', line 114

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

#current_branch_type(path) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/big_keeper/service/git_service.rb', line 101

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



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/big_keeper/service/git_service.rb', line 127

def pull(path, branch_name)
  git = GitOperator.new
  current_branch_name = git.current_branch(path)
  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



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/big_keeper/service/git_service.rb', line 10

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)
    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

#verify_checkout(path, branch_name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/big_keeper/service/git_service.rb', line 32

def verify_checkout(path, branch_name)
  Dir.chdir(path) do
    cmd = "git checkout -b #{branch_name}"
    if GitOperator.new.has_branch(path, branch_name)
      cmd = "git checkout #{branch_name}"
    end
    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



46
47
48
49
# File 'lib/big_keeper/service/git_service.rb', line 46

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



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/big_keeper/service/git_service.rb', line 139

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/big_keeper/service/git_service.rb', line 69

def verify_home_branch(path, branch_name, type)
  Logger.highlight('Sync local branchs from remote, waiting...')
  git = GitOperator.new

  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



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/big_keeper/service/git_service.rb', line 158

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, "#{GitOperator.remote_local_name(path)}/#{branch_name}")
  else
    Logger.default("Nothing to push for '#{name}'.")
  end
end

#verify_rebase(path, branch_name, name) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/big_keeper/service/git_service.rb', line 178

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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/big_keeper/service/git_service.rb', line 51

def verify_special_branch(path, name)
  git = GitOperator.new

  if git.has_remote_branch(path, name)
    if git.has_local_branch(path, name)
      if git.has_commits(path, name)
        Logger.error(%Q('#{name}' has unpushed commits, you should fix it manually...))
      end
      pull(path, name)
    else
      git.checkout(path, name)
    end
  else
    verify_checkout(path, name)
    git.push_to_remote(path, name)
  end
end