Class: Pixab::GitUtils
- Inherits:
-
Object
- Object
- Pixab::GitUtils
- Defined in:
- lib/git/GitUtils.rb
Class Method Summary collapse
- .check_git_repo(path) ⇒ Object
- .check_has_uncommit_code(path) ⇒ Object
- .check_is_code_conflicts(branch1, branch2) ⇒ Object
- .check_is_code_conflicts_in_current_branch ⇒ Object
- .check_local_and_remote_branch_synced(branch) ⇒ Object
-
.check_remote_branch_exists(remote_branch) ⇒ Object
在远程仓库检查远程分支是否存在 remote_branch: 远程分支名称,不需要remote前缀,使用origin作为remote.
-
.check_remote_branch_exists_fast(remote_branch) ⇒ Object
在本地仓库检查远程分支是否存在 remote_branch: 远程分支名称.
-
.current_branch ⇒ Object
获取当前分支.
-
.current_remote_branch ⇒ Object
获取当前分支的远程分支.
-
.fetch_origin(origin = nil, local = nil) ⇒ Object
拉取远程仓库信息 未指定origin分支时,拉取所有远程仓库信息 只指定origin分支时,拉取远程仓库指定分支信息 同时指定local分支时,拉取远程仓库指定分支信息后,会将远程分支合并到local分支.
-
.find_git_repos(root_dir) ⇒ Object
查找包含 Git 仓库的目录.
-
.get_remote_branch_name(branch, remote = 'origin') ⇒ Object
获取远程分支名的方法.
-
.has_remote_branch(branch = "HEAD") ⇒ Object
检查指定分支是否关联了远程分支.
-
.has_uncommit_code ⇒ Object
检查当前是否有未提交的代码.
-
.is_branch_synced(branch1, branch2) ⇒ Object
判断branch1的代码是否已经同步到branch2.
-
.is_code_conflicts(branch1, branch2) ⇒ Object
检查两个分支是否存在冲突.
-
.is_code_conflicts_in_current_branch ⇒ Object
检查当前分支是否有冲突内容.
-
.is_git_repo ⇒ Object
判断当前是否为git仓库.
-
.is_local_and_remote_branch_synced(branch) ⇒ Object
检查指定分支的本地代码和远程代码是否已经同步.
-
.latest_commit_id(branch) ⇒ Object
获取指定分支的最新提交.
-
.push(branch = nil) ⇒ Object
推送代码 branch: 指定推送分支.
-
.remote_branch(local_branch) ⇒ Object
获取指定分支的远程分支.
Class Method Details
.check_git_repo(path) ⇒ Object
154 155 156 157 158 159 |
# File 'lib/git/GitUtils.rb', line 154 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
161 162 163 164 165 166 |
# File 'lib/git/GitUtils.rb', line 161 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
188 189 190 191 192 193 |
# File 'lib/git/GitUtils.rb', line 188 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_branch ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/git/GitUtils.rb', line 175 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
168 169 170 171 172 173 |
# File 'lib/git/GitUtils.rb', line 168 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
122 123 124 125 126 127 128 |
# File 'lib/git/GitUtils.rb', line 122 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.}".red return false end |
.check_remote_branch_exists_fast(remote_branch) ⇒ Object
在本地仓库检查远程分支是否存在remote_branch: 远程分支名称
132 133 134 135 |
# File 'lib/git/GitUtils.rb', line 132 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_branch ⇒ Object
获取当前分支
85 86 87 88 |
# File 'lib/git/GitUtils.rb', line 85 def current_branch branch = `git rev-parse --abbrev-ref HEAD`.chomp return branch end |
.current_remote_branch ⇒ Object
获取当前分支的远程分支
101 102 103 |
# File 'lib/git/GitUtils.rb', line 101 def current_remote_branch return remote_branch(current_branch) 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/git/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 |
.find_git_repos(root_dir) ⇒ Object
查找包含 Git 仓库的目录
145 146 147 148 |
# File 'lib/git/GitUtils.rb', line 145 def find_git_repos(root_dir) # 使用 Dir.glob 匹配所有包含 .git 子目录的目录 Dir.glob("#{root_dir}/**/.git").map { |git_dir| File.dirname(File.(git_dir)) } end |
.get_remote_branch_name(branch, remote = 'origin') ⇒ Object
获取远程分支名的方法
138 139 140 141 142 |
# File 'lib/git/GitUtils.rb', line 138 def get_remote_branch_name(branch, remote = 'origin') remote_branches = `git branch -r`.split("\n").map(&:strip) remote_branch = remote_branches.find { |b| b == "#{remote}/#{branch}" } return remote_branch end |
.has_remote_branch(branch = "HEAD") ⇒ Object
检查指定分支是否关联了远程分支
28 29 30 31 32 |
# File 'lib/git/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_code ⇒ Object
检查当前是否有未提交的代码
22 23 24 25 |
# File 'lib/git/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/git/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/git/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_branch ⇒ Object
检查当前分支是否有冲突内容
73 74 75 76 |
# File 'lib/git/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_repo ⇒ Object
判断当前是否为git仓库
16 17 18 19 |
# File 'lib/git/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/git/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/git/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/git/GitUtils.rb', line 92 def push(branch = nil) commad = "git push" if !branch.nil? commad += " origin #{branch}" end return Utilities.execute_shell(commad) end |
.remote_branch(local_branch) ⇒ Object
获取指定分支的远程分支
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/git/GitUtils.rb', line 106 def remote_branch(local_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.}".red return nil end |