Module: MGit::Workspace::WorkspaceHelper

Included in:
MGit::Workspace
Defined in:
lib/m-git/workspace/workspace_helper.rb

Instance Method Summary collapse

Instance Method Details

#invalid_move?(from_path, to_path) ⇒ Boolean

判断是否可以移动目录,若目标目录包含源目录,则无法移动

Parameters:

  • from_path (String)

    源目录

  • to_path (String)

    目标目录

Returns:

  • (Boolean)


153
154
155
# File 'lib/m-git/workspace/workspace_helper.rb', line 153

def invalid_move?(from_path, to_path)
  return Pathname(to_path).cleanpath.to_s.include?(Pathname(from_path).cleanpath.to_s)
end

#pop(root, light_repo) ⇒ Object

将缓存的仓库移动到工作区

Parameters:

  • root (String)

    mgit工程根目录

  • light_repo (Manifest::LightRepo)

    操作仓库的配置repo



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/m-git/workspace/workspace_helper.rb', line 64

def pop(root, light_repo)
  cache_path = File.join(light_repo.cache_store_dir(root), light_repo.name)
  workspace_path = light_repo.abs_dest(root)
  workspace_dir = File.dirname(workspace_path)
  return if invalid_move?(cache_path, workspace_dir)

  if Dir.exist?(cache_path) && !Dir.exist?(workspace_path)

    # 工作区目录不存在则创建
    FileUtils.mkdir_p(workspace_dir) if !Dir.exist?(workspace_dir)

    begin
      # 【注意】 FileUtils.mv(a,b) 如果b路径的basename不存在,那么自动创建b并将【a文件夹内的所有文件】拷贝到b(b/<content of a>),如果basename存在,那么直接把a文件夹整个移动到b下(b/a/<content of a>)。
      FileUtils.mv(cache_path, workspace_dir)
    rescue => _
    end
  end
end

#pop_git_entity(root, light_repo) ⇒ Object

弹出托管的.git实体

Parameters:

  • root (String)

    mgit工程根目录

  • light_repo (Manifest::LightRepo)

    操作仓库的配置repo



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/m-git/workspace/workspace_helper.rb', line 12

def pop_git_entity(root, light_repo)
  # 仓库工作区目录
  repo_dir = light_repo.abs_dest(root)

  # 仓库工作区.git软链接路径
  workspace_git_link_path = File.join(repo_dir, '.git')

  # 仓库.git实体托管路径
  git_entity_path = File.join(light_repo.git_store_dir(root), '.git')

  if Dir.exist?(git_entity_path) && Dir.exist?(workspace_git_link_path) && File.symlink?(workspace_git_link_path)
    # 仓库工作区.git软链接指向路径
    abs_link_point = File.join(repo_dir, File.readlink(workspace_git_link_path))

    # 若指向路径就是.git实体托管路径,则删除工作区软链接,并把.git弹出
    if Pathname.new(abs_link_point).realpath.to_s == git_entity_path
      FileUtils.rm_f(workspace_git_link_path)
      FileUtils.mv(git_entity_path, repo_dir)
    end
  end
end

#push(root, light_repo) ⇒ Object

将工作区的仓库缓存起来

Parameters:

  • root (String)

    mgit工程根目录

  • light_repo (Manifest::LightRepo)

    操作仓库的配置repo



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/m-git/workspace/workspace_helper.rb', line 89

def push(root, light_repo)
  cache_dir = light_repo.cache_store_dir(root)
  workspace_path = light_repo.abs_dest(root)
  return if invalid_move?(workspace_path, cache_dir)

  cache_path = File.join(cache_dir, light_repo.name)
  if Dir.exist?(workspace_path)
    # 缓存存在则删除缓存
    FileUtils.rm_rf(cache_path) if Dir.exist?(cache_path)

    # 缓存目录不存在则创建
    FileUtils.mkdir_p(cache_dir) if !Dir.exist?(cache_dir)

    begin
      FileUtils.mv(workspace_path, cache_dir)
    rescue => _
    end
  end
end

#push_git_entity(root, light_repo) ⇒ Object

压入(托管).git实体

Parameters:

  • root (String)

    mgit工程根目录

  • light_repo (Manifest::LightRepo)

    操作仓库的配置repo



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/m-git/workspace/workspace_helper.rb', line 40

def push_git_entity(root, light_repo)
  # 仓库工作区目录
  repo_dir = light_repo.abs_dest(root)
  git_store_dir = light_repo.git_store_dir(root)

  # 仓库工作区.git实体路径
  workspace_git_entity_path = File.join(repo_dir, '.git')

  # 仓库.git实体托管路径
  cache_git_entity_path = File.join(git_store_dir, '.git')

  # 工作区.git存在,且mgit没有已存在的托管的.git
  if Dir.exist?(workspace_git_entity_path) && !Dir.exist?(cache_git_entity_path)
    # 移动并链接
    Utils.link_git(repo_dir, git_store_dir)
  end
end

#replace(root, light_repo_a, light_repo_b) ⇒ Object

将工作区的仓库a替换为b

Parameters:



117
118
119
120
# File 'lib/m-git/workspace/workspace_helper.rb', line 117

def replace(root, light_repo_a, light_repo_b)
  push(root, light_repo_a)
  pop(root, light_repo_b)
end

#sync_workspace(root, light_repo) ⇒ Object

根据工作区仓库url和配置url来替换仓库

Parameters:

  • root (String)

    mgit工程根目录

  • light_repo (Manifest::LightRepo)

    操作仓库的配置repo



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/m-git/workspace/workspace_helper.rb', line 128

def sync_workspace(root, light_repo)
  name = light_repo.name
  path = light_repo.abs_dest(root)

  # 若工作区存在该仓库,且url与配置不匹配,则压入缓存,此时如果配置的url有对应缓存,则将其弹出
  if Repo.is_git_repo?(path)
    repo = Repo.new(name, path)
    url = repo.status_checker.default_url
    if !Utils.url_consist?(url, light_repo.url)
      repo_config = Manifest::LightRepoGenerator.simple_init(name, path, url)
      replace(root, repo_config, light_repo)
    end

    # 若工作区不存在该仓库,则弹出配置url对应缓存(如有缓存的话)
  else
    pop(root, light_repo)
  end
end