Class: Pindo::Command::Utils::Tag
- Inherits:
-
Pindo::Command::Utils
- Object
- CLAide::Command
- Pindo::Command
- Pindo::Command::Utils
- Pindo::Command::Utils::Tag
- Defined in:
- lib/pindo/command/utils/tag.rb
Constant Summary
Constants inherited from Pindo::Command
DEFAULT_OPTIONS, DEFAULT_ROOT_OPTIONS
Instance Attribute Summary
Attributes inherited from Pindo::Command
Class Method Summary collapse
-
.options ⇒ Object
命令的选项列表.
Instance Method Summary collapse
- #add_release_tag(project_dir: nil, increment_mode: "minor", force_retag: false, custom_tag: nil) ⇒ Object
-
#initialize(argv) ⇒ Tag
constructor
A new instance of Tag.
- #merge_to_release_branch(project_dir: nil, release_branch: nil, coding_branch: nil) ⇒ Object
- #run ⇒ Object
Methods inherited from Pindo::Command
Methods included from Funlog::Mixin
Methods included from Pindoconfig::Mixin
Methods included from Githelper
#add_branch, #add_tag, #add_tag_with_check, #clone_clang_repo, #clone_devclang_repo, #clone_pindo_common_config_repo, #clone_pindo_env_config_repo, #clong_buildconfig_repo, #get_repo_base_name, #getcode_to_dir, #git_addpush_repo, #git_latest_commit_id, #git_root_directory, #is_git_directory?, #local_branch_exists?, #local_tag_exists?, #prepare_gitenv, #process_need_add_files, #remote_branch_exists?, #remote_tag_exists?, #remove_branch, #remove_tag
Methods included from Executable
capture_command, #executable, execute_command, which, which!
Constructor Details
#initialize(argv) ⇒ Tag
Returns a new instance of Tag.
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/pindo/command/utils/tag.rb', line 48 def initialize(argv) @mode = argv.option('mode') || 'minor' @force_retag = argv.flag?('retag', false) @custom_tag = argv.option('tag') # 如果指定了自定义tag,不需要验证mode unless @custom_tag || ['major', 'minor', 'patch'].include?(@mode) raise Informative, "mode参数必须是 major, minor 或 patch" end super @additional_args = argv.remainder! end |
Class Method Details
.options ⇒ Object
命令的选项列表
40 41 42 43 44 45 46 |
# File 'lib/pindo/command/utils/tag.rb', line 40 def self. [ ['--mode', '指定版本号增加模式(major/minor/patch),默认minor'], ['--retag', '强制重新打最新的tag'], ['--tag', '直接指定tag版本号(如 1.1.3),会自动添加v前缀'] ].concat(super) end |
Instance Method Details
#add_release_tag(project_dir: nil, increment_mode: "minor", force_retag: false, custom_tag: nil) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/pindo/command/utils/tag.rb', line 102 def add_release_tag(project_dir: nil, increment_mode: "minor", force_retag: false, custom_tag: nil) raise ArgumentError, "项目目录不能为空" if project_dir.nil? # 如果指定了自定义tag,直接使用 if custom_tag && !custom_tag.empty? Funlog.instance.("使用指定的tag版本: #{custom_tag}") # 确保tag有v前缀 new_tag = custom_tag.start_with?('v') ? custom_tag : "v#{custom_tag}" # 检查tag是否已存在 = git!(%W(-C #{project_dir} tag -l)).split("\n") if .include?(new_tag) if force_retag Funlog.instance.("tag #{new_tag} 已存在,强制重新打tag") git!(%W(-C #{project_dir} tag -d #{new_tag})) git!(%W(-C #{project_dir} push origin :refs/tags/#{new_tag})) else Funlog.instance.("tag #{new_tag} 已存在") Funlog.instance.("仓库路径: #{project_dir}") return end end # 创建tag并推送 git!(%W(-C #{project_dir} tag #{new_tag})) git!(%W(-C #{project_dir} push origin #{new_tag})) Funlog.instance.("创建tag: #{new_tag}") Funlog.instance.("仓库路径: #{project_dir}") return end # 原有的自动递增逻辑 Funlog.instance.("开始创建初tag") latest_tag = get_latest_version_tag(project_dir: project_dir) if latest_tag.nil? new_tag = create_next_version_tag( project_dir: project_dir, tag_prefix: "v", increment_mode: increment_mode, force_retag: false ) Funlog.instance.("创建初始tag: #{new_tag}") Funlog.instance.("仓库路径: #{project_dir}") return end if is_tag_at_head?(git_root_dir: project_dir, tag_name: latest_tag) Funlog.instance.("当前commit已有tag: #{latest_tag},无需重新打tag") Funlog.instance.("仓库路径: #{project_dir}") return end Funlog.instance.("最近的上次tag是: #{latest_tag} ") new_tag = create_next_version_tag( project_dir: project_dir, tag_prefix: "v", increment_mode: increment_mode, force_retag: force_retag ) Funlog.instance.("当前仓库的tag: #{new_tag}") Funlog.instance.("仓库路径: #{project_dir}") end |
#merge_to_release_branch(project_dir: nil, release_branch: nil, coding_branch: nil) ⇒ Object
168 169 170 171 172 173 174 175 176 177 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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/pindo/command/utils/tag.rb', line 168 def merge_to_release_branch(project_dir: nil, release_branch: nil, coding_branch: nil) current_project_dir = project_dir Funlog.instance.("开始合并到#{release_branch}分支") if !coding_branch.eql?(release_branch) coding_branch_commit_id = git!(%W(-C #{current_project_dir} rev-parse #{coding_branch})).strip release_branch_commit_id = nil if remote_branch_exists?(local_repo_dir: current_project_dir, branch: release_branch) Funlog.instance.("存在#{release_branch}远程分支") if local_branch_exists?(local_repo_dir: current_project_dir, branch: release_branch) Funlog.instance.("存在#{release_branch}本地分支") git!(%W(-C #{current_project_dir} checkout #{release_branch})) else Funlog.instance.("不存在#{release_branch}本地分支") git!(%W(-C #{current_project_dir} checkout -b #{release_branch} origin/#{release_branch})) end git!(%W(-C #{current_project_dir} branch --set-upstream-to=origin/#{release_branch} #{release_branch})) git!(%W(-C #{current_project_dir} fetch origin #{release_branch})) git!(%W(-C #{current_project_dir} merge origin/#{release_branch})) # 执行合并操作,捕获输出以便后续检查冲突 stdout, exit_status = Executable.capture_command('git', %W(-C #{current_project_dir} merge #{coding_branch}), :capture => :out) # 检查是否有冲突(git merge 在有冲突时返回非0状态) conflict_filelist = git!(%W(-C #{current_project_dir} diff --name-only --diff-filter=U --relative)) if !conflict_filelist.nil? && conflict_filelist.size > 0 puts "合并代码冲突, 冲突文件如下:" raise Informative, "请手动处理冲突的文件!!!" else git!(%W(-C #{current_project_dir} push)) Funlog.instance.("代码已经合并到#{release_branch}分支") # 获取 release_branch 的 commit ID(处理空分支情况) begin release_branch_commit_id = git!(%W(-C #{current_project_dir} rev-parse #{release_branch})).strip rescue => e # 分支可能存在但没有提交(空分支),此时使用 coding_branch 的 commit Funlog.instance.("#{release_branch}分支为空或获取commit失败,将使用当前分支commit") release_branch_commit_id = coding_branch_commit_id end end else if local_branch_exists?(local_repo_dir: current_project_dir, branch: release_branch) Funlog.instance.("不存在#{release_branch}远程分支") Funlog.instance.("存在#{release_branch}本地分支") git!(%W(-C #{current_project_dir} checkout #{release_branch})) git!(%W(-C #{current_project_dir} checkout -b #{release_branch}_temp)) git!(%W(-C #{current_project_dir} checkout #{coding_branch})) git!(%W(-C #{current_project_dir} branch -D #{release_branch})) else Funlog.instance.("不存在#{release_branch}远程分支") Funlog.instance.("不存在#{release_branch}本地分支") end git!(%W(-C #{current_project_dir} checkout -b #{release_branch})) git!(%W(-C #{current_project_dir} push origin #{release_branch})) git!(%W(-C #{current_project_dir} branch --set-upstream-to=origin/#{release_branch} #{release_branch})) Funlog.instance.("代码已经合并到#{release_branch}分支") # 获取 release_branch 的 commit ID(处理空分支情况) begin release_branch_commit_id = git!(%W(-C #{current_project_dir} rev-parse #{release_branch})).strip rescue => e # 新创建的分支,commit ID 应该与 coding_branch 相同 release_branch_commit_id = coding_branch_commit_id end end git!(%W(-C #{current_project_dir} checkout #{coding_branch})) if release_branch_commit_id && !release_branch_commit_id.eql?(coding_branch_commit_id) git!(%W(-C #{current_project_dir} merge #{release_branch})) Funlog.instance.("已将#{release_branch}合并到#{coding_branch}") end git!(%W(-C #{current_project_dir} push origin #{coding_branch})) Funlog.instance.("已推送#{coding_branch}分支到远程") else Funlog.instance.("代码处于#{coding_branch}分支,无需合并") end end |
#run ⇒ Object
62 63 64 65 66 67 68 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 100 |
# File 'lib/pindo/command/utils/tag.rb', line 62 def run current_project_dir = Dir.pwd puts current_project_dir # 检查当前目录是否是git仓库 unless is_git_directory?(local_repo_dir: current_project_dir) Funlog.instance.("当前目录不是git仓库") raise Informative, "请在git仓库目录下执行此命令" end # 获取git仓库根目录 root_dir = git_root_directory(local_repo_dir: current_project_dir) if root_dir.nil? Funlog.instance.("无法获取git仓库根目录") raise Informative, "获取git仓库根目录失败" end release_branch = "master" process_need_add_files(project_dir: root_dir) current_branch = git!(%W(-C #{root_dir} rev-parse --abbrev-ref HEAD)).strip coding_branch = current_branch Funlog.instance.("开始合并到#{release_branch}分支") merge_to_release_branch( project_dir: root_dir, release_branch: release_branch, coding_branch: coding_branch ) add_release_tag( project_dir: root_dir, increment_mode: @mode, force_retag: @force_retag, custom_tag: @custom_tag ) end |