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



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

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



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

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



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/big_keeper/util/git_operator.rb', line 205

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

#checkout(path, branch_name) ⇒ Object



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

def checkout(path, branch_name)
  Dir.chdir(path) do
    p "----------cd:#{path}"
    p "----------git checkout #{branch_name}"
    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



66
67
68
69
70
# File 'lib/big_keeper/util/git_operator.rb', line 66

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

#commit(path, message) ⇒ Object



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

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



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

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

#del_remote(path, branch_name) ⇒ Object



128
129
130
131
132
# File 'lib/big_keeper/util/git_operator.rb', line 128

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

#discard(path) ⇒ Object



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

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

#fetch(path) ⇒ Object



54
55
56
57
58
# File 'lib/big_keeper/util/git_operator.rb', line 54

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



105
106
107
108
109
110
111
112
113
114
# File 'lib/big_keeper/util/git_operator.rb', line 105

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



95
96
97
98
99
100
101
102
103
# File 'lib/big_keeper/util/git_operator.rb', line 95

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



199
200
201
202
203
# File 'lib/big_keeper/util/git_operator.rb', line 199

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



87
88
89
90
91
92
93
# File 'lib/big_keeper/util/git_operator.rb', line 87

def pull(path)
  Dir.chdir(path) do
    p "----------cd:#{path}"
    p "----------git pull"
    `git pull`
  end
end

#push_to_remote(path, branch_name) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/big_keeper/util/git_operator.rb', line 79

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

#rebase(path, branch_name) ⇒ Object



60
61
62
63
64
# File 'lib/big_keeper/util/git_operator.rb', line 60

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

#tag(path, version) ⇒ Object



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

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



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

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



134
135
136
# File 'lib/big_keeper/util/git_operator.rb', line 134

def user
  `git config user.name`.chop
end