Method: MGit::Repo::SyncHelper#__sync_local_repo

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

#__sync_local_repo(light_repo, root, link_git: true) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/m-git/repo/sync_helper.rb', line 21

def __sync_local_repo(light_repo, root, link_git:true)
  output, repo = nil, nil
  git_entity = File.join(light_repo.git_store_dir(root), '.git')
  # 先git clone -- local_git_repo,再软链git_entity,再checkout分支
  clone_url = "git clone -- #{git_entity} #{light_repo.abs_dest(root)}"
  Utils.execute_shell_cmd(clone_url) { |stdout, stderr, status|
    if status.success?
      repo = Repo.generate_strictly(root, light_repo)
      repo_git = File.join(repo.path, '.git')
      manage_git = MGitConfig.query_with_key(root, :managegit)
      if manage_git && link_git
        FileUtils.rm_rf(repo_git)
        Utils.link(git_entity, repo_git)
      end
      msg = ''
      # 如果从本地clone的话,remote url是指向本地的,需要更新
      error_message = sync_remote_url(repo, light_repo)
      msg += error_message + "\n" if !error_message.nil?
      # 本地仓库可能太旧,执行pull更新代码和新分支
      success, error_message = repo.execute_git_cmd('fetch', '')
      if !success && !error_message.nil? && error_message.length > 0
        msg += "由于存在本地仓库源,已从本地克隆,但代码更新失败,请自行fetch最新代码。原因:\n" + error_message + "\n"
      end

      # 同步锁定点
      error_message = sync_lock_point(repo, light_repo)
      msg += error_message if !error_message.nil?

      output = msg.length > 0 ? msg : nil
    else
      output = "同步仓库\"#{light_repo.name}\"时clone失败,如果远程仓库不存在,请在配置文件中删除该仓库并重试。原因:\n#{stderr}"
    end
  }
  [output, repo]
end