Method: MGit::Repo::SyncHelper.sync_branch

Defined in:
lib/m-git/repo/sync_helper.rb

.sync_branch(repo, light_repo) ⇒ string

同步分支

Parameters:

Returns:

  • (string)

    执行结果,成功返回nil,错误返回错误信息



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
# File 'lib/m-git/repo/sync_helper.rb', line 180

def sync_branch(repo, light_repo)

  current_branch = repo.status_checker.current_branch(strict_mode:false)
  local_branch_exist = repo.status_checker.local_branch_exist?(light_repo.branch)
  remote_branch_exist = repo.status_checker.remote_branch_exist?(light_repo.branch)
  is_dirty = repo.status_checker.status == Status::GIT_REPO_STATUS[:dirty]

  # 当前已在目标切换分支则不操作
  if current_branch == light_repo.branch
    return nil

    # 本地或远程存在目标分支则切换
  elsif local_branch_exist || remote_branch_exist || Utils.branch_exist_on_remote?(light_repo.branch, light_repo.url)

    # 本地无目标分支则先拉取
    if !local_branch_exist && !remote_branch_exist
      success, error = repo.execute_git_cmd('fetch', '')
      return error if !success
    end

    if !is_dirty
      success, output = repo.execute_git_cmd('checkout', light_repo.branch)
      return output if !success
    else
      return "本地有改动, 无法切换到分支\"#{light_repo.branch}\", 请处理后重试!"
    end

  else
    return "仓库分支\"#{light_repo.branch}\"不存在,请检查是否拼写错误!"
  end

end