Class: BigKeeper::GitOperator

Inherits:
Object
  • Object
show all
Defined in:
lib/big_keeper/util/git_operator.rb

Overview

Operator for got

Instance Method Summary collapse

Instance Method Details

#check_diff(path, branch, compare_branch) ⇒ Object

TODO: 需要改造,util方法不应该有业务逻辑



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/big_keeper/util/git_operator.rb', line 200

def check_diff(path, branch, compare_branch)
  compare_branch_commits = Array.new
  IO.popen("cd '#{path}'; git log --left-right #{branch}...#{compare_branch} --pretty=oneline") do |io|
    io.each do |line|
      compare_branch_commits.push(line) if (line.include? '>') && (line.include? "Merge branch #{branch} into #{compare_branch}")
    end
  end
  if compare_branch_commits.size > 0
    compare_branch_commits.map { |item|
        Logger.default(item)
    }
    Logger.error("#{compare_branch} branch has commit doesn't committed in #{branch}, please check")
  else
    Logger.highlight("#{compare_branch} branch doesn't have commit before #{branch}")
  end
end

#check_merge(path, condition) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/big_keeper/util/git_operator.rb', line 169

def check_merge(path, condition)
  unmerged_branch = Array.new
  IO.popen("cd '#{path}'; git branch --no-merged") do |io|
    io.each do |line|
      unmerged_branch.push(line) if line.include? "#{condition}"
    end
  end
  if (unmerged_branch.size > 0)
    unmerged_branch.map { |item|
        Logger.default(item)
    }
    Logger.error("Still has unmerged feature branch, please check")
  end
end

#check_push_success(path, branch, compare_branch) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/big_keeper/util/git_operator.rb', line 223

def check_push_success(path, branch, compare_branch)
  compare_branch_commits = Array.new
  IO.popen("cd '#{path}'; git log --left-right #{branch}...#{compare_branch} --pretty=oneline") do |io|
    io.each do |line|
      compare_branch_commits.push(line) if (line.include? '>') || (line.include? 'fatal')
    end
  end
  if compare_branch_commits.size > 0
    compare_branch_commits.map { |item|
        Logger.default(item)
    }
    Logger.error("#{branch} branch push unsuccess, please check")
  else
    Logger.highlight("#{branch} branch push success")
  end
end

#check_remote_branch_diff(path, branch, compare_branch) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/big_keeper/util/git_operator.rb', line 184

def check_remote_branch_diff(path, branch, compare_branch)
  fetch(path)
  compare_branch_commits = Array.new
  IO.popen("cd '#{path}';git log --left-right #{branch}...origin/#{compare_branch} --pretty=oneline") do |io|
    io.each do |line|
      compare_branch_commits.push(line) unless (line.include? '>') && (line.include? "Merge branch \'#{branch}\'")
    end
  end
  if compare_branch_commits.size > 0
    return true
  else
    return false
  end
end

#checkout(path, branch_name) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/big_keeper/util/git_operator.rb', line 42

def checkout(path, branch_name)
  Dir.chdir(path) do
    IO.popen("git checkout #{branch_name}") do |io|
      io.each do |line|
        Logger.error("Checkout #{branch_name} failed.") if line.include? 'error'
      end
    end
  end
end

#clone(path, git_base) ⇒ Object



64
65
66
67
68
# File 'lib/big_keeper/util/git_operator.rb', line 64

def clone(path, git_base)
  Dir.chdir(path) do
    `git clone #{git_base}`
  end
end

#commit(path, message) ⇒ Object



70
71
72
73
74
75
# File 'lib/big_keeper/util/git_operator.rb', line 70

def commit(path, message)
  Dir.chdir(path) do
    `git add .`
    `git commit -m "#{Logger.formatter_output(message)}"`
  end
end

#current_branch(path) ⇒ Object



6
7
8
9
10
# File 'lib/big_keeper/util/git_operator.rb', line 6

def current_branch(path)
  Dir.chdir(path) do
    `git rev-parse --abbrev-ref HEAD`.chop
  end
end

#del_local(path, branch_name) ⇒ Object



117
118
119
120
121
# File 'lib/big_keeper/util/git_operator.rb', line 117

def del_local(path, branch_name)
  Dir.chdir(path) do
    `git branch -D #{branch_name}`
  end
end

#del_remote(path, branch_name) ⇒ Object



123
124
125
126
127
# File 'lib/big_keeper/util/git_operator.rb', line 123

def del_remote(path, branch_name)
  Dir.chdir(path) do
    `git push origin --delete #{branch_name}`
  end
end

