Class: Pixab::GitUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/GitUtils.rb

Class Method Summary collapse

Class Method Details

.check_git_repo(path) ⇒ Object



137
138
139
140
141
142
# File 'lib/GitUtils.rb', line 137

def check_git_repo(path)
  if !is_git_repo
    puts "Error: #{path} is not a git repository".red
    exit(1)
  end
end

.check_has_uncommit_code(path) ⇒ Object



144
145
146
147
148
149
# File 'lib/GitUtils.rb', line 144

def check_has_uncommit_code(path)
  if has_uncommit_code
    puts "Please commit first, project path: #{path}".red
    exit(1)
  end
end

.check_is_code_conflicts(branch1, branch2) ⇒ Object



171
172
173
174
175
176
# File 'lib/GitUtils.rb', line 171

def check_is_code_conflicts(branch1, branch2)
  if is_code_conflicts(branch1, branch2)
    puts "Error: #{branch1} and #{branch2} has code conflicts".red
    exit(1)
  end
end

.check_is_code_conflicts_in_current_branchObject



158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/GitUtils.rb', line 158

def check_is_code_conflicts_in_current_branch
  is_code_conflicts = is_code_conflicts_in_current_branch()
  if !is_code_conflicts
    return
  end
  project = File.basename(Dir.pwd)
  conflict_hint = "Error: code conflict!\n"
  conflict_hint += "step1: Resolve project:#{project}, branch:#{current_branch} code conflicts\n"
  conflict_hint += "step2: Execute this script again"
  puts conflict_hint.red
  exit(1)
end

.check_local_and_remote_branch_synced(branch) ⇒ Object



151
152
153
154
155
156
# File 'lib/GitUtils.rb', line 151

def check_local_and_remote_branch_synced(branch)
  if !is_local_and_remote_branch_synced
    puts "Please sync remote branch, use `git pull` or `git push`".red
    exit(1)
  end
end

.check_remote_branch_exists(remote_branch) ⇒ Object

在远程仓库检查远程分支是否存在 remote_branch: 远程分支名称,不需要remote前缀,使用origin作为remote



118
119
120
121
122
123
124
# File 'lib/GitUtils.rb', line 118

def check_remote_branch_exists(remote_branch)
  exisit_remote_branch, status= Open3.capture2("git ls-remote --heads origin #{remote_branch}")
  return status.success? && !exisit_remote_branch.strip.empty?
rescue => e
  puts "Error: check remote branch exists failed, #{e.message}".red
  return false
end

.check_remote_branch_exists_fast(remote_branch) ⇒ Object

在本地仓库检查远程分支是否存在 remote_branch: 远程分支名称



128
129
130
131
# File 'lib/GitUtils.rb', line 128

def check_remote_branch_exists_fast(remote_branch)
  exisit_remote_branch, status = Open3.capture2("git branch -r --list #{remote_branch}")
  status.success? && !exisit_remote_branch.strip.empty?
end

.current_branchObject

获取当前分支



85
86
87
88
# File 'lib/GitUtils.rb', line 85

def current_branch
  branch = `git rev-parse --abbrev-ref HEAD`.chomp
  return branch
end

.current_remote_branchObject

获取当前分支的远程分支



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/GitUtils.rb', line 101

def current_remote_branch
  local_branch = current_branch
  return nil if local_branch.nil?

  remote_branch, status = Open3.capture2("git for-each-ref --format='%(upstream:short)' refs/heads/#{local_branch}")
  remote_branch.strip!
  return nil if !status.success? || remote_branch.empty?

  return nil unless check_remote_branch_exists_fast(remote_branch)
  return remote_branch 
rescue => e
  puts "Error: get current remote branch failed, #{e.message}".red
  return nil
end

.fetch_origin(origin = nil, local = nil) ⇒ Object

拉取远程仓库信息 未指定origin分支时,拉取所有远程仓库信息 只指定origin分支时,拉取远程仓库指定分支信息 同时指定local分支时,拉取远程仓库指定分支信息后,会将远程分支合并到local分支



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/GitUtils.rb', line 59

def fetch_origin(origin = nil, local = nil)
  commad = "git fetch origin"
  if origin.nil?
    return Utilities.execute_shell(commad)
  end
  commad += " #{origin}"
  if local.nil?
    return Utilities.execute_shell(commad)
  end
  commad += ":#{local}"
  return Utilities.execute_shell(commad)
end

.has_remote_branch(branch = "HEAD") ⇒ Object

检查指定分支是否关联了远程分支



28
29
30
31
32
# File 'lib/GitUtils.rb', line 28

def has_remote_branch(branch="HEAD")
  branch_full_name = `git rev-parse --symbolic-full-name #{branch}`
  remote_branch = `git for-each-ref --format='%(upstream:short)' #{branch_full_name}`.chomp
  return !remote_branch.empty?
end

.has_uncommit_codeObject

检查当前是否有未提交的代码



22
23
24
25
# File 'lib/GitUtils.rb', line 22

def has_uncommit_code()
  git_status = `git status -s`
  return !git_status.empty?
end

.is_branch_synced(branch1, branch2) ⇒ Object

判断branch1的代码是否已经同步到branch2



42
43
44
45
46
47
48
# File 'lib/GitUtils.rb', line 42

def is_branch_synced(branch1, branch2)
  if branch1.nil? || branch2.nil?
    return true
  end
  unsynced_commit = `git cherry #{branch2} #{branch1}`
  return unsynced_commit.empty?
end

.is_code_conflicts(branch1, branch2) ⇒ Object

检查两个分支是否存在冲突



79
80
81
82
# File 'lib/GitUtils.rb', line 79

def is_code_conflicts(branch1, branch2)
  conflicts = `git diff --name-status #{branch1} #{branch2} | grep "^U"`
  return !conflicts.empty?
end

.is_code_conflicts_in_current_branchObject

检查当前分支是否有冲突内容



73
74
75
76
# File 'lib/GitUtils.rb', line 73

def is_code_conflicts_in_current_branch
  `git --no-pager diff --check`
  return !Utilities.is_shell_execute_success
end

.is_git_repoObject

判断当前是否为git仓库



16
17
18
19
# File 'lib/GitUtils.rb', line 16

def is_git_repo()
  is_git_repo = `git rev-parse --is-inside-work-tree`.chomp
  return is_git_repo == "true"
end

.is_local_and_remote_branch_synced(branch) ⇒ Object

检查指定分支的本地代码和远程代码是否已经同步



35
36
37
38
39
# File 'lib/GitUtils.rb', line 35

def is_local_and_remote_branch_synced(branch)
  local_log = `git log #{branch} -n 1 --pretty=format:"%H"`
  remote_log = `git log remotes/origin/#{branch} -n 1 --pretty=format:"%H"`
  return local_log == remote_log
end

.latest_commit_id(branch) ⇒ Object

获取指定分支的最新提交



51
52
53
# File 'lib/GitUtils.rb', line 51

def latest_commit_id(branch)
  return `git log #{branch} -n 1 --pretty=format:"%H"`
end

.push(branch = nil) ⇒ Object

推送代码 branch: 指定推送分支



92
93
94
95
96
97
98
# File 'lib/GitUtils.rb', line 92

def push(branch = nil)
  commad = "git push"
  if !branch.nil?
    commad += " origin #{branch}"
  end
  return Utilities.execute_shell(commad)
end