#discard(path) ⇒ Object



111
112
113
114
115
# File 'lib/big_keeper/util/git_operator.rb', line 111

def discard(path)
  Dir.chdir(path) do
    `git checkout . && git clean -df`
  end
end

#fetch(path) ⇒ Object



52
53
54
55
56
# File 'lib/big_keeper/util/git_operator.rb', line 52

def fetch(path)
  Dir.chdir(path) do
    `git fetch origin`
  end
end

#has_branch(path, branch_name) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/big_keeper/util/git_operator.rb', line 32

def has_branch(path, branch_name)
  has_branch = false
  IO.popen("cd '#{path}'; git branch -a") do |io|
    io.each do |line|
      has_branch = true if line.include? branch_name
    end
  end
  has_branch
end

#has_changes(path) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/big_keeper/util/git_operator.rb', line 100

def has_changes(path)
  has_changes = true
  clear_flag = 'nothing to commit, working tree clean'
  IO.popen("cd '#{path}'; git status") do |io|
    io.each do |line|
      has_changes = false if line.include? clear_flag
    end
  end
  has_changes
end

#has_commits(path, branch_name) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/big_keeper/util/git_operator.rb', line 90

def has_commits(path, branch_name)
  has_commits = false
  IO.popen("cd '#{path}'; git log --branches --not --remotes") do |io|
    io.each do |line|
      has_commits = true if line.include? "(#{branch_name})"
    end
  end
  has_commits
end

#has_local_branch(path, branch_name) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/big_keeper/util/git_operator.rb', line 22

def has_local_branch(path, branch_name)
  has_branch = false
  IO.popen("cd '#{path}'; git branch") do |io|
    io.each do |line|
      has_branch = true if line.include? branch_name
    end
  end
  has_branch
end

#has_remote_branch(path, branch_name) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/big_keeper/util/git_operator.rb', line 12

def has_remote_branch(path, branch_name)
  has_branch = false
  IO.popen("cd '#{path}'; git branch -r") do |io|
    io.each do |line|
      has_branch = true if line.include? branch_name
    end
  end
  has_branch
end

#merge(path, branch_name) ⇒ Object



217
218
219
220
221
# File 'lib/big_keeper/util/git_operator.rb', line 217

def merge(path, branch_name)
  IO.popen("cd '#{path}'; git merge #{branch_name}") do |line|
    Logger.error("Merge conflict in #{branch_name}") if line.include? 'Merge conflict'
  end
end

#pull(path) ⇒ Object



84
85
86
87
88
# File 'lib/big_keeper/util/git_operator.rb', line 84

def pull(path)
  Dir.chdir(path) do
    `git pull`
  end
end

#push_to_remote(path, branch_name) ⇒ Object



77
78
79
80
81
82
# File 'lib/big_keeper/util/git_operator.rb', line 77

def push_to_remote(path, branch_name)
  Dir.chdir(path) do
    `git push -u origin #{branch_name}`
  end
  GitOperator.new.check_push_success(path, branch_name, "origin/#{branch_name}")
end

#rebase(path, branch_name) ⇒ Object



58
59
60
61
62
# File 'lib/big_keeper/util/git_operator.rb', line 58

def rebase(path, branch_name)
  Dir.chdir(path) do
    `git rebase origin/#{branch_name}`
  end
end

#tag(path, version) ⇒ Object



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

def tag(path, version)
  tags = Array.new
  IO.popen("cd '#{path}'; git tag") do |io|
    io.each do |line|
      tags << line
    end
  end
  unless tags.include? "#{version}\n"
    Dir.chdir(path) do
      `git tag -a #{version} -m "release: V #{version}" master;`
      `git push --tags`
    end
    return
  end
  Logger.highlight("tag already exists in the remote, skip this step")
end

#tag_list(path) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
# File 'lib/big_keeper/util/git_operator.rb', line 157

def tag_list(path)
  tag_list = Array.new
  IO.popen("cd '#{path}'; git tag -l") do |io|
    io.each do |line|
      unless line=~(/[a-zA-Z]/)
        tag_list << line
      end
    end
  end
  tag_list
end

#userObject



129
130
131
132
133
134
135
136
137
138
# File 'lib/big_keeper/util/git_operator.rb', line 129

def user
  name = `git config user.name`.chop
  cn_reg = /[\u4e00-\u9fa5]{1}/
  cn_arr = name.scan(cn_reg)
  if cn_arr.count > 0
    Logger.error("git config user.name has Chinese character")
  else
    name.gsub(/[^0-9A-Za-z]/, '').downcase
  end
